⚠️ 待更新·2026-08-29核验 · 更新时间待核验 · 本文信息可能已过期,请以官方文档为准
更新时间:2026-08-29 · 核验状态:待更新 · 官方溯源待补
简介
DeepSeek 是国内顶尖的开源大模型团队,其 deepseek-chat(V3)与 deepseek-reasoner(R1)在推理与代码任务上表现优异,且 token 价格远低于 GPT-4o。本篇演示接入步骤,涵盖普通对话、推理模型、流式与函数调用四种场景。
架构图
flowchart TD
A[Register at platform.deepseek.com] --> B[Verify phone]
B --> C[Top up ¥10 or use bonus]
C --> D[Create API Key]
D --> E[Use OpenAI SDK]
E --> F{Choose model}
F -->|deepseek-chat| G[V3: general chat]
F -->|deepseek-reasoner| H[R1: math/code reasoning]
申请 Key
- 访问 https://platform.deepseek.com,注册账号并完成手机验证。
- 进入 API Keys → Create API Key,复制
sk-...。
- 在 Billing 充值最低 ¥10(也可用赠送额度体验)。
安装与配置
DeepSeek 完全兼容 OpenAI 协议,直接复用 openai SDK:
pip install openai
export DEEPSEEK_API_KEY="sk-..."
普通对话(deepseek-chat)
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com/v1",
)
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "你是简洁的中文助手"},
{"role": "user", "content": "用一句话解释什么是反向索引"},
],
temperature=0.3,
max_tokens=256,
)
print(resp.choices[0].message.content)
推理模型(deepseek-reasoner)
R1 会输出思维链,适合数学、逻辑、复杂代码:
resp = client.chat.completions.create(
model="deepseek-reasoner",
messages=[
{"role": "user", "content": "一个水池有两个进水管 A、B 和一个出水管 C。A 3 小时注满,B 6 小时注满,C 4 小时排空。三管同开多久注满?"},
],
)
print("思维链:")
print(resp.choices[0].message.reasoning_content)
print("\n最终答案:")
print(resp.choices[0].message.content)
注意 reasoning_content 是 R1 特有字段,V3 不会返回。
流式输出
stream = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "写一首关于秋天的五言绝句"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
函数调用(Function Calling)
tools = [{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "查询股票实时价格",
"parameters": {
"type": "object",
"properties": {"symbol": {"type": "string"}},
"required": ["symbol"],
},
},
}]
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "查一下 AAPL 的价格"}],
tools=tools,
)
print(resp.choices[0].message.tool_calls[0].function)
价格(参考)
deepseek-chat:输入 ¥0.5 / 1M(cache 命中 ¥0.1),输出 ¥8 / 1M
deepseek-reasoner:输入 ¥4 / 1M,输出 ¥16 / 1M(非缓存)
相比 GPT-4o 便宜一个数量级。Prefix 缓存对重复 system prompt 的场景能再省 80%。
上下文缓存(Context Caching)
DeepSeek 对重复 system prompt 的场景提供上下文缓存,命中后输入价降到 ¥0.1/1M(原价 ¥0.5)。这对 Agent 多轮对话、RAG 检索等场景非常友好。缓存自动管理,无需手动调用,但要求 prompt 前缀完全一致。建议把固定的 system instruction 与工具定义放在 messages 数组最前面,最大化缓存命中率。
常见问题
402 Insufficient Balance:余额不足,去 Billing 充值。
- R1 输出过长:用
max_tokens 限制,或截断 reasoning_content。
- 想本地部署:下载开源权重 + vLLM/SGLang 自建服务,2× A100 即可跑 R1 蒸馏版。
- 响应慢:海外用户可能延迟较高,可走代理或换 OpenRouter 上的 DeepSeek 镜像。
DeepSeek 是中文场景性价比最高的选择之一。
最佳实践
- deepseek-reasoner 用于推理任务:数学题、代码审查、逻辑题用 R1;日常对话用 V3 更省 token。
- 价格表按 token 计算:输入 0.27 元/M,输出 1.1 元/M(人民币),比 GPT-4o 便宜 100 倍。
- 流式输出必须开:reasoner 思考时间长,不开流式用户会以为卡住了。
- API Key 不要在前端硬编码:DeepSeek 按 Key 计费,前端硬编码等于送钱。
⚠️ Pending Update · 2026-08-29 Verification · Content may be outdated, please refer to official docs
Updated: 2026-08-29 · Status: Pending Verification
Introduction
DeepSeek is a top-tier open-source LLM team. Its deepseek-chat (V3) and deepseek-reasoner (R1) excel at reasoning and coding tasks, with token prices far below GPT-4o. This article shows how to integrate them, covering standard chat, the reasoning model, streaming, and function calling.
架构图
flowchart TD
A[Register at platform.deepseek.com] --> B[Verify phone]
B --> C[Top up ¥10 or use bonus]
C --> D[Create API Key]
D --> E[Use OpenAI SDK]
E --> F{Choose model}
F -->|deepseek-chat| G[V3: general chat]
F -->|deepseek-reasoner| H[R1: math/code reasoning]
Get an API Key
- Visit https://platform.deepseek.com, register, and verify your phone number.
- Go to API Keys → Create API Key and copy the
sk-... value.
- Top up at least ¥10 in Billing (or use the welcome credit).
DeepSeek is fully OpenAI-compatible, so reuse the openai SDK:
pip install openai
export DEEPSEEK_API_KEY="sk-..."
Standard Chat (deepseek-chat)
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com/v1",
)
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain an inverted index in one sentence."},
],
temperature=0.3,
max_tokens=256,
)
print(resp.choices[0].message.content)
Reasoning Model (deepseek-reasoner)
R1 returns a chain-of-thought — great for math, logic, and complex code:
resp = client.chat.completions.create(
model="deepseek-reasoner",
messages=[
{"role": "user", "content": "A pool has two inlet pipes A (3h to fill) and B (6h to fill) and an outlet C (4h to drain). How long to fill with all three open?"},
],
)
print("Reasoning:")
print(resp.choices[0].message.reasoning_content)
print("\nFinal answer:")
print(resp.choices[0].message.content)
Note: reasoning_content is R1-specific; V3 does not return it.
Streaming
stream = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "Write a haiku about autumn."}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
Function Calling
tools = [{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "Get the real-time price of a stock",
"parameters": {
"type": "object",
"properties": {"symbol": {"type": "string"}},
"required": ["symbol"],
},
},
}]
resp = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "What's the price of AAPL?"}],
tools=tools,
)
print(resp.choices[0].message.tool_calls[0].function)
Pricing (reference)
deepseek-chat: ¥0.5 / 1M input (¥0.1 cache hit), ¥8 / 1M output
deepseek-reasoner: ¥4 / 1M input, ¥16 / 1M output (non-cache)
About an order of magnitude cheaper than GPT-4o. Prefix caching saves another 80% for repeated system prompts.
Context Caching
DeepSeek supports context caching for repeated system prompts — cache hits drop input price to ¥0.1/1M (from ¥0.5). This is great for multi-turn agents and RAG lookups. Caching is automatic, but the prompt prefix must be byte-identical. Put fixed system instructions and tool definitions at the start of the messages array to maximize hit rate.
Troubleshooting
402 Insufficient Balance: Out of credit. Top up in Billing.
- R1 output too long: Cap with
max_tokens or truncate reasoning_content.
- Self-host: Download the open weights and serve with vLLM or SGLang. R1 distill fits on 2x A100.
- Slow responses from abroad: Latency may be high. Use a proxy or the DeepSeek mirror on OpenRouter.
DeepSeek is one of the most cost-effective choices for Chinese-language workloads.
Best Practices
- Use deepseek-reasoner for reasoning tasks: R1 for math/code review/logic; V3 for daily chat saves tokens.
- Pricing is per-token: input 0.27 yuan/M, output 1.1 yuan/M (CNY) — 100x cheaper than GPT-4o.
- Always enable streaming: reasoner takes long to think; without streaming users think it's stuck.
- Never hardcode API Key in frontend: DeepSeek bills per key; frontend hardcode is giving money away.