How to Make DeepSeek API Calls in Python: A Complete Guide

Last update: 17/02/2025
Author Isaac
  • DeepSeek offers a powerful API compatible with OpenAI, facilitating its integration into projects IA.
  • To use the API, you need to generate a key from the platform and configure the environment in Python.
  • Using the Requests library in Python allows you to make POST requests to interact with DeepSeek.
  • There are advanced options such as local execution of the model and the use of streaming for more fluid responses.

deepseek api

DeepSeek has revolutionized the world of Artificial Intelligence with its advanced language model, allowing developers and businesses to leverage its powerful API for a variety of tasks natural language processingIf you are a programmer and want to integrate DeepSeek into your projects, in this guide you will learn step by step how to make calls to its API using Python. From obtaining the API key to running the model locally, we will cover all the essential details.

Before we begin, it is important to understand that DeepSeek offers an API compatible with OpenAI, which makes it much easier to integrate into applications already designed to work with models like GPT. This means that if you already have experience working with OpenAI, the transition to DeepSeek will be easy for you. Now, let's see how to set everything up to start making our first API calls.

1. Get the DeepSeek API key

To start using the DeepSeek API, the first thing you need is a API key. To obtain it, follow these steps:

  1. Access the DeepSeek platform and log in to your account.
  2. Go to the API Keys section and click “Generate New Key”.
  3. Copy the generated key and store it in a safe place, as you will need it to authenticate yourself in each request.
  4. Please make sure your account has available funds, as DeepSeek requires an initial top-up to activate the key.

2. Set up the environment in Python

python deepseek

Before doing any request to the API, it is necessary set up a Python development environment. To do this, follow these steps:

  How to easily stream Telegram videos to Chromecast

Install Python

If you don't have Python installed on your computer yet, you can download the latest version from python.orgIt is recommended to use the version 3.8 or higher.

Install the Requests library

Library Requests is essential for sending HTTP requests to the DeepSeek API. To install it, use the following command:

pip install requests

If you have multiple Python environments installed, make sure you run the command in the correct environment.

3. Make the first call to the DeepSeek API

Once the environment is set up, we can make our first API call. To do this, we will create a file called deepseek.py and we will write the following code:

import requests API_KEY = "your_API_key" URL = "https://api.deepseek.com/v1/chat/completions" headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } data = { "model": "deepseek-reasoner", "messages": [{"role": "user", "content": "What is DeepSeek?"}], "stream": False } response = requests.post(URL, headers=headers, json=data) print(response.json())

This code sends a question to DeepSeek and displays the response in the console. Remember to replace your_api_key with the key obtained previously.

4. Advanced API Usage

Streaming responses

If you want to get the answer in a simple way, progressive Instead of receiving it all at once, you can activate streaming mode by changing the key value stream in the JSON of the request to True.

"stream": True

Multi-turn dialogue

DeepSeek allows you to maintain the context of the conversation by adding previous messages in the requestAn example of a fluid dialogue would be:

data = { "model": "deepseek-reasoner", "messages": [ {"role": "user", "content": "Explain to me the concept of neural networks"}, {"role": "assistant", "content": "A neural network is a system of ..."}, {"role": "user", "content": "And how are they trained?"} ] }

5. Run DeepSeek locally

For those who prefer not to rely on the API and want to run the model on their own machine, DeepSeek offers versions optimized for local execution. Some of the options include:

  • Run the model with Don't, a tool that allows you to load AI models directly onto your machine.
  • Use vLLM o SGLang to serve models locally and reduce query latency.
  Microsoft Project: What It Is, Features, Advantages And More

To install Don't and run DeepSeek-R1, follow these steps:

pip install Olama Olama run deepseek-r1:8b

This will download and run the version 8B of the model directly onto your machine.

With this guide, you should have everything you need to start integrating DeepSeek into your Python projects. From generating your API key to running the model locally, you can now leverage this powerful AI tool to answer questions, generate text and even train custom modelsAs the platform evolves, it is recommended to review the official DeepSeek documentation to stay up to date with the latest updates and improvements.

Leave a comment