Files
LocalAgent/tests/test_intent_classifier.py
Mimikko-zeus 68f4f01cd7 feat:增强需求澄清与任务管理功能
更新了 .env.example,新增聊天模型配置,以提升对话处理能力。
增强了 README.md,反映了包括需求澄清、代码复用和自动重试在内的新功能。
重构了 agent.py,以支持多模型交互,并为无法在本地执行的任务新增了引导处理逻辑。
改进了 SandboxRunner,增加了任务执行成功校验,并加入了工作区清理功能。

扩展了 HistoryManager,支持任务摘要生成以及记录的批量删除。
优化了 chat_view.py 和 history_view.py 中的 UI 组件,提升用户体验,包括 Markdown 渲染和任务管理选项。
2026-01-07 12:35:27 +08:00

97 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
意图分类器单元测试
"""
import unittest
import sys
from pathlib import Path
# 添加项目根目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent))
from intent.labels import CHAT, EXECUTION, GUIDANCE, VALID_LABELS, EXECUTION_CONFIDENCE_THRESHOLD
class TestIntentLabels(unittest.TestCase):
"""意图标签测试"""
def test_labels_defined(self):
"""测试标签已定义"""
self.assertEqual(CHAT, "chat")
self.assertEqual(EXECUTION, "execution")
self.assertEqual(GUIDANCE, "guidance")
def test_valid_labels(self):
"""测试有效标签集合"""
self.assertIn(CHAT, VALID_LABELS)
self.assertIn(EXECUTION, VALID_LABELS)
self.assertIn(GUIDANCE, VALID_LABELS)
self.assertEqual(len(VALID_LABELS), 3)
def test_confidence_threshold(self):
"""测试置信度阈值"""
self.assertGreater(EXECUTION_CONFIDENCE_THRESHOLD, 0)
self.assertLessEqual(EXECUTION_CONFIDENCE_THRESHOLD, 1)
class TestIntentClassifierParsing(unittest.TestCase):
"""意图分类器解析测试(不需要 API"""
def setUp(self):
from intent.classifier import IntentClassifier
self.classifier = IntentClassifier()
def test_parse_valid_chat_response(self):
"""测试解析有效的 chat 响应"""
response = '{"label": "chat", "confidence": 0.95, "reason": "这是一个问答"}'
result = self.classifier._parse_response(response)
self.assertEqual(result.label, CHAT)
self.assertEqual(result.confidence, 0.95)
self.assertEqual(result.reason, "这是一个问答")
def test_parse_valid_execution_response(self):
"""测试解析有效的 execution 响应"""
response = '{"label": "execution", "confidence": 0.9, "reason": "需要复制文件"}'
result = self.classifier._parse_response(response)
self.assertEqual(result.label, EXECUTION)
self.assertEqual(result.confidence, 0.9)
def test_parse_low_confidence_execution(self):
"""测试低置信度的 execution 降级为 chat"""
response = '{"label": "execution", "confidence": 0.5, "reason": "不太确定"}'
result = self.classifier._parse_response(response)
# 低于阈值应该降级为 chat
self.assertEqual(result.label, CHAT)
def test_parse_invalid_label(self):
"""测试无效标签降级为 chat"""
response = '{"label": "unknown", "confidence": 0.9, "reason": "测试"}'
result = self.classifier._parse_response(response)
self.assertEqual(result.label, CHAT)
def test_parse_invalid_json(self):
"""测试无效 JSON 降级为 chat"""
response = 'not a json'
result = self.classifier._parse_response(response)
self.assertEqual(result.label, CHAT)
self.assertEqual(result.confidence, 0.0)
def test_extract_json_with_prefix(self):
"""测试从带前缀的文本中提取 JSON"""
text = 'Here is the result: {"label": "chat", "confidence": 0.8, "reason": "test"}'
json_str = self.classifier._extract_json(text)
self.assertTrue(json_str.startswith('{'))
self.assertTrue(json_str.endswith('}'))
def test_extract_json_with_suffix(self):
"""测试从带后缀的文本中提取 JSON"""
text = '{"label": "chat", "confidence": 0.8, "reason": "test"} That is my answer.'
json_str = self.classifier._extract_json(text)
self.assertTrue(json_str.startswith('{'))
self.assertTrue(json_str.endswith('}'))
if __name__ == '__main__':
unittest.main()