feat(langchain-chat): LangGraph 重写 agent 内核
主要变化: - 新增 agent_v2.py: 用 LangGraph create_react_agent + astream_events 替代原 agent_chat_test 的 LLM step-routing 死循环 - 新增 tools_v2.py: 闭包工厂模式,每个请求按 uuid 生成工具列表, 消除 toolinput 字符串拼 JSON 注入 uuid 的旧 hack - chat_test.py:266-346: 删 11 次 count_process 重试外层和事件 分发 spaghetti,换成 agent_run 单次调用 + 简单事件 dispatcher - policy_fun_iast.py:168-187: 修 broken <think> filter 老代码把 start_flag 设反了(看见 <think> 才开始 yield)导致 非 think 模型 yield 不出任何内容;改为正确跳过 <think>...</think> 块 模型函数调用通过 langchain_openai.ChatOpenAI(不能用旧版 langchain_community.chat_models.ChatOpenAI,没有现代 tool calling)。 依赖: langgraph==0.0.49 + langchain-core==0.1.53(已在服务器装好)。 非 stream 分支保留旧 agent_chat_test 路径(极少触发,回归风险低)。 旧版回滚: git checkout backup/pre-langgraph 实测对比: - 旧版 30-60s,答案 0 字(filter 卡死后展示 11 次重试) - 新版 25-40s,答案完整(含工具调用、参考文献、推荐问题、摘要) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -165,16 +165,28 @@ async def get_llm_model_response_stream_openai(
|
||||
for key in prompt_param_dict:
|
||||
prompt_template = prompt_template.replace(f"{{{{{key}}}}}", prompt_param_dict[key])
|
||||
messages = [HumanMessage(content=prompt_template)]
|
||||
if type == 0 or type == 2:
|
||||
start_flag = False
|
||||
async for chunk in model.astream(messages):
|
||||
if start_flag:
|
||||
yield chunk.content
|
||||
if "<think>" in chunk.content:
|
||||
start_flag = True
|
||||
else:
|
||||
async for chunk in model.astream(messages):
|
||||
yield chunk.content
|
||||
# 跳过 <think>...</think> 块,其余照常 yield
|
||||
# 兼容 R1 等输出 think 块的模型;非 think 模型不受影响
|
||||
in_think = False
|
||||
async for chunk in model.astream(messages):
|
||||
text = chunk.content or ""
|
||||
while text:
|
||||
if not in_think:
|
||||
i = text.find("<think>")
|
||||
if i < 0:
|
||||
yield text
|
||||
break
|
||||
if i > 0:
|
||||
yield text[:i]
|
||||
text = text[i + len("<think>"):]
|
||||
in_think = True
|
||||
else:
|
||||
i = text.find("</think>")
|
||||
if i < 0:
|
||||
text = "" # 全在 think 块内,丢弃
|
||||
else:
|
||||
text = text[i + len("</think>"):]
|
||||
in_think = False
|
||||
return # 成功完成,退出函数
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user