DeepSeek API uses an API format compatible with OpenAI. By modifying the configuration, you can use the OpenAI SDK to access the DeepSeek API, or use software compatible with the OpenAI API.
| PARAM | VALUE | base_url* | | api_key | apply for an API key |
* For OpenAI compatibility, you can also set base_url to https://api.deepseek.com/v1, but note that v1 here is unrelated to the model version.
* The deepseek-chat model has been fully upgraded to DeepSeek-V3, and the API remains unchanged. You can call DeepSeek-V3 by specifying model='deepseek-chat'.
* deepseek-reasoner is DeepSeek's latest reasoning model, DeepSeek-R1. You can call DeepSeek-R1 by specifying model='deepseek-reasoner'.
Calling the Chat API
After creating an API key, you can use the following sample scripts to access the DeepSeek API. The examples use non-streaming output; you can set stream to true to use streaming output.
Python- # Please install OpenAI SDK first: `pip3 install openai`
- 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)
复制代码
Curl- 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
- }'
复制代码
nodejs
- // Please install OpenAI SDK first: `npm install openai`
- 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();
复制代码 |