DeepSeek V4
DeepSeek V4 通过 OpenAI 兼容的 Chat Completions 接口调用,支持长上下文、思考模式、流式输出和 Function Calling。输入与输出均为文本。
请求地址
http
POST /v1/chat/completions支持模型
| 模型 | 说明 |
|---|---|
deepseek-v4-pro | DeepSeek V4 Pro 通用版本 |
deepseek-v4-pro-0813 | 2026-08-13 稳定快照,适合需要固定模型版本的应用 |
模型支持最长约 100 万 Token 上下文。实际可用模型和上下文额度以账户权限及平台配置为准。
基础请求
bash
curl https://cubicspaces.cloud/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro-0813",
"messages": [
{ "role": "system", "content": "你是一个严谨、简洁的技术助手。" },
{ "role": "user", "content": "用一句话解释什么是幂等性。" }
],
"max_completion_tokens": 2048,
"stream": false
}'思考模式
DeepSeek V4 默认启用思考模式。可使用以下字段控制:
| 参数 | 类型 | 说明 |
|---|---|---|
enable_thinking | boolean | true 开启思考,false 关闭思考 |
reasoning_effort | string | 推荐使用 high 或 max;max 通常会产生更深入、也更长的推理 |
json
{
"model": "deepseek-v4-pro-0813",
"messages": [
{ "role": "user", "content": "分析这个并发控制方案可能产生的竞态条件。" }
],
"enable_thinking": true,
"reasoning_effort": "high",
"max_completion_tokens": 8192
}非流式响应中,思考内容位于 choices[0].message.reasoning_content,最终答案位于 choices[0].message.content。
json
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning_content": "先检查共享状态和锁的持有范围……",
"content": "该方案主要存在检查与写入之间的竞态窗口。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 42,
"completion_tokens": 318,
"total_tokens": 360
}
}业务侧只需要展示最终答案时,可以忽略 reasoning_content。
流式输出
bash
curl https://cubicspaces.cloud/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"messages": [
{ "role": "user", "content": "分三点说明数据库索引失效的常见原因。" }
],
"enable_thinking": true,
"reasoning_effort": "high",
"stream": true,
"stream_options": {
"include_usage": true
}
}'流式响应使用 SSE。客户端需要分别处理:
choices[0].delta.reasoning_content:思考内容片段。choices[0].delta.content:最终答案片段。usage:启用stream_options.include_usage后,通常在流末尾返回。data: [DONE]:流结束标志。
工具调用
DeepSeek V4 支持 OpenAI 格式的 Function Calling。
json
{
"model": "deepseek-v4-pro-0813",
"messages": [
{ "role": "user", "content": "查询东京当前天气。" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}当 finish_reason 为 tool_calls 时:
- 读取
choices[0].message.tool_calls。 - 执行对应工具。
- 将工具结果作为
role: "tool"消息追加到历史记录。 - 使用相同模型继续请求,获取最终答案。
每条工具结果的 tool_call_id 必须与对应的 tool_calls[].id 完全一致。
Python SDK
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://cubicspaces.cloud/v1",
)
response = client.chat.completions.create(
model="deepseek-v4-pro-0813",
messages=[
{"role": "user", "content": "用一句话解释什么是幂等性。"}
],
max_completion_tokens=2048,
extra_body={
"enable_thinking": True,
"reasoning_effort": "high",
},
)
message = response.choices[0].message
print(message.content)参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | deepseek-v4-pro 或 deepseek-v4-pro-0813 |
messages | array | OpenAI Chat 消息数组 |
max_completion_tokens | integer | 最大输出 Token 数,思考模型建议优先使用该字段 |
enable_thinking | boolean | 是否启用思考模式 |
reasoning_effort | string | 推理强度,推荐 high 或 max |
stream | boolean | 是否使用流式输出 |
stream_options | object | 流式附加配置,例如 include_usage |
tools | array | Function Calling 工具定义 |
tool_choice | string/object | 工具选择策略 |
注意事项
- DeepSeek V4 仅支持文本输入,不要在消息中传入图片或视频。
- 不支持
top_k,建议不要发送该参数。 - 思考内容和最终答案都可能计入输出 Token,请合理设置输出上限。
- 工具 Schema 应使用有效的 JSON Schema,并保证工具名称唯一。
- 需要固定模型行为时使用
deepseek-v4-pro-0813;需要使用当前通用版本时使用deepseek-v4-pro。 - 若返回模型不可用,请先确认账户已开放该模型。