首次调用 API
DeepSeek API 使用与 OpenAI/Anthropic 兼容的 API 格式,通过修改配置,您可以使用 OpenAI/Anthropic SDK 来访问 DeepSeek API,或使用与 OpenAI/Anthropic API 兼容的软件。
| PARAM | VALUE |
|---|---|
| base_url (OpenAI) | https://api.deepseek.com |
| base_url (Anthropic) | https://api.deepseek.com/anthropic |
| api_key | apply for an API key |
| model* | deepseek-v4-flashdeepseek-v4-prodeepseek-chat (将于 2026/07/24 弃用)deepseek-reasoner (将于 2026/07/24 弃用) |
* deepseek-chat 与 deepseek-reasoner 两个模型名将于 2026/07/24 弃用。出于兼容考虑,二者分别对应 deepseek-v4-flash 的非思考与思考模式。
调用对话 API
在创建 API key 之后,你可以使用以下样例脚本,通过 OpenAI API 格式来访问 DeepSeek 模型。样例为非流式输出,您可以将 stream 设置为 true 来使用流式输出。
Anthropic API 格式的访问样例,请参考Anthropic API。
- curl
- python
- node
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/on" \
-H "Authorization: Bearer ${DEEPSEEK_API_KEY}" \
-d '{
"model": "deepseek-v4-pro",
"messages": [
{"role": "system""content": "You are a helpful assistant."},
{"role": "user""content": "Hello!"}
],
"thinking": {"type": "enabled"},
"reasoning_effort": "high",
"stream": false
}'
# Please install OpenAI SDK first: `pip3 install openai`
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False,
reasoning_effort="high",
extra_body={"thinking": {"type": "enabled"}}
)
print(response.choices[0].message.content)
// Please install OpenAI SDK first: `npm install openai`
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: process.env.DEEPSEEK_API_KEY,
});
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "deepseek-v4-pro",
thinking: {"type": "enabled"},
reasoning_effort: "high",
stream: false,
});
console.log(completion.choices[0].message.content);
}
main();