DeepSeek R1 API 教程 2026 — 推理模型使用指南 | APIMaster.ai
DeepSeek R1 API 使用教程:推理模式、数学解题、代码验证,Python 代码示例。说明 DeepSeek V4 思考模式与 R1 兼容入口的迁移。
DeepSeek R1 API 教程
DeepSeek R1 是用户常用来搜索 DeepSeek 推理能力的关键词。当前 API 侧建议优先使用 DeepSeek V4 Pro 的思考模式;旧 deepseek-reasoner 兼容入口将在 2026-07-24 后弃用。
DeepSeek R1 的特点
| 特点 | 说明 |
|---|---|
| 推理输出 | 思考模式通过 reasoning_content 返回推理内容 |
| 数学能力强 | 在 MATH 测试集上接近 o3 水平 |
| 开源权重 | 模型权重在 HuggingFace 公开 |
| 价格低 | 具体价格随模型版本和缓存命中变化,以实时价格为准 |
快速开始
from openai import OpenAI
client = OpenAI(
api_key="你的 APIMaster Key",
base_url="https://apimaster.ai/v1",
)
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{
"role": "user",
"content": "证明:对任意正整数 n,n³ - n 能被 6 整除。"
}
],
max_tokens=3000, # R1 需要更多 Token 写推理过程
)
message = response.choices[0].message
print(getattr(message, "reasoning_content", ""))
print(message.content)
理解推理输出格式
DeepSeek V4 思考模式通常把推理内容放在 reasoning_content,最终答案放在 content。生产代码里优先读取字段,不要依赖 <think> 标签正则解析。
message = response.choices[0].message
reasoning = getattr(message, "reasoning_content", "")
final_answer = message.content
print(final_answer)
适合 DeepSeek R1 的任务
数学解题
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{
"role": "user",
"content": """解方程组,给出详细步骤:
2x + 3y = 12
5x - y = 7"""
}
],
max_tokens=2000,
)
代码逻辑验证
code = """
def binary_search(arr, target):
left, right = 0, len(arr)
while left < right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid
return -1
"""
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{
"role": "user",
"content": f"验证这段二分查找代码的正确性,找出所有边界情况:\n\n```python\n{code}\n```"
}
],
)
逻辑推理题
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{
"role": "user",
"content": "有 5 个盒子,编号 1-5。奇数盒里放苹果,偶数盒里放橙子。第 3 盒被移走后,剩余盒子重新按 1-4 编号,规则不变。原来 4 号盒现在放什么水果?"
}
],
)
DeepSeek R1 vs 其他推理模型
| 模型 | MATH 得分 | 价格(输入/M) | 适合场景 |
|---|---|---|---|
| o3 | ~97% | $10.00 | 最高质量推理 |
| DeepSeek V4 Pro(思考模式) | 高 | $0.435 原价 | 高性价比推理 |
| o4-mini | ~95% | $1.10 | 快速推理 |
| Claude Opus 4.8 | ~90% | $5.00 | 综合推理 |
DeepSeek V4 Pro 思考模式适合数学/逻辑任务,但具体成本需要按实时价格和输出长度估算。
流式输出(查看实时推理过程)
with client.chat.completions.stream(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "证明哥德尔不完备性定理的核心思想。"}],
max_tokens=4000,
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
价格与注意事项
- 输入/输出价格以模型广场实时价格为准
- 推理模型的输出 Token 往往多于普通模型,实际成本要用输出量来计算
- 建议设
max_tokens=2048–4096,避免简单题也生成超长推理