LangGraph构建RAG智能体

2026-08-01 约 5 分钟阅读

简单RAG只做一次检索。RAG智能体自己决定何时搜索、搜什么、何时回答。

RAG Agent with LangGraph 2026: Build a Research Agent That Uses Tools

Plain RAG does one retrieval, then answers. A RAG agent (or agentic RAG) decides dynamically: it can search multiple sources, rephrase queries, and decide when it has enough information. LangGraph (38,600 GitHub stars, MIT) is the standard framework for building these stateful agent loops in 2026.

RAG vs RAG Agent

Aspect Basic RAG RAG Agent
Retrieval Once, fixed Repeated, adaptive
Query As typed Can be rewritten
Sources One store Multiple tools
Decides when to answer Never Yes
Follow-up handling Stateless Full state

The Graph Structure

[start] -> retrieve -> grade_documents
             ^            |
             |            v
             |      relevant? --no--> rewrite_query -> retrieve
             |            |
             |           yes
             |            v
             +----- generate -> [end]

Key nodes: retrieve (search the vector store), grade (check if results answer the question), rewrite (improve the query if not), generate (final answer).

Minimal Implementation

from langgraph.graph import StateGraph

def retrieve(state):
    docs = vectorstore.search(state["question"])
    return {"docs": docs}

def generate(state):
    answer = llm.invoke(f"Answer using: {state['docs']}")
    return {"answer": answer}

graph = StateGraph(ResearchState)
graph.add_node("retrieve", retrieve)
graph.add_node("generate", generate)
graph.add_edge("retrieve", "generate")
graph.set_entry_point("retrieve")
app = graph.compile()

When You Actually Need an Agent

FAQ

LangGraph or LangChain? LangGraph is the newer, graph-based framework built by the LangChain team; use it for agents. Plain LangChain chains are fine for simple linear RAG.

What is the retriever + grader pattern? The most common RAG agent: retrieve, score the docs, and if scores are low, rewrite the query and retry - improving accuracy on hard questions.

Does it work with local models? Yes - swap the LLM for Ollama and the vectorstore for Chroma; the graph logic is model-agnostic.

相关文章
2026-06-29
高收益主线龙头策略——不花钱的数据,也能抓到龙头
2026-06-29
你笔记本里,藏着一个AI
2026-07-14
免费AI编程助手 2026 配置指南:VS Code 5分钟装 Continue、Copilot、Windsurf

💬 评论 (0)

暂无评论,来说两句吧~

登录后评论