LangGraph构建RAG智能体
简单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
- Queries span multiple knowledge domains
- Users ask vague questions that need query rewriting
- You have multiple tools (vector search + SQL + web search)
- You want the system to say "I don't know" instead of hallucinating
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.
