Spaces:
Sleeping
Sleeping
File size: 4,749 Bytes
e53ed5b 2b20519 6d36a0d e53ed5b 2b20519 e53ed5b 8c24a9d 6d36a0d e53ed5b 86c5051 e53ed5b d81440f e53ed5b d81440f e53ed5b d81440f cd4408f e53ed5b d81440f cd4408f 6d36a0d b180c39 6d36a0d b180c39 cd4408f 6d36a0d 86c5051 6d36a0d cd4408f d81440f e53ed5b cd4408f d81440f e53ed5b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# modules/travel_assistant.py - 正确版本
from .config_loader import ConfigLoader
from .ai_model import AIModel
from .knowledge_base import KnowledgeBase
from .intent_classifier import IntentClassifier
from .info_extractor import InfoExtractor
from .session_manager import SessionManager
from .response_generator import ResponseGenerator
from utils.logger import log
class TravelAssistant:
def __init__(self):
# 依赖注入:在这里实例化所有需要的模块
log.info("开始初始化 Travel Assistant 核心模块...")
self.config = ConfigLoader()
self.kb = KnowledgeBase()
self.ai_model = AIModel()
self.session_manager = SessionManager()
self.info_extractor = InfoExtractor()
self.intent_classifier = IntentClassifier(self.ai_model)
self.response_generator = ResponseGenerator(self.ai_model, self.kb)
log.info("✅ Travel Assistant 核心模块全部初始化完成!")
def chat(self, message: str, session_id: str, history: list, persona_key: str = None):
log.info(f"📞 === 聊天请求开始 ===")
log.info(f"📝 消息: '{message[:30]}...'")
log.info(f"🆔 前端传入session_id: '{session_id}'")
log.info(f"🎭 persona_key: '{persona_key}'")
# 1. 获取或创建会话
session_state = self.session_manager.get_or_create_session(session_id)
current_session_id = session_state['session_id']
log.info(f"📋 使用后端session_id: '{current_session_id}'")
# 2. 设置persona
if persona_key and persona_key in self.config.personas:
persona_info = {
'key': persona_key,
'name': self.config.personas[persona_key]['name'],
'style': self.config.personas[persona_key]['style'],
'source': 'frontend_selection'
}
self.session_manager.update_session(current_session_id, {'persona': persona_info})
session_state = self.session_manager.get_or_create_session(current_session_id)
log.info(f"✅ 设置persona: {persona_info['name']}")
# 3. 意图识别 (前置守卫)
raw_intent = self.intent_classifier.classify(message)
log.info(f"🔍 用户意图识别结果: '{raw_intent}'")
extracted_info = {}
intent = 'OTHER'
if 'PROVIDING_TRAVEL_INFO' in raw_intent:
intent = 'PROVIDING_TRAVEL_INFO'
elif 'GREETING' in raw_intent:
intent = 'GREETING'
log.info(f"✅ 解析后用户意图: '{intent}'")
# 4.: 根据意图进行逻辑分流
if intent == 'PROVIDING_TRAVEL_INFO':
# 场景A: 用户提供了旅行信息,执行完整的信息提取
extracted_info = self.info_extractor.extract(message)
if extracted_info:
self.session_manager.update_session(current_session_id, extracted_info)
session_state = self.session_manager.get_or_create_session(current_session_id)
# 无论是否提取成功,都让 response_generator 来生成上下文感知的回复
bot_response = self.response_generator.generate(message, session_state, extracted_info)
else:
# 场景B: 用户意图是问候或其它,直接生成引导性回复,完全绕过信息提取
log.info(f"💬 意图为 '{intent}',绕过信息提取,直接生成引导性回复。")
if intent == 'GREETING':
bot_response = "您好!很高兴能为您规划旅程。请问您想去哪里,玩几天,预算大概是多少呢?您可以输入:我想去巴黎玩三天"
elif intent == 'INQUIRY':
bot_response = "当然!为了给您更精准的推荐,可以告诉我您的兴趣偏好吗?比如您对历史文化、自然风光、美食购物还是夜生活更感兴趣呢?这样我才能更好地为您量身定制哦!"
else: # 'OTHER'
# 对于其它问题,可以调用通用的生成器,让它决定如何回复
bot_response = self.response_generator.generate(message, session_state, {})
# 6. 返回结果
status_info = self.session_manager.format_session_info(session_state)
new_history = history + [[message, bot_response]]
log.info(f"✅ 聊天完成,返回session_id: {current_session_id}")
log.info(f"📊 最终状态: {self.session_manager._get_session_summary(current_session_id)}")
log.info(f"📞 === 聊天请求结束 ===")
return bot_response, current_session_id, status_info, new_history |