资讯动态

大模型API调用实战②-零基础保姆级教程

发布时间:2026/8/21 7:14:35 来源:尧图企业网站定制
第 4 章读懂响应对象新手第一坑4.1 SDK 返回的是对象不是字典resp.json()拿到的是字典用data[choices][0]取值。SDK 不一样——它把 JSON 转成了 Python 对象必须用点号访问属性print(response[choices]) # ❌ TypeError对象不能用方括号 print(response.choices[0].message.content) # ✅ 点号访问属性完整解剖一次响应r response print(r.id) # 本次请求的唯一ID排查问题时给平台客服用 print(r.model) # 实际使用的模型 print(r.choices[0].message.role) # assistant print(r.choices[0].message.content) # 回答正文 print(r.choices[0].finish_reason) # 结束原因下节详解 print(r.usage.prompt_tokens) # 输入消耗 token print(r.usage.completion_tokens) # 输出消耗 token print(r.usage.total_tokens) # 合计 —— 你的账单来源 # 需要字典/JSON 时落日志、存数据库 d r.model_dump() # 对象 → 字典 print(d[usage][total_tokens]) # 变回字典后就能用方括号了model_dump()来自Pydantic——SDK 的响应对象全是 Pydantic 模型。现在先记住这对搭档对象用点号落盘先 dump。4.2 finish_reason每次调用都该看一眼的字段值含义你的代码该做什么stop正常说完了正常处理length撞到 max_tokens 被截断内容不完整提高上限或续写别把半句话给用户tool_calls模型申请调用工具进入 Function Calling 流程第 11 章主线剧情content_filter触发内容安全拦截给用户兜底话术记录日志养成职业习惯生产代码里永远if r.choices[0].finish_reason length: ...做检查——被截断的 JSON 是解析事故的头号来源。第 5 章多轮对话 —— 亲手证明API 是失忆的5.1 失忆实验必做API 无状态现在亲眼见证r1 client.chat.completions.create(modeldeepseek-chat, messages[{role: user, content: 我叫王小明请记住我的名字。}]) print(第1轮:, r1.choices[0].message.content) # 它会热情答应记住 r2 client.chat.completions.create(modeldeepseek-chat, messages[{role: user, content: 我叫什么名字}]) print(第2轮:, r2.choices[0].message.content) # 它根本不知道你是谁两次调用是两个完全独立的宇宙。所谓记忆 你的代码把历史拼进 messages 重发。修复版messages [{role: system, content: 你是一个友好的助手。}] def chat(user_input: str) - str: messages.append({role: user, content: user_input}) r client.chat.completions.create(modeldeepseek-chat, messagesmessages) answer r.choices[0].message.content messages.append({role: assistant, content: answer}) # 回答也要入史 return answer print(chat(我叫王小明请记住。)) print(chat(我叫什么名字)) # 这次它记得了 —— 因为历史在 messages 里 print(f当前历史 {len(messages)} 条本轮输入token会随历史增长)两个必须刻进肌肉的细节①assistant 的回答也要 append 进历史漏了它下一轮模型会看到用户连问两句没人理的诡异剧本②历史越长每轮越贵5.2 封装成类class ChatSession: def __init__(self, client, model: str, system_prompt: str): self.client client self.model model self.messages: list[dict] [{role: system, content: system_prompt}] def ask(self, user_input: str, **kwargs) - str: **kwargs 透传 temperature 等参数01教程10.4的知识落地 self.messages.append({role: user, content: user_input}) r self.client.chat.completions.create( modelself.model, messagesself.messages, **kwargs) answer r.choices[0].message.content self.messages.append({role: assistant, content: answer}) return answer session ChatSession(client, deepseek-chat, 你是一个毒舌但专业的编程导师。) print(session.ask(Python 的列表和元组有什么区别)) print(session.ask(那我刚才问的是什么话题)) # 验证记忆第 6 章参数实验室 —— 用真模型验证理论建一个param_lab.py做完下面四个实验并记录观察结论这是把知识变成手感的过程别跳PROMPT 给一款AI学习APP起一个名字只输出名字本身。 # 实验1temperature —— 同题各问5次 for t in [0.0, 1.3]: print(f\n--- temperature{t} ---) for _ in range(5): r client.chat.completions.create(modeldeepseek-chat, messages[{role: user, content: PROMPT}], temperaturet) print(r.choices[0].message.content) # 预期观察0.0 几乎5次同名1.3 五花八门偶尔还会不听话加解释# 实验2max_tokens 截断 finish_reason 检查 r client.chat.completions.create(modeldeepseek-chat, messages[{role: user, content: 详细介绍一下Python的历史}], max_tokens30) print(r.choices[0].message.content) # 话说一半戛然而止 print(r.choices[0].finish_reason) # length —— 第4章的表格应验# 实验3stop 停止词 —— 第2批埋的伏笔手写ReAct的关键 r client.chat.completions.create(modeldeepseek-chat, messages[{role: user, content: 从1数到10每个数字一行}], stop[5]) print(r.choices[0].message.content) # 数到4就停 —— 碰到5立刻刹车# 实验4体验推理模型第2批第8章的新物种 r client.chat.completions.create(modeldeepseek-reasoner, messages[{role: user, content: 9.11 和 9.9 哪个大请给出理由}]) print(【思考过程】, r.choices[0].message.reasoning_content[:300], ...) print(【最终回答】, r.choices[0].message.content) # 观察reasoning_content 是它的草稿对比 deepseek-chat 答同一题的差异与耗时

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价