Spaces:
Running
Running
fix : recover file
Browse files- modules/config_loader.py +33 -0
- modules/knowledge_base.py +29 -0
- modules/travel_assistant.py +45 -0
modules/config_loader.py
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/config_loader.py
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from utils.logger import log
|
| 5 |
+
|
| 6 |
+
class ConfigLoader:
|
| 7 |
+
def __init__(self, config_dir: Path = Path("./config")):
|
| 8 |
+
self.config_dir = config_dir
|
| 9 |
+
self.cities = {}
|
| 10 |
+
self.personas = {}
|
| 11 |
+
self.interests = {}
|
| 12 |
+
try:
|
| 13 |
+
self._load_all()
|
| 14 |
+
log.info("✅ 所有配置文件加载成功")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
log.error(f"❌ 配置文件加载失败: {e}", exc_info=True)
|
| 17 |
+
raise
|
| 18 |
+
|
| 19 |
+
def _load_all(self):
|
| 20 |
+
# 加载城市
|
| 21 |
+
with open(self.config_dir / "cities.json", 'r', encoding='utf-8') as f:
|
| 22 |
+
cities_data = json.load(f)
|
| 23 |
+
for city in cities_data['cities']:
|
| 24 |
+
for alias in [city['name']] + city.get('aliases', []):
|
| 25 |
+
self.cities[alias.lower()] = city
|
| 26 |
+
|
| 27 |
+
# 加载 personas
|
| 28 |
+
with open(self.config_dir / "personas.json", 'r', encoding='utf-8') as f:
|
| 29 |
+
self.personas = json.load(f)['personas']
|
| 30 |
+
|
| 31 |
+
# 加载兴趣
|
| 32 |
+
with open(self.config_dir / "interests.json", 'r', encoding='utf-8') as f:
|
| 33 |
+
self.interests = json.load(f)['interests']
|
modules/knowledge_base.py
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/knowledge_base.py
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from utils.logger import log
|
| 5 |
+
|
| 6 |
+
class KnowledgeBase:
|
| 7 |
+
def __init__(self, file_path: Path = Path("./config/general_travelplan.json")):
|
| 8 |
+
self.knowledge = []
|
| 9 |
+
try:
|
| 10 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 11 |
+
self.knowledge = json.load(f).get('clean_knowledge', [])
|
| 12 |
+
log.info(f"✅ 知识库加载完成")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
log.error(f"❌ 知识库加载失败: {e}", exc_info=True)
|
| 15 |
+
raise
|
| 16 |
+
|
| 17 |
+
def search(self, query: str) -> list:
|
| 18 |
+
relevant_knowledge = []
|
| 19 |
+
query_lower = query.lower()
|
| 20 |
+
|
| 21 |
+
for item in self.knowledge:
|
| 22 |
+
# 简单实现:如果查询的城市在知识库的目的地中,则返回该知识
|
| 23 |
+
destinations = item.get('knowledge', {}).get('travel_knowledge', {}).get('destination_info', {}).get('primary_destinations', [])
|
| 24 |
+
for dest in destinations:
|
| 25 |
+
if dest.lower() in query_lower:
|
| 26 |
+
if item not in relevant_knowledge:
|
| 27 |
+
relevant_knowledge.append(item)
|
| 28 |
+
break
|
| 29 |
+
return relevant_knowledge
|
modules/travel_assistant.py
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/travel_assistant.py
|
| 2 |
+
from .config_loader import ConfigLoader
|
| 3 |
+
from .ai_model import AIModel
|
| 4 |
+
from .knowledge_base import KnowledgeBase
|
| 5 |
+
from .info_extractor import InfoExtractor
|
| 6 |
+
from .session_manager import SessionManager
|
| 7 |
+
from .response_generator import ResponseGenerator
|
| 8 |
+
from utils.logger import log
|
| 9 |
+
|
| 10 |
+
class TravelAssistant:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
# 依赖注入:在这里实例化所有需要的模块
|
| 13 |
+
log.info("开始初始化 Travel Assistant 核心模块...")
|
| 14 |
+
self.config = ConfigLoader()
|
| 15 |
+
self.kb = KnowledgeBase()
|
| 16 |
+
self.ai_model = AIModel()
|
| 17 |
+
self.session_manager = SessionManager()
|
| 18 |
+
self.info_extractor = InfoExtractor(self.config)
|
| 19 |
+
self.response_generator = ResponseGenerator(self.ai_model, self.kb)
|
| 20 |
+
log.info("✅ Travel Assistant 核心模块全部初始化完成!")
|
| 21 |
+
|
| 22 |
+
def chat(self, message: str, session_id: str, history: list):
|
| 23 |
+
# 1. 获取或创建会话
|
| 24 |
+
session_state = self.session_manager.get_or_create_session(session_id)
|
| 25 |
+
current_session_id = session_state['session_id']
|
| 26 |
+
|
| 27 |
+
# 2. 从用户输入中提取信息
|
| 28 |
+
extracted_info = self.info_extractor.extract(message)
|
| 29 |
+
|
| 30 |
+
# 3. 更新会话状态
|
| 31 |
+
if extracted_info:
|
| 32 |
+
self.session_manager.update_session(current_session_id, extracted_info)
|
| 33 |
+
# 重新获取更新后的状态
|
| 34 |
+
session_state = self.session_manager.get_or_create_session(current_session_id)
|
| 35 |
+
|
| 36 |
+
# 4. 生成回复
|
| 37 |
+
bot_response = self.response_generator.generate(message, session_state)
|
| 38 |
+
|
| 39 |
+
# 5. 格式化状态信息用于前端显示
|
| 40 |
+
status_info = self.session_manager.format_session_info(session_state)
|
| 41 |
+
|
| 42 |
+
# 6. 更新对话历史
|
| 43 |
+
new_history = history + [[message, bot_response]]
|
| 44 |
+
|
| 45 |
+
return bot_response, current_session_id, status_info, new_history
|