88API88API
使用指南AI 應用API 文件幫助支援
聊天(Chat)

Deepseek reasoning 對話格式(Reasoning Content)

📝 簡介

Deepseek-reasoner 是 DeepSeek 推出的推理模型。在輸出最終回答之前,模型會先輸出一段思維鏈內容,以提升最終答案的準確性。API 向使用者開放 deepseek-reasoner 思維鏈的內容,以供使用者檢視、展示、蒸餾使用。

💡 請求示例

基礎文字對話 ✅

curl https://api.deepseek.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "deepseek-reasoner",
    "messages": [
      {
        "role": "user",
        "content": "9.11 and 9.8, which is greater?"
      }
    ],
    "max_tokens": 4096
  }'

響應示例:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "deepseek-reasoner",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "reasoning_content": "讓我一步步思考:\n1. 我們需要比較9.11和9.8的大小\n2. 兩個數都是小數,我們可以直接比較\n3. 9.8 = 9.80\n4. 9.11 `< 9.80\n5. 所以9.8更大",
        "content": "9.8 is greater than 9.11."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 15,
    "total_tokens": 25
  }
}

流式響應 ✅

curl https://api.deepseek.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "deepseek-reasoner",
    "messages": [
      {
        "role": "user",
        "content": "9.11 and 9.8, which is greater?"
      }
    ],
    "stream": true
  }'

流式響應示例:

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"讓我"},"finish_reason":null}]}

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"reasoning_content":"一步步"},"finish_reason":null}]}

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"reasoning_content":"思考:"},"finish_reason":null}]}

// ... 更多思維鏈內容 ...

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"content":"9.8"},"finish_reason":null}]}

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"content":" is greater"},"finish_reason":null}]}

// ... 更多最終答案內容 ...

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

📮 請求

端點

POST /v1/chat/completions

鑑權方法

在請求頭中包含以下內容進行 API 金鑰認證:

Authorization: Bearer $API_KEY

其中 $DEEPSEEK_API_KEY 是您的 API 金鑰。

請求體引數

messages

  • 型別:陣列
  • 必需:是

到目前為止包含對話的訊息列表。請注意,如果您在輸入的 messages 序列中傳入了 reasoning_content,API 會返回 400 錯誤。

model

  • 型別:字串
  • 必需:是
  • 值:deepseek-reasoner

要使用的模型 ID。目前僅支援 deepseek-reasoner。

max_tokens

  • 型別:整數
  • 必需:否
  • 預設值:4096
  • 最大值:8192

最終回答的最大長度(不含思維鏈輸出)。請注意,思維鏈的輸出最多可以達到 32K tokens。

stream

  • 型別:布林值
  • 必需:否
  • 預設值:false

是否使用流式響應。

不支援的引數

以下引數當前不支援:

  • temperature
  • top_p
  • presence_penalty
  • frequency_penalty
  • logprobs
  • top_logprobs

注意:為了相容已有軟體,設定 temperature、top_p、presence_penalty、frequency_penalty 引數不會報錯,但也不會生效。設定 logprobs、top_logprobs 會報錯。

支援的功能

  • 對話補全
  • 對話字首續寫 (Beta)

不支援的功能

  • Function Call
  • Json Output
  • FIM 補全 (Beta)

📥 響應

成功響應

返回一個聊天補全物件,如果請求被流式傳輸,則返回聊天補全塊物件的流式序列。

id

  • 型別:字串
  • 說明:響應的唯一識別符號

object

  • 型別:字串
  • 說明:物件型別,值為 "chat.completion"

created

  • 型別:整數
  • 說明:響應建立時間戳

model

  • 型別:字串
  • 說明:使用的模型名稱,值為 "deepseek-reasoner"

choices

  • 型別:陣列
  • 說明:包含生成的回覆選項
  • 屬性:
    • index: 選項索引
    • message: 包含角色、思維鏈內容和最終回答的訊息物件
      • role: 角色,值為 "assistant"
      • reasoning_content: 思維鏈內容
      • content: 最終回答內容
    • finish_reason: 完成原因

usage

  • 型別:物件
  • 說明:token 使用統計
  • 屬性:
    • prompt_tokens: 提示使用的 token 數
    • completion_tokens: 補全使用的 token 數
    • total_tokens: 總 token 數

📝 上下文拼接說明

在每一輪對話過程中,模型會輸出思維鏈內容(reasoning_content)和最終回答(content)。在下一輪對話中,之前輪輸出的思維鏈內容不會被拼接到上下文中,如下圖所示:

Deepseek reasoning 上下文拼接示意圖

注意

如果您在輸入的 messages 序列中,傳入了reasoning_content,API 會返回 400 錯誤。因此,請刪除 API 響應中的 reasoning_content 欄位,再發起 API 請求,方法如下方使用示例所示。

使用示例:

from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>`", base_url="https://api.deepseek.com")

# 第一輪對話
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=messages
)

reasoning_content = response.choices[0].message.reasoning_content
content = response.choices[0].message.content

# 第二輪對話 - 只拼接最終回答content
messages.append({'role': 'assistant', 'content': content})
messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=messages
)

流式響應示例:

# 第一輪對話
messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}]
response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=messages,
    stream=True
)

reasoning_content = ""
content = ""

for chunk in response:
    if chunk.choices[0].delta.reasoning_content:
        reasoning_content += chunk.choices[0].delta.reasoning_content
    else:
        content += chunk.choices[0].delta.content

# 第二輪對話 - 只拼接最終回答content
messages.append({"role": "assistant", "content": content})
messages.append({'role': 'user', 'content': "How many Rs are there in the word 'strawberry'?"})
response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=messages,
    stream=True
)