首次调用 API | DeepSeek API Docs

Getting Started with DeepSeek API: A Quick Guide for Developers

DeepSeek API provides a powerful platform for integrating AI capabilities into your applications. Designed with compatibility in mind, it leverages the OpenAI-compatible API format, allowing you to seamlessly transition using the OpenAI SDK or other compatible software. This guide will walk you through the process of making your first API call and highlight key features for a smooth start.

Why Choose DeepSeek API?

  • OpenAI Compatibility: Easy integration with existing OpenAI-based projects.
  • Powerful Models: Access state-of-the-art models like DeepSeek-V3 (via deepseek-chat) and DeepSeek-R1 (via deepseek-reasoner).
  • Flexible Output: Supports both non-streaming and streaming output.

Prerequisites

Before you begin, you'll need the following:

  • An API Key: Obtain an API key from the DeepSeek Platform.
  • OpenAI SDK (Python or Node.js): Install the OpenAI SDK for your preferred language.
    • Python: pip3 install openai
    • Node.js: npm install openai

Making Your First API Call

DeepSeek follows a standard API structure. Here's a breakdown using curl, Python, and Node.js:

Key Parameters:

  • base_url: https://api.deepseek.com (or https://api.deepseek.com/v1 for OpenAI compatibility)
  • api_key: Your DeepSeek API key.
  • model: Specifies the DeepSeek model to use (e.g., deepseek-chat, deepseek-reasoner).

1. Using 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
  }'

2. Using Python

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)

3. Using Node.js

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

Understanding the Parameters

  • model: Crucially defines which DeepSeek model will process your request. Use "deepseek-chat" to access DeepSeek-V3, and "deepseek-reasoner" for DeepSeek-R1.
  • messages: An array of message objects defining the conversation history. Each object includes a role (either "system", "user", or "assistant") and corresponding content.
  • stream: Set to true for streaming output, enabling real-time responses. Set to false for a single, complete response.

Exploring DeepSeek's Capabilities

Once you've successfully made your first API call, explore the API documentation for more advanced features and models. Consider these next steps:

  • Model & Pricing: Get a detailed breakdown of available models and pricing on the Model & Pricing page.
  • Temperature Settings: Adjust the temperature parameter to control the randomness and creativity of the generated text. Learn more on the Temperature Settings page.
  • Inference Models: Leverage the DeepSeek-R1 via deepseek-reasoner for advanced reasoning tasks. See API Guides for instructions.
  • Token Usage: Track your token consumption to optimize your API usage. See Token Usage Calculation.

Staying Updated

DeepSeek regularly releases new models, features, and updates. Stay informed through the following channels:

  • News: Check the News section for the latest announcements.
  • Update Logs: You can see detailed logs of API changes at the Update Logs section.
  • Community: Engage with other developers on Discord or Twitter.

By following this guide, you're well on your way to harnessing the power of DeepSeek API in your projects. Remember to consult the official documentation for comprehensive information and explore the various models and functionalities available.

. . .