172 lines
5.5 KiB
Python
172 lines
5.5 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""
|
|||
|
|
完整的混合路由测试脚本
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 添加后端路径
|
|||
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / "backend"))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_imports():
|
|||
|
|
"""测试所有导入是否正常"""
|
|||
|
|
print("="*70)
|
|||
|
|
print("🧪 步骤 1/5 - 测试导入")
|
|||
|
|
print("="*70)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.model_services.chat_services import get_chat_service, get_small_llm_service
|
|||
|
|
print("✅ chat_services 导入成功")
|
|||
|
|
|
|||
|
|
from app.main_graph.nodes.hybrid_router import (
|
|||
|
|
hybrid_router_node,
|
|||
|
|
fast_chitchat_node,
|
|||
|
|
route_from_hybrid_decision,
|
|||
|
|
check_fast_path_success
|
|||
|
|
)
|
|||
|
|
print("✅ hybrid_router 导入成功")
|
|||
|
|
|
|||
|
|
from app.main_graph.utils.main_graph_builder import build_react_main_graph
|
|||
|
|
print("✅ main_graph_builder 导入成功")
|
|||
|
|
|
|||
|
|
from app.core.intent import react_reason, react_reason_async
|
|||
|
|
print("✅ intent 导入成功")
|
|||
|
|
|
|||
|
|
print("\n✅ 所有导入测试通过!")
|
|||
|
|
return True
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 导入测试失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_small_llm():
|
|||
|
|
"""测试小模型服务"""
|
|||
|
|
print("\n" + "="*70)
|
|||
|
|
print("🧪 步骤 2/5 - 测试小模型服务")
|
|||
|
|
print("="*70)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.model_services.chat_services import get_small_llm_service
|
|||
|
|
llm = get_small_llm_service()
|
|||
|
|
print(f"✅ 小模型服务获取成功: {type(llm)}")
|
|||
|
|
return True
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 小模型服务测试失败: {e}")
|
|||
|
|
print("💡 小模型服务不可用是正常的,会自动降级到大模型")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_rules_based_redirect():
|
|||
|
|
"""测试规则分流"""
|
|||
|
|
print("\n" + "="*70)
|
|||
|
|
print("🧪 步骤 3/5 - 测试规则分流")
|
|||
|
|
print("="*70)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.main_graph.nodes.hybrid_router import _rule_based_redirect
|
|||
|
|
|
|||
|
|
# 测试 1: 问候
|
|||
|
|
result = _rule_based_redirect("你好")
|
|||
|
|
if result and result.path == "fast_chitchat":
|
|||
|
|
print(f"✅ 问候测试通过: path={result.path}")
|
|||
|
|
else:
|
|||
|
|
print(f"⚠️ 问候测试: result={result}")
|
|||
|
|
|
|||
|
|
# 测试 2: 感谢
|
|||
|
|
result = _rule_based_redirect("谢谢")
|
|||
|
|
if result and result.path == "fast_chitchat":
|
|||
|
|
print(f"✅ 感谢测试通过: path={result.path}")
|
|||
|
|
else:
|
|||
|
|
print(f"⚠️ 感谢测试: result={result}")
|
|||
|
|
|
|||
|
|
# 测试 3: 子图关键词
|
|||
|
|
result = _rule_based_redirect("查一下通讯录")
|
|||
|
|
if result and result.path == "fast_tool":
|
|||
|
|
print(f"✅ 通讯录关键词测试通过: path={result.path}")
|
|||
|
|
else:
|
|||
|
|
print(f"⚠️ 通讯录关键词测试: result={result}")
|
|||
|
|
|
|||
|
|
# 测试 4: 复杂问题(不触发规则)
|
|||
|
|
result = _rule_based_redirect("什么是 LangGraph?")
|
|||
|
|
if result is None:
|
|||
|
|
print(f"✅ 复杂问题测试通过: 规则不触发,走模型分类")
|
|||
|
|
else:
|
|||
|
|
print(f"⚠️ 复杂问题测试: result={result}")
|
|||
|
|
|
|||
|
|
print("\n✅ 规则分流测试完成!")
|
|||
|
|
return True
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 规则分流测试失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_build_graph():
|
|||
|
|
"""测试图构建"""
|
|||
|
|
print("\n" + "="*70)
|
|||
|
|
print("🧪 步骤 4/5 - 测试图构建(混合路由模式)")
|
|||
|
|
print("="*70)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from app.main_graph.utils.main_graph_builder import build_react_main_graph
|
|||
|
|
|
|||
|
|
# 构建启用混合路由的图
|
|||
|
|
graph = build_react_main_graph(use_hybrid_router=True)
|
|||
|
|
print(f"✅ 图构建成功(混合路由)")
|
|||
|
|
|
|||
|
|
# 编译图
|
|||
|
|
compiled_graph = graph.compile()
|
|||
|
|
print(f"✅ 图编译成功(混合路由)")
|
|||
|
|
|
|||
|
|
# 构建纯 React 的图(兼容模式)
|
|||
|
|
graph_react = build_react_main_graph(use_hybrid_router=False)
|
|||
|
|
compiled_graph_react = graph_react.compile()
|
|||
|
|
print(f"✅ 图构建成功(纯 React)")
|
|||
|
|
|
|||
|
|
print("\n✅ 图构建测试完成!")
|
|||
|
|
return True
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ 图构建测试失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_summary():
|
|||
|
|
"""测试总结"""
|
|||
|
|
print("\n" + "="*70)
|
|||
|
|
print("🎉 完整的混合路由优化已实现!")
|
|||
|
|
print("="*70)
|
|||
|
|
print("\n✅ 已完成的优化:")
|
|||
|
|
print(" 1. 双模型服务 (llm + smallLLM)")
|
|||
|
|
print(" 2. 规则快速分流 (无 LLM, 超快速)")
|
|||
|
|
print(" 3. 轻量级意图分类 (smallLLM)")
|
|||
|
|
print(" 4. 快速路径 (fast_chitchat, fast_rag, fast_tool)")
|
|||
|
|
print(" 5. 自动升级机制 (快速路径失败 -> React 循环)")
|
|||
|
|
print(" 6. SSE 事件增强 (intent_classified, path_decision, fast_path_*)")
|
|||
|
|
print(" 7. 向后兼容 (可切换 use_hybrid_router=True/False)")
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
print("\n" + "🚀"*10)
|
|||
|
|
print("🚀 混合路由系统测试")
|
|||
|
|
print("🚀"*10 + "\n")
|
|||
|
|
|
|||
|
|
results = []
|
|||
|
|
results.append(test_imports())
|
|||
|
|
results.append(test_small_llm())
|
|||
|
|
results.append(test_rules_based_redirect())
|
|||
|
|
results.append(test_build_graph())
|
|||
|
|
test_summary()
|
|||
|
|
|
|||
|
|
if all(results):
|
|||
|
|
print("\n✅ 所有测试通过!")
|
|||
|
|
sys.exit(0)
|
|||
|
|
else:
|
|||
|
|
print("\n⚠️ 部分测试失败,请检查")
|
|||
|
|
sys.exit(1)
|