MaynorAI
返回博客

DeepSeek V4 Flash 接入实战:Responses API、Codex 与百万上下文

手把手接入 DeepSeek V4 Flash:OpenAI 兼容调用、Responses API、Codex 适配、思考模式与提示词缓存,附 curl/Python 示例。

2026年7月31日Maynor
DeepSeek V4 Flash 接入实战:Responses API、Codex 与百万上下文

DeepSeek V4 Flash 接入实战:Responses API、Codex 与百万上下文

模型再强,接不进你的工作流也是白搭。好消息是,V4-Flash 正式版是 OpenAI 兼容的,而且原生支持 Responses API、针对 Codex 做了适配。本文给你一套可直接复制的接入方案。

一、三种接入方式

DeepSeek API 与 OpenAI / Anthropic 格式兼容,你可以不改业务代码,只改配置:

方式 base_url 说明
OpenAI Chat Completions https://api.deepseek.com 最通用,SDK 直接改 base_url
OpenAI Responses API https://api.deepseek.com 仅 V4-Flash 支持,适合 Codex / Agent 新范式
Anthropic 格式 https://api.deepseek.com/anthropic 兼容 Claude 工具链

注意:Responses API 目前仅支持 deepseek-v4-flash,V4-Pro 预计 2026 年 8 月初接入。

二、第一个请求(非思考模式)

curl

curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DEEPSEEK_API_KEY" \
  -d '{
    "model": "deepseek-v4-flash",
    "messages": [{"role": "user", "content": "用三句话介绍北京的故宫。"}],
    "thinking": {"type": "disabled"},
    "stream": false
  }'

Python(OpenAI SDK)

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com",
)

resp = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": "你好"}],
    extra_body={"thinking": {"type": "disabled"}},
)
print(resp.choices[0].message.content)

三、思考模式:该开就开,该关就关

V4 系列默认开启思考模式(先输出推理链 reasoning_content,再给最终答案)。你可以在请求里控制:

# 开启思考(默认),并指定强度
resp = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": "证明 √2 是无理数。"}],
    reasoning_effort="high",                      # high / max
    extra_body={"thinking": {"type": "enabled"}},
)
print("推理过程:", resp.choices[0].message.reasoning_content)
print("最终答案:", resp.choices[0].message.content)

要点:

  • 开关:{"thinking": {"type": "enabled" / "disabled"}}
  • 强度:reasoning_efforthighmax(low/medium 会被映射为 high)
  • 思考模式下 temperaturetop_p 等不生效,设置也不会报错但无效
  • 涉及工具调用(tool call)时,必须把 reasoning_content 原样回传给后续请求,否则会返回 400

四、Responses API + Codex 适配

V4-Flash 正式版原生支持 Responses API,并针对 Codex 做了适配。以下代码即可调用:

from openai import OpenAI

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

response = client.responses.create(
    model="deepseek-v4-flash",
    instructions="You are a helpful assistant.",
    input="用 Python 写一个快速排序。",
)
print(response.output_text)

把 DeepSeek 接进 Codex 也很直接:在 Codex 配置里把模型提供方设为 DeepSeek、base_url 填 https://api.deepseek.com,即可在 Codex CLI、ChatGPT 桌面端、VS Code 的 Codex 插件里调用 V4-Flash,多个客户端共享同一套配置。

五、提示词缓存与成本控制

V4-Flash 的价格已经很低,但用对方式还能更省:

  • 缓存命中输入仅 ¥0.02/百万 token:把长 system prompt、知识库、项目上下文放前面,复用即可大幅降低输入成本(RAG、客服、长 prompt 场景尤其明显)。
  • 峰谷定价:高峰时段价格翻倍(输出高峰约 ¥4/百万),非高峰更低,批量任务可错峰。
  • 简单任务关掉思考:日常分类、抽取、简单问答用 thinking: disabled,既降延迟又省输出 token。

六、常见坑

  1. Responses API 只支持 V4-Flash,别把 Pro 名塞进去。
  2. 思考模式下调 temperature 无效,别困惑为什么没变化。
  3. tool call 必须回传 reasoning_content,否则 400。
  4. 上下文 1M 但请求超窗口会直接 400,长任务注意分片。

DeepSeek V4 Flash 使用链接

👉 https://apipro.maynor1024.live/

(该链接为 DeepSeek V4 Flash 的使用入口,按页面说明自行判断和使用。)

继续阅读

相关推荐