知识检索与 RAG (Retrieval)
为你的 deep agent 连接外部知识库与向量检索系统
检索增强生成(RAG)让大语言模型能够突破自身训练数据的时空限制,精准引用私有领域知识、最新技术文档和业务事实。
构建知识库 (Building a knowledge base)
从简单检索到成熟 RAG
传统的检索仅根据关键词或相似度返回原始文本;而智能体 RAG(Agentic RAG)赋予了智能体主动评估检索结果质量、改写查询语句、多跳追问以及综合交叉验证的能力。
检索流水线 (Retrieval pipeline)
一个标准的数据摄取流水线包括四个核心阶段:
- 加载 (Load):从 PDF、Markdown、Web 页面或数据库中提取原始内容。
- 切分 (Split):将长篇文档合理切分成语义完整的数据块(Chunks)。
- 向量嵌入 (Embed):使用嵌入模型将文本块转换为密集向量表示。
- 存储索引 (Store):存入向量数据库以支持毫秒级近似最近邻(ANN)检索。
核心构件 (Building blocks)
Deep Agents 可以无缝接入 LangChain 庞大的向量存储生态,包括 Pinecone、Qdrant、Chroma、Milvus、PgVector 等。
RAG 核心架构演进 (RAG architectures)
1. 两阶段朴素 RAG (2-step RAG)
流水线式执行:收到用户提问 -> 检索 Top-K 相关文本块 -> 拼入提示词交由模型一次性生成回答。适合简单、意图明确的静态知识问答。
%%{init: {'flowchart': {'nodeSpacing': 12, 'rankSpacing': 18, 'padding': 4}, 'themeVariables': {'fontSize': '12px'}}}%%
flowchart TB
subgraph ingest[" "]
direction LR
S(["Sources<br>(Google Drive, Slack, Notion, etc.)"]) --> L[Document Loaders]
L --> A([Documents])
end
A --> B[Split into chunks]
B --> C[Turn into embeddings]
C --> D[(Vector Store)]
Q([User Query]) --> E[Query embedding]
E --> D
D --> F[Retriever]
F --> G[LLM uses retrieved info]
G --> H([Answer])
classDef trigger fill:#F6FFDB,stroke:#6E8900,stroke-width:2px,color:#2E3900
classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:2px,color:#030710
classDef output fill:#EBD0F0,stroke:#885270,stroke-width:2px,color:#441E33
classDef neutral fill:#F2FAFF,stroke:#40668D,stroke-width:2px,color:#2F4B68
class S,Q trigger
class L,B,C,E,F,G process
class D output
class A,H neutral2. 智能体自主 RAG (Agentic RAG)
将检索系统封装为一个或多个工具赋予 deep agent。 智能体根据当前对话上下文,自主决定:
- 是否需要检索外部知识?
- 需要检索什么具体关键词?
- 当前检索到的内容是否足够?若不足,是否需要调整检索词再次发起查询?
- 如何从多个分散来源中剔除矛盾、交叉印证并合成结论?
%%{init: {'flowchart': {'nodeSpacing': 12, 'rankSpacing': 18, 'padding': 4}, 'themeVariables': {'fontSize': '12px'}}}%%
graph TB
A[User Question] --> B["Retrieve Relevant Documents"]
B --> C["Generate Answer"]
C --> D[Return Answer to User]
%% Styling
classDef startend fill:#F6FFDB,stroke:#6E8900,stroke-width:2px,color:#2E3900
classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:1.5px,color:#030710
class A,D startend
class B,C process3. 混合式 RAG (Hybrid RAG)
将密集向量检索(语义相似性)与稀疏关键词检索(BM25 精确匹配)相结合,并通过重排模型(Reranker)对初筛结果进行语义精细打分,确保召回精度与覆盖率达到极致平衡。
%%{init: {'flowchart': {'nodeSpacing': 12, 'rankSpacing': 18, 'padding': 4}, 'themeVariables': {'fontSize': '12px'}}}%%
graph TB
A[User Input / Question] --> B["Agent (LLM)"]
B --> C{Need external info?}
C -- Yes --> D["Search using tool(s)"]
D --> H{Enough to answer?}
H -- No --> B
H -- Yes --> I[Generate final answer]
C -- No --> I
I --> J[Return to user]
%% Dark-mode friendly styling
classDef startend fill:#F6FFDB,stroke:#6E8900,stroke-width:2px,color:#2E3900
classDef decision fill:#FDF3FF,stroke:#7E65AE,stroke-width:2px,color:#504B5F
classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:1.5px,color:#030710
class A,J startend
class B,D,I process
class C,H decisionimport { tool, createAgent } from "langchain";
const fetchUrl = tool(
(url: string) => {
return `Fetched content from ${url}`;
},
{ name: "fetch_url", description: "Fetch text content from a URL" }
);
const agent = createAgent({
model: "claude-sonnet-4-6",
tools: [fetchUrl],
systemPrompt,
});Extended example: Agentic RAG for LangGraph's llms.txt
This example implements an Agentic RAG system to assist users in querying LangGraph documentation. The agent begins by loading llms.txt, which lists available documentation URLs, and can then dynamically use a fetch_documentation tool to retrieve and process the relevant content based on the user’s question.
import { tool, createAgent, HumanMessage } from "langchain";
import * as z from "zod";
const ALLOWED_DOMAINS = ["https://docs.langchain.com/"];
const LLMS_TXT = "https://docs.langchain.com/llms.txt";
const fetchDocumentation = tool(
async (input) => {
if (!ALLOWED_DOMAINS.some((domain) => input.url.startsWith(domain))) {
return `Error: URL not allowed. Must start with one of: ${ALLOWED_DOMAINS.join(", ")}`;
}
const response = await fetch(input.url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
},
{
name: "fetch_documentation",
description: "Fetch and convert documentation from a URL",
schema: z.object({
url: z.string().describe("The URL of the documentation to fetch"),
}),
}
);
const llmsTxtResponse = await fetch(LLMS_TXT);
const llmsTxtContent = await llmsTxtResponse.text();
const systemPrompt = `
You are an expert TypeScript developer and technical assistant.
Your primary role is to help users with questions about LangGraph and related tools.
Instructions:
1. If a user asks a question you're unsure about—or one that likely involves API usage,
behavior, or configuration—you MUST use the \`fetch_documentation\` tool to consult the relevant docs.
2. When citing documentation, summarize clearly and include relevant context from the content.
3. Do not use any URLs outside of the allowed domain.
4. If a documentation fetch fails, tell the user and proceed with your best expert understanding.
You can access official documentation from the following approved sources:
${llmsTxtContent}
You MUST consult the documentation to get up to date documentation
before answering a user's question about LangGraph.
Your answers should be clear, concise, and technically accurate.
`;
const tools = [fetchDocumentation];
const agent = createAgent({
model: "claude-sonnet-4-6"
tools,
systemPrompt,
name: "Agentic RAG",
});
const response = await agent.invoke({
messages: [
new HumanMessage(
"Write a short example of a langgraph agent using the " +
"prebuilt create react agent. the agent should be able " +
"to look up stock pricing information."
),
],
});
console.log(response.messages.at(-1)?.content);Tutorial: RAG with Deep Agents
Build a documentation Q&A agent that retrieves relevant chunks at query time, offloads them to the filesystem, and delegates analysis to subagents.
Hybrid RAG
Hybrid RAG combines characteristics of both 2-Step and Agentic RAG. It introduces intermediate steps such as query preprocessing, retrieval validation, and post-generation checks. These systems offer more flexibility than fixed pipelines while maintaining some control over execution.
Typical components include:
- Query enhancement: Modify the input question to improve retrieval quality. This can involve rewriting unclear queries, generating multiple variations, or expanding queries with additional context.
- Retrieval validation: Evaluate whether retrieved documents are relevant and sufficient. If not, the system may refine the query and retrieve again.
- Answer validation: Check the generated answer for accuracy, completeness, and alignment with source content. If needed, the system can regenerate or revise the answer.
The architecture often supports multiple iterations between these steps:
%%{init: {'flowchart': {'nodeSpacing': 12, 'rankSpacing': 18, 'padding': 4}, 'themeVariables': {'fontSize': '12px'}}}%%
graph TB
A[User Question] --> B[Query Enhancement]
B --> C[Retrieve Documents]
C --> D{Sufficient Info?}
D -- No --> E[Refine Query]
E --> C
D -- Yes --> F[Generate Answer]
F --> G{Answer Quality OK?}
G -- No --> H{Try Different Approach?}
H -- Yes --> E
H -- No --> I[Return Best Answer]
G -- Yes --> I
I --> J[Return to User]
classDef startend fill:#F6FFDB,stroke:#6E8900,stroke-width:2px,color:#2E3900
classDef decision fill:#FDF3FF,stroke:#7E65AE,stroke-width:2px,color:#504B5F
classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:1.5px,color:#030710
class A,J startend
class B,C,E,F,I process
class D,G,H decisionThis architecture is suitable for:
- Applications with ambiguous or underspecified queries
- Systems that require validation or quality control steps
- Workflows involving multiple sources or iterative refinement
Tutorial: Agentic RAG with Self-Correction
An example of Hybrid RAG that combines agentic reasoning with retrieval and self-correction.
Connect these docs to your agent of choice via MCP for real-time answers.
