To use the DeepSeek API with Python, follow these steps for installation, setup, and basic interaction. This guide will help you integrate the API into your projects effectively.
Step 1: Install the DeepSeek API
Prerequisites
- Ensure you have Python 3.x installed on your system.- It is recommended to create a virtual environment to avoid conflicts with other packages.
Create a Virtual Environment
Run the following commands in your terminal:bash
python -m venv deepseek_env
Activate the virtual environment:
- On Windows:
bash
deepseek_env\Scripts\activate
- On macOS/Linux:
bash
source deepseek_env/bin/activate
Install the DeepSeek API
Use pip to install the Chat DeepSeek R1 API:bash
pip install chat-deepseek-api
Step 2: Configure Environment Variables
Create a `.env` file in your project directory and fill it with your DeepSeek credentials:
DEEPSEEK_EMAIL=
<script language='JavaScript' type='text/javascript'>
<!--
var prefix = 'mailto:';
var suffix = '';
var attribs = '';
var path = 'hr' + 'ef' + '=';
var addy90239 = 'your_email' + '@';
addy90239 = addy90239 + 'example' + '.' + 'com';
document.write( '<a ' + path + '\'' + prefix + addy90239 + suffix + '\'' + attribs + '>' );
document.write( addy90239 );
document.write( '<\/a>' );
//-->
</script><script language='JavaScript' type='text/javascript'>
<!--
document.write( '<span style=\'display: none;\'>' );
//-->
</script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
<script language='JavaScript' type='text/javascript'>
<!--
document.write( '</' );
document.write( 'span>' );
//-->
</script>
DEEPSEEK_PASSWORD=your_password
DEEPSEEK_DEVICE_ID=your_device_id
DEEPSEEK_COOKIES=your_cookies
DEEPSEEK_DS_POW_RESPONSE=your_ds_pow_response
Step 3: Basic Usage of the DeepSeek API
Hereâs a simple Python script that demonstrates how to interact with the DeepSeek API. This script initializes the API, starts a chat session, and sends a message:
python
import asyncio
import os
from dotenv import load_dotenv
from deepseek_api import DeepseekAPI
from deepseek_api.model import MessageData
load_dotenv() # Load environment variables
async def main():
email = os.environ.get("DEEPSEEK_EMAIL")
password = os.environ.get("DEEPSEEK_PASSWORD")
device_id = os.environ.get("DEEPSEEK_DEVICE_ID")
cookies = os.environ.get("DEEPSEEK_COOKIES")
ds_pow_response = os.environ.get("DEEPSEEK_DS_POW_RESPONSE")
app = await DeepseekAPI.create(
email=email,
password=password,
save_login=True,
device_id=device_id,
custom_headers={
"cookie": cookies,
"x-ds-pow-response": ds_pow_response,
},
)
chat_session_id = await app.new_chat()
print(f"Starting chat session with id: {chat_session_id}")
message_id = None
async for chunk in app.chat(message="who are you", id=chat_session_id, parent_message_id=message_id):
chunk_data: MessageData = chunk
print(chunk_data.choices.delta.content, end="")
cur_message_id = chunk.get_message_id() or 0
if not message_id or cur_message_id > message_id:
message_id = cur_message_id
print()
await app.close()
if __name__ == "__main__":
asyncio.run(main())
Explanation of the Script
1. Imports: The script imports necessary libraries and modules.2. Load Environment Variables: It loads credentials from the `.env` file.
3. Initialize API: It creates an instance of `DeepseekAPI` using your credentials.
4. Start Chat Session: A new chat session is initiated.
5. Send Message: The script sends a message ("who are you") and prints the response asynchronously.
6. Close Session: Finally, it closes the chat session.
By following these steps and using the provided script, you can successfully set up and interact with the DeepSeek API using Python. This setup allows you to leverage advanced AI capabilities in your applications.
Citations:
[1] https://thinhdanggroup.github.io/blog-on-chat-deepseek-r1-api/
[2] https://www.youtube.com/watch?v=qbUELF9Et4s
[3] https://dev.to/mattsu014/run-deepseek-r1-locally-with-ollama-and-python-44b6
[4] https://github.com/topics/deepseek-api
[5] https://api-docs.deepseek.com/guides/function_calling
[6] https://github.com/deepseek-ai/DeepSeek-R1/activity
[7] https://www.datacamp.com/tutorial/deepseek-v3