How to use DeepSeek R1 for complex reasoning
Quick answer
Use the
deepseek-reasoner model via the OpenAI-compatible DeepSeek API by sending your reasoning prompts to client.chat.completions.create(). This model specializes in complex reasoning and can be accessed with your API key and the correct base URL.PREREQUISITES
Python 3.8+DeepSeek API keypip install openai>=1.0
Setup
Install the openai Python package (version 1.0 or higher) and set your DeepSeek API key as an environment variable. Use the DeepSeek API endpoint https://api.deepseek.com to access their models.
pip install openai>=1.0 Step by step
This example demonstrates how to call the deepseek-reasoner model for complex reasoning tasks using the OpenAI-compatible DeepSeek API client.
import os
from openai import OpenAI
# Initialize DeepSeek client with base_url
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
# Define the reasoning prompt
messages = [
{"role": "user", "content": "Explain the steps to solve a complex math problem involving calculus and linear algebra."}
]
# Create chat completion request
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=messages,
max_tokens=1024
)
# Extract and print the reasoning output
print(response.choices[0].message.content) output
Explain the steps to solve a complex math problem involving calculus and linear algebra. 1. Understand the problem statement and identify the variables. 2. Break down the problem into calculus and linear algebra components. 3. Apply differentiation or integration techniques as needed. 4. Use matrix operations or vector spaces for the linear algebra part. 5. Combine results to reach the final solution. 6. Verify the solution by checking boundary conditions and consistency.
Common variations
You can adjust max_tokens for longer or shorter responses, switch to other DeepSeek models like deepseek-chat for general chat, or implement asynchronous calls using Python asyncio with the OpenAI SDK.
import asyncio
from openai import OpenAI
async def async_reasoning():
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
response = await client.chat.completions.acreate(
model="deepseek-reasoner",
messages=[{"role": "user", "content": "Explain the theory of relativity in simple terms."}],
max_tokens=512
)
print(response.choices[0].message.content)
asyncio.run(async_reasoning()) output
The theory of relativity, developed by Einstein, explains how space and time are linked and how gravity affects them. It shows that time can slow down or speed up depending on how fast you move or how strong gravity is.
Troubleshooting
- If you receive authentication errors, verify your
DEEPSEEK_API_KEYenvironment variable is set correctly. - For timeout or connection issues, check your network and ensure the base URL
https://api.deepseek.comis reachable. - If responses are incomplete, increase
max_tokensor check for rate limits on your API key.
Key Takeaways
- Use the OpenAI-compatible SDK with
base_url="https://api.deepseek.com"to access DeepSeek models. - The
deepseek-reasonermodel is optimized for complex reasoning tasks. - Adjust
max_tokensand use async calls for flexible integration. - Always set your API key in the environment variable
DEEPSEEK_API_KEY. - Check network connectivity and API limits if you encounter errors.