DeepSeek AI offers a powerful API that's compatible with the OpenAI API format. This means you can leverage your existing OpenAI SDK knowledge and tools with DeepSeek by simply modifying the configuration. This guide will walk you through your first API call, providing essential information and code examples.
The DeepSeek API offers several advantages:
model='deepseek-chat'
) and DeepSeek-R1 (accessed via model='deepseek-reasoner'
), designed for reasoning tasks. See the DeepSeek API documentation for a full list of models and their capabilities.Before making your first call, you'll need two key pieces of information:
api_key
: Apply for an API key on the DeepSeek Platform. This key authenticates your requests.base_url
: The base URL for the DeepSeek API is https://api.deepseek.com
. For OpenAI compatibility, you can also use https://api.deepseek.com/v1
, but note that the v1
here is not related to model versioning.Once you have your API key, you can use various programming languages to interact with the DeepSeek API. Here are examples in curl
, Python, and Node.js, all demonstrating a simple chat completion task:
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
}'
This curl
command sends a request to the /chat/completions
endpoint, providing the model to use (deepseek-chat
), and a simple conversation history. The "stream": false
parameter specifies that you want a non-streaming response.
# 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)
Important: Make sure you have the OpenAI SDK installed (pip3 install openai
). This example demonstrates the compatibility—you can use the OpenAI SDK seamlessly with the DeepSeek API.
// 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();
Key Points: Ensure you install the OpenAI SDK for Node.js (npm install openai
). The code initializes the OpenAI client with the DeepSeek API's base URL and your API key.
All three examples share these common elements:
/chat/completions
endpoint, which is designed for generating chat responses.Content-Type: application/json
header and the Authorization: Bearer <DeepSeek API Key>
header, providing the necessary information about the request and your authentication credentials.model
(e.g., deepseek-chat
), and an array of messages
. Each message has a role
(either "system" or "user") and content
.stream
parameter can be set to true
for streaming output, which provides a more interactive experience.This is just the beginning! The DeepSeek API offers a wide range of capabilities:
model='deepseek-reasoner'
.temperature
to control the creativity of the responses. See the Temperature Settings documentation for more details.By leveraging the DeepSeek API's compatibility and powerful models, you can unlock new possibilities in your AI-powered applications. Start experimenting and discover the potential of DeepSeek today!