To interact with the DeepSeek API, you can use the following sample code in Python. This example demonstrates how to set up the API, initiate a chat session, and send a message. Ensure you have the necessary credentials and libraries installed.
Setup Instructions
1. Install Required Libraries:
Make sure to install the DeepSeek API package using pip:
bash
pip install chat-deepseek-api python-dotenv
2. Environment Variables:
Create a `.env` file in your project directory and add your DeepSeek credentials:
DEEPSEEK_EMAIL=
<script language='JavaScript' type='text/javascript'>
<!--
var prefix = 'mailto:';
var suffix = '';
var attribs = '';
var path = 'hr' + 'ef' + '=';
var addy28449 = 'your_email' + '@';
addy28449 = addy28449 + 'example' + '.' + 'com';
document.write( '<a ' + path + '\'' + prefix + addy28449 + suffix + '\'' + attribs + '>' );
document.write( addy28449 );
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
Sample Python Code
python
import asyncio
import os
from deepseek_api import DeepseekAPI
from dotenv import load_dotenv
load_dotenv()
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):
print(chunk.choices.delta.content, end="")
cur_message_id = chunk.get_message_id()
if not cur_message_id:
cur_message_id = 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 Code
- Environment Setup: The script loads environment variables containing your DeepSeek credentials.
- API Initialization: It initializes the `DeepseekAPI` with your credentials and custom headers.
- Chat Session: A new chat session is created, and a message is sent to the API.
- Asynchronous Handling: The response from the API is handled asynchronously, allowing for efficient interaction.
This code serves as a foundational example for integrating with the DeepSeek API, enabling further customization and functionality as needed.
Citations:
[1] https://api-docs.deepseek.com
[2] https://deepinfra.com/deepseek-ai/DeepSeek-R1/api
[3] https://thinhdanggroup.github.io/blog-on-chat-deepseek-r1-api/
[4] https://github.com/rabilrbl/deepseek-api
[5] https://api-docs.deepseek.com/guides/function_calling
[6] https://www.datacamp.com/tutorial/deepseek-v3
[7] https://www.youtube.com/watch?v=WbKa-gxVybA
[8] https://github.com/deepseek-ai/deepseek-coder/?tab=readme-ov-file