首次调用 API | DeepSeek API Docs

Getting Started with DeepSeek API: A Developer's Guide

DeepSeek API offers a powerful suite of tools for developers looking to integrate advanced AI capabilities into their applications. This guide will walk you through your first API call and show you how to leverage DeepSeek's functionality. The DeepSeek API is designed to be compatible with the OpenAI API format, allowing you to use the OpenAI SDK and other compatible software with minimal configuration changes.

Leveraging OpenAI Compatibility

One of the key advantages of DeepSeek API is its compatibility with the OpenAI API format. This means you can adapt existing code and tools designed for OpenAI to work with DeepSeek, reducing development time and enabling a smooth transition. By simply modifying the configuration, you can seamlessly integrate DeepSeek’s models into your projects.

Quick Start: Your First API Call

Let's dive into making your first API call to the DeepSeek API. Here’s what you need to get started:

  1. Base URL: The base URL for the DeepSeek API is https://api.deepseek.com. You can also use https://api.deepseek.com/v1 for OpenAI compatibility, though the v1 here doesn't refer to the model version.
  2. API Key: You'll need an API key to authenticate your requests. You can apply for one on the DeepSeek Platform.

Calling the Chat API

Once you have your API key, you can use the following examples to access the DeepSeek API. These examples demonstrate a non-streaming output. If you prefer streaming output, set stream to true.

Here's how to call the Chat API using curl, Python, and Node.js:

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
  }'

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)

Node.js

// 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 and Best Practices

  • Install OpenAI SDK: The Python and Node.js examples require the OpenAI SDK. Install it using pip3 install openai for Python or npm install openai for Node.js.
  • Model Selection: Specify the model you want to use. deepseek-chat is used in the examples, and it now utilizes DeepSeek-V3 under the hood.
  • Authorization: Properly authenticate your requests by including your API key in the Authorization header.

Exploring DeepSeek Models

DeepSeek offers various models tailored for different use cases, each with unique capabilities. To specify a particular model, use the model parameter in your API request.

  • DeepSeek-V3: The latest iteration of the deepseek-chat model, ideal for general conversational tasks. Invoke by setting model='deepseek-chat'.
  • DeepSeek-R1 (deepseek-reasoner): DeepSeek's most advanced reasoning model. Designed to tackle complex problem-solving tasks. Invoke by setting model='deepseek-reasoner'.

Further Exploration {#further-exploration}

Once you've made your first API call, there's a whole range of features & functionalities to explore:

  • You can dive into various API guides to understand how to use each model.
  • Check out the parameter settings to understand how different models can be used

By following this guide, you're well-equipped to start building amazing applications with the DeepSeek API. Good luck, and happy coding!

. . .