def HarmonyChainSpace():
"Multi-Agent RAG System Implementation"
// 打造結合邏輯與玄學的衝突解決引擎
speaker_profile.json
{ "speaker": "Yu-Jun Wang", "role": "AI Application Developer", "stack": [ "LangChain", "LCEL", "Synthetic Data" ] }
main*
Ln 1, Col 1Python 3.10
1
2
3
4

Engineering_Goals

// 工程目標:解決結構化法規與非結構化民俗觀點的整合挑戰
objectives.py
# Core Challenge: Logic vs Metaphysics Architecture = "Declarative LCEL Workflow" Data_Strategy = "Synthetic Data Generation" # Solving data scarcity in niche domains Stability = "Structured Parsers (Pydantic)" # Taming probabilistic LLM outputs
Rules
(法規)
Metaphysics
(玄學)
0 Errors
UTF-8

System_Architecture

Data Flow (資料流)
Control Flow (控制流)
Input
(Text/Image)
Brain
(LangChain Router)
Memory
(RAG Pipelines)
Output
(UI + GenAI)
Fig 1.0 - High Level Data & Control Flow
(架構總覽:輸入處理 -> 路由分發 -> 混合檢索 -> 生成輸出)
Diagram Rendered

RAG_Strategy_I: Data_Eng

// 資料工程:法規文檔與合成場景的異質處理流程
regulations.pdf (真實法規)
# Source: Government Regulations Type: Structured / PDF PROCESS: 1. Optical Character Recognition (OCR) 2. Semantic Chunking (by Article) 3. Embedding ("text-embedding-3-small") # Characteristics: >> Rigid, Logical, Fact-based.
synthetic_gen.py (合成數據)
# Source: Generative Metaphysics (Feng Shui) Type: Unstructured / Scenarios def Data_Augmentation_Process(): raw_text = "Lu Ban Jing (Ancient)" modern_text = LLM_Translate(raw_text) scenarios = Generate_Scenarios(modern_text) # e.g., "Apartment facing elevator..." return Embed(scenarios)
Processing...

RAG_Strategy_II: Retrieval

// 檢索策略:混合動態上下文注入 (Dynamic Mixing)

Dynamic merging of heterogeneous data sources to provide comprehensive context.

retrieval_logic.py
class DualRetriever: def get_rag_context(self, query: str) -> str: # Parallel Search (平行檢索) law_docs = self.law_db.similarity_search(query, k=3) fs_docs = self.fengshui_db.similarity_search(query, k=3) # Dynamic Context Mixing (動態權重混合) context = f""" Legal Context: {law_docs} Metaphysical Context: {fs_docs} """ return context
Model: all-MiniLM-L6-v2 Latency: ~120ms
Ready

Why_LCEL?

// 技術選擇:從傳統鏈式結構轉向聲明式管線 (Declarative Pipe)

Legacy Chain

old_chain.py
chain = SequentialChain( chains=[a, b, c], input_variables=["..."], verbose=True ) # Issues: # - Hard to debug (除錯困難) # - Hidden state (隱藏狀態) # - Class-based boilerplate

LCEL (Expression)

new_pipeline.py
chain = ( {"context": retriever, "query": RunnablePassthrough()} | prompt | model | StrOutputParser() ) # Benefits: # - Unix Pipe Style (|) (管線風格) # - Streaming built-in (原生串流) # - Highly readable
Refactoring Complete

Multi-Agent_Workflow

// 多代理人協作:結構審查、民俗禁忌與辯證綜合

Architect

Temp: 0.2 [建築師]
Role: "Structure" Prompt: "Strict adherence to building codes and physical feasibility."

Feng Shui

Temp: 0.7 [風水師]
Role: "Energy (Qi)" Prompt: "Focus on cultural taboos and energy flow."

Agent C: Mediator

Logic: Dialectic Synthesis [協調者]
Synthesize(A_Output, B_Output) -> "Balanced Solution"
Agents Online

Stability: Structured_Output

// 系統穩定性:利用 Pydantic 強制輸出格式與類型檢查

Pain Point:

LLM output is probabilistic strings. Parsing fails if the model is "chatty" or adds markdown ticks.

(痛點:LLM 輸出不可控,導致解析錯誤)

Solution:

Enforce schema validation at the generation step, not just post-processing.

(解法:在生成階段強制執行 Schema 驗證)

schemas.py
from langchain_core.pydantic_v1 import BaseModel, Field class MediatorResponse(BaseModel): verdict: str = Field( description="Final advice for user" ) image_prompt: str = Field( description="Optimized prompt for Flux AI" ) parser = PydanticOutputParser( pydantic_object=MediatorResponse )
Schema Validated

Live_Demo: "Chuan_Tang_Sha"

// 實境演示:針對「穿堂煞」案例的跨領域衝突調解
Architect (建築師) Good for ventilation. Compliant with Law Art. 43. Structure is sound.
Feng Shui (風水師) Wealth leakage risk! The Qi flows out directly through the balcony.
Mediator (系統建議) Recommendation: Install a translucent screen (Buffer Zone).

1. Retains light (Legal compliance).
2. Slows Qi flow (Metaphysical harmony).
HarmonyChain App
Floor Plan: Generated
Buffer Zone Applied
Running...

Thank You

Yu-Jun Wang

github.com/YuJunWang

QR Code

Summary_&_Skills

// 總結:核心產出與全端 AI 技術矩陣

Key_Takeaways = [

  • "LCEL for production-grade pipelines",
  • "Synthetic Data for domain adaptation",
  • "Reliability via Pydantic & Multi-Agent"

]

Backend

Python, FastAPI
LangChain, Pydantic

AI Models

Gemini \ Groq
HuggingFace
Stable Diffusion

Tools

Git, Docker
Streamlit, VS Code

End of File