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:
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user