本示例演示如何基于 LlmAgent + LangchainKnowledge 构建一个 RAG(Retrieval-Augmented Generation)知识库问答助手,支持 PGVector、Elasticsearch、腾讯云向量数据库三种后端,验证 文档加载 → 向量化 → 检索 → 生成回答 的完整 RAG 链路。
- 多向量数据库支持:通过环境变量
VECTORSTORE_TYPE一键切换 PGVector / Elasticsearch / 腾讯云向量数据库后端 - Langchain 生态集成:通过
LangchainKnowledge无缝对接 Langchain 的 Document Loader、Text Splitter、Embeddings、VectorStore 等组件 - 知识库检索增强:Agent 在对话时自动调用
simple_search工具检索知识库,基于检索到的文档内容生成更准确的回答 - 流式事件处理:通过
runner.run_async(...)处理 partial/full event,并打印工具调用与工具返回
本例是单 Agent 示例,不涉及多 Agent 分层路由:
rag_agent (LlmAgent)
├── model: OpenAIModel
├── tools:
│ └── simple_search(query) — 基于 LangchainKnowledge 检索向量库并返回最相关文档
├── knowledge: LangchainKnowledge
│ ├── document_loader: TextLoader
│ ├── document_transformer: RecursiveCharacterTextSplitter
│ ├── embedder: HuggingFaceEmbeddings / TencentVDB 内置
│ └── vectorstore: PGVector / ElasticsearchStore / TencentVectorDB
└── session: InMemorySessionService
关键文件:
- examples/knowledge_with_vectorstore/agent/agent.py:构建
LlmAgent、挂载工具、设置指令 - examples/knowledge_with_vectorstore/agent/tools.py:知识库构建 + 检索搜索工具实现
- examples/knowledge_with_vectorstore/agent/prompts.py:提示词模板与 RAG Prompt
- examples/knowledge_with_vectorstore/agent/config.py:环境变量读取(LLM 与向量数据库配置)
- examples/knowledge_with_vectorstore/run_agent.py:测试入口,构建向量库并执行对话
这一节用于快速定位"知识库构建、检索工具、Agent 组装"三条核心链路。
- 通过
_BUILDERS字典按VECTORSTORE_TYPE分发到_build_pgvector_knowledge()、_build_elasticsearch_knowledge()、_build_tencentvdb_knowledge()三个构建函数 - 每个构建函数使用
TextLoader加载文档、RecursiveCharacterTextSplitter切片、对应 Embeddings + VectorStore 组装为LangchainKnowledge实例 - 模块级变量
rag = build_knowledge()在加载时即完成知识链构建
simple_search(query)作为FunctionTool注册到 Agent,执行rag.search()返回最相关文档- RAG Prompt 模板使用
{query}占位符,由ChatPromptTemplate生成,传入LangchainKnowledge用于检索时的问题格式化 - Agent 根据用户问题自动决策是否需要调用
simple_search工具
create_agent()将OpenAIModel、INSTRUCTION指令和FunctionTool(simple_search)组装为LlmAgentrun_agent.py执行流程:加载.env→ 调用rag.create_vectorstore_from_document()构建向量库 → 创建Runner发起对话- 使用
runner.run_async(...)消费事件流,区分并打印function_call(工具调用)与function_response(工具返回)
- Python 3.12
git clone https://github.com/trpc-group/trpc-agent-python.git
cd trpc-agent-python
python3 -m venv .venv
source .venv/bin/activate
pip3 install -e .根据选择的向量数据库后端安装 RAG 相关依赖:
PGVector:
pip3 install langchain-community langchain-huggingface sentence-transformers langchain-postgresElasticsearch:
pip3 install langchain-community langchain-huggingface sentence-transformers langchain-elasticsearch腾讯云向量数据库:
pip3 install langchain-community tcvectordb使用 PGVector / Elasticsearch 时,首次运行会自动从 HuggingFace Hub 下载
BAAI/bge-small-en-v1.5嵌入模型,请确保网络可访问 HuggingFace。
在 examples/knowledge_with_vectorstore/.env 中配置(或通过 export):
通用配置(必填):
TRPC_AGENT_API_KEY— LLM API KeyTRPC_AGENT_BASE_URL— LLM API 地址TRPC_AGENT_MODEL_NAME— LLM 模型名称VECTORSTORE_TYPE— 向量数据库类型:pgvector/elasticsearch/tencentvdb
PGVector 配置:
PGVECTOR_CONNECTION— PostgreSQL 连接字符串,如postgresql+psycopg://langchain:langchain@X.X.X.X:5432/langchainPGVECTOR_COLLECTION_NAME— 集合名称(默认my_docs)
Elasticsearch 配置:
ES_URL— Elasticsearch 地址,如https://X.X.X.X:9200ES_INDEX_NAME— 索引名称(默认langchain_index)ES_API_KEY— Elasticsearch API Key
腾讯云向量数据库配置:
TENCENT_VDB_URL— 数据库连接地址,如http://10.0.X.XTENCENT_VDB_KEY— 数据库密钥TENCENT_VDB_USERNAME— 用户名(默认root)TENCENT_VDB_DATABASE— 数据库名(默认LangChainDatabase)TENCENT_VDB_COLLECTION— 集合名(默认LangChainCollection)TENCENT_VDB_EMBEDDING— 腾讯云嵌入模型(默认bge-base-zh)
可选配置:
KNOWLEDGE_FILE— 知识库文档路径(默认使用项目下的test.txt)
准备知识库文档(默认使用项目下的 test.txt,也可替换为自己的文档):
echo "shenzhen weather: sunny
guangzhou weather: rain
shanghai weather: cloud" > test.txtcd examples/knowledge_with_vectorstore
python3 run_agent.py🆔 Session ID: a3f1b2c8...
📝 User: shenzhen weather
🤖 Assistant:
🔧 [调用工具: simple_search({'query': 'shenzhen weather'})]
📊 [工具结果: {'status': 'success', 'report': 'content: shenzhen weather: sunny'}]
Based on the information I found, the weather in Shenzhen is currently sunny. It's a great day to enjoy some outdoor activities! Let me know if you'd like to know about the weather in other cities.
----------------------------------------
结论:符合本示例测试要求。
- 工具路由正确:Agent 收到天气相关问题后自动调用
simple_search检索知识库 - 检索结果正确:查询 "shenzhen weather" 返回了知识库中最匹配的文档
shenzhen weather: sunny - 工具结果被正确消费:回复内容与知识库检索结果一致,并组织为自然语言回答
- RAG 链路完整:从文档加载 → 切片 → 向量化 → 存入向量库 → 检索 → 生成回答,全流程通过
说明:该示例需要先通过 rag.create_vectorstore_from_document() 构建向量库,确保向量数据库服务已启动且配置正确。
- 快速验证 RAG 知识库检索 + Agent 问答主链路:适合使用本示例
- 需要对比不同向量数据库后端(PGVector / Elasticsearch / 腾讯云)的接入差异:适合使用本示例
- 验证 Langchain 生态组件与 trpc-agent 的集成能力:适合使用本示例
- 需要测试自定义 Document Loader / Embeddings / Prompt 的灵活组合:建议使用
examples/knowledge_with_custom_components