Commit 65450023 by sunqi02 Committed by 胡悦

feat: 更新 OpenAIChatFormatter,静默丢弃 thinking block,避免 LLM 误解上下文

1 parent f1eded1c
......@@ -14,11 +14,15 @@ from agentscope.formatter import OpenAIChatFormatter as _OpenAIChatFormatter
class OpenAIChatFormatter(_OpenAIChatFormatter):
"""扩展 AgentScope 的 OpenAI formatter,支持 thinking block → text 转换。
"""扩展 AgentScope 的 OpenAI formatter,静默丢弃 thinking block。
ReActAgent 在 reasoning 阶段产出的 thinking block 会存入 InMemoryMemory,
后续 API 调用格式化历史消息时会遇到该类型。原生 formatter 不认识 thinking
类型会打 warning,这里将其转为 text block 保留推理上下文。
类型会打 warning。
thinking 文本是 LLM 的内部推理(如"我需要调用XX Agent"),只能通过
post_reasoning hook 摘要后发 IM 给用户看,绝不能回灌给 LLM 作为上下文,
否则 LLM 会误以为是未完成的任务指令而重复执行。
"""
async def _format(self, msgs: list) -> list:
......@@ -26,15 +30,10 @@ class OpenAIChatFormatter(_OpenAIChatFormatter):
blocks = msg.content if isinstance(msg.content, list) else []
if not blocks:
continue
new_blocks = []
for block in blocks:
if isinstance(block, dict) and block.get("type") == "thinking":
text = block.get("thinking", "")
if text.strip():
new_blocks.append({"type": "text", "text": text})
else:
new_blocks.append(block)
msg.content = new_blocks
msg.content = [
b for b in blocks
if not (isinstance(b, dict) and b.get("type") == "thinking")
]
return await super()._format(msgs)
from agentscope.message import Msg
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!