Deepseek 推理模型 (deepseek-reasoner)

2025-02-05 11:04 更新

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

在使用 deepseek-reasoner 時,請先升級 OpenAI SDK 以支持新參數(shù)。

pip3 install -U openai

API 參數(shù)?

  • 輸入參數(shù):max_tokens:最終回答的最大長度(不含思維鏈輸出),默認為 4K,最大為 8K。請注意,思維鏈的輸出最多可以達到 32K tokens,控思維鏈的長度的參數(shù)(reasoning_effort)將會在近期上線。
  • 輸出字段:reasoning_content:思維鏈內容,與 content 同級,訪問方法見訪問樣例content:最終回答內容
  • 上下文長度:API 最大支持 64K 上下文,輸出的 reasoning_content 長度不計入 64K 上下文長度中
  • 支持的功能:對話補全,對話前綴續(xù)寫 (Beta)
  • 不支持的功能:Function Call、Json Output、FIM 補全 (Beta)
  • 不支持的參數(shù):temperature、top_p、presence_penalty、frequency_penalty、logprobs、top_logprobs。請注意,為了兼容已有軟件,設置 temperature、top_p、presence_penalty、frequency_penalty 參數(shù)不會報錯,但也不會生效。設置 logprobs、top_logprobs 會報錯。

上下文拼接?

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

請注意,如果您在輸入的 messages 序列中,傳入了reasoning_content,API 會返回 400 錯誤。因此,請刪除 API 響應中的 reasoning_content 字段,再發(fā)起 API 請求,方法如訪問樣例所示。

訪問樣例?

下面的代碼以 Python 語言為例,展示了如何訪問思維鏈和最終回答,以及如何在多輪對話中進行上下文拼接。

非流式示例

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

# Round 1
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

# Round 2
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
)
# ...

流式示例

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

# Round 1
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

# Round 2
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
)
# ...


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號