remove: 移除快速路径逻辑,全部走 React 模式
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m36s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m36s
This commit is contained in:
@@ -16,6 +16,7 @@ from app.core.intent_classifier import get_intent_classifier
|
|||||||
from app.logger import info, warning
|
from app.logger import info, warning
|
||||||
from app.main_graph.state import MainGraphState, CurrentAction
|
from app.main_graph.state import MainGraphState, CurrentAction
|
||||||
|
|
||||||
|
|
||||||
class AIAgentService:
|
class AIAgentService:
|
||||||
def __init__(self, checkpointer):
|
def __init__(self, checkpointer):
|
||||||
self.checkpointer = checkpointer
|
self.checkpointer = checkpointer
|
||||||
@@ -112,7 +113,7 @@ class AIAgentService:
|
|||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
async def process_message_stream(self, message: str, thread_id: str, model_name: str, user_id: str = "default_user"):
|
async def process_message_stream(self, message: str, thread_id: str, model_name: str, user_id: str = "default_user"):
|
||||||
"""流式处理消息,返回异步生成器(支持混合路由)"""
|
"""流式处理消息,返回异步生成器(全部走 React 模式)"""
|
||||||
graph = self.graphs.get(model_name)
|
graph = self.graphs.get(model_name)
|
||||||
if not graph:
|
if not graph:
|
||||||
raise ValueError(f"模型 '{model_name}' 未找到或未初始化")
|
raise ValueError(f"模型 '{model_name}' 未找到或未初始化")
|
||||||
@@ -128,7 +129,7 @@ class AIAgentService:
|
|||||||
"current_action": CurrentAction.NONE
|
"current_action": CurrentAction.NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
# ========== 新增:混合路由 ==========
|
# ========== 意图识别(保留用于日志) ==========
|
||||||
intent_result = await self.intent_classifier.classify(message)
|
intent_result = await self.intent_classifier.classify(message)
|
||||||
info(f"🧠 意图识别: {intent_result.intent_type} (置信度: {intent_result.confidence:.2f})")
|
info(f"🧠 意图识别: {intent_result.intent_type} (置信度: {intent_result.confidence:.2f})")
|
||||||
info(f"📝 推理: {intent_result.reasoning}")
|
info(f"📝 推理: {intent_result.reasoning}")
|
||||||
@@ -141,24 +142,14 @@ class AIAgentService:
|
|||||||
"reasoning": intent_result.reasoning
|
"reasoning": intent_result.reasoning
|
||||||
}
|
}
|
||||||
|
|
||||||
# 根据意图决定路径
|
# 发送路径决策事件(现在都是 react_loop)
|
||||||
use_react_loop = True
|
|
||||||
if intent_result.confidence >= 0.6:
|
|
||||||
intent_str = intent_result.intent_type.value
|
|
||||||
if intent_str in ["chitchat", "clarify"]:
|
|
||||||
use_react_loop = False
|
|
||||||
elif intent_str == "knowledge" and self.rag_pipeline:
|
|
||||||
use_react_loop = False
|
|
||||||
|
|
||||||
# 发送路径决策事件
|
|
||||||
yield {
|
yield {
|
||||||
"type": "path_decision",
|
"type": "path_decision",
|
||||||
"path": "react_loop" if use_react_loop else "fast",
|
"path": "react_loop",
|
||||||
"intent": intent_result.intent_type.value
|
"intent": intent_result.intent_type.value
|
||||||
}
|
}
|
||||||
# ====================================
|
# ========================================
|
||||||
|
|
||||||
if use_react_loop:
|
|
||||||
# ========== React 循环路径 ==========
|
# ========== React 循环路径 ==========
|
||||||
current_node = None
|
current_node = None
|
||||||
tool_calls_in_progress = {}
|
tool_calls_in_progress = {}
|
||||||
@@ -227,7 +218,7 @@ class AIAgentService:
|
|||||||
processed_event = {
|
processed_event = {
|
||||||
"type": "llm_token",
|
"type": "llm_token",
|
||||||
"node": node_name,
|
"node": node_name,
|
||||||
"token": token_content, # ✅ 改为 token
|
"token": token_content,
|
||||||
"reasoning_token": reasoning_token
|
"reasoning_token": reasoning_token
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,123 +278,3 @@ class AIAgentService:
|
|||||||
yield {
|
yield {
|
||||||
"type": "done"
|
"type": "done"
|
||||||
}
|
}
|
||||||
|
|
||||||
else:
|
|
||||||
# ========== 快速路径 ==========
|
|
||||||
intent_str = intent_result.intent_type.value
|
|
||||||
|
|
||||||
if intent_str == "chitchat":
|
|
||||||
# 闲聊直接回答
|
|
||||||
reply = await self._generate_fast_reply(
|
|
||||||
message,
|
|
||||||
"你是一个友好的助手,请礼貌回应用户的问候或闲聊。"
|
|
||||||
)
|
|
||||||
for char in reply:
|
|
||||||
yield {
|
|
||||||
"type": "llm_token",
|
|
||||||
"node": "fast_path",
|
|
||||||
"token": char # ✅ 改为 token
|
|
||||||
}
|
|
||||||
await asyncio.sleep(0.03)
|
|
||||||
|
|
||||||
elif intent_str == "clarify":
|
|
||||||
# 澄清反问
|
|
||||||
reply = await self._generate_fast_reply(
|
|
||||||
message,
|
|
||||||
"用户的问题不够明确,请礼貌地询问更多细节,以便更好地帮助用户。"
|
|
||||||
)
|
|
||||||
for char in reply:
|
|
||||||
yield {
|
|
||||||
"type": "llm_token",
|
|
||||||
"node": "fast_path",
|
|
||||||
"token": char # ✅ 改为 token
|
|
||||||
}
|
|
||||||
await asyncio.sleep(0.03)
|
|
||||||
|
|
||||||
elif intent_str == "knowledge" and self.rag_pipeline:
|
|
||||||
# 快速 RAG
|
|
||||||
yield {
|
|
||||||
"type": "node_start",
|
|
||||||
"node": "fast_rag"
|
|
||||||
}
|
|
||||||
yield {
|
|
||||||
"type": "reasoning",
|
|
||||||
"node": "fast_rag",
|
|
||||||
"content": "正在查询知识库..."
|
|
||||||
}
|
|
||||||
|
|
||||||
# 模拟 RAG 检索
|
|
||||||
await asyncio.sleep(0.3)
|
|
||||||
|
|
||||||
# 使用 RAG 生成回答
|
|
||||||
reply = await self._generate_rag_reply(message)
|
|
||||||
|
|
||||||
yield {
|
|
||||||
"type": "node_end",
|
|
||||||
"node": "fast_rag"
|
|
||||||
}
|
|
||||||
|
|
||||||
for char in reply:
|
|
||||||
yield {
|
|
||||||
"type": "llm_token",
|
|
||||||
"node": "fast_path",
|
|
||||||
"token": char # ✅ 改为 token
|
|
||||||
}
|
|
||||||
await asyncio.sleep(0.03)
|
|
||||||
|
|
||||||
else:
|
|
||||||
# 兜底:直接回答
|
|
||||||
reply = await self._generate_fast_reply(
|
|
||||||
message,
|
|
||||||
"请简洁回答用户的问题。"
|
|
||||||
)
|
|
||||||
for char in reply:
|
|
||||||
yield {
|
|
||||||
"type": "llm_token",
|
|
||||||
"node": "fast_path",
|
|
||||||
"token": char # ✅ 改为 token
|
|
||||||
}
|
|
||||||
await asyncio.sleep(0.03)
|
|
||||||
|
|
||||||
yield {
|
|
||||||
"type": "done"
|
|
||||||
}
|
|
||||||
|
|
||||||
async def _generate_fast_reply(self, message: str, system_prompt: str) -> str:
|
|
||||||
"""快速生成回复(不经过 React 循环)"""
|
|
||||||
# 使用默认模型生成回复
|
|
||||||
model_name = next(iter(self.graphs.keys()), "zhipu")
|
|
||||||
llm = get_all_chat_services().get(model_name)
|
|
||||||
|
|
||||||
if not llm:
|
|
||||||
return "抱歉,服务暂时不可用。"
|
|
||||||
|
|
||||||
prompt = f"{system_prompt}\n\n用户: {message}"
|
|
||||||
response = await llm.ainvoke(prompt)
|
|
||||||
return response.content if hasattr(response, 'content') else str(response)
|
|
||||||
|
|
||||||
async def _generate_rag_reply(self, message: str) -> str:
|
|
||||||
"""使用 RAG 生成回复"""
|
|
||||||
if not self.rag_pipeline:
|
|
||||||
return await self._generate_fast_reply(message, "请简洁回答用户的问题。")
|
|
||||||
|
|
||||||
# 检索文档
|
|
||||||
docs = await self.rag_pipeline.aretrieve(message)
|
|
||||||
context = self.rag_pipeline.format_context(docs)
|
|
||||||
|
|
||||||
# 生成回答
|
|
||||||
model_name = next(iter(self.graphs.keys()), "zhipu")
|
|
||||||
llm = get_all_chat_services().get(model_name)
|
|
||||||
|
|
||||||
if not llm:
|
|
||||||
return "抱歉,服务暂时不可用。"
|
|
||||||
|
|
||||||
prompt = f"""请根据以下参考文档回答用户问题。
|
|
||||||
|
|
||||||
参考文档:
|
|
||||||
{context or "(无相关文档)"}
|
|
||||||
|
|
||||||
用户问题: {message}
|
|
||||||
"""
|
|
||||||
response = await llm.ainvoke(prompt)
|
|
||||||
return response.content if hasattr(response, 'content') else str(response)
|
|
||||||
@@ -143,7 +143,7 @@ def _handle_ai_response():
|
|||||||
# 1. 处理 LLM Token 流 (打字机效果)
|
# 1. 处理 LLM Token 流 (打字机效果)
|
||||||
if event_type == "llm_token":
|
if event_type == "llm_token":
|
||||||
# 确保只处理来自 LLM 的 token,避免将工具的输出作为 token 显示
|
# 确保只处理来自 LLM 的 token,避免将工具的输出作为 token 显示
|
||||||
if event.get("node") in ("llm_call", "fallback", "fast_path"):
|
if event.get("node") in ("llm_call", "fallback"):
|
||||||
token = str(event.get("token", ""))
|
token = str(event.get("token", ""))
|
||||||
reasoning_token = str(event.get("reasoning_token", ""))
|
reasoning_token = str(event.get("reasoning_token", ""))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user