进阶 api-docs.deepseek.com 2026-10-13 22:45:35 · 23 阅读

第16章 使用 Responses API 调用 DeepSeek 模型

# 使用 Responses API 为了满足 Codex 用户的需求,我们的 API 现已支持 Responses API 格式,base_url 为 https://api.deepseek.com。 只需简单配置,即可在 Codex 中使用 DeepSeek 模型。 ## 在 Codex 中集成 DeepSeek 模型 请参考 Integrate with Codex。 ## 通过 Responses API 调用 DeepSeek 模型 # 请先安装 OpenAI SDK:`pip3 install openai`
from openai import OpenAI

client = OpenAI(api_key="", base_url="https://api.deepseek.com")

response = client.responses.create(
model="deepseek-flash",
instructions="You are a helpful assistant.",
input="Hi, how are you?",
)

print(response.output_text)
## 流式输出 设置 stream: true,即可通过一系列语义化的 server-sent events(SSE)接收响应。每个事件包含一个 event 字段表示事件类型,以及一个单调递增的 sequence_number。流以 response.completed / response.incomplete / response.failed 事件结束——没有 data: [DONE] 消息。 stream = client.responses.create(
model="deepseek-flash",
instructions="You are a helpful assistant.",
input="Hi, how are you?",
stream=True,
)

for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="")
完整事件列表: 事件描述 response.created 第一个事件;响应已创建,状态为 in_progress response.in_progress 正在生成响应 response.output_item.added / response.output_item.done 输出项(推理/消息/函数调用/自定义工具调用)开始/完成 response.content_part.added / response.content_part.done 输出项内的内容部分开始/完成 response.reasoning_text.delta / response.reasoning_text.done 增量思维链文本 / 完整思维链文本 response.output_text.delta / response.output_text.done 增量输出文本 / 完整输出文本 response.function_call_arguments.delta / response.function_call_arguments.done 增量函数调用参数 / 完整参数 response.custom_tool_call_input.delta / response.custom_tool_call_input.done 增量自定义工具调用(apply_patch)输入 / 完整输入 response.completed 正常完成时的最终事件,携带包含 usage 的完整响应对象 response.incomplete 截断(如达到 max_output_tokens)时的最终事件,携带完整响应对象 response.failed 失败时的最终事件,携带包含错误详情的完整响应对象 图像输入​ Responses API 在 deepseek-flash 模型中支持图像。与 Chat Completions 相同的图像限制和格式要求适用。 图像通过消息项中的 input_image 内容部分提供,使用 image_url(http(s) URL 或 base64 数据 URL)或 file_id(通过 Files API 上传的图像): response = client.responses.create(
model="deepseek-flash",
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": "What is in this image?"},
{"type": "input_image", "image_url": "https://example.com/image.jpg", "detail": "low"},
],
}
],
)
print(response.output_text)
input_image 部分也可能出现在 function_call_output / custom_tool_call_output 项的输出中,因此模型可以接收由你的工具生成的图像: input=[
{"role": "user", "content": "Read the screenshot the tool returned."},
{"type": "function_call", "call_id": "fc1", "name": "take_screenshot", "arguments": "{}"},
{"type": "function_call_output", "call_id": "fc1",
"output": [{"type": "input_image", "image_url": "data:image/png;base64,"}]},
]
input_image 字段 image_url:http(s) URL(最长 8192 字符)或 base64 编码的 data URL(data:image/jpeg;base64,...)。支持 JPEG、PNG、GIF、WebP 格式。 file_id:通过 Files API 上传的图片 ID,格式为 file-api-...。 detail:low / high / original / auto。low 会在推理前把图片降采样到 512x512,其他值保留原始图片。设置 file_id 时该参数会被忽略。 image_url 和 file_id 互斥:两者都不传会返回 400 错误("input_image must have image_url or file_id"),两者同时传也会返回 400 错误("input_image cannot have both image_url and file_id")。 限制 图片只允许出现在 user / developer 消息项以及 function_call_output / custom_tool_call_output 的输出中。system 或 assistant 消息中的图片会返回 400 错误。 deepseek-flash 会将 input_image 部分作为真实图片处理。 与 Chat Completions 相同的共享图片限制依然适用(单张内联图片 32 MiB、单个 file_id 图片 64 MiB、不含 file_id 图片时总量 64 MiB、包含时最多 200 MiB、每次请求最多 600 张图片等)——详见 Vision: Limits。 兼容性细节 本节列出 DeepSeek API 与 Responses API 的兼容性细节。完整的 Responses API 格式定义请参考 OpenAI 官方 API 文档。 顶层请求参数 参数支持情况model支持。deepseek-flash,见 Models & Pricinginput支持。字符串或 input 项列表;input 和 instructions 至少需要提供一个instructions支持。会作为第一条 system 消息插入stream支持temperature支持(范围 [0.0, 2.0];思考模式下无效)top_p支持(思考模式下生效,下限 0.95;非思考模式下固定为 1.0)max_output_tokens支持top_logprobs支持(范围 [0, 20])tools部分支持。支持 function,其他类型会被忽略,见下方的 Tools 表tool_choice支持。none / auto / required / 指定某个工具({"type": "function", "name": ...})reasoning部分支持。支持 effort;summary 可以传入但不会生成摘要text部分支持。format 完全支持;verbosity 可以传入但不生效user支持。见 Rate Limit & Isolationparallel_tool_calls忽略(并行工具调用始终开启)max_tool_calls忽略previous_response_id不支持(无状态 API)conversation不支持(无状态 API)store不支持。响应始终携带 store: falsebackground不支持metadata不支持include不支持prompt不支持truncation不支持。超出上下文窗口的请求会返回 400 错误service_tier不支持safety_identifier不支持prompt_cache_key / prompt_cache_retention不支持。上下文缓存由系统自动管理,见 Context Cachingcontext_management不支持stream_options不支持 不支持的参数会被静默忽略、不会报错,因此现有的 Responses API 客户端无需修改即可直接接入。 Input 项 类型支持情况message支持。角色为 user / assistant / system / developer(developer 视作 user);content 支持字符串以及 input_text / output_text / input_image 内容部分。input_image 部分会作为真实图片处理(仅允许出现在 user / developer 消息中;system / assistant 消息中的图片会返回 400 错误)。不支持文件输入function_call支持。会合并进相邻的 assistant 消息function_call_output支持。输出可以是字符串或内容部分列表;输出中的 input_image 部分会作为真实图片处理reasoning支持。纯文本内容会合并进相邻的 assistant 消息;不支持 summary 和 encrypted_contentcustom_tool_call / custom_tool_call_output支持(用于 apply_patch 自定义工具,并校验 call_id 配对)。输出中的 input_image 部分会作为真实图片处理其他类型忽略 注意:input 中回传的 web_search_call 项——例如之前用旧模型请求产生的搜索结果——仍会被还原并拼接进上下文。 Tools 类型支持情况function支持custom仅支持 {"type": "custom", "name": "apply_patch"}(为了兼容 Codex);其他名称会返回 400 错误web_search / file_search / code_interpreter / computer_use / mcp / 其他内置工具忽略 响应字段 响应对象与 OpenAI Responses API 的响应结构兼容。依赖不支持能力的字段始终为固定值(如 store: false、previous_response_id: null、parallel_tool_calls: true)。 Token 用量在 usage 中返回: input_tokens:输入 token 数,其中 input_tokens_details.cached_tokens 是命中上下文缓存的 token 数 output_tokens:输出 token 数,其中 output_tokens_details.reasoning_tokens 是思维链 token 数

评论 (0)