Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

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
    .env
    file containing your
    DEEPSEEK_EMAIL
    and
    DEEPSEEK_PASSWORD
    .
  • The
    model_class
    parameter allows you to choose between different DeepSeek models, such as
    "deepseek_chat"
    for general conversations or
    "deepseek_code"
    for coding assistance.

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
    "your_deepseek_api_key"
    with your actual DeepSeek API key.
  • The
    messages
    list contains the conversation history. The system message sets the assistant's behavior, and the user message is your input.
  • Setting
    "stream": False
    will return the complete response in a single message.

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.

caa January 28 2025 96 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 14
Newest Member: Frank_nKansas