Commit 515a321e by sunqi02

feat: 添加 ThinkingBlockSafeFormatter 以处理 ThinkingBlock,避免消息丢失

1 parent da731aa5
...@@ -19,6 +19,29 @@ from agentscope.agent import ReActAgent ...@@ -19,6 +19,29 @@ from agentscope.agent import ReActAgent
from agentscope.memory import InMemoryMemory from agentscope.memory import InMemoryMemory
class ThinkingBlockSafeFormatter(OpenAIChatFormatter):
"""OpenAI formatter that strips ThinkingBlock before formatting.
The upstream OpenAIChatFormatter does not handle ThinkingBlock
(type="thinking"), causing "Unsupported block type" warnings and
risking silent message drops when a message contains only thinking
blocks. This wrapper pre-filters them so they never reach the upstream
block-handling loop.
"""
async def _format(self, msgs: list[Msg]) -> list[dict[str, Any]]:
# TruncatedFormatterBase.format() deepcopies msgs before calling
# _format, so we can safely mutate the copies here.
for msg in msgs:
if hasattr(msg, "content") and isinstance(msg.content, list):
msg.content = [
block
for block in msg.content
if not (isinstance(block, dict) and block.get("type") == "thinking")
]
return await super()._format(msgs)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# sub_agent 专用低 temperature:调用子 Agent 处理确定性任务时使用更稳定的输出 # sub_agent 专用低 temperature:调用子 Agent 处理确定性任务时使用更稳定的输出
...@@ -138,7 +161,7 @@ class AgentHub: ...@@ -138,7 +161,7 @@ class AgentHub:
stream=False, stream=False,
client_kwargs={"base_url": self.main_model_config.api_base} client_kwargs={"base_url": self.main_model_config.api_base}
) )
self.formatter = OpenAIChatFormatter() self.formatter = ThinkingBlockSafeFormatter()
# 加载回复风格指南(soul/agent.md) # 加载回复风格指南(soul/agent.md)
soul_path = Path(__file__).parent.parent.parent / "soul" / "agent.md" soul_path = Path(__file__).parent.parent.parent / "soul" / "agent.md"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!