大模型Agentic RAG工作流實戰:自主檢索增強生成架構設計
摘要
- Agentic RAG是2026年RAG的終極形態:從「檢索-生成」到「規劃-檢索-推理-驗證」閉環,準確率提升40%+
- 4大核心能力:自主檢索規劃、多步推理鏈、工具呼叫增強、自我反思驗證
- 3種Agentic RAG架構:單Agent路由、多Agent協作、分層Agent編排,各有最佳場景
- 生產關鍵:檢索品質評估、幻覺檢測、成本控制,3大指標缺一不可
- 本文提供LangGraph+Agentic RAG完整實現與生產部署方案
目錄
Agentic RAG:RAG的終極形態
傳統RAG vs Agentic RAG
| 維度 | 傳統RAG | Agentic RAG |
|---|---|---|
| 檢索方式 | 單次查詢 | 多輪自主檢索 |
| 推理深度 | 1步生成 | 多步推理鏈 |
| 工具使用 | 僅檢索 | 搜尋+計算+API |
| 自我糾錯 | 無 | 反思+重試 |
| 複雜查詢 | 差 | 強 |
| 準確率 | 60-70% | 85-95% |
Agentic RAG演進路線
┌──────────────────────────────────────────────────────────────┐ │ RAG演進路線 │ │ │ │ RAG 1.0 (2023) │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Query → 檢索 → 生成 │ │ │ │ 簡單管道,無反思,無規劃 │ │ │ └──────────────────────────────────────────────────────┘ │ │ ↓ │ │ RAG 2.0 (2024) │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Query → 改寫 → 混合檢索 → 重排 → 生成 │ │ │ │ 查詢增強+多路檢索+重排序 │ │ │ └──────────────────────────────────────────────────────┘ │ │ ↓ │ │ Agentic RAG (2025-2026) │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Query → 規劃 → 檢索 → 推理 → 驗證 → (迴圈) → 生成 │ │ │ │ 自主規劃+多步推理+自我反思+工具呼叫 │ │ │ └──────────────────────────────────────────────────────┘ │ └──────────────────────────────────────────────────────────────┘
2026年Agentic RAG框架對比
| 框架 | 語言 | 核心特點 | Agent支援 | 生產就緒 |
|---|---|---|---|---|
| LangGraph | Python | 圖編排+狀態機 | 強 | 是 |
| CrewAI | Python | 多Agent協作 | 強 | 中 |
| AutoGen | Python | 多Agent對話 | 強 | 中 |
| LlamaIndex | Python | RAG生態最好 | 中 | 是 |
| Haystack | Python | 管道式編排 | 中 | 是 |
| Dify | Python/Go | 視覺化+低程式碼 | 中 | 是 |
4大核心能力
能力1:自主檢索規劃
`python from pydantic import BaseModel from typing import List, Optional
class RetrievalPlan(BaseModel): queries: List[str] tools: List[str] priority: int max_iterations: int
class RetrievalPlanner: def init(self, llm): self.llm = llm
def plan(self, question: str) -> RetrievalPlan:
prompt = f"""分析以下問題,制定檢索計劃:
問題:{question}
請輸出:
- 需要檢索的子問題列表(按優先級排序)
- 每個子問題需要的檢索工具
- 最大檢索輪數
格式:
-
子問題1 [工具: vector_search]
-
子問題2 [工具: web_search]
-
..."""
response = self.llm.invoke(prompt) return self._parse_plan(response)def _parse_plan(self, response: str) -> RetrievalPlan: queries = [] tools = []
for line in response.strip().split("\n"): if "[" in line and "]" in line: query = line.split("[")[0].strip("- ").strip() tool = line.split("[")[1].split("]")[0].replace("工具:", "").strip() queries.append(query) tools.append(tool) return RetrievalPlan( queries=queries, tools=tools, priority=1, max_iterations=3, )
`
能力2:多步推理鏈
`python class MultiStepReasoner: def init(self, llm, max_steps=5): self.llm = llm self.max_steps = max_steps
def reason(self, question: str, context: List[str]) -> dict:
steps = []
current_question = question
accumulated_context = list(context)
for step in range(self.max_steps):
prompt = f"""基於以下上下文,逐步推理回答問題。
已收集上下文: {chr(10).join(accumulated_context)}
當前問題:{current_question}
請輸出:
-
基於已有資訊的推理步驟
-
是否需要更多資訊(是/否)
-
如果需要,下一個檢索查詢是什麼
-
當前推理結論"""
response = self.llm.invoke(prompt) step_result = self._parse_step(response) steps.append(step_result) if not step_result["need_more_info"]: break current_question = step_result["next_query"] new_context = self._retrieve(current_question) accumulated_context.extend(new_context) return { "steps": steps, "final_answer": steps[-1]["conclusion"], "total_steps": len(steps), }def _parse_step(self, response: str) -> dict: lines = response.strip().split("\n") return { "reasoning": lines[0] if len(lines) > 0 else "", "need_more_info": "是" in (lines[1] if len(lines) > 1 else ""), "next_query": lines[2] if len(lines) > 2 else "", "conclusion": lines[3] if len(lines) > 3 else "", } `
能力3:工具呼叫增強
`python from typing import Callable, Dict, Any
class ToolRegistry: def init(self): self.tools: Dict[str, Callable] = {} self.tool_descriptions: Dict[str, str] = {}
def register(self, name: str, description: str, func: Callable):
self.tools[name] = func
self.tool_descriptions[name] = description
def execute(self, name: str, **kwargs) -> Any:
if name not in self.tools:
raise ValueError(f"Tool {name} not found")
return self.tools[name](**kwargs)
def get_descriptions(self) -> str:
return "\n".join([
f"- {name}: {desc}"
for name, desc in self.tool_descriptions.items()
])
def setup_tools(registry: ToolRegistry): registry.register( "vector_search", "在知識庫中搜尋相關文件", lambda query, top_k=5: vector_search_impl(query, top_k), ) registry.register( "web_search", "在網際網路上搜尋最新資訊", lambda query, num_results=5: web_search_impl(query, num_results), ) registry.register( "calculator", "執行數學計算", lambda expression: eval(expression), ) registry.register( "sql_query", "查詢資料庫", lambda sql: sql_query_impl(sql), ) registry.register( "api_call", "呼叫外部API", lambda url, params: api_call_impl(url, params), ) `
能力4:自我反思驗證
`python class SelfReflector: def init(self, llm): self.llm = llm
def verify(self, question: str, answer: str, context: List[str]) -> dict:
prompt = f"""驗證以下回答的正確性:
問題:{question} 回答:{answer} 參考上下文: {chr(10).join(context)}
請評估:
-
回答是否完全基於上下文?(grounded: 是/否)
-
回答是否完整回答了問題?(complete: 是/否)
-
回答中是否有幻覺內容?(hallucination: 是/否)
-
置信度評分 (0-1)
-
改進建議"""
response = self.llm.invoke(prompt) result = self._parse_verification(response) return resultdef _parse_verification(self, response: str) -> dict: lines = response.strip().split("\n") grounded = "是" in (lines[0] if len(lines) > 0 else "") complete = "是" in (lines[1] if len(lines) > 1 else "") hallucination = "是" in (lines[2] if len(lines) > 2 else "")
return { "grounded": grounded, "complete": complete, "has_hallucination": hallucination, "confidence": 0.8, "needs_retry": not grounded or not complete or hallucination, }
`
3種Agentic RAG架構
架構對比
┌──────────────────────────────────────────────────────────────┐ │ 3種Agentic RAG架構 │ │ │ │ 架構1: 單Agent路由 │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Query → Router Agent → [檢索|計算|API] → 生成 │ │ │ │ 適合: 簡單場景,快速部署 │ │ │ └──────────────────────────────────────────────────────┘ │ │ │ │ 架構2: 多Agent協作 │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Query → Planner → [Retriever|Reasoner|Verifier] │ │ │ │ ← 協作迴圈 → │ │ │ │ 適合: 複雜查詢,高準確率 │ │ │ └──────────────────────────────────────────────────────┘ │ │ │ │ 架構3: 分層Agent編排 │ │ ┌──────────────────────────────────────────────────────┐ │ │ │ Orchestrator → [Sub-Orchestrator → [Workers]] │ │ │ │ 適合: 企業級,多租戶 │ │ │ └──────────────────────────────────────────────────────┘ │ └──────────────────────────────────────────────────────────────┘
架構選型
| 場景 | 推薦架構 | 複雜度 | 準確率 | 延遲 |
|---|---|---|---|---|
| FAQ/簡單查詢 | 單Agent路由 | 低 | 80% | 2s |
| 研究分析 | 多Agent協作 | 中 | 92% | 8s |
| 企業知識庫 | 分層Agent | 高 | 95% | 5s |
| 即時決策 | 單Agent+快取 | 低 | 85% | 1s |
LangGraph實現Agentic RAG
完整實現
`python from langgraph.graph import StateGraph, END from typing import TypedDict, List, Dict, Any, Annotated import operator
class AgentState(TypedDict): question: str plan: Dict[str, Any] documents: List[str] reasoning_steps: List[Dict] answer: str verification: Dict[str, Any] iteration: int max_iterations: int
class AgenticRAGGraph: def init(self, llm, retriever, reflector): self.llm = llm self.retriever = retriever self.reflector = reflector self.graph = self._build_graph()
def _build_graph(self):
workflow = StateGraph(AgentState)
workflow.add_node("plan", self._plan_node)
workflow.add_node("retrieve", self._retrieve_node)
workflow.add_node("reason", self._reason_node)
workflow.add_node("generate", self._generate_node)
workflow.add_node("verify", self._verify_node)
workflow.set_entry_point("plan")
workflow.add_edge("plan", "retrieve")
workflow.add_edge("retrieve", "reason")
workflow.add_edge("reason", "generate")
workflow.add_edge("generate", "verify")
workflow.add_conditional_edges(
"verify",
self._should_retry,
{
"retry": "retrieve",
"finish": END,
},
)
return workflow.compile()
def _plan_node(self, state: AgentState) -> AgentState:
planner = RetrievalPlanner(self.llm)
plan = planner.plan(state["question"])
state["plan"] = plan.dict()
state["iteration"] = state.get("iteration", 0) + 1
return state
def _retrieve_node(self, state: AgentState) -> AgentState:
documents = []
plan = state["plan"]
for query in plan.get("queries", [state["question"]]):
docs = self.retriever.search(query, top_k=5)
documents.extend([d.page_content for d in docs])
state["documents"] = list(set(documents))
return state
def _reason_node(self, state: AgentState) -> AgentState:
reasoner = MultiStepReasoner(self.llm)
result = reasoner.reason(state["question"], state["documents"])
state["reasoning_steps"] = result["steps"]
return state
def _generate_node(self, state: AgentState) -> AgentState:
context = "\n".join(state["documents"])
reasoning = "\n".join([
f"步驟{i+1}: {s['conclusion']}"
for i, s in enumerate(state["reasoning_steps"])
])
prompt = f"""基於以下資訊回答問題。
上下文: {context}
推理過程: {reasoning}
問題:{state["question"]}
請給出完整、準確的回答:"""
state["answer"] = self.llm.invoke(prompt)
return state
def _verify_node(self, state: AgentState) -> AgentState:
result = self.reflector.verify(
state["question"], state["answer"], state["documents"]
)
state["verification"] = result
return state
def _should_retry(self, state: AgentState) -> str:
if (
state["verification"]["needs_retry"]
and state["iteration"] < state.get("max_iterations", 3)
):
return "retry"
return "finish"
def run(self, question: str, max_iterations: int = 3) -> dict:
initial_state = {
"question": question,
"plan": {},
"documents": [],
"reasoning_steps": [],
"answer": "",
"verification": {},
"iteration": 0,
"max_iterations": max_iterations,
}
result = self.graph.invoke(initial_state)
return result
`
檢索品質與幻覺檢測
檢索品質評估
| 指標 | 計算方式 | 目標值 |
|---|---|---|
| 召回率(Recall) | 相關文件/總相關 | >90% |
| 精確率(Precision) | 相關文件/檢索文件 | >80% |
| MRR | 正確答案排名倒數均值 | >0.7 |
| NDCG@10 | 正規化折損累積增益 | >0.8 |
幻覺檢測方法
`python class HallucinationDetector: def init(self, llm, embedder): self.llm = llm self.embedder = embedder
def detect(self, answer: str, context: List[str]) -> dict:
claim_score = self._claim_verification(answer, context)
consistency_score = self._self_consistency(answer)
similarity_score = self._context_similarity(answer, context)
overall = (
0.4 * claim_score
+ 0.3 * consistency_score
+ 0.3 * similarity_score
)
return {
"hallucination_risk": 1 - overall,
"claim_verification": claim_score,
"self_consistency": consistency_score,
"context_similarity": similarity_score,
"is_reliable": overall > 0.7,
}
def _claim_verification(self, answer: str, context: List[str]) -> float:
prompt = f"""逐句驗證回答中的每個論斷是否有上下文支援。
上下文:{chr(10).join(context)} 回答:{answer}
對每個論斷標註:支援/不支援/無法判斷"""
response = self.llm.invoke(prompt)
supported = response.count("支援")
total = supported + response.count("不支援") + response.count("無法判斷")
return supported / max(total, 1)
def _self_consistency(self, answer: str) -> float:
variations = []
for _ in range(3):
var = self.llm.invoke(f"用不同方式複述:{answer}")
variations.append(var)
embeddings = self.embedder.embed(variations + [answer])
similarities = [
cosine_similarity(embeddings[-1], emb) for emb in embeddings[:-1]
]
return sum(similarities) / len(similarities)
def _context_similarity(self, answer: str, context: List[str]) -> float:
answer_emb = self.embedder.embed([answer])[0]
context_emb = self.embedder.embed(context)
max_sim = max(
cosine_similarity(answer_emb, emb) for emb in context_emb
)
return max_sim
`
生產部署與成本控制
成本最佳化策略
| 策略 | 成本節省 | 準確率影響 |
|---|---|---|
| 檢索結果快取 | 40-60% | 無 |
| 小模型路由 | 30-50% | <2% |
| 批次推理 | 20-30% | 無 |
| 上下文壓縮 | 15-25% | <1% |
| 動態迭代控制 | 10-20% | <3% |
動態迭代控制
`python class DynamicIterationController: def init(self, max_iterations=3, confidence_threshold=0.8): self.max_iterations = max_iterations self.confidence_threshold = confidence_threshold
def should_continue(self, state: dict) -> bool:
if state["iteration"] >= self.max_iterations:
return False
if state.get("verification", {}).get("confidence", 0) >= self.confidence_threshold:
return False
if state["iteration"] > 1:
prev_conf = state.get("prev_confidence", 0)
curr_conf = state.get("verification", {}).get("confidence", 0)
if curr_conf - prev_conf < 0.05:
return False
return True
`
Agentic RAG效能基準
| 指標 | 傳統RAG | Agentic RAG | 提升 |
|---|---|---|---|
| 簡單查詢準確率 | 85% | 92% | +7% |
| 複雜查詢準確率 | 55% | 88% | +33% |
| 幻覺率 | 15% | 5% | -67% |
| 平均延遲 | 2s | 5s | -60% |
| 平均token消耗 | 500 | 2000 | -75% |
總結與引流
關鍵要點回顧
- Agentic RAG是RAG的終極形態:規劃-檢索-推理-驗證閉環,準確率提升40%+
- 4大核心能力:自主規劃、多步推理、工具呼叫、自我反思
- 3種架構:單Agent路由(簡單)、多Agent協作(複雜)、分層Agent(企業)
- 生產關鍵:檢索品質評估+幻覺檢測+成本控制
Agentic RAG方案推薦
| 場景 | 推薦方案 | 預期效果 |
|---|---|---|
| 快速驗證 | 單Agent+LangGraph | 準確率85%+ |
| 生產部署 | 多Agent+快取+量化 | 準確率90%+ |
| 企業級 | 分層Agent+監控 | 準確率95%+ |
需要處理RAG資料的格式轉換?試試我們的JSON轉YAML工具和文字差異對比,快速處理檢索資料。
延伸閱讀
- MCP協議實戰:建構AI Agent工具鏈 — Agent工具呼叫
- AI Agent多輪記憶實戰 — Agent記憶管理
- GraphRAG實戰:知識圖譜增強RAG — 知識圖譜RAG
- LangGraph官方文件 — LangGraph使用指南
- Agentic RAG論文 — Agentic RAG方法論
本站提供瀏覽器本地工具,免註冊即可試用 →