first commit
This commit is contained in:
1
src/dialogue/__init__.py
Normal file
1
src/dialogue/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# 对话模块
|
||||
BIN
src/dialogue/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
src/dialogue/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
src/dialogue/__pycache__/dialogue_manager.cpython-311.pyc
Normal file
BIN
src/dialogue/__pycache__/dialogue_manager.cpython-311.pyc
Normal file
Binary file not shown.
BIN
src/dialogue/__pycache__/realtime_chat.cpython-311.pyc
Normal file
BIN
src/dialogue/__pycache__/realtime_chat.cpython-311.pyc
Normal file
Binary file not shown.
276
src/dialogue/dialogue_manager.py
Normal file
276
src/dialogue/dialogue_manager.py
Normal file
@@ -0,0 +1,276 @@
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
from ..core.database import db_manager
|
||||
from ..core.models import WorkOrder, Conversation
|
||||
from ..core.llm_client import QwenClient
|
||||
from ..knowledge_base.knowledge_manager import KnowledgeManager
|
||||
from ..vehicle.vehicle_data_manager import VehicleDataManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DialogueManager:
|
||||
"""对话管理器"""
|
||||
|
||||
def __init__(self):
|
||||
self.llm_client = QwenClient()
|
||||
self.knowledge_manager = KnowledgeManager()
|
||||
self.vehicle_manager = VehicleDataManager()
|
||||
self.conversation_history = {} # 存储对话历史
|
||||
|
||||
def process_user_message(
|
||||
self,
|
||||
user_message: str,
|
||||
work_order_id: Optional[int] = None,
|
||||
user_id: Optional[str] = None,
|
||||
vehicle_id: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""处理用户消息"""
|
||||
try:
|
||||
# 搜索相关知识库(只搜索已验证的)
|
||||
knowledge_results = self.knowledge_manager.search_knowledge(
|
||||
user_message, top_k=3, verified_only=True
|
||||
)
|
||||
|
||||
# 获取车辆实时数据
|
||||
vehicle_data = None
|
||||
if vehicle_id:
|
||||
vehicle_data = self.vehicle_manager.get_latest_vehicle_data(vehicle_id)
|
||||
|
||||
# 构建上下文
|
||||
context = self._build_context(work_order_id, user_id)
|
||||
|
||||
# 准备知识库信息
|
||||
knowledge_context = ""
|
||||
if knowledge_results:
|
||||
knowledge_context = "相关知识库信息:\n"
|
||||
for i, result in enumerate(knowledge_results[:2], 1):
|
||||
knowledge_context += f"{i}. 问题: {result['question']}\n"
|
||||
knowledge_context += f" 答案: {result['answer']}\n"
|
||||
knowledge_context += f" 置信度: {result['confidence_score']:.2f}\n\n"
|
||||
|
||||
# 准备车辆数据信息
|
||||
vehicle_context = ""
|
||||
if vehicle_data:
|
||||
vehicle_context = "车辆实时数据:\n"
|
||||
for data_type, data_info in vehicle_data.items():
|
||||
vehicle_context += f"- {data_type}: {json.dumps(data_info['value'], ensure_ascii=False)}\n"
|
||||
vehicle_context += f" 更新时间: {data_info['timestamp']}\n"
|
||||
vehicle_context += "\n"
|
||||
|
||||
# 生成回复
|
||||
response_result = self.llm_client.generate_response(
|
||||
user_message=user_message,
|
||||
context=context,
|
||||
knowledge_base=[knowledge_context] if knowledge_context else None,
|
||||
vehicle_data=[vehicle_context] if vehicle_context else None
|
||||
)
|
||||
|
||||
if "error" in response_result:
|
||||
return response_result
|
||||
|
||||
# 保存对话记录
|
||||
conversation_id = self._save_conversation(
|
||||
work_order_id=work_order_id,
|
||||
user_message=user_message,
|
||||
assistant_response=response_result["response"],
|
||||
knowledge_used=json.dumps([r["id"] for r in knowledge_results], ensure_ascii=False)
|
||||
)
|
||||
|
||||
# 更新对话历史
|
||||
if user_id:
|
||||
if user_id not in self.conversation_history:
|
||||
self.conversation_history[user_id] = []
|
||||
self.conversation_history[user_id].append({
|
||||
"role": "user",
|
||||
"content": user_message,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
self.conversation_history[user_id].append({
|
||||
"role": "assistant",
|
||||
"content": response_result["response"],
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
|
||||
# 保持历史记录在限制范围内
|
||||
if len(self.conversation_history[user_id]) > 20: # 10轮对话
|
||||
self.conversation_history[user_id] = self.conversation_history[user_id][-20:]
|
||||
|
||||
return {
|
||||
"response": response_result["response"],
|
||||
"conversation_id": conversation_id,
|
||||
"knowledge_used": knowledge_results,
|
||||
"confidence_score": self._calculate_confidence(knowledge_results),
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理用户消息失败: {e}")
|
||||
return {"error": f"处理失败: {str(e)}"}
|
||||
|
||||
def _build_context(self, work_order_id: Optional[int], user_id: Optional[str]) -> str:
|
||||
"""构建对话上下文"""
|
||||
context_parts = []
|
||||
|
||||
# 添加工单信息
|
||||
if work_order_id:
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
work_order = session.query(WorkOrder).filter(
|
||||
WorkOrder.id == work_order_id
|
||||
).first()
|
||||
|
||||
if work_order:
|
||||
context_parts.append(f"当前工单信息:")
|
||||
context_parts.append(f"工单号: {work_order.order_id}")
|
||||
context_parts.append(f"标题: {work_order.title}")
|
||||
context_parts.append(f"描述: {work_order.description}")
|
||||
context_parts.append(f"类别: {work_order.category}")
|
||||
context_parts.append(f"优先级: {work_order.priority}")
|
||||
context_parts.append(f"状态: {work_order.status}")
|
||||
except Exception as e:
|
||||
logger.error(f"获取工单信息失败: {e}")
|
||||
|
||||
# 添加用户历史对话
|
||||
if user_id and user_id in self.conversation_history:
|
||||
recent_history = self.conversation_history[user_id][-6:] # 最近3轮对话
|
||||
if recent_history:
|
||||
context_parts.append("最近的对话历史:")
|
||||
for msg in recent_history:
|
||||
role = "用户" if msg["role"] == "user" else "助手"
|
||||
context_parts.append(f"{role}: {msg['content']}")
|
||||
|
||||
return "\n".join(context_parts) if context_parts else ""
|
||||
|
||||
def _save_conversation(
|
||||
self,
|
||||
work_order_id: Optional[int],
|
||||
user_message: str,
|
||||
assistant_response: str,
|
||||
knowledge_used: str
|
||||
) -> int:
|
||||
"""保存对话记录"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
conversation = Conversation(
|
||||
work_order_id=work_order_id,
|
||||
user_message=user_message,
|
||||
assistant_response=assistant_response,
|
||||
knowledge_used=knowledge_used,
|
||||
timestamp=datetime.now()
|
||||
)
|
||||
session.add(conversation)
|
||||
session.commit()
|
||||
return conversation.id
|
||||
except Exception as e:
|
||||
logger.error(f"保存对话记录失败: {e}")
|
||||
return 0
|
||||
|
||||
def _calculate_confidence(self, knowledge_results: List[Dict[str, Any]]) -> float:
|
||||
"""计算回复置信度"""
|
||||
if not knowledge_results:
|
||||
return 0.5 # 默认置信度
|
||||
|
||||
# 基于知识库匹配度和置信度计算
|
||||
max_similarity = max(result.get("similarity_score", 0) for result in knowledge_results)
|
||||
avg_confidence = sum(result.get("confidence_score", 0) for result in knowledge_results) / len(knowledge_results)
|
||||
|
||||
# 综合评分
|
||||
confidence = (max_similarity * 0.6 + avg_confidence * 0.4)
|
||||
return min(confidence, 1.0)
|
||||
|
||||
def create_work_order(
|
||||
self,
|
||||
title: str,
|
||||
description: str,
|
||||
category: str,
|
||||
priority: str = "medium"
|
||||
) -> Dict[str, Any]:
|
||||
"""创建工单"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
work_order = WorkOrder(
|
||||
order_id=f"WO{datetime.now().strftime('%Y%m%d%H%M%S')}",
|
||||
title=title,
|
||||
description=description,
|
||||
category=category,
|
||||
priority=priority,
|
||||
status="open",
|
||||
created_at=datetime.now()
|
||||
)
|
||||
session.add(work_order)
|
||||
session.commit()
|
||||
|
||||
logger.info(f"创建工单成功: {work_order.order_id}")
|
||||
return {
|
||||
"work_order_id": work_order.id,
|
||||
"order_id": work_order.order_id,
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"创建工单失败: {e}")
|
||||
return {"error": f"创建失败: {str(e)}"}
|
||||
|
||||
def update_work_order(
|
||||
self,
|
||||
work_order_id: int,
|
||||
status: Optional[str] = None,
|
||||
resolution: Optional[str] = None,
|
||||
satisfaction_score: Optional[float] = None
|
||||
) -> bool:
|
||||
"""更新工单"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
work_order = session.query(WorkOrder).filter(
|
||||
WorkOrder.id == work_order_id
|
||||
).first()
|
||||
|
||||
if not work_order:
|
||||
return False
|
||||
|
||||
if status:
|
||||
work_order.status = status
|
||||
if resolution:
|
||||
work_order.resolution = resolution
|
||||
if satisfaction_score is not None:
|
||||
work_order.satisfaction_score = satisfaction_score
|
||||
|
||||
work_order.updated_at = datetime.now()
|
||||
session.commit()
|
||||
|
||||
# 如果工单已解决,学习知识
|
||||
if status == "resolved" and resolution:
|
||||
self.knowledge_manager.learn_from_work_order(work_order_id)
|
||||
|
||||
logger.info(f"更新工单成功: {work_order_id}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新工单失败: {e}")
|
||||
return False
|
||||
|
||||
def get_conversation_history(self, work_order_id: int) -> List[Dict[str, Any]]:
|
||||
"""获取工单对话历史"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
conversations = session.query(Conversation).filter(
|
||||
Conversation.work_order_id == work_order_id
|
||||
).order_by(Conversation.timestamp).all()
|
||||
|
||||
return [
|
||||
{
|
||||
"id": conv.id,
|
||||
"user_message": conv.user_message,
|
||||
"assistant_response": conv.assistant_response,
|
||||
"timestamp": conv.timestamp.isoformat(),
|
||||
"confidence_score": conv.confidence_score
|
||||
}
|
||||
for conv in conversations
|
||||
]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取对话历史失败: {e}")
|
||||
return []
|
||||
377
src/dialogue/realtime_chat.py
Normal file
377
src/dialogue/realtime_chat.py
Normal file
@@ -0,0 +1,377 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
实时对话管理器
|
||||
提供实时对话功能,集成知识库搜索和LLM回复
|
||||
"""
|
||||
|
||||
import logging
|
||||
import json
|
||||
import time
|
||||
from typing import Dict, List, Any, Optional
|
||||
from datetime import datetime
|
||||
from dataclasses import dataclass
|
||||
|
||||
from ..core.llm_client import QwenClient
|
||||
from ..knowledge_base.knowledge_manager import KnowledgeManager
|
||||
from ..core.database import db_manager
|
||||
from ..core.models import Conversation, WorkOrder
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@dataclass
|
||||
class ChatMessage:
|
||||
"""聊天消息"""
|
||||
role: str # user, assistant, system
|
||||
content: str
|
||||
timestamp: datetime
|
||||
message_id: str
|
||||
work_order_id: Optional[int] = None
|
||||
knowledge_used: Optional[List[Dict]] = None
|
||||
confidence_score: Optional[float] = None
|
||||
|
||||
class RealtimeChatManager:
|
||||
"""实时对话管理器"""
|
||||
|
||||
def __init__(self):
|
||||
self.llm_client = QwenClient()
|
||||
self.knowledge_manager = KnowledgeManager()
|
||||
self.active_sessions = {} # 存储活跃的对话会话
|
||||
self.message_history = {} # 存储消息历史
|
||||
|
||||
def create_session(self, user_id: str, work_order_id: Optional[int] = None) -> str:
|
||||
"""创建新的对话会话"""
|
||||
session_id = f"session_{user_id}_{int(time.time())}"
|
||||
|
||||
session_data = {
|
||||
"user_id": user_id,
|
||||
"work_order_id": work_order_id,
|
||||
"created_at": datetime.now(),
|
||||
"last_activity": datetime.now(),
|
||||
"message_count": 0,
|
||||
"context": []
|
||||
}
|
||||
|
||||
self.active_sessions[session_id] = session_data
|
||||
self.message_history[session_id] = []
|
||||
|
||||
logger.info(f"创建新会话: {session_id}")
|
||||
return session_id
|
||||
|
||||
def process_message(self, session_id: str, user_message: str) -> Dict[str, Any]:
|
||||
"""处理用户消息"""
|
||||
try:
|
||||
if session_id not in self.active_sessions:
|
||||
return {"error": "会话不存在"}
|
||||
|
||||
session = self.active_sessions[session_id]
|
||||
session["last_activity"] = datetime.now()
|
||||
session["message_count"] += 1
|
||||
|
||||
# 创建用户消息
|
||||
user_msg = ChatMessage(
|
||||
role="user",
|
||||
content=user_message,
|
||||
timestamp=datetime.now(),
|
||||
message_id=f"msg_{int(time.time())}_{session['message_count']}"
|
||||
)
|
||||
|
||||
# 添加到消息历史
|
||||
self.message_history[session_id].append(user_msg)
|
||||
|
||||
# 搜索相关知识
|
||||
knowledge_results = self._search_knowledge(user_message)
|
||||
|
||||
# 生成回复
|
||||
assistant_response = self._generate_response(
|
||||
user_message,
|
||||
knowledge_results,
|
||||
session["context"]
|
||||
)
|
||||
|
||||
# 创建助手消息
|
||||
assistant_msg = ChatMessage(
|
||||
role="assistant",
|
||||
content=assistant_response["content"],
|
||||
timestamp=datetime.now(),
|
||||
message_id=f"msg_{int(time.time())}_{session['message_count'] + 1}",
|
||||
work_order_id=session["work_order_id"],
|
||||
knowledge_used=knowledge_results,
|
||||
confidence_score=assistant_response.get("confidence", 0.5)
|
||||
)
|
||||
|
||||
# 添加到消息历史
|
||||
self.message_history[session_id].append(assistant_msg)
|
||||
|
||||
# 更新上下文
|
||||
session["context"].append({
|
||||
"role": "user",
|
||||
"content": user_message
|
||||
})
|
||||
session["context"].append({
|
||||
"role": "assistant",
|
||||
"content": assistant_response["content"]
|
||||
})
|
||||
|
||||
# 保持上下文长度
|
||||
if len(session["context"]) > 20: # 保留最近10轮对话
|
||||
session["context"] = session["context"][-20:]
|
||||
|
||||
# 保存到数据库
|
||||
self._save_conversation(session_id, user_msg, assistant_msg)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"response": assistant_response["content"], # 修改为response字段
|
||||
"message_id": assistant_msg.message_id,
|
||||
"content": assistant_response["content"], # 保留content字段以兼容
|
||||
"knowledge_used": knowledge_results,
|
||||
"confidence_score": assistant_response.get("confidence", 0.5),
|
||||
"work_order_id": session["work_order_id"],
|
||||
"timestamp": assistant_msg.timestamp.isoformat()
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理消息失败: {e}")
|
||||
return {"error": f"处理消息失败: {str(e)}"}
|
||||
|
||||
def _search_knowledge(self, query: str, top_k: int = 3) -> List[Dict[str, Any]]:
|
||||
"""搜索相关知识"""
|
||||
try:
|
||||
results = self.knowledge_manager.search_knowledge(query, top_k)
|
||||
return results
|
||||
except Exception as e:
|
||||
logger.error(f"搜索知识库失败: {e}")
|
||||
return []
|
||||
|
||||
def _generate_response(self, user_message: str, knowledge_results: List[Dict], context: List[Dict]) -> Dict[str, Any]:
|
||||
"""生成回复"""
|
||||
try:
|
||||
# 构建提示词
|
||||
prompt = self._build_chat_prompt(user_message, knowledge_results, context)
|
||||
|
||||
# 调用大模型
|
||||
response = self.llm_client.chat_completion(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
temperature=0.7,
|
||||
max_tokens=1000
|
||||
)
|
||||
|
||||
if response and 'choices' in response:
|
||||
content = response['choices'][0]['message']['content']
|
||||
confidence = self._calculate_confidence(knowledge_results, content)
|
||||
|
||||
return {
|
||||
"content": content,
|
||||
"confidence": confidence
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"content": "抱歉,我暂时无法处理您的问题。请稍后再试或联系人工客服。",
|
||||
"confidence": 0.1
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"生成回复失败: {e}")
|
||||
return {
|
||||
"content": "抱歉,系统出现错误,请稍后再试。",
|
||||
"confidence": 0.1
|
||||
}
|
||||
|
||||
def _build_chat_prompt(self, user_message: str, knowledge_results: List[Dict], context: List[Dict]) -> str:
|
||||
"""构建聊天提示词"""
|
||||
prompt = f"""
|
||||
你是一个专业的奇瑞汽车客服助手。请根据用户的问题和提供的知识库信息,给出专业、友好的回复。
|
||||
|
||||
用户问题:{user_message}
|
||||
|
||||
相关知识库信息:
|
||||
"""
|
||||
|
||||
if knowledge_results:
|
||||
for i, result in enumerate(knowledge_results, 1):
|
||||
prompt += f"\n{i}. 问题:{result.get('question', '')}"
|
||||
prompt += f"\n 答案:{result.get('answer', '')}"
|
||||
prompt += f"\n 相似度:{result.get('similarity_score', 0):.3f}\n"
|
||||
else:
|
||||
prompt += "\n未找到相关知识库信息。\n"
|
||||
|
||||
# 添加上下文
|
||||
if context:
|
||||
prompt += "\n对话历史:\n"
|
||||
for msg in context[-6:]: # 最近3轮对话
|
||||
prompt += f"{msg['role']}: {msg['content']}\n"
|
||||
|
||||
prompt += """
|
||||
请按照以下要求回复:
|
||||
1. 语言要专业、友好、易懂
|
||||
2. 如果知识库中有相关信息,优先使用知识库内容
|
||||
3. 如果没有相关知识,请提供一般性建议
|
||||
4. 如果问题需要进站处理,请明确说明
|
||||
5. 回复要简洁明了,避免冗长
|
||||
6. 如果涉及技术问题,要提供具体的操作步骤
|
||||
7. 始终以"您好"开头,以"如有其他问题,请随时联系"结尾
|
||||
|
||||
请直接给出回复内容,不要包含其他格式:
|
||||
"""
|
||||
|
||||
return prompt
|
||||
|
||||
def _calculate_confidence(self, knowledge_results: List[Dict], response_content: str) -> float:
|
||||
"""计算回复置信度"""
|
||||
if not knowledge_results:
|
||||
return 0.3
|
||||
|
||||
# 基于知识库结果计算基础置信度
|
||||
max_similarity = max([r.get('similarity_score', 0) for r in knowledge_results])
|
||||
base_confidence = min(max_similarity * 1.2, 0.9) # 最高0.9
|
||||
|
||||
# 根据回复长度调整
|
||||
if len(response_content) < 50:
|
||||
base_confidence *= 0.8
|
||||
elif len(response_content) > 500:
|
||||
base_confidence *= 0.9
|
||||
|
||||
return base_confidence
|
||||
|
||||
def _save_conversation(self, session_id: str, user_msg: ChatMessage, assistant_msg: ChatMessage):
|
||||
"""保存对话到数据库"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
# 保存用户消息
|
||||
user_conversation = Conversation(
|
||||
work_order_id=user_msg.work_order_id,
|
||||
user_message=user_msg.content,
|
||||
assistant_response="", # 用户消息没有助手回复
|
||||
timestamp=user_msg.timestamp,
|
||||
confidence_score=None,
|
||||
knowledge_used=None,
|
||||
response_time=None
|
||||
)
|
||||
session.add(user_conversation)
|
||||
|
||||
# 保存助手消息
|
||||
assistant_conversation = Conversation(
|
||||
work_order_id=assistant_msg.work_order_id,
|
||||
user_message="", # 助手消息没有用户输入
|
||||
assistant_response=assistant_msg.content,
|
||||
timestamp=assistant_msg.timestamp,
|
||||
confidence_score=assistant_msg.confidence_score,
|
||||
knowledge_used=json.dumps(assistant_msg.knowledge_used, ensure_ascii=False) if assistant_msg.knowledge_used else None,
|
||||
response_time=0.5 # 模拟响应时间
|
||||
)
|
||||
session.add(assistant_conversation)
|
||||
|
||||
session.commit()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"保存对话失败: {e}")
|
||||
|
||||
def get_session_history(self, session_id: str) -> List[Dict[str, Any]]:
|
||||
"""获取会话历史"""
|
||||
if session_id not in self.message_history:
|
||||
return []
|
||||
|
||||
history = []
|
||||
for msg in self.message_history[session_id]:
|
||||
history.append({
|
||||
"role": msg.role,
|
||||
"content": msg.content,
|
||||
"timestamp": msg.timestamp.isoformat(),
|
||||
"message_id": msg.message_id,
|
||||
"knowledge_used": msg.knowledge_used,
|
||||
"confidence_score": msg.confidence_score
|
||||
})
|
||||
|
||||
return history
|
||||
|
||||
def create_work_order(self, session_id: str, title: str, description: str, category: str, priority: str = "medium") -> Dict[str, Any]:
|
||||
"""创建工单"""
|
||||
try:
|
||||
if session_id not in self.active_sessions:
|
||||
return {"error": "会话不存在"}
|
||||
|
||||
session = self.active_sessions[session_id]
|
||||
|
||||
with db_manager.get_session() as db_session:
|
||||
work_order = WorkOrder(
|
||||
order_id=f"WO_{int(time.time())}",
|
||||
title=title,
|
||||
description=description,
|
||||
category=category,
|
||||
priority=priority,
|
||||
status="open",
|
||||
created_at=datetime.now()
|
||||
)
|
||||
db_session.add(work_order)
|
||||
db_session.commit()
|
||||
|
||||
# 更新会话的工单ID
|
||||
session["work_order_id"] = work_order.id
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"work_order_id": work_order.id,
|
||||
"order_id": work_order.order_id,
|
||||
"message": "工单创建成功"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"创建工单失败: {e}")
|
||||
return {"error": f"创建工单失败: {str(e)}"}
|
||||
|
||||
def get_work_order_status(self, work_order_id: int) -> Dict[str, Any]:
|
||||
"""获取工单状态"""
|
||||
try:
|
||||
with db_manager.get_session() as session:
|
||||
work_order = session.query(WorkOrder).filter(WorkOrder.id == work_order_id).first()
|
||||
|
||||
if not work_order:
|
||||
return {"error": "工单不存在"}
|
||||
|
||||
return {
|
||||
"work_order_id": work_order.id,
|
||||
"order_id": work_order.order_id,
|
||||
"title": work_order.title,
|
||||
"status": work_order.status,
|
||||
"priority": work_order.priority,
|
||||
"created_at": work_order.created_at.isoformat(),
|
||||
"updated_at": work_order.updated_at.isoformat() if work_order.updated_at else None,
|
||||
"resolution": work_order.resolution,
|
||||
"satisfaction_score": work_order.satisfaction_score
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取工单状态失败: {e}")
|
||||
return {"error": f"获取工单状态失败: {str(e)}"}
|
||||
|
||||
def end_session(self, session_id: str) -> bool:
|
||||
"""结束会话"""
|
||||
try:
|
||||
if session_id in self.active_sessions:
|
||||
del self.active_sessions[session_id]
|
||||
|
||||
if session_id in self.message_history:
|
||||
del self.message_history[session_id]
|
||||
|
||||
logger.info(f"结束会话: {session_id}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"结束会话失败: {e}")
|
||||
return False
|
||||
|
||||
def get_active_sessions(self) -> List[Dict[str, Any]]:
|
||||
"""获取活跃会话列表"""
|
||||
sessions = []
|
||||
for session_id, session_data in self.active_sessions.items():
|
||||
sessions.append({
|
||||
"session_id": session_id,
|
||||
"user_id": session_data["user_id"],
|
||||
"work_order_id": session_data["work_order_id"],
|
||||
"created_at": session_data["created_at"].isoformat(),
|
||||
"last_activity": session_data["last_activity"].isoformat(),
|
||||
"message_count": session_data["message_count"]
|
||||
})
|
||||
|
||||
return sessions
|
||||
Reference in New Issue
Block a user