重构代码,统一config配置
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 47m14s

This commit is contained in:
2026-04-21 11:02:16 +08:00
parent 726236eaff
commit 8b354b7ccc
50 changed files with 4025 additions and 6 deletions

54
backend/app/rag/tools.py Normal file
View File

@@ -0,0 +1,54 @@
"""
RAG 工具模块
将检索功能封装为 LangChain Tool供 Agent 调用。
采用固定流水线:多路改写 → 并行检索 → RRF 融合 → 重排序 → 返回父文档。
"""
from typing import Callable
from langchain_core.tools import tool
from langchain_core.language_models import BaseLanguageModel
from langchain_core.retrievers import BaseRetriever
from .pipeline import RAGPipeline
def create_rag_tool_sync(
retriever: BaseRetriever,
llm: BaseLanguageModel,
num_queries: int = 3,
rerank_top_n: int = 5,
collection_name: str = "rag_documents",
) -> Callable:
"""
创建一个配置好的 RAG 检索工具(同步版本,用于不支持异步的旧版 Agent
参数同 create_rag_tool。
"""
pipeline = RAGPipeline(
retriever=retriever,
llm=llm,
num_queries=num_queries,
rerank_top_n=rerank_top_n,
)
@tool
def search_knowledge_base_sync(query: str) -> str:
"""在知识库中搜索与查询相关的文档片段(同步版本)。
功能与异步版本相同:多路改写 → RRF融合 → 重排序 → 返回父文档。
Args:
query: 用户提出的问题或查询字符串
Returns:
格式化后的相关文档内容。
"""
try:
documents = pipeline.retrieve(query) # 内部调用异步方法并等待
if not documents:
return f"在知识库 '{collection_name}' 中未找到与 '{query}' 相关的信息。"
context = pipeline.format_context(documents)
return context
except Exception as e:
return f"检索过程中发生错误: {str(e)}"
return search_knowledge_base_sync