← 返回文章列表 / Back to list
usage

DeepSeek API 接入步骤

DeepSeek API Integration Steps

⚠️ 待更新·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

  1. 访问 https://platform.deepseek.com,注册账号并完成手机验证。
  2. 进入 API KeysCreate API Key,复制 sk-...
  3. 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 计费,前端硬编码等于送钱。

相关文章 / Related

Gemini API 免费层调用在本地 IDE 中集成免费 APIOpenRouter API Key 申请与费率OpenCode 接入免费模型实战免费 OCR 与文档解析 API 实战