qwen_agent/.features/thinking/decisions/2026-06-middleware-not-native-reasoning.md
2026-06-01 11:51:21 +08:00

29 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
date: "2026-06-01"
status: adopted
topic: "middleware-thinking"
impact: [thinking, model-compat]
---
# 用中间件实现思考,而非依赖模型原生 reasoning
## 背景
"思考功能"可以有两种实现:
A. 透传 `enable_thinking` 给底层模型,依赖模型自带的 reasoning/extended-thinking 能力。
B. 在主请求前自己加一次"指引思考"的辅助 LLM 调用。
模型 A 路线要求底层模型支持原生 reasoning且不同模型行为/输出格式不一致,难以统一前端处理。
## 决策
采用 B实现 `GuidelineMiddleware`,在 `before_agent` 阶段用业务指引 prompt 调一次模型生成思考,
统一包成 `<think>...</think>` + `message_tag:"THINK"`
## 影响
- 与具体模型解耦,任何 LLMOpenAI/Claude/Qwen都能用。
- 思考阶段可注入业务规则、工具描述、术语分析、记忆上下文,可控性强。
- 代价:每次多一次 LLM 调用(延迟 + 成本);思考内容非流式。
## Gotchas
- 思考依赖 `config._mem0_context`,需保证 memory 中间件先于本中间件执行。
- 思考后补空 `HumanMessage` 以兼容"末条须为 user"的模型,勿删。