DeepSeek 是中国领先的 AI 公司,以高性价比和强大的推理能力著称。其开源模型 DeepSeek-V3 和 DeepSeek-R1 在全球范围内受到广泛关注,在多项基准测试中与 GPT-4o、Claude 3.5 Sonnet 等顶级闭源模型处于同一水平,而价格仅为后者的十分之一左右。
2026 年 4 月 24 日,DeepSeek 正式发布 V4 系列(DeepSeek-V4-Pro / DeepSeek-V4-Flash),在推理能力、Agent 能力、超长上下文(1M)和成本效率方面均有重大突破。V4 内容见下方独立章节。
| 属性 | 值 |
|---|---|
| 官网 | https://www.deepseek.com |
| API 文档 | https://platform.deepseek.com/api-docs |
| 基础 URL | https://api.deepseek.com |
| 认证方式 | Bearer Token (API Key) |
| OpenAI 兼容 | ✅ 是(完全兼容 OpenAI SDK) |
| Anthropic 兼容 | ✅ 是(https://api.deepseek.com/anthropic) |
| 支持流式 | ✅ SSE 格式 |
| 支持 Function Calling | ✅ |
| 支持系统消息 | ✅ |
| 开源模型 | ✅ MIT 许可证 |
| 模型 | 上下文 | 架构 | 特点 | 价格(输入/输出,per M tokens) |
|---|---|---|---|---|
deepseek-chat (V3) |
64K | MoE 671B-37B | 通用对话,多语言 | ¥2 / ¥8 |
deepseek-reasoner (R1) |
64K | MoE 671B | 推理模型,展示思维链 | ¥4 / ¥16 |
deepseek-coder |
16K | MoE 16B | 代码生成与补全 | ¥2 / ¥8 |
⚠️
deepseek-chat和deepseek-reasoner将于 2026-07-24 15:59 UTC 正式废弃,目前分别映射到deepseek-v4-flash的非思考模式和思考模式。
DeepSeek-V3 采用 MoE(Mixture-of-Experts,混合专家) 架构,总参数量 671B,但每次推理仅激活 37B 参数,这使得其推理效率远高于同参数量的稠密模型。
架构特点:
┌──────────────────────────────────────────────────┐
│ DeepSeek-V3 架构 │
├──────────────────────────────────────────────────┤
│ ┌──────────────┐ ┌──────────────────┐ │
│ │ Tokenizer │ ──▶ │ Embedding + │ │
│ │ (Byte-level │ │ Positional PE │ │
│ │ BPE) │ │ (RoPE) │ │
│ └──────────────┘ └────────┬─────────┘ │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ Transformer × 61 Layers │ │
│ │ ┌──────────────────────────────────┐ │ │
│ │ │ MLA Attention Layer │ │ │
│ │ │ (LV = 128, LK = 128, QR = 64) │ │ │
│ │ └──────────────┬───────────────────┘ │ │
│ │ ▼ │ │
│ │ ┌──────────────────────────────────┐ │ │
│ │ │ MoE FFN Layer (256 experts, │ │ │
│ │ │ top-8 activated, 37B active) │ │ │
│ │ └──────────────────────────────────┘ │ │
│ └──────────────────────────────────────────┘ │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ LM Head + Softmax │ │
│ └──────────────────────────────────────────┘ │
└──────────────────────────────────────────────────┘
DeepSeek-R1 是专注于复杂推理的模型,使用纯强化学习方法从基础模型训练而来,不依赖人工标注的思维链数据。
训练方法创新:
能力分布:
| 能力维度 | R1 表现 | 对比 GPT-4o | 对比 DeepSeek-V3 |
|---|---|---|---|
| 数学竞赛(AIME 2024) | 79.8% | 59.4% | 39.2% |
| 数学推理(MATH-500) | 97.3% | 94.7% | 90.2% |
| 代码竞赛(Codeforces) | 96.3 百分位 | 80.6 百分位 | 58.7 百分位 |
| 综合知识(MMLU) | 90.8% | 88.7% | 88.5% |
| 逻辑推理(BBH) | 92.8% | 88.4% | 88.2% |
可以看到,R1 在需要深度推理的任务(数学、代码)上与 V3 拉开了明显差距,而在通用知识任务上差距不大。
| 特性 | V3 / R1 时代 | V4 时代(2026-04-24 起) |
|---|---|---|
| 核心模型 | deepseek-chat / deepseek-reasoner |
deepseek-v4-pro / deepseek-v4-flash |
| 上下文长度 | 64K | 1M(100 万 token) |
| 总参数量 | 671B | 1.6T (Pro) / 284B (Flash) |
| 激活参数 | 37B | 49B (Pro) / 13B (Flash) |
| 推理模式 | R1 单独模型 | 双模型均支持 Thinking / Non-Thinking |
| 价格(输入/输出) | ¥2–4 / ¥8–16 | $0.14–1.74 / $0.28–3.48 |
| 开源许可 | MIT | MIT |
核心能力:
核心能力:
V4 系列引入了两大注意力机制创新,使 1M 上下文成为标准配置:
{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 1.0,
"max_tokens": 4096,
"top_p": 1.0,
"stream": false,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}
| 参数 | 范围 | 默认值 | 说明 |
|---|---|---|---|
temperature |
0–2 | 1.0 | 采样温度,控制输出的随机性。推理任务建议 0.0–0.3,创意任务 0.7–1.0 |
top_p |
0–1 | 1.0 | Nucleus sampling 参数。1.0 表示不考虑 top_p 截断 |
max_tokens |
1–8192 | 4096 | 单次生成的最大 token 数。包含输出中的所有 token |
frequency_penalty |
−2.0~2.0 | 0.0 | 对已出现的 token 施加频率惩罚,值越高越不易重复 |
presence_penalty |
−2.0~2.0 | 0.0 | 对已出现的 token 施加存在惩罚,鼓励模型探索新话题 |
stop |
字符串/数组 | null | 停止序列,遇到这些 token 将终止生成 |
stream |
bool | false | 是否启用流式输出 |
以下测试使用 deepseek-chat 模型,Prompt 为:"写一句关于春天的诗。"
| temperature 值 | 输出示例 | 特点 |
|---|---|---|
| 0.0 | "春风又绿江南岸" | 确定性极高,几乎每次输出相同 |
| 0.3 | "春雨绵绵润万物,微风拂柳绿枝头" | 轻微变化,质量稳定 |
| 0.7 | "春天在柳枝上荡着秋千,把冬天的尾巴荡落了" | 有一定创意,但不失连贯 |
| 1.2 | "花瓣把路牌的颜色偷走了,变成路上的小裙子" | 创意但偶尔跳跃 |
| 1.8 | "春——啊,光在逃跑,也跑不过花的长发" | 可能不连贯,风格怪异 |
建议:
temperature = 0.0–0.3temperature = 0.5–0.7temperature = 0.8–1.2temperature = 1.0–1.5两个参数都用于降低输出中的重复,但机制不同:
最佳实践:
| 场景 | 推荐设置 | 理由 |
|---|---|---|
| 代码生成 | frequency_penalty=0 |
代码中重复是正常的(循环、函数调用) |
| 长文本生成 | frequency_penalty=0.3–0.7 |
防止整段重复 |
| 列表生成 | presence_penalty=0.3–0.5 |
鼓励输出多样化的列表项 |
| 对话式 | 两者保持默认 0.0 | 正常对话不需要额外惩罚 |
DeepSeek 的流式输出与 OpenAI 格式完全一致,便于兼容现有代码。
data: {"id":"deepseek-chat-xxx","object":"chat.completion.chunk","created":1234567890,"model":"deepseek-chat","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"deepseek-chat-xxx",...,"delta":{"content":"你好"},...}
data: {"id":"deepseek-chat-xxx",...,"delta":{"content":"世界"},...}
data: [DONE]
from openai import OpenAI
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
stream = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "用 50 字解释什么是递归"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
使用 deepseek-reasoner 模型时,流式输出会逐步展示思维链内容,包含在 \n<think> 标签内:
data: {"delta":{"content":"\n<think>"}}
data: {"delta":{"content":"用户想了解递归的概念"}}
data: {"delta":{"content":"递归是函数调用自身..."}}
...
data: {"delta":{"content":"\n</think>"}}
data: {"delta":{"content":"\n\n## 什么是递归"}}
data: {"delta":{"content":"递归是…"}}
前端需要解析这些 chunk,将 \n<think>...\n</think> 中的内容作为推理过程展示(如折叠区域),将标签外的内容作为最终答案展示。
R1 的输出包含两个部分:
\n<think>...\n</think> 标签内,展示模型的推理过程完整的输出示例(输入:计算 1+2+...+100):
<think>
用户想要计算从1加到100的和。
这是一个等差数列求和问题。
n = 100, a1 = 1, a100 = 100
求和公式:S = n(a1 + an) / 2
S = 100(1+100)/2 = 100×101/2 = 5050
所以答案是5050。
</think>
从1加到100的和是 **5050**。
计算过程:
- 首项 a1 = 1,末项 a100 = 100
- 项数 n = 100
- 应用等差数列求和公式 $S_n = \frac{n(a_1 + a_n)}{2}$
$$
S_{100} = \frac{100 \times (1 + 100)}{2} = \frac{100 \times 101}{2} = 5050
$$
数学推理(中学数学题):
输入:一个长方体,长宽高之和为 24,且长宽高成等差数列,求最大体积。
R1 的思维链会逐步展开:
逻辑推理(谁在说谎):
输入:A说B在说谎,B说C在说谎,C说A和B都在说谎。谁说实话?
R1 会枚举所有可能性组合,逐一验证矛盾:
代码推理(算法优化):
输入:给定长度为 N 的数组,找出和为 K 的最长子数组长度。
R1 的思考过程:
对于 R1 模型,可以通过 API 参数控制是否在输出中包含思维链:
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=[{"role": "user", "content": "解题"}],
extra_body={"include_reasoning": True} # 是否包含思维链
)
基础 URL:
| API 格式 | 基础 URL |
|---|---|
| OpenAI ChatCompletions | https://api.deepseek.com |
| Anthropic Messages | https://api.deepseek.com/anthropic |
模型名称映射:
| 新模型名 | 旧模型别名 | 说明 |
|---|---|---|
deepseek-v4-pro |
— | 旗舰模型,最强推理 |
deepseek-v4-flash |
deepseek-chat(非思考模式)deepseek-reasoner(思考模式) |
高效模型,默认启用思考模式 |
迁移示例:
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://api.deepseek.com" # ✅ 保持不变
)
# 通用对话(非思考模式)
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Hello"}],
extra_body={"thinking": {"type": "disabled"}}
)
# 推理任务(思考模式,默认开启)
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "解方程"}],
reasoning_effort="high",
extra_body={"thinking": {"type": "enabled"}}
)
V4 系列的两个模型均支持思考模式,且默认启用。
控制参数:
| 控制项 | OpenAI 格式 | Anthropic 格式 | 默认值 |
|---|---|---|---|
| 思考开关 | extra_body={"thinking": {"type": "enabled/disabled"}} |
thinking: {type: "enabled"} |
enabled |
| 思考强度 | reasoning_effort: "high/max" |
output_config: {effort: "high/max"} |
high |
V4 与 R1 思维链的差异:
| R1 (旧) | V4 (新) | |
|---|---|---|
| 思维链位置 | content 内的 \n<think>...\n</think> |
独立的 reasoning_content 字段 |
| 获取方式 | 字符串解析 | response.choices[0].message.reasoning_content |
| 多轮传递 | 自动包含在 content 中 | 无工具调用时 API 自动忽略;工具调用时必须传回 |
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "9.11 和 9.8 哪个大?"}],
reasoning_effort="high",
extra_body={"thinking": {"type": "enabled"}}
)
reasoning = response.choices[0].message.reasoning_content # 思维链
answer = response.choices[0].message.content # 最终答案
多轮对话中的思维链处理:
reasoning_content 不需要参与上下文拼接,传给 API 会被自动忽略reasoning_content 必须完整传回 API,否则返回 400 错误思考模式的限制:
思考模式下不支持 temperature、top_p、presence_penalty、frequency_penalty(设置后不会报错,但无效果)。
基础用法:
from openai import OpenAI
client = OpenAI(api_key="<your api key>", base_url="https://api.deepseek.com")
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "城市名称"}
},
"required": ["location"]
}
}
}
]
messages = [{"role": "user", "content": "杭州天气怎么样?"}]
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=messages,
tools=tools
)
思考模式下的工具调用:
def run_turn(messages):
while True:
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=messages,
tools=tools,
reasoning_effort="high",
extra_body={"thinking": {"type": "enabled"}}
)
messages.append(response.choices[0].message)
if not response.choices[0].message.tool_calls:
break
for tool in response.choices[0].message.tool_calls:
result = execute_tool(tool.function.name, json.loads(tool.function.arguments))
messages.append({
"role": "tool",
"tool_call_id": tool.id,
"content": result
})
Strict 模式(Beta):
Strict 模式下模型严格遵循 JSON Schema 格式输出工具调用参数。
base_url="https://api.deepseek.com/beta""strict": true"additionalProperties": falseimport json
from openai import OpenAI
client = OpenAI(api_key="<your api key>", base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{"role": "system", "content": "解析用户提供的考试文本,提取 question 和 answer,以 JSON 格式输出。"},
{"role": "user", "content": "世界上最长的河流是什么?尼罗河。"}
],
response_format={"type": "json_object"}
)
result = json.loads(response.choices[0].message.content)
# {"question": "世界上最长的河流是什么?", "answer": "尼罗河"}
DeepSeek API 的上下文缓存技术对所有用户默认启用。
工作原理:
缓存持久化规则:
检查缓存状态:
{
"usage": {
"prompt_tokens": 1000,
"completion_tokens": 200,
"prompt_cache_hit_tokens": 800,
"prompt_cache_miss_tokens": 200
}
}
| 项目 | deepseek-v4-flash | deepseek-v4-pro |
|---|---|---|
| 1M 输入 token(缓存命中) | $0.0028 $0.003625 | |
| 1M 输入 token(缓存未命中) | $0.14 $0.435 | |
| 1M 输出 token | $0.28 $0.87 | |
| 上下文长度 | 1M | 1M |
| 最大输出长度 | 384K | 384K |
| 并发限制 | 2500 | 500 |
注:
- 缓存命中价格为发布价的 1/10
deepseek-v4-pro当前享受 75% 折扣,折扣期至 2026-05-31 15:59 UTC,之后恢复原价(输入 $1.74/M,输出 $3.48/M)- 价格单位为美元
与其他模型的成本对比(2000 输入 + 500 输出):
| 模型 | 单次成本 | 100 万次调用成本 |
|---|---|---|
| DeepSeek-V4-Flash | ~$0.00021 ~$210 | |
| DeepSeek-V4-Pro(折扣期) | ~$0.00065 ~$650 | |
| DeepSeek-V4-Pro(原价) | ~$2.61 ~$2,610 | |
| GPT-5.4 | ~$5.00 ~$5,000 | |
| Claude Opus 4.7 | ~$8.00 ~$8,000 |
DeepSeek-V4 已与主流 AI 工具无缝集成:
Claude Code:
export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
export ANTHROPIC_AUTH_TOKEN=<your DeepSeek API Key>
export ANTHROPIC_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_OPUS_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-v4-pro[1m]
export ANTHROPIC_DEFAULT_HAIKU_MODEL=deepseek-v4-flash
export CLAUDE_CODE_SUBAGENT_MODEL=deepseek-v4-flash
export CLAUDE_CODE_EFFORT_LEVEL=max
OpenCode: 执行 opencode → /connect → 选择 deepseek → 输入 API Key → 选择 DeepSeek-V4-Pro
OpenClaw: openclaw onboard --install-daemon → QuickStart → DeepSeek → 输入 API Key → 模型名 deepseek-v4-pro
| Anthropic 模型前缀 | 映射到 DeepSeek |
|---|---|
claude-opus-* |
deepseek-v4-pro |
claude-haiku-* / claude-sonnet-* |
deepseek-v4-flash |
完全支持的字段: max_tokens、stop_sequences、stream、system、temperature([0.0, 2.0])、top_p、thinking、tools、tool_choice、消息 content(string / text / thinking / tool_use / tool_result)
不支持的字段: anthropic-beta、anthropic-version、top_k、container、mcp_servers、cache_control、citations、image、document、search_result、redacted_thinking、code_execution_tool_result、mcp_tool_use / mcp_tool_result、container_upload
{
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "北京现在天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,如北京、上海"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}
DeepSeek 支持同时调用多个函数:
{
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "帮我比较一下北京和上海的天气,并推荐一个更宜居的城市"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"date": {"type": "string"}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "get_air_quality",
"description": "获取空气质量指数",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
}
]
}
DeepSeek-R1 在工具调用场景下表现尤其出色,因为它会在选择工具前进行推理:
<think>
用户想比较北京和上海的天气,并推荐城市。
我需要同时调用:
1. get_weather("北京") + get_weather("上海")
2. get_air_quality("北京") + get_air_quality("上海")
这样拿到数据后才能综合分析。
</think>
from openai import OpenAI
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "你是一个物理学家,请用通俗语言解释量子纠缠。"},
{"role": "user", "content": "什么是量子纠缠?"}
],
temperature=0.7,
max_tokens=1024
)
print(response.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=[
{"role": "user", "content": "一个水池有一个进水管和一个出水管。单独打开进水管 3 小时注满,单独打开出水管 4 小时排空。如果两个管同时打开,问多长时间水池能注满?"}
],
max_tokens=2048
)
# 提取思维链和答案
content = response.choices[0].message.content
if "\n<think>" in content:
reasoning = content[content.index("\n<think>")+7:content.index("\n</think>")]
answer = content[content.index("\n</think>")+8:]
print("=== 推理过程 ===")
print(reasoning.strip())
print("\n=== 最终答案 ===")
print(answer.strip())
else:
print(content)
from openai import OpenAI
import json
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
stream = client.chat.completions.create(
model="deepseek-reasoner",
messages=[{"role": "user", "content": "证明:对于任意正整数 n,n³ + 5n 能被 6 整除。"}],
stream=True
)
reasoning_buffer = ""
answer_buffer = ""
in_reasoning = False
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
if "\n<think>" in delta:
in_reasoning = True
delta = delta.replace("\n<think>", "")
elif "\n</think>" in delta:
in_reasoning = False
delta = delta.replace("\n</think>", "")
if in_reasoning:
reasoning_buffer += delta
print(f"[推理] {delta}", end="")
else:
answer_buffer += delta
print(delta, end="")
print("\n\n=== 完整推理过程 ===")
print(reasoning_buffer)
print("\n=== 完整答案 ===")
print(answer_buffer)
from openai import OpenAI
import json
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
def get_weather(location, unit="celsius"):
"""模拟天气查询"""
weather_db = {
"北京": {"temp": 22, "condition": "晴"},
"上海": {"temp": 25, "condition": "多云"},
"杭州": {"temp": 24, "condition": "晴"}
}
data = weather_db.get(location, {"temp": 20, "condition": "未知"})
return json.dumps({
"location": location,
"temperature": data["temp"],
"condition": data["condition"],
"unit": unit
})
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "城市名称"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}
]
messages = [{"role": "user", "content": "杭州今天天气怎么样?适合出门吗?"}]
# 第一轮:模型决定调用工具
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
messages.append(message)
# 执行工具调用
if message.tool_calls:
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
if function_name == "get_weather":
result = get_weather(**function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result
})
# 第二轮:模型根据工具结果生成最终回答
final_response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
tools=tools
)
print(final_response.choices[0].message.content)
from openai import OpenAI
import asyncio
from concurrent.futures import ThreadPoolExecutor
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
def single_request(prompt):
return client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": prompt}],
max_tokens=512
)
prompts = [
"用一句话解释什么是机器学习",
"用一句话解释什么是深度学习",
"用一句话解释什么是神经网络",
"用一句话解释什么是 Transformer"
]
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(single_request, prompts))
for i, result in enumerate(results):
print(f"Prompt {i+1}: {result.choices[0].message.content}")
| 模型 | 输入价格(per M tokens) | 输出价格(per M tokens) |
|---|---|---|
| deepseek-chat (V3) | ¥2 | ¥8 |
| deepseek-reasoner (R1) | ¥4 | ¥16 |
| deepseek-v4-flash | $0.14 $0.28 | |
| deepseek-v4-pro(折扣期) | $0.435 $0.87 | |
| deepseek-v4-pro(原价) | $1.74 $3.48 |
假设一个典型任务:2000 输入 token + 500 输出 token:
| 模型 | 单次成本 | 100 万次调用成本 |
|---|---|---|
| DeepSeek-V3 | ~¥0.008 | ~¥8,000 |
| DeepSeek-R1 | ~¥0.016 | ~¥16,000 |
| DeepSeek-V4-Flash | ~$0.00021 ~$210 | |
| DeepSeek-V4-Pro(折扣期) | ~$0.00065 ~$650 | |
| GPT-4o | ~$0.0075 ~$7,500 | |
| GPT-4 Turbo | ~$0.03 ~$30,000 | |
| Claude 3.5 Sonnet | ~$0.006 ~$6,000 |
结论:DeepSeek 的价格优势极其明显,V4-Flash 的成本仅为 GPT-4o 的约 1/35。
DeepSeek 官方推荐推理任务使用 较低 temperature(0.0–0.3),这与 OpenAI 的通用建议不同。OpenAI 对于 GPT-4 通常推荐 0.5–1.0 的区间。这可能与模型架构差异有关——DeepSeek 的 MoE 架构本身具有更多采样多样性,因此需要更低的 temperature 来保持一致性。
R1 模型会输出 \n<think>...\n</think> 标签包裹的推理过程,这是 DeepSeek 独有的特性:
reasoning_content 字段DeepSeek 对 system message 的处理与 OpenAI 基本一致,但 DeepSeek 的 system prompt 遵从率在某些场景下略低于 GPT-4o。建议在 system message 中保持简洁明确的指令。
DeepSeek API 不内置内容审核(moderation)机制,而 OpenAI API 默认会拦截某些内容。这意味着:
❌ 不好的 System Prompt:
"你需要帮助用户解决问题。"
✅ 好的 System Prompt:
"你是一个专业的 Python 后端开发工程师。
用户会请你编写代码或回答技术问题。
规则:
1. 提供完整可运行的代码
2. 添加必要的注释和类型注解
3. 解释核心设计思路
4. 不要使用不推荐的库"
from openai import OpenAI, APIError, APIConnectionError
client = OpenAI(
api_key="your-deepseek-api-key",
base_url="https://api.deepseek.com"
)
def safe_chat_completion(messages, model="deepseek-chat"):
try:
return client.chat.completions.create(
model=model,
messages=messages,
timeout=30 # 30 秒超时
)
except APIConnectionError:
print("网络连接失败,请检查网络")
except APIError as e:
print(f"API 错误: {e.status_code} - {e.message}")
if e.status_code == 401:
print("API Key 无效,请检查")
elif e.status_code == 429:
print("超出速率限制,请等待")
except Exception as e:
print(f"未预期的错误: {e}")
return None
def estimate_cost(messages, model="deepseek-chat"):
"""估算 API 调用成本"""
# 粗略估算:1 个汉字 ≈ 1.5 tokens
total_text = sum(len(m.get("content", "")) for m in messages)
estimated_input_tokens = int(total_text * 1.5)
prices = {
"deepseek-chat": (2, 8), # (input ¥/M, output ¥/M)
"deepseek-reasoner": (4, 16),
"deepseek-v4-flash": (0.14, 0.28), # $/M
"deepseek-v4-pro": (0.435, 0.87), # $/M (折扣期)
}
input_price, output_price = prices.get(model, (2, 8))
input_cost = estimated_input_tokens * input_price / 1_000_000
expected_output = 500 # 假设平均输出 500 tokens
output_cost = expected_output * output_price / 1_000_000
return {
"estimated_input_tokens": estimated_input_tokens,
"estimated_cost": round(input_cost + output_cost, 6)
}
目前不支持。DeepSeek-V3、R1 和 V4 都是纯文本模型。如果需求涉及图片、音频或视频处理,需要搭配其他模型使用。
deepseek-reasoner 或 deepseek-v4-pro 模型temperature = 0.0–0.3max_tokens(建议 4096 以上)以完成完整推理DeepSeek-V3、R1 和 V4 当前不提供 API 微调服务。开源模型权重可以在本地微调,但需要相应的硬件支持。
访问 https://platform.deepseek.com → 注册账号 → API Keys → 创建新 Key。
DeepSeek API 没有免费额度。注册后会赠送少量体验金额(通常 ¥3–¥5),后续需要充值。
从 V3/R1 迁移到 V4 时,请逐项确认:
model="deepseek-chat" 改为 model="deepseek-v4-flash"(非思考模式)model="deepseek-reasoner" 改为 model="deepseek-v4-pro" 或 deepseek-v4-flash(思考模式)thinking 参数控制思考模式,不再区分 chat/reasoner 模型reasoning_content 字段获取思维链(替代 R1 的 \n<think>...\n</think>)reasoning_content 的传递规则(工具调用场景必须传回)base_url 无需变更(仍为 https://api.deepseek.com)deepseek-chat / deepseek-reasoner 将于 2026-07-24 废弃,请在此之前完成迁移本文档最后更新:2026-05-27