模型参数与推理
temperature、top-p、streaming、reasoning models。
30 分钟
模型参数与推理
同一套 Agent 代码,换参数或换模型,行为可以 从稳定执行变成天马行空。本课讲 temperature、top-p、streaming、reasoning models 在 Agent 场景怎么选——不是背默认值,而是 规划、工具调用、最终回答 分阶段调参。
学习目标
- 解释 temperature / top-p 对工具选择与 JSON 合法率的影响
- 会用 streaming 改善交互,并处理「半条 tool call」边界
- 区分普通模型与推理型模型在 Agent 中的分工
Temperature 与 top-p
| 参数 | 低值 | 高值 | Agent 建议 |
|---|---|---|---|
| temperature | 确定性高、重复 | 多样、易偏题 | 工具参数、结构化规划 → 低 |
| top-p | 候选集窄 | 候选集宽 | 与 temperature 二选一微调即可 |
# 伪代码:分阶段参数
plan = llm.chat(messages, temperature=0.2, response_format=plan_schema)
draft = llm.chat(messages + plan, temperature=0.7) # 用户可见文案可略高
常见错误:全链路 temperature=0.9 → tool name 拼错、JSON 缺括号、重复调用同一 API。
Streaming
Streaming 逐 token 返回,适合 用户可见文本;工具调用需注意:
buffer = ""
for chunk in llm.stream(messages, tools=tools):
if chunk.tool_call_delta:
buffer += chunk.tool_call_delta
# 等完整 tool_call 再 execute,勿对半截 JSON 调 API
elif chunk.content:
yield chunk.content # UI 逐字显示
| 场景 | streaming |
|---|---|
| 聊天展示 | 开 |
| 严格 JSON / 并行多 tool | 关或等完整 message |
| 可取消长任务 | 开 + 客户端 abort |
Reasoning models(推理型模型)
部分模型会输出 长内部推理链(或隐藏 reasoning token),再给出答案或 tool call。
| 维度 | 普通模型 | 推理型 |
|---|---|---|
| 多步规划 | 靠 prompt(CoT/ReAct) | 内置更长思考 |
| 成本/延迟 | 较低 | 较高 |
| 工具调用 | 成熟 API 支持不一 | 需查厂商文档 |
Agent 实践:复杂规划 可用推理型或单独「Planner」调用;高频工具环 用快模型 + 低 temperature,避免每步都烧长推理。
与路线图
对应 ai-agents 路线图 的模型推理节点。入门见 ;本课强调 的分阶段配置。