alex_bn_lee

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

[1098] Getting Started with the DeepSeek API

References:


Making API Requests with Python

Now that you have your API key and necessary dependencies, you’re ready to start making API requests to DeepSeek. Here’s a simple example of how to do that in Python.

Step 1: Create Your Python Script

Open your code editor (such as Visual Studio Code) and create a new Python file. Let’s name it deepseek.py. In this file, you’ll write the code to send a request to the DeepSeek API.

Step 2: Add the Code

Below is the basic Python script that sends a request to the DeepSeek API using the requests library.

import requests
# Replace with your OpenRouter API key
API_KEY = 'your_openrouter_api_key'
API_URL = 'https://openrouter.ai/api/v1/chat/completions'
# Define the headers for the API request
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
# Define the request payload (data)
data = {
"model": "deepseek/deepseek-chat:free",
"messages": [{"role": "user", "content": "What is the meaning of life?"}]
}
# Send the POST request to the DeepSeek API
response = requests.post(API_URL, json=data, headers=headers)
# Check if the request was successful
if response.status_code == 200:
print("API Response:", response.json())
else:
print("Failed to fetch data from API. Status Code:", response.status_code)

Step 3: Run the Script

Once you’ve added the code, save the file and open your terminal or command prompt. Navigate to the directory where your deepseek.py file is located, and run the script using:

python deepseek.py

If everything is set up correctly, you should see a response from DeepSeek in your terminal.

posted on   McDelfino  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示