feat: optimize AI suggestion and workorder sync - support same-day multiple update numbering - insert new suggestions at top maintaining reverse chronological order - reference process history when generating suggestions - simplify prompts to avoid forcing log analysis - fix Chinese comment encoding issues
This commit is contained in:
Binary file not shown.
BIN
src/dialogue/__pycache__/conversation_history.cpython-311.pyc
Normal file
BIN
src/dialogue/__pycache__/conversation_history.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,3 @@
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
实时对话管理器
|
||||
提供实时对话功能,集成知识库搜索和LLM回复
|
||||
@@ -109,7 +107,8 @@ class RealtimeChatManager:
|
||||
assistant_response = self._generate_response(
|
||||
user_message,
|
||||
knowledge_results,
|
||||
session["context"]
|
||||
session["context"],
|
||||
session["work_order_id"]
|
||||
)
|
||||
|
||||
# 创建助手消息
|
||||
@@ -167,11 +166,14 @@ class RealtimeChatManager:
|
||||
logger.error(f"搜索知识库失败: {e}")
|
||||
return []
|
||||
|
||||
def _generate_response(self, user_message: str, knowledge_results: List[Dict], context: List[Dict]) -> Dict[str, Any]:
|
||||
def _generate_response(self, user_message: str, knowledge_results: List[Dict], context: List[Dict], work_order_id: Optional[int] = None) -> Dict[str, Any]:
|
||||
"""生成回复"""
|
||||
try:
|
||||
# 检查是否有相关的工单AI建议
|
||||
ai_suggestions = self._get_workorder_ai_suggestions(work_order_id)
|
||||
|
||||
# 构建提示词
|
||||
prompt = self._build_chat_prompt(user_message, knowledge_results, context)
|
||||
prompt = self._build_chat_prompt(user_message, knowledge_results, context, ai_suggestions)
|
||||
|
||||
# 调用大模型
|
||||
response = self.llm_client.chat_completion(
|
||||
@@ -184,24 +186,31 @@ class RealtimeChatManager:
|
||||
content = response['choices'][0]['message']['content']
|
||||
confidence = self._calculate_confidence(knowledge_results, content)
|
||||
|
||||
# 如果有AI建议,在回复中包含
|
||||
if ai_suggestions:
|
||||
content = self._format_response_with_ai_suggestions(content, ai_suggestions)
|
||||
|
||||
return {
|
||||
"content": content,
|
||||
"confidence": confidence
|
||||
"confidence": confidence,
|
||||
"ai_suggestions": ai_suggestions
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"content": "抱歉,我暂时无法处理您的问题。请稍后再试或联系人工客服。",
|
||||
"confidence": 0.1
|
||||
"confidence": 0.1,
|
||||
"ai_suggestions": ai_suggestions
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"生成回复失败: {e}")
|
||||
return {
|
||||
"content": "抱歉,系统出现错误,请稍后再试。",
|
||||
"confidence": 0.1
|
||||
"confidence": 0.1,
|
||||
"ai_suggestions": []
|
||||
}
|
||||
|
||||
def _build_chat_prompt(self, user_message: str, knowledge_results: List[Dict], context: List[Dict]) -> str:
|
||||
def _build_chat_prompt(self, user_message: str, knowledge_results: List[Dict], context: List[Dict], ai_suggestions: List[str] = None) -> str:
|
||||
"""构建聊天提示词"""
|
||||
prompt = f"""
|
||||
你是一个专业的奇瑞汽车客服助手。请根据用户的问题和提供的知识库信息,给出专业、友好的回复。
|
||||
@@ -219,6 +228,12 @@ class RealtimeChatManager:
|
||||
else:
|
||||
prompt += "\n未找到相关知识库信息。\n"
|
||||
|
||||
# 添加AI建议信息
|
||||
if ai_suggestions:
|
||||
prompt += "\n相关AI建议:\n"
|
||||
for suggestion in ai_suggestions:
|
||||
prompt += f"- {suggestion}\n"
|
||||
|
||||
# 添加上下文
|
||||
if context:
|
||||
prompt += "\n对话历史:\n"
|
||||
@@ -233,13 +248,72 @@ class RealtimeChatManager:
|
||||
4. 如果问题需要进站处理,请明确说明
|
||||
5. 回复要简洁明了,避免冗长
|
||||
6. 如果涉及技术问题,要提供具体的操作步骤
|
||||
7. 始终以"您好"开头,以"如有其他问题,请随时联系"结尾
|
||||
|
||||
请直接给出回复内容,不要包含其他格式:
|
||||
"""
|
||||
|
||||
return prompt
|
||||
|
||||
def _get_workorder_ai_suggestions(self, work_order_id: Optional[int]) -> List[str]:
|
||||
"""
|
||||
获取工单的AI建议
|
||||
|
||||
Args:
|
||||
work_order_id: 工单ID
|
||||
|
||||
Returns:
|
||||
AI建议列表
|
||||
"""
|
||||
try:
|
||||
if not work_order_id:
|
||||
return []
|
||||
|
||||
with db_manager.get_session() as session:
|
||||
# 查询工单的AI建议
|
||||
from ..core.models import WorkOrderSuggestion
|
||||
suggestions = session.query(WorkOrderSuggestion).filter(
|
||||
WorkOrderSuggestion.work_order_id == work_order_id
|
||||
).order_by(WorkOrderSuggestion.created_at.desc()).limit(3).all()
|
||||
|
||||
ai_suggestions = []
|
||||
for suggestion in suggestions:
|
||||
if suggestion.ai_suggestion:
|
||||
ai_suggestions.append(suggestion.ai_suggestion)
|
||||
|
||||
return ai_suggestions
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取工单AI建议失败: {e}")
|
||||
return []
|
||||
|
||||
def _format_response_with_ai_suggestions(self, content: str, ai_suggestions: List[str]) -> str:
|
||||
"""
|
||||
在回复中格式化AI建议
|
||||
|
||||
Args:
|
||||
content: 原始回复内容
|
||||
ai_suggestions: AI建议列表
|
||||
|
||||
Returns:
|
||||
包含AI建议的格式化回复
|
||||
"""
|
||||
try:
|
||||
if not ai_suggestions:
|
||||
return content
|
||||
|
||||
# 在回复末尾添加AI建议
|
||||
formatted_content = content
|
||||
|
||||
formatted_content += "\n\n📋 **相关AI建议:**\n"
|
||||
for i, suggestion in enumerate(ai_suggestions, 1):
|
||||
formatted_content += f"{i}. {suggestion}\n"
|
||||
|
||||
return formatted_content
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"格式化AI建议失败: {e}")
|
||||
return content
|
||||
|
||||
def _extract_vin(self, text: str) -> Optional[str]:
|
||||
"""从文本中提取VIN(17位,I/O/Q不使用,常见校验)"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user