前言:DeepSeek API 采用与 OpenAI 兼容的 API 格式,可通过修改配置用 OpenAI SDK 或兼容软件访问。其base_url为https://api.deepseek.com
,需申请api_key。deepseek-chat已升级为 DeepSeek-V3,deepseek-reasoner即 DeepSeek-R1,分别通过指定model='deepseek-chat'
和model='deepseek-reasoner'
调用。文中提供了 Curl、Python、Node.js 的调用对话 API 示例代码。
关键参数
PARAM | VALUE |
---|---|
base_url | https://api.deepseek.com (也可设为https://api.deepseek.com/v1 ,v1 与模型版本无关) |
api_key | 需申请 |
模型情况
model='deepseek-chat'
即可调用。model='deepseek-reasoner'
即可调用。调用示例:在获取 API key 后,可使用以下示例代码调用对话 API,样例默认非流式输出,将stream
设置为true
可使用流式输出。
bash
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
pip3 install openai
)。python
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False
)
print(response.choices[0].message.content)
npm install openai
)。javascript
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com',
apiKey: '<DeepSeek API Key>'
});
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "deepseek-chat",
});
console.log(completion.choices[0].message.content);
}
main();
model
等参数。stream=False
修改为stream=True
即可使用流式输出。"model": "deepseek-chat"
修改为"model": "deepseek-reasoner"
,其他部分保持不变。修改后的代码如下:bash
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-reasoner",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'