コンテンツにスキップ

DeepSeek reasoning の対話フォーマット(Chat Completions風)

Deepseek reasoning 対話形式(Chat Completions 風)

Section titled “Deepseek reasoning 対話形式(Chat Completions 風)”

本ページの概要

公式ドキュメント

推理モデル (deepseek-reasoner)

Deepseek-reasoner は DeepSeek が提供する推理モデルです。最終回答を出力する前に、モデルはまず思考の連鎖(Chain of Thought)を出力し、最終的な回答精度を高めます。API は deepseek-reasoner の思考の連鎖の内容をユーザーに公開しており、確認、表示、蒸留に利用できます。

curl https://4All API地址/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $4All API_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://4All API地址/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $4All API_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 $4All API_API_KEY

ここで $DEEPSEEK_API_KEY は、あなたの API キーです。

リクエストボディのパラメータ

Section titled “リクエストボディのパラメータ”
  • 種類: 配列
  • 必須: はい

これまでの会話を含むメッセージ一覧です。なお、入力した messages シーケンスに reasoning_content を含めると、API は 400 エラーを返します。

  • 種類: 文字列
  • 必須: はい
  • 値: deepseek-reasoner

使用するモデル ID です。現時点では deepseek-reasoner のみサポートされています。

  • 種類: 整数
  • 必須: いいえ
  • デフォルト値: 4096
  • 最大値: 8192

最終回答の最大長です(思考の連鎖の出力は含みません)。なお、思考の連鎖は最大 32K tokens まで出力可能です。

  • 種類: ブール値
  • 必須: いいえ
  • デフォルト値: false

ストリーミングレスポンスを使用するかどうかを指定します。

サポートされていないパラメータ

Section titled “サポートされていないパラメータ”

以下のパラメータは現在サポートされていません:

  • 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)

チャット補完オブジェクトを返します。リクエストがストリーミングの場合は、チャット補完チャンクオブジェクトのストリームを返します。

  • 種類: 文字列
  • 説明: レスポンスの一意な識別子
  • 種類: 文字列
  • 説明: オブジェクトの種類。値は “chat.completion”
  • 種類: 整数
  • 説明: レスポンス生成時のタイムスタンプ
  • 種類: 文字列
  • 説明: 使用したモデル名。値は “deepseek-reasoner”
  • 種類: 配列
  • 説明: 生成された返信候補を含みます
  • 属性:
  • index : 候補のインデックス
  • message : 役割、思考の連鎖の内容、最終回答を含むメッセージオブジェクト role : 役割。値は “assistant” reasoning_content : 思考の連鎖の内容 content : 最終回答の内容
  • finish_reason : 終了理由
  • 種類: オブジェクト
  • 説明: token 使用統計
  • 属性:
  • prompt_tokens : プロンプトで使用した token 数
  • completion_tokens : 補完で使用した token 数
  • total_tokens : 合計 token 数

📝 コンテキストの連結について

Section titled “📝 コンテキストの連結について”

各ターンの対話では、モデルは思考の連鎖(reasoning_content)と最終回答(content)を出力します。次のターンでは、前回の思考の連鎖はコンテキストに連結されません。以下の図のとおりです:

注意

入力した messages シーケンスに reasoning_content を含めると、API は 400 エラーを返します。そのため、API レスポンス内の reasoning_content フィールドは削除してから再度 API リクエストを送信してください。方法は、以下の使用例をご覧ください。

使用例:

from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://4All API地址")
# 第一輪対話
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
)