DeepSeek API 完全兼容 OpenAI API 格式,现有 OpenAI SDK 和代码可零成本迁移。同时提供 Anthropic API 兼容格式。DeepSeek 以极低价格提供顶级模型能力,性价比业界领先。
Authorization: Bearer your-deepseek-api-key
API Key 在 platform.deepseek.com/api_keys 创建。
OpenAI兼容格式:https://api.deepseek.com
Anthropic兼容格式:https://api.deepseek.com/anthropic
聊天补全端点:https://api.deepseek.com/chat/completions
| 模型ID | 上下文 | 最大输出 | 说明 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| deepseek-v4-pro | 128K | 16K | 最新旗舰,支持思考模式 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deepseek-v4-flash | 128K | 16K | 快速版,高性价比 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deepseek-chat | 128K | 8K | ⚠️将废弃(2026/07/24),等同v4-flash非思考 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deepseek-reasoner | 128K | 8K | ⚠️将废弃(2026/07/24),等同v4-flash思考 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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-v4-pro",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
stream=False
)
print(response.choices[0].message.content)
DeepSeek 独有的深度推理能力,模型在回答前进行长链推理:
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{"role": "user", "content": "求解 x^2 + 5x + 6 = 0"}
],
thinking={"type": "enabled"},
reasoning_effort="high", # low / medium / high
stream=False
)
{
"choices": [{
"message": {
"role": "assistant",
"content": "方程 x^2 + 5x + 6 = 0 的解为 x = -2 或 x = -3",
"reasoning_content": "这是一个一元二次方程...n使用因式分解...n(x+2)(x+3)=0..."
}
}]
}
| 值 | 效果 | 适用场景 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| low | 快速推理,思考短 | 简单问题 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| medium | 平衡推理 | 一般问题 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| high | 深度推理,思考长 | 复杂数学/编程/逻辑 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stream = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "写一首诗"}],
stream=True
)
for chunk in stream:
# 普通内容
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
# 思考内容(思考模式下)
if hasattr(chunk.choices[0].delta, 'reasoning_content') and chunk.choices[0].delta.reasoning_content:
# 可选择不输出思考过程
pass
curl https://api.deepseek.com/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer $DEEPSEEK_API_KEY"
-d '{
"model": "deepseek-v4-pro",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"thinking": {"type": "enabled"},
"reasoning_effort": "high",
"stream": false
}'
与 OpenAI 格式完全一致:
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名"}
},
"required": ["city"]
}
}
}]
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "北京天气如何?"}],
tools=tools
)
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "列出中国前5大城市"}],
response_format={"type": "json_object"}
)
| 模型 | 输入价格 | 输出(含思考) | 缓存命中 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| deepseek-v4-pro | ¥4/1M tokens | ¥16/1M tokens | ¥1/1M tokens | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deepseek-v4-flash | ¥1/1M tokens | ¥4/1M tokens | ¥0.1/1M tokens | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
无需编码,直接在主流AI工具中使用:
| 工具 | 配置方式 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Claude Code | 自定义API端点 → https://api.deepseek.com | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GitHub Copilot | 选择 DeepSeek 模型 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| One API | 添加 DeepSeek 渠道 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OpenCode | 直接支持 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
默认限制(付费用户):
余额不足时自动降级为免费层级限速。
| 状态码 | 含义 | 处理 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 400 | 请求格式错误 | 检查参数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | API Key 无效 | 检查密钥 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 余额不足 | 充值 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 速率限制 | 退避重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 服务器错误 | 稍后重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 服务过载 | 稍后重试 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
评论区