任务#1701 产出(测试工程师)
> 自动落盘:2026-11-04 19:21
---
检查是否连续两次以上相同意图,避免重复原句
if len(self.history) >= 2 and self.history[-1].get('intent') == intent: last_reply = self.history[-1].get('reply', '') if last_reply in script or script.endswith(last_reply): script += '(换个说法)'
reply = f"[{self.turn}] {script}" self.history.append({ 'role': 'user', 'content': text, 'intent': intent }) self.history.append({ 'role': 'assistant', 'reply': reply, 'intent': intent })
log_event = { 'turn': self.turn, 'intent': intent, 'text': text, 'reply': reply } self.log(log_event) self.turn += 1
return {'intent': intent, 'reply': reply, 'confidence': confidence, 'turn': self.turn - 1}
def log(self, event: dict): filepath = os.path.join(os.path.dirname(__file__), 'session_log.jsonl') try: with open(filepath, 'a', encoding='utf-8') as f: f.write(json.dumps(event, ensure_ascii=False) + '\n') except Exception: pass # 静默忽略,不阻塞主流程
if __name__ == '__main__': dialogue = Dialogue()
# 自测用例:问候 / 报价 / 希望加功能 / 投诉 / 闲聊 test_cases = [ "你好", "这个产品多少钱?", "我希望加上语音识别功能", "太贵了,能不能便宜点?", "今天天气怎么样?" ]
for i, case in enumerate(test_cases): result = dialogue.respond(case) assert result['intent'] != '', f"[{i}] 意图为空" assert result['reply'], f"[{i}] reply 为空" print(f"{case} -> {result}")
# 断言两次相同意图 reply 不同(故意重复一次) r1 = dialogue.respond("你好") r2 = dialogue.respond("你好") assert r1['reply'] != r2['reply'], "重复意图应生成不同回复" print("\n✅ All assertions passed!")
---
【session_log.jsonl】(运行后自动生成,示例内容)
{"turn": 0, "intent": "greeting", "text": "你好", "reply": "[0] 你好!有什么可以帮你的?"} {"turn": 1, "intent": "pricing", "text": "这个产品多少钱?", "reply": "[1] 价格是 ¥599/年,支持按月订阅。"} {"turn": 2, "intent": "feature_request", "text": "我希望加上语音识别功能", "reply": "[2] 已记录需求:语音识别。我们会评估优先级。"} {"turn": 3, "intent": "complaint", "text": "太贵了,能不能便宜点?", "reply": "[3] 理解你的顾虑,我们可以提供学生优惠。"} {"turn": 4, "intent": "chitchat", "text": "今天天气怎么样?", "reply": "[4] 今天晴转多云,气温25℃。出门记得带伞哦~"}