from utils.logger import log from .ai_model import AIModel class IntentClassifier: """ 一个专门用于识别用户输入意uto的分类器。 它使用LLM来判断用户的消息属于预定义的哪个类别。 """ def __init__(self, ai_model: AIModel): self.ai_model = ai_model self.VALID_INTENTS = ['PROVIDING_TRAVEL_INFO', 'GREETING', 'OTHER'] def _build_prompt(self, message: str) -> str: """构建用于意图分类的、轻量级的Prompt。""" return f""" 你的任务是分析用户的单条消息,并判断其意图。 请从以下几个意图中选择一个最匹配的,并且只返回这个意图的字符串,不要添加任何其他解释或标点符号。 可用意图: - PROVIDING_TRAVEL_INFO: 用户明确提到了旅行相关的具体信息,如目的地、时间、预算、人数等。例如:"我想去巴黎玩一周" 或 "预算5000块钱"。 - GREETING: 用户只是在打招呼或进行简单的问候。例如:"你好", "hi", "在吗?"。 - OTHER: 不属于以上任何一种的通用查询或闲聊。例如:"法国有什么好玩的?" 或 "帮我订票"。 用户消息: --- {message} --- 你的判断结果 (请只返回一个意图字符串): """ def classify(self, message: str) -> str: """对给定的消息进行意图分类。""" log.info(f"🚀 开始对消息进行意图分类: '{message[:30]}...'") prompt = self._build_prompt(message) try: response = self.ai_model.chat_completion( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0.0, max_tokens=10 ) intent = response.strip().replace("'", "").replace("\"", "") if intent in self.VALID_INTENTS: log.info(f"✅ 意图分类成功: {intent}") return intent else: log.warning(f"⚠️ LLM返回了未知的意图 '{intent}',将回退到 'OTHER'") return 'OTHER' except Exception as e: log.error(f"❌ 意图分类时发生错误: {e}", exc_info=True) return 'OTHER'