Browse Source

feat: 机器人群内自动回复,返回纯文本消息

zhangwl 2 months ago
parent
commit
8f4c10b2bd
1 changed files with 26 additions and 8 deletions
  1. 26 8
      app/routers/chat_tools.py

+ 26 - 8
app/routers/chat_tools.py

@@ -12,6 +12,20 @@ from ..db.ai_config import get_config_by_app_name
 from ..db.mongo import  save_chat_history, get_chat_history, delete_chat_history, get_sessions
 from ..db.mongo import  save_chat_history, get_chat_history, delete_chat_history, get_sessions
 router = APIRouter()
 router = APIRouter()
 
 
+import re
+
+
+def _strip_markdown(text: str) -> str:
+    text = re.sub(r'(?m)^#{1,6}\s+', '', text)
+    text = re.sub(r'\*{1,3}(.+?)\*{1,3}', r'\1', text)
+    text = re.sub(r'_{1,3}(.+?)_{1,3}', r'\1', text)
+    text = re.sub(r'`{1,3}(.+?)`{1,3}', r'\1', text)
+    text = re.sub(r'(?m)^[ \t]*[-*]\s+', '', text)
+    text = re.sub(r'(?m)^[ \t]*\d+\.\s+', '', text)
+    text = re.sub(r'(?m)^[ \t]*>\s?', '', text)
+    text = re.sub(r'\n{3,}', '\n\n', text)
+    return text.strip()
+
 
 
 def _build_prompt(product_text: str, prompt_config: dict) -> str:
 def _build_prompt(product_text: str, prompt_config: dict) -> str:
     name = prompt_config.get("name", "兴趣圈")
     name = prompt_config.get("name", "兴趣圈")
@@ -301,14 +315,16 @@ async def group_chat(
                - 时效性表达应自然融入回答,不要生硬罗列。
                - 时效性表达应自然融入回答,不要生硬罗列。
 
 
                ### 4. 格式层面
                ### 4. 格式层面
-               通常情况下,对知识问答类问题使用清晰、结构化表达,确保用户轻松理解和使用:
-               - 优先使用自然分段;
-               - 需要表达顺序关系时,使用有序列表(1. 2. 3.);
-               - 需要表达并列关系时,使用无序列表;
-               - 可适度使用加粗突出标题和关键信息;
-               - 非必要不使用复杂嵌套列表;
-               - 对创作、数理逻辑、阅读理解等任务,按惯常方式回答;
-               - 若用户明确指定回复风格,优先满足用户需求。
+               回复必须使用纯文本,不得包含任何 Markdown 语法:
+               - 不得使用 **加粗**、*斜体*、`代码`、~~删除线~~ 等标记符号;
+               - 不得使用 # 标题符号;
+               - 不得使用 - 或 * 开头的无序列表,也不得使用 1. 2. 3. 形式的有序列表;
+               - 不得使用 > 引用块;
+               - 用自然的分段和标点(逗号、句号、分号)来组织内容;
+               - 如需表达并列关系,用"一是……二是……三是……"或顿号分隔;
+               - 如需表达顺序关系,用"首先……其次……最后……"等自然语言;
+               - 对创作、数理逻辑、阅读理解等任务,同样遵守纯文本要求;
+               - 若用户明确要求特定格式,优先满足用户需求。
 
 
                ## 六、特殊场景处理
                ## 六、特殊场景处理
                1. 如果知识库已有云悦、XX宝、陈沛相关信息,优先使用知识库内容,不主动联网补充。
                1. 如果知识库已有云悦、XX宝、陈沛相关信息,优先使用知识库内容,不主动联网补充。
@@ -360,6 +376,8 @@ async def group_chat(
             else:
             else:
                 result += str(item.content)
                 result += str(item.content)
 
 
+    result = _strip_markdown(result)
+
     if not result:
     if not result:
         save_chat_log(
         save_chat_log(
             user_id=request.user_id,
             user_id=request.user_id,