DeepSeek API offers a powerful suite of language models accessible through an OpenAI-compatible API format. This means developers can leverage existing OpenAI SDKs and tools to interact with DeepSeek's advanced AI capabilities with minimal code adjustments. This article will guide you through your first API call, explaining the key concepts and code snippets needed to get started.
DeepSeek distinguishes itself through:
deepseek-chat
) and DeepSeek-R1 (via the deepseek-reasoner
model), designed for specific tasks.Before diving into the code, you'll need the following:
pip3 install openai
(for Python) or npm install openai
(for Node.js).Parameter | Value | Description |
---|---|---|
base_url |
https://api.deepseek.com or https://api.deepseek.com/v1 |
The base URL for the DeepSeek API. While /v1 is an option for OpenAI compatibility, be aware that it doesn't relate to model versioning. |
api_key |
Your DeepSeek API key | The authentication key obtained from the DeepSeek platform. |
model |
deepseek-chat or deepseek-reasoner |
Specifies which DeepSeek model to use. deepseek-chat utilizes DeepSeek-V3, while deepseek-reasoner enables the DeepSeek-R1 model. |
Let's look at examples in cURL, Python, and Node.js for a basic conversation API call. These examples demonstrate how to send a simple "Hello!" message to the deepseek-chat
model.
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
}'
# 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)
// 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();
Explanation:
Content-Type
header specifies that the request body is in JSON format. The Authorization
header carries your API key for authentication.model
(specifying which DeepSeek model to use), messages
(an array of message objects representing the conversation), and stream
(set to false
for non-streaming output).messages
array contains dictionaries with role
(either "system" or "user") and content
(the actual message text).Once you've successfully made your first API call with the DeepSeek API, there are several avenues to explore its full potential:
stream
to true
to receive real-time, chunked responses, improving the user experience for interactive applications.deepseek-reasoner
for tasks requiring advanced reasoning capabilities. See also the DeepSeek API documentationtemperature
to control the randomness and creativity of the model's responses. See Temperature SettingsDeepSeek regularly releases updates and new models. Keep an eye on the news and updates sections of the DeepSeek API documentation for the latest features and improvements.
By following this guide, you're well-equipped to start building innovative applications powered by DeepSeek's cutting-edge AI models. Remember to consult the official DeepSeek API Documentation for detailed information and advanced features.