test_thinking_control.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. """
  3. 测试thinking内容控制功能
  4. 验证DISPLAY_RESULT_THINKING参数是否正确控制thinking内容的显示/隐藏
  5. """
  6. import sys
  7. import os
  8. # 添加项目根目录到Python路径
  9. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  10. def test_thinking_removal():
  11. """测试thinking内容移除功能"""
  12. from customllm.base_llm_chat import BaseLLMChat
  13. # 创建一个测试类来测试_remove_thinking_content方法
  14. class TestLLM(BaseLLMChat):
  15. def submit_prompt(self, prompt, **kwargs):
  16. return "测试响应"
  17. # 创建测试实例
  18. test_llm = TestLLM(config={})
  19. # 测试用例
  20. test_cases = [
  21. # 基本thinking标签
  22. {
  23. "input": "<think>这是思考内容</think>这是最终答案",
  24. "expected": "这是最终答案"
  25. },
  26. # 多行thinking标签
  27. {
  28. "input": "<think>\n这是多行\n思考内容\n</think>\n\n这是最终答案",
  29. "expected": "这是最终答案"
  30. },
  31. # 大小写不敏感
  32. {
  33. "input": "<THINK>大写思考</THINK>最终答案",
  34. "expected": "最终答案"
  35. },
  36. # 多个thinking标签
  37. {
  38. "input": "<think>第一段思考</think>中间内容<think>第二段思考</think>最终答案",
  39. "expected": "中间内容最终答案"
  40. },
  41. # 没有thinking标签
  42. {
  43. "input": "这是没有thinking标签的普通文本",
  44. "expected": "这是没有thinking标签的普通文本"
  45. },
  46. # 空文本
  47. {
  48. "input": "",
  49. "expected": ""
  50. },
  51. # None输入
  52. {
  53. "input": None,
  54. "expected": None
  55. }
  56. ]
  57. print("=== 测试thinking内容移除功能 ===")
  58. for i, test_case in enumerate(test_cases, 1):
  59. input_text = test_case["input"]
  60. expected = test_case["expected"]
  61. result = test_llm._remove_thinking_content(input_text)
  62. if result == expected:
  63. print(f"✅ 测试用例 {i}: 通过")
  64. else:
  65. print(f"❌ 测试用例 {i}: 失败")
  66. print(f" 输入: {repr(input_text)}")
  67. print(f" 期望: {repr(expected)}")
  68. print(f" 实际: {repr(result)}")
  69. print()
  70. def test_config_integration():
  71. """测试配置集成"""
  72. print("=== 测试配置集成 ===")
  73. try:
  74. from app_config import DISPLAY_RESULT_THINKING
  75. print(f"✅ 成功导入配置: DISPLAY_RESULT_THINKING = {DISPLAY_RESULT_THINKING}")
  76. from customllm.base_llm_chat import BaseLLMChat
  77. print("✅ 成功导入BaseLLMChat类")
  78. # 检查类中是否正确导入了配置
  79. import customllm.base_llm_chat as base_module
  80. if hasattr(base_module, 'DISPLAY_RESULT_THINKING'):
  81. print(f"✅ BaseLLMChat模块中的配置: DISPLAY_RESULT_THINKING = {base_module.DISPLAY_RESULT_THINKING}")
  82. else:
  83. print("❌ BaseLLMChat模块中未找到DISPLAY_RESULT_THINKING配置")
  84. except ImportError as e:
  85. print(f"❌ 导入失败: {e}")
  86. print()
  87. def test_vanna_instance():
  88. """测试Vanna实例的thinking处理"""
  89. print("=== 测试Vanna实例thinking处理 ===")
  90. try:
  91. from common.vanna_instance import get_vanna_instance
  92. vn = get_vanna_instance()
  93. print(f"✅ 成功获取Vanna实例: {type(vn).__name__}")
  94. # 检查实例是否有_remove_thinking_content方法
  95. if hasattr(vn, '_remove_thinking_content'):
  96. print("✅ Vanna实例具有_remove_thinking_content方法")
  97. # 测试方法
  98. test_text = "<think>测试思考</think>测试结果"
  99. cleaned = vn._remove_thinking_content(test_text)
  100. if cleaned == "测试结果":
  101. print("✅ thinking内容移除功能正常工作")
  102. else:
  103. print(f"❌ thinking内容移除异常: {repr(cleaned)}")
  104. else:
  105. print("❌ Vanna实例缺少_remove_thinking_content方法")
  106. except Exception as e:
  107. print(f"❌ 测试Vanna实例失败: {e}")
  108. print()
  109. def main():
  110. """主测试函数"""
  111. print("开始测试thinking内容控制功能...\n")
  112. # 运行所有测试
  113. test_thinking_removal()
  114. test_config_integration()
  115. test_vanna_instance()
  116. print("测试完成!")
  117. if __name__ == "__main__":
  118. main()