newtw2
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import os
|
||||
import re
|
||||
import hashlib
|
||||
import logging
|
||||
from typing import List, Optional, Any, Dict
|
||||
from pydantic_ai import Agent, RunContext
|
||||
@@ -85,19 +87,26 @@ class CustomerServiceBrain:
|
||||
self.agent = Agent(model=model, system_prompt=system_prompt)
|
||||
register_agent_tools(self.agent)
|
||||
|
||||
async def think_and_reply(self, msg: StandardMessage, history: List[dict] = []) -> StandardResponse:
|
||||
async def think_and_reply(self, msg: StandardMessage, history: Optional[List[dict]] = None) -> StandardResponse:
|
||||
if history is None:
|
||||
history = []
|
||||
try:
|
||||
# 构造增强上下文
|
||||
user_content = msg.content
|
||||
user_content = msg.content or ""
|
||||
|
||||
# 客户已发图:在上下文中明确告知 AI,避免再回"先发图"
|
||||
if msg.image_urls:
|
||||
user_content = f"【系统通知:收到客户 {len(msg.image_urls)} 张图】\n{user_content}"
|
||||
|
||||
user_content = (
|
||||
f"【系统通知:客户已发送 {len(msg.image_urls)} 张图片,不要再让客户发图!"
|
||||
f"请直接问客户需求(找原图还是修复),然后转接设计师】\n{user_content}"
|
||||
)
|
||||
|
||||
recent_context = ""
|
||||
if history:
|
||||
lines = [
|
||||
f"[{_fmt_time(h.get('timestamp'))}] {('客户' if h['role']=='user' else '我')}:{h['content']}"
|
||||
for h in history[-6:]
|
||||
]
|
||||
lines = []
|
||||
for h in history[-6:]:
|
||||
role = "客户" if h.get("role") == "user" else "我"
|
||||
content = h.get("content", "")
|
||||
lines.append(f"[{_fmt_time(h.get('timestamp'))}] {role}:{content}")
|
||||
recent_context = "【近期对话回顾】\n" + "\n".join(lines) + "\n----------------\n"
|
||||
|
||||
full_input = f"{recent_context}现在的对话:{user_content}"
|
||||
@@ -132,15 +141,20 @@ class CustomerServiceBrain:
|
||||
reply_text = found_magic
|
||||
# ----------------------------------------
|
||||
|
||||
# 清理可能的乱码/代码标记
|
||||
import re
|
||||
reply_text = re.sub(r'\[\]<\|[^|]+\|>', '', reply_text) # 清理 []<|xxx|>
|
||||
reply_text = re.sub(r'<\|[^|]+\|>', '', reply_text) # 清理 <|xxx|>
|
||||
reply_text = re.sub(r'\[Function[^\]]*\]', '', reply_text) # 清理 [FunctionXxx]
|
||||
reply_text = re.sub(r'<think[^>]*>.*', '', reply_text, flags=re.DOTALL) # 清理 <think_xxx>内部思考泄漏
|
||||
reply_text = re.sub(r'</?think[^>]*>', '', reply_text) # 清理 think 标签
|
||||
reply_text = re.sub(r'```[^`]*```', '', reply_text) # 清理代码块
|
||||
reply_text = re.sub(r'\{["\'][^}]+\}', '', reply_text) # 清理 JSON
|
||||
# 清理模型泄露的内部标记/乱码(覆盖所有已知格式)
|
||||
reply_text = re.sub(r'\[\]<\|[^|]+\|>', '', reply_text)
|
||||
reply_text = re.sub(r'<\|[^|]*\|>', '', reply_text)
|
||||
reply_text = re.sub(r'\[Function[^\]]*\]', '', reply_text)
|
||||
reply_text = re.sub(r'\[/?Tool[^\]]*\]', '', reply_text)
|
||||
reply_text = re.sub(r'</?tool[_\-]?[^>]*>', '', reply_text, flags=re.IGNORECASE)
|
||||
reply_text = re.sub(r'<think[^>]*>.*?</think[^>]*>', '', reply_text, flags=re.DOTALL)
|
||||
reply_text = re.sub(r'<think[^>]*>.*', '', reply_text, flags=re.DOTALL)
|
||||
reply_text = re.sub(r'</?think[^>]*>', '', reply_text)
|
||||
reply_text = re.sub(r'```[^`]*```', '', reply_text)
|
||||
reply_text = re.sub(r'\{["\'][^}]+\}', '', reply_text)
|
||||
reply_text = re.sub(r'AgentRunResult\([^)]*\)', '', reply_text)
|
||||
reply_text = re.sub(r'\[/?[A-Z][a-zA-Z]*(?:Call|End|Start|Result|Return)[^\]]*\]', '', reply_text)
|
||||
reply_text = re.sub(r'[\[\]]{2,}', '', reply_text)
|
||||
reply_text = reply_text.strip()
|
||||
|
||||
# 过滤"在呢铁子"
|
||||
|
||||
Reference in New Issue
Block a user