DeepSeek's R1 large language model offers powerful capabilities for various applications. This tutorial provides a detailed, beginner-friendly guide on how to call the DeepSeek-R1 API using Python. Whether you're new to programming or an experienced developer, this guide will walk you through the process with clear instructions and visuals.
First, you need an API key to access DeepSeek's services.
sk-123456789abc
) and store it securely. You'll need it later.If you haven't already, install Python on your computer. macOS comes with Python 3 pre-installed. You can download the latest version from python.org. It's recommended to download version 3.8 or later.
Important: Make sure to check the "Add Python to PATH" option during installation.
The requests
library simplifies making HTTP requests in Python. Open your IDE tool (like VSCode) and run the following command in the terminal (Terminal
→ New Terminal
):
pip install requests
# or
pip3 install requests
A successful installation will show a message like "Successfully installed requests-x.x.x."
Troubleshooting: If the installation fails, especially with multiple Python versions on your system, ensure you're using the correct pip
version for your desired Python environment. VSCode's "Python" extension can help manage environments and terminal selection.
Create a new Python file (e.g., deepseek.py
) and paste the following code, replacing "sk-your_key"
with your actual DeepSeek API key:
import requests
# Replace with your actual API Key
API_KEY = "sk-your_key"
url = "https://api.deepseek.com/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"model": "deepseek-reasoner", # Use 'deepseek-reasoner' for R1 or 'deepseek-chat' for V3
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"}
],
"stream": False # Turn off stream
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
print(result['choices'][0]['message']['content'])
else:
print("Request failed, error code:", response.status_code)
Key Points:
model
: Specifies the DeepSeek model to use. Use "deepseek-reasoner"
for R1 and "deepseek-chat"
for V3.Execute the script in one of these ways:
python3 deepseek.py
or python deepseek.py
in the terminal.A successful call will output the model's response to your question. For example:
Hello! I am the DeepSeek-R1 that is developped by the DeepSeek company. I can help with any problems you have.
Parameter | Description |
---|---|
model |
Specifies the model (deepseek-reasoner for R1). |
messages |
Conversation history. Supports multi-turn conversations. |
stream |
Enables/disables streamed output (True/False). |
To engage in multi-turn conversations, structure the messages
list as follows:
messages = [
{"role": "system", "content": "You are a poet."},
{"role": "user", "content": "Write a poem about spring."},
{"role": "assistant", "content": "Spring breeze brushes long willow branches..."},
{"role": "user", "content": "Please continue with a second verse."}
]
To enable streaming output, modify the following parameters:
data["stream"] = True
response = requests.post(url, headers=headers, json=data, stream=True)
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
print(decoded_line)
model: "deepseek-chat"
model: "deepseek-reasoner"
pip install requests
was executed in the correct environment/terminal. Check the Python interpreter in VSCode.requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1
: This usually indicates a server-side issue (busy server). The API might be returning an empty response.For a simpler approach, consider using Apifox to interact with the DeepSeek-R1 or V3 API with or without streaming option. For more detailed instructions, see this article: Deepseek API Debugging.
This tutorial provides a comprehensive foundation for calling the DeepSeek API. Start with basic conversations, gradually trying to implement more advanced features like streaming and multi-turn dialogues.