test_routing_modes.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # test_routing_modes.py - 测试不同路由模式的功能
  2. import sys
  3. import os
  4. # 添加项目根目录到sys.path,以便导入app_config.py
  5. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. def test_routing_modes():
  7. """测试不同路由模式的配置和分类器行为"""
  8. print("=== 路由模式测试 ===")
  9. # 1. 测试配置参数
  10. try:
  11. import app_config
  12. print(f"✓ 配置导入成功")
  13. print(f"当前路由模式: {getattr(app_config, 'QUESTION_ROUTING_MODE', '未找到')}")
  14. except ImportError as e:
  15. print(f"✗ 配置导入失败: {e}")
  16. return False
  17. # 2. 测试分类器
  18. try:
  19. from agent.classifier import QuestionClassifier, ClassificationResult
  20. classifier = QuestionClassifier()
  21. print(f"✓ 分类器创建成功")
  22. # 测试问题
  23. test_questions = [
  24. "查询本月服务区营业额",
  25. "你好,请介绍一下平台功能",
  26. "请问负责每个服务区的经理的名字是什么?"
  27. ]
  28. # 临时修改路由模式进行测试
  29. original_mode = getattr(app_config, 'QUESTION_ROUTING_MODE', 'hybrid')
  30. for mode in ["hybrid", "llm_only", "database_direct", "chat_direct"]:
  31. print(f"\n--- 测试路由模式: {mode} ---")
  32. app_config.QUESTION_ROUTING_MODE = mode
  33. for question in test_questions:
  34. try:
  35. result = classifier.classify(question)
  36. print(f"问题: {question}")
  37. print(f" 分类: {result.question_type}")
  38. print(f" 置信度: {result.confidence}")
  39. print(f" 方法: {result.method}")
  40. print(f" 理由: {result.reason[:50]}...")
  41. except Exception as e:
  42. print(f" 分类异常: {e}")
  43. # 恢复原始配置
  44. app_config.QUESTION_ROUTING_MODE = original_mode
  45. print(f"\n✓ 分类器测试完成")
  46. except ImportError as e:
  47. print(f"✗ 分类器导入失败: {e}")
  48. return False
  49. except Exception as e:
  50. print(f"✗ 分类器测试异常: {e}")
  51. return False
  52. # 3. 测试Agent状态
  53. try:
  54. from agent.state import AgentState
  55. print(f"✓ Agent状态定义正确")
  56. except ImportError as e:
  57. print(f"✗ Agent状态导入失败: {e}")
  58. return False
  59. # 4. 测试Agent工作流创建(基础测试,不实际运行)
  60. try:
  61. from agent.citu_agent import CituLangGraphAgent
  62. print(f"✓ Agent类导入成功")
  63. # 注意:这里只测试导入,不实际创建Agent实例
  64. # 因为可能涉及LLM连接等复杂依赖
  65. except ImportError as e:
  66. print(f"✗ Agent类导入失败: {e}")
  67. return False
  68. except Exception as e:
  69. print(f"警告: Agent相关模块可能有依赖问题: {e}")
  70. print(f"\n=== 路由模式测试完成 ===")
  71. return True
  72. if __name__ == "__main__":
  73. success = test_routing_modes()
  74. if success:
  75. print("✓ 所有测试通过!路由模式功能实现成功!")
  76. else:
  77. print("✗ 测试失败,请检查实现。")