refactor: 重构快速路径流程,统一通过 llm_call 输出
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m31s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m31s
- 重构 fast_paths.py,让 fast_chitchat 和 fast_rag 都进入 llm_call 而不是直接设置 final_result - 修改 check_fast_path_success 函数返回 'llm_call' 而不是 'success' - 更新 main_graph_builder.py 的条件边配置,支持路由到 llm_call - 在快速路径节点中添加清除 state.final_result 的逻辑,避免复用旧结果 - 重构 RAG 工具初始化方式,使用模块级变量管理 - 修改 finalize.py 让它返回 final_result - 更新 agent_service.py 的 RAG 工具注入方式 - 简化 hybrid_router.py 的代码结构 - 清理 rag_nodes.py 的全局变量相关代码 - 更新相关测试文件
This commit is contained in:
@@ -3,12 +3,43 @@ from app.rag.tools import create_rag_tool
|
||||
from app.rag.retriever import create_parent_hybrid_retriever
|
||||
from app.model_services import get_embedding_service
|
||||
from app.logger import info, warning
|
||||
import sys
|
||||
|
||||
# 全局 RAG 工具
|
||||
_rag_tool = None
|
||||
_initialized = False
|
||||
|
||||
|
||||
def get_rag_tool() -> callable:
|
||||
"""获取全局 RAG 工具"""
|
||||
return _rag_tool
|
||||
|
||||
|
||||
def is_initialized() -> bool:
|
||||
"""检查是否已初始化"""
|
||||
return _initialized
|
||||
|
||||
|
||||
async def init_rag_tool(local_llm_creator, force: bool = False):
|
||||
"""
|
||||
初始化 RAG 工具(注册到模块级变量)
|
||||
|
||||
Args:
|
||||
local_llm_creator: 返回 LLM 实例的函数
|
||||
force: 是否强制重新初始化
|
||||
|
||||
Returns:
|
||||
RAG 工具(@tool 装饰函数)或 None
|
||||
"""
|
||||
global _rag_tool, _initialized
|
||||
|
||||
# 防止重复初始化
|
||||
if _initialized and not force:
|
||||
info("[RAG] 已初始化,跳过")
|
||||
return _rag_tool
|
||||
|
||||
async def init_rag_tool(local_llm_creator):
|
||||
"""初始化 RAG 工具,失败返回 None"""
|
||||
try:
|
||||
info("🔄 正在初始化 RAG 检索系统...")
|
||||
# 使用统一的嵌入服务获取接口
|
||||
embeddings = get_embedding_service()
|
||||
retriever = create_parent_hybrid_retriever(
|
||||
collection_name="rag_documents",
|
||||
@@ -16,12 +47,26 @@ async def init_rag_tool(local_llm_creator):
|
||||
embeddings=embeddings
|
||||
)
|
||||
rewrite_llm = local_llm_creator()
|
||||
|
||||
rag_tool = create_rag_tool(
|
||||
retriever, rewrite_llm,
|
||||
num_queries=3, rerank_top_n=5
|
||||
retriever=retriever,
|
||||
llm=rewrite_llm,
|
||||
num_queries=3,
|
||||
rerank_top_n=5
|
||||
)
|
||||
info("✅ RAG 检索工具初始化成功(全异步版本)")
|
||||
|
||||
_rag_tool = rag_tool
|
||||
_initialized = True
|
||||
info(f"✅ RAG 检索工具初始化成功 (id={id(rag_tool)})")
|
||||
return rag_tool
|
||||
|
||||
except Exception as e:
|
||||
warning(f"⚠️ RAG 检索工具初始化失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def reset():
|
||||
"""重置(用于测试)"""
|
||||
global _rag_tool, _initialized
|
||||
_rag_tool = None
|
||||
_initialized = False
|
||||
|
||||
Reference in New Issue
Block a user