2025-02-28 08:43

Python访问DeepSeek-API

码自答

Python

(212)

(0)

收藏

一    注册DeepSeek,获取ak

        image.png


二    Python添加第三方库request

        image.png


三    Python代码

import requests

# 填写你的 API Key
API_KEY = "sk-9600*****************************78a9"

url = "https://api.deepseek.com/chat/completions"
headers = {
    "Content-Type": "application/json",  #传输数据格式为JSON格式
    "Authorization": f"Bearer {API_KEY}"
}

content = input("请输入:");

data = {
    "model": "deepseek-reasoner",  # 指定访问DeepSeek的模型 使用 R1 模型(deepseek-reasoner)
    "messages": [
        {"role": "user", "content": content}
    ],
    "stream": False  # 关闭流式传输
}

#向DeepSeek发送请求
response = requests.post(url, headers=headers, json=data);

if response.status_code == 200:
    result = response.json();#接收DeepSeek响应
    print(result['choices'][0]['message']['content'])
else:
    print("请求失败,错误码:", response.status_code)

0条评论

点击登录参与评论