:理解 LLM —— Chat Completion、参数调优与结构化输出)
本文是 AI Agent 入门系列的第 2 篇。学完第 1 篇的基础调用后我们来深入理解 LLM 的工作机制。掌握这些知识下一课就能实现 Tool Use函数调用这是 Agent 最核心的能力。本课目标理解 LLM 对话补全的本质消息结构、多轮对话掌握关键参数的含义与调优temperature、max_tokens学会结构化输出 —— 让 LLM 返回 JSON本课产出新建文件lesson02_structured_output.py粘贴下文完整代码运行python lesson02_structured_output.py效果让 LLM 稳定输出 JSON 格式数据掌握 temperature 和 max_tokens 的影响一、Chat Completion 的本质消息结构LLM 的输入是一个消息列表每条消息有 role 和 contentmessages[{role:system,content:你是 AI 助手},# 系统指令{role:user,content:你好},# 用户输入{role:assistant,content:你好},# LLM 回复{role:user,content:什么是 Agent},# 继续发问]role含义说明system系统指令设定 LLM 身份和行为规则user用户消息用户的输入assistantLLM 回复模型生成的回复多轮时保留历史关键理解LLM 本质上是下一个词预测器。给定前面的所有文本它预测最可能的下一个词。对话补全只是包装成了聊天的形式。多轮对话每次调用 API 时你需要把完整的对话历史传回去# 第一轮messages[{role:user,content:11 等于几}]responsellm(messages)# 第二轮 —— 带上第一轮的历史messages[{role:user,content:11 等于几},{role:assistant,content:等于2},{role:user,content:再加上3呢},]responsellm(messages)# LLM 才能理解上下文这就是 LLM没有记忆的体现——每次调用独立记忆靠你把历史传回去。二、关键参数详解temperature —— 创造力的旋钮temperature行为适用场景0 ~ 0.2确定性输出相同输入几乎总是相同输出代码生成、分类0.3 ~ 0.7平衡创造力和准确性日常对话0.8 ~ 1.5高随机性创意写作、头脑风暴核心temperature 控制概率分布的采样策略。越低越倾向选概率最高的词越高低概率词也有机会被选中。max_tokens —— 输出长度上限LLM 生成到max_tokens个 token 后停止一个汉字约 1~2 个 token设置太小会导致回答被截断其他参数参数作用说明top_p核采样和 temperature 二选一stream流式输出逐 token 返回体验更流畅stop停止序列遇到指定字符串时停止生成三、结构化输出 —— JSON mode这是 Agent 开发中最重要的技能之一。Agent 需要 LLM 返回机器可解析的数据而非自然语言Tool Use 的参数下节课核心信息抽取的结果分类/判定的结果方式一在 prompt 中要求responseclient.chat.completions.create(modelMODEL,messages[{role:system,content:你只输出 JSON 格式。},{role:user,content:从张三25岁北京中提取信息返回 {name: , age: 0}}])方式二使用 response_format推荐responseclient.chat.completions.create(modelMODEL,messages[...],response_format{type:json_object},# 原生 JSON mode)不是所有模型都支持response_format。不支持时方式一的 prompt 方式同样有效。四、完整代码 环境准备本课使用与第 1 课相同的环境。如尚未配置请先完成pip install openai python-dotenv进入code/目录复制.env.example为.env填入你的 API Key详见第 1 课新建lesson02_structured_output.py粘贴以下代码 第 2 课理解 LLM —— 参数调优与结构化输出 importjson,osfromdotenvimportload_dotenv load_dotenv()fromopenaiimportOpenAI clientOpenAI(api_keyos.getenv(DEEPSEEK_API_KEY),base_urlhttps://api.deepseek.com,)MODELdeepseek-chat# 切换 OpenAI 或通义千问请参考第 1 课ifnotclient.api_key:print(❌ 未检测到 API Key)exit(1)# 示例 1多轮对话 print(*50,多轮对话,*50,sep\n)messages[{role:system,content:你是 AI 助手回答要简洁。},{role:user,content:什么是 AI Agent},]respclient.chat.completions.create(modelMODEL,messagesmessages)print(第一轮回答,resp.choices[0].message.content[:100],...)messages.append({role:assistant,content:resp.choices[0].message.content})messages.append({role:user,content:它和普通 LLM 有什么区别})respclient.chat.completions.create(modelMODEL,messagesmessages)print(第二轮回答,resp.choices[0].message.content[:100],...)# 示例 2temperature 对比 print(\n,*50,temperature 对比,*50,sep\n)fortempin[0.0,0.7,1.2]:print(f\n--- temperature {temp}---)respclient.chat.completions.create(modelMODEL,messages[{role:user,content:给出一个 Python 斐波那契函数。}],temperaturetemp,)print(resp.choices[0].message.content[:120]...)# 示例 3JSON 结构化输出prompt 方式print(\n,*50,JSON 结构化输出,*50,sep\n)respclient.chat.completions.create(modelMODEL,messages[{role:system,content:你只输出 JSON 格式。},{role:user,content:从王小明今年28岁住在深圳中提取信息。格式: {\name\: \...\, \age\: 0, \city\: \...\}}],)try:datajson.loads(resp.choices[0].message.content)print(解析成功,json.dumps(data,indent2,ensure_asciiFalse))exceptjson.JSONDecodeError:print(解析失败原始输出,resp.choices[0].message.content)# 示例 4response_format JSON mode print(\n,*50,response_format JSON mode,*50,sep\n)print(部分模型不支持此功能不支持时会自动降级\n)try:respclient.chat.completions.create(modelMODEL,messages[{role:system,content:你是一个 JSON 信息提取助手。},{role:user,content:提取李四30岁上海设计师。}],response_format{type:json_object},)datajson.loads(resp.choices[0].message.content)print(解析成功,json.dumps(data,indent2,ensure_asciiFalse))exceptExceptionase:print(fresponse_format 方式失败此模型可能不支持:{e})# 示例 5max_tokens 截断 print(\n,*50,max_tokens 截断效果,*50,sep\n)forlimitin[20,100,500]:print(f\n--- max_tokens {limit}---)respclient.chat.completions.create(modelMODEL,messages[{role:user,content:用 200 字介绍 AI Agent。}],max_tokenslimit,)print(f实际输出{len(resp.choices[0].message.content)}字,→ 被截断ifresp.choices[0].finish_reasonlengthelse→ 完整)运行结果示例 多轮对话 第一轮回答AI Agent 是一个能自主感知环境、做出决策并采取行动... 第二轮回答主要区别在于LLM 只生成文本回复而 Agent... temperature 对比 --- temperature 0.0 --- def fibonacci(n): ... --- temperature 1.2 --- Oh, 斐波那契这玩意有趣我想想... JSON 结构化输出 解析成功{ name: 王小明, age: 28, city: 深圳 }常见报错问题原因解决json.JSONDecodeErrorLLM 返回的不是有效 JSON优化 prompt或使用response_formatresponse_format 方式失败模型不支持原生 JSON mode切回 prompt 方式finish_reason: length输出被 max_tokens 截断增大 max_tokens 值动手练习写一个 prompt 让 LLM 稳定输出包含姓名、年龄、城市的 JSON把 temperature 设为 0连续调用 5 次观察输出是否完全一致把 max_tokens 设为 5观察被截断的效果思考题temperature 0 时输出是完全确定的吗为什么在 Agent 场景中结构化输出为什么比自然语言输出更适合如果你的 Agent 需要从一段文本中提取关键信息你会怎么设计 prompt 完整代码本课程所有代码已托管在 GitCodegit clone gitgitcode.com:gcw_A202cbBm/ai-agent.git cd ai-agent/code也可直接访问https://gitcode.com/gcw_A202cbBm/ai-agent下一篇预告AI Agent 入门三Tool Use 入门 —— Function Calling 原理与实战你将学会给 LLM 绑定工具计算器、天气查询让它从只能说话变成能做事。这是 Agent 最核心的一课。如果您觉得有用欢迎点赞、转发、评论、关注。