fix(langchain-chat): R1 思考过程显示 + 选题推荐放宽 + RAG 诊断日志

三个独立修复 / 排查:

1. R1 思考过程不显示
   - 根因: chat_test.py 等 <think> 开标签出现才进思考态,但 R1
     流式输出本来就在 reasoning 态启动,永远不出 <think>,所有
     reasoning 全部当 text 走到答案区
   - 修法: 引入 r1_thinking_done 状态机,默认在思考态,
     看到 </think> 切换;R1-70B 直连本地代理 deepseek-r1
     (官方 deepseek-reasoner 把 reasoning 放独立字段,旧版
     callback 取不到)
   - 结果验证: "1+1" → 269 think + 40 text,思考与答案正确分流

2. 选题推荐场景拒答 + chat 模板标记泄漏
   - 根因: prompt 写死了 "你只能回答有关选题推荐的问题"
     + 直接嵌入 <|im_start|>/<|im_end|> Qwen chat 标记
   - 修法: 改写 Topic Recommend Assistant prompt,删 chat 标记,
     行为准则改为"沾边查询用工具回答";agent_v2 增加 strip
     防守层
   - 结果验证: "钢铁行业研究重点方向" → agent 调工具,不再拒答

3. 知识库召回 0 排查(数据问题,未根治)
   - 根因: kb_config.py 把所有 KB(政策库/钢铁库/报告库等)
     都映射到 t_policy_total_bge_new_v2,但 Milvus 里根本没有
     这个 collection(实际只有 11 个 p_* 个人库 + 1 个
     t_journal_article_bge_v1)
   - 临时改: search_tool.py 加诊断日志 [RAG诊断] 输出每个 KB
     召回数;rag_search 内 for 循环里首个 KB 空就 return 的
     bug 改 continue
   - 待决策: kb_config 是否把默认 KB 映射到唯一存在的
     t_journal_article_bge_v1,或重建对应集合

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 15:44:05 +08:00
parent 316def2145
commit 846380879b
5 changed files with 55 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ LangGraph 版 Agent runner。
import asyncio
import json
import logging
import re
from typing import AsyncIterable, List, Optional
from langgraph.prebuilt import create_react_agent
@@ -26,10 +27,20 @@ from server.chat.tools_v2 import make_tools
logger = logging.getLogger(__name__)
_CHAT_MARKER_RE = re.compile(r"<\|im_(?:start|end)\|>")
def _strip_chat_markers(text: str) -> str:
"""剥掉 prompt 内嵌的 Qwen chat template 标记,避免模型 echo 泄漏到答案。"""
return _CHAT_MARKER_RE.sub("", text or "")
def _build_system_prompt(user_prompt_name: str, query: str, think_content: str) -> str:
"""复用旧版 Think Test Bak + 用户业务 prompt 的拼装逻辑,但简化为单条 system message。"""
base = get_prompt_template("agent_chat", "Think Test Bak")
user = get_prompt_template("llm_chat", user_prompt_name) if user_prompt_name else ""
user = _strip_chat_markers(user)
think_content = _strip_chat_markers(think_content)
parts = []
parts.append("你是浪潮开发的智能专家。回答用户问题前可以使用工具检索资料。")
parts.append("严格要求:")

View File

@@ -123,7 +123,9 @@ async def chat_test(
query = query if len(query)<20000 else TextRank(query,num_sentences=70)
query = query if len(query)<20000 else TextRank(query,num_sentences=10)
if model_name == "R1-70B":
model_name = DEEPSEEK_MODELS[0]
# 本地代理 deepseek-r1 把 reasoning 放 content 里,能被 callback 捕获;
# 官方 deepseek-reasoner 把 reasoning 放独立的 reasoning_content 字段,旧版 langchain callback 取不到
model_name = "deepseek-r1"
elif model_name in ["QIANWEN", "Qwen1.5-32B-Chat"]:
model_name = LLM_MODELS[0]
if prompt_name == "customer_service":
@@ -590,6 +592,8 @@ async def chat_test(
if stream:
menu = 0 #0处于deepseek思考过程中的状态1处于生成正文状态
include_think = False #是否包含思考(源码修改的手动拼接的思考标签)
include_think1 = False
r1_thinking_done = False # R1: 见到 </think> 之前默认在思考态
async for token in callback.aiter():
if not utils.get_shared_variable(time_based_uuid)["status"]:
logging.info("\n==============================STOPPED==============================\n")
@@ -633,19 +637,24 @@ async def chat_test(
yield json.dumps({"text": token}, ensure_ascii=False)
else:
if model_name in DEEPSEEK_MODELS:
if "<think>" in token:
include_think = True
token = token.replace("<think>","")
logger.info(f"think:{token}")
yield json.dumps({"think": token}, ensure_ascii=False)
# R1 流式输出特点:默认从 reasoning 开始(不带 <think> 开标签),
# 看到 </think> 才切换到正式答案。
if not r1_thinking_done:
if "</think>" in token:
before, _, after = token.partition("</think>")
if before:
yield json.dumps({"think": before}, ensure_ascii=False)
r1_thinking_done = True
if after.strip():
yield json.dumps({"text": after}, ensure_ascii=False)
else:
# 兼容偶发输出 <think> 开标签的场景:剥掉后直接 yield think
if "<think>" in token:
token = token.replace("<think>", "")
if token:
yield json.dumps({"think": token}, ensure_ascii=False)
else:
if menu == 1:
yield json.dumps({"text": token}, ensure_ascii=False)
if menu == 0 and include_think:
yield json.dumps({"text": token}, ensure_ascii=False)
menu = 1
if not include_think:
yield json.dumps({"text": token}, ensure_ascii=False)
yield json.dumps({"text": token}, ensure_ascii=False)
else:
yield json.dumps(
{"text": token, "message_id": message_id},