首次调用 API | DeepSeek API Docs

Getting Started with DeepSeek API: A Comprehensive Guide for Developers

DeepSeek API offers a powerful suite of tools for developers looking to integrate advanced AI capabilities into their applications. Compatible with the OpenAI API format, DeepSeek makes it easy to transition and leverage existing knowledge and tools. This article will walk you through the essentials of making your first API call and exploring the key features.

Understanding DeepSeek API Compatibility

One of the most advantageous aspects of DeepSeek API is its compatibility with the OpenAI API format. This means you can use the OpenAI SDK and other OpenAI-compatible software with minimal modifications to access DeepSeek's functionalities.

Key Benefits of OpenAI Compatibility:

  • Ease of Integration: Use familiar tools and libraries.
  • Reduced Learning Curve: Leverage existing knowledge of the OpenAI ecosystem.
  • Simplified Migration: Seamlessly switch between APIs with configuration adjustments.

Initial Setup and API Key Acquisition

Before diving into code, you’ll need an API key.

Steps to Obtain Your DeepSeek API Key:

  1. Visit DeepSeek Platform.
  2. Register or log in to your account.
  3. Navigate to the API Keys section.
  4. Generate a new API key.

Making Your First API Call: A Step-by-Step Guide

With your API key in hand, you're ready to make your first call to the DeepSeek API. Let's explore different code examples using cURL, Python, and Node.js.

Essential Parameters

  • 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. Note that the v1 here is for compatibility and doesn't relate to the model version.
  • api_key: Your unique API key obtained from the DeepSeek Platform.

Example 1: Using cURL

cURL is a versatile command-line tool for making HTTP requests. Here’s how to use it with the DeepSeek API:

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 command sends a JSON payload to the /chat/completions endpoint, instructing the deepseek-chat model (now upgraded to DeepSeek-V3) to respond to the user's "Hello!" message, while adopting the persona of a helpful assistant.

Example 2: Using Python

For Python developers, the OpenAI SDK provides a straightforward way to interact with the DeepSeek API.

First, install the OpenAI SDK:

pip3 install openai

Then, use the following code:

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)

This script initializes the OpenAI client with your DeepSeek API key and base URL, then sends a similar request as the cURL example.

Example 3: Using Node.js

Node.js developers can also leverage the OpenAI SDK.

First, install the OpenAI package:

npm install openai

Then, use the following code:

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();

This code snippet performs the same function as the Python example, but within a Node.js environment.

Exploring Advanced Models: DeepSeek-R1 (deepseek-reasoner)

DeepSeek offers more than just chat models. The deepseek-reasoner model, powered by DeepSeek-R1, is designed for advanced reasoning tasks (Reasoning Model Guide). To use it, simply specify model='deepseek-reasoner' in your API call.

Considerations for Production Use

When integrating DeepSeek API into your applications, keep the following points in mind:

  • Asynchronous operations: For improved performance, consider utilizing asynchronous operations to avoid blocking the main thread, especially when dealing with time-consuming requests.
  • Rate limits: Be aware of the API's rate limits (Rate Limit Documentation) to prevent service disruptions and ensure smooth operation.
  • Context Caching: Learn how to use context hard drive cache for efficient memory management (Context hard drive cache).
  • Token Usage: Track and optimize token usage to minimize costs (Token Usage Calculation).

Staying Updated

To stay informed about the latest features, updates, and news regarding DeepSeek, consider the following:

  • Official Documentation: Regularly visit the DeepSeek API Docs to access the most up-to-date information about the API, models, and functionalities.
  • News: Check the News section for recent updates.

By following this guide, you should now be well-equipped to start experimenting with the DeepSeek API and integrating it into your projects.

. . .