Skip to content

Connecting DeepSeek API in Python

Overview

Step-by-step guide: get your DeepSeek key, install the OpenAI SDK, and send your first request from Python.

1. Get the API Key

Sign up on the DeepSeek platform:

https://platform.deepseek.com/

Top up your balance and create a key in the API Keys section.

2. Install the Library

DeepSeek uses an OpenAI-compatible API, so the official OpenAI SDK works:

pip install openai

3. Write the First Request

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_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": "Explain what a token is."},
    ],
)

print(response.choices[0].message.content)

4. Main Models

Model Purpose
deepseek-chat Chatting and everyday tasks
deepseek-reasoner Reasoning and complex tasks

5. Useful Settings

  • temperature — from 0 to 1, controls creativity;
  • max_tokens — limit on response length.

More about temperature — in the article Temperature.

See Also