Định dạng trò chuyện lý luận Deepseek (Nội dung lý luận)
Tài liệu chính thức
📝 Giới thiệu
Deepseek-reasoner là một mô hình lý luận được DeepSeek đưa ra. Trước khi đưa ra đáp án cuối cùng, đầu tiên mô hình sẽ xuất ra chuỗi suy nghĩ (nội dung suy luận) để nâng cao độ chính xác của đáp án cuối cùng. API hiển thị nội dung chuỗi suy nghĩ của người tìm kiếm sâu để người dùng xem, hiển thị hoặc chắt lọc.
💡 Yêu cầu Ví dụ
Trò chuyện bằng văn bản cơ bản ✅
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
}'Response Example:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "deepseek-reasoner",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning_content": "Let me think step by step:\n1. We need to compare 9.11 and 9.8\n2. Both numbers are decimals, we can compare directly\n3. 9.8 = 9.80\n4. 9.11 `< 9.80\n5. So 9.8 is greater",
"content": "9.8 is greater than 9.11."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}Phản hồi trực tuyến ✅
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
}'Streaming Response Example:
{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"Let me"},"finish_reason":null}]}
{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"reasoning_content":"think step by step"},"finish_reason":null}]}
{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{"reasoning_content":":"},"finish_reason":null}]}
// ... more reasoning content ...
{"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}]}
// ... more final answer content ...
{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"deepseek-reasoner","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}📮 Yêu cầu
Điểm cuối
POST /v1/chat/completionsPhương thức xác thực
Bao gồm nội dung sau trong tiêu đề yêu cầu để xác thực khóa API:
Authorization: Bearer $API_KEYTrong đó $DEEPSEEK_API_KEY là khóa API của bạn.
Thông số nội dung yêu cầu
messages
- Kiểu: Mảng
- Bắt buộc: Có
Danh sách các tin nhắn trong cuộc trò chuyện cho đến nay. Lưu ý: Nếu bạn chuyển Reason_content trong chuỗi thông báo đầu vào, API sẽ trả về lỗi 400.
model
- Loại: Chuỗi
- Bắt buộc: Có
- Giá trị: deepseek-reasoner
ID mẫu sẽ sử dụng. Hiện tại chỉ hỗ trợ deepseek-reasoner.
max_tokens
- Kiểu: Số nguyên
- Bắt buộc: Không
- Mặc định: 4096
- Tối đa: 8192
Độ dài tối đa của câu trả lời cuối cùng (không bao gồm nội dung lý luận). Lưu ý: Đầu ra của nội dung lý luận có thể lên tới 32K token.
stream
- Kiểu: Boolean
- Bắt buộc: Không
- Mặc định: sai
Có nên sử dụng phản hồi phát trực tuyến hay không.
Thông số không được hỗ trợ
Các tham số sau hiện không được hỗ trợ:
- nhiệt độ
- top_p
- hiện diện_penalty
- tần suất_hình phạt
- logprobs
- top_logprobs
Lưu ý: Để tương thích với phần mềm hiện có, cài đặt nhiệt độ, top_p, hiện diện_penalty, tần số_penalty sẽ không gây ra lỗi nhưng sẽ không có hiệu lực. Đặt logprobs hoặc top_logprobs sẽ gây ra lỗi.
Tính năng được hỗ trợ
- Hoàn tất trò chuyện
- Tiếp tục tiền tố trò chuyện (Beta)
Tính năng không được hỗ trợ
- Gọi hàm
- Đầu ra JSON
- Hoàn thành FIM (Beta)
📥 Phản hồi
###Phản hồi thành công
Trả về một đối tượng hoàn thành trò chuyện. Nếu yêu cầu được truyền trực tuyến, sẽ trả về chuỗi phát trực tuyến các đối tượng đoạn hoàn thành trò chuyện.
id
- Loại: Chuỗi
- Mô tả: Mã định danh duy nhất cho phản hồi
object
- Loại: Chuỗi
- Mô tả: Loại đối tượng, giá trị là "chat.completion"
created
- Kiểu: Số nguyên
- Mô tả: Dấu thời gian tạo phản hồi
model
- Loại: Chuỗi
- Mô tả: Tên model được sử dụng, giá trị là "deepseek-reasoner"
choices
- Kiểu: Mảng
- Mô tả: Chứa các tùy chọn trả lời được tạo
- Của cải:
index: Chỉ số tùy chọnmessage: Đối tượng thông điệp chứa vai trò, nội dung suy luận và câu trả lời cuối cùngrole: Vai trò, giá trị là “trợ lý”reasoning_content: Chuỗi nội dung suy nghĩcontent: Nội dung đáp án cuối cùngfinish_reason: Lý do kết thúc
usage
- Loại: Đối tượng
- Mô tả: Thống kê sử dụng Token
- Của cải:
prompt_tokens: Số lượng token dùng cho lời nhắccompletion_tokens: Số lượng token được sử dụng để hoàn thànhtotal_tokens: Tổng số token
📝 Giải thích về nối ngữ cảnh
Trong mỗi vòng trò chuyện, mô hình đưa ra Reasoning_content (chuỗi suy nghĩ) và câu trả lời cuối cùng (nội dung). Trong vòng trò chuyện tiếp theo, đầu ra Reason_content từ vòng trước sẽ KHÔNG được nối vào ngữ cảnh, như hiển thị bên dưới:

Lưu ý
Nếu bạn chuyển Reason_content vào chuỗi thông báo đầu vào, API sẽ trả về lỗi 400. Vì vậy, vui lòng xóa trường Reason_content khỏi phản hồi API trước khi thực hiện yêu cầu API tiếp theo, như được hiển thị trong phần sử dụng ví dụ dưới đây.
Ví dụ sử dụng:
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>`", base_url="https://api.deepseek.com")
# First round of conversation
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
# Second round of conversation - only concatenate the final answer 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
)Ví dụ về phản hồi truyền phát:
# First round of conversation
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
# Second round of conversation - only concatenate the final answer 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
)