DeepSeek API api sample program
to integrate the DeepSeek API into your application, you can utilize the unofficial Python wrapper or interact directly with the API using HTTP requests. Below are sample programs demonstrating both approaches.
1. Using the Unofficial Python Wrapper
An unofficial Python wrapper is available for DeepSeek's API, simplifying the integration process.
Installation:
Install the package directly from its GitHub repository:
pip install git+https://github.com/rabilrbl/deepseek-api.git
Usage Example:
This script demonstrates how to authenticate and interact with the DeepSeek API using the unofficial wrapper:
import asyncio
import os
from deepseek_api import DeepseekAPI
from dotenv import load_dotenv
# Load environment variables from a .env file
load_dotenv()
async def main():
# Retrieve your DeepSeek credentials from environment variables
email = os.getenv("DEEPSEEK_EMAIL")
password = os.getenv("DEEPSEEK_PASSWORD")
# Initialize the DeepSeek API client
async with DeepseekAPI(
email=email,
password=password,
save_login=True, # Option to save login credentials to login.json
model_class="deepseek_chat", # Choose between "deepseek_chat" or "deepseek_code"
) as app:
print("Start chatting with DeepSeek! Type 'exit' to quit.")
while True:
message = input("> ")
if message.lower() == "exit":
break
elif message.lower() == "new":
await app.new_chat()
continue
# Send the user's message to DeepSeek and print the response
async for chunk in app.chat(message):
try:
print(chunk["choices"][0]["delta"]["content"], end="")
except KeyError as ke:
print(ke)
print()
if __name__ == "__main__":
asyncio.run(main())
Notes:
- Ensure you have a
file containing your.env
andDEEPSEEK_EMAIL
.DEEPSEEK_PASSWORD
- The
parameter allows you to choose between different DeepSeek models, such asmodel_class
for general conversations or"deepseek_chat"
for coding assistance."deepseek_code"
Reference:
2. Direct API Access Using HTTP Requests
If you prefer not to use the unofficial wrapper, you can interact directly with the DeepSeek API using HTTP requests.
Prerequisites:
- Obtain your API key from DeepSeek.
Usage Example:
This script demonstrates how to send a message to the DeepSeek API and receive a response:
import requests
# Your DeepSeek API key
api_key = "your_deepseek_api_key"
# API endpoint
url = "https://api.deepseek.com/chat/completions"
# Headers for the HTTP request
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
# The message payload
data = {
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
],
"stream": False,
}
# Send the request to the DeepSeek API
response = requests.post(url, headers=headers, json=data)
# Check if the request was successful
if response.status_code == 200:
response_data = response.json()
assistant_message = response_data["choices"][0]["message"]["content"]
print("Assistant:", assistant_message)
else:
print("Error:", response.status_code, response.text)
Notes:
- Replace
with your actual DeepSeek API key."your_deepseek_api_key"
- The
list contains the conversation history. The system message sets the assistant's behavior, and the user message is your input.messages
- Setting
will return the complete response in a single message."stream": False
Reference:
Additional Information:
- The DeepSeek API is designed to be compatible with OpenAI's API format. This compatibility allows you to use existing OpenAI SDKs or software with minimal configuration changes.
- For more detailed information and advanced usage, refer to the official DeepSeek API documentation linked above.
By following these examples, you can integrate DeepSeek's chatbot capabilities into your applications, enabling advanced conversational AI interactions.
No Comments have been Posted.