📌 模型列表接口:超强 AI 阵容任你选,总有一款适合你!✨
💰 用户余额接口:随时查询,余额不足?别慌,充就完事!💡
💬 对话补全接口:让 AI 陪你聊天、写代码、搞创作,智能到飞起~ 🌟

让 AI 替你打工,不香吗?

🤖 快上车,文档 + 课程「DeepSeek API 接入指南」,带你弯道超车!🚗💨

一、/models 模型列表接口

https://api-docs.deepseek.com/zh-cn/api/list-models

image.png

代码

import requests

url = "https://api.deepseek.com/models"

payload={}
headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>' # <TOKEN> 替换成你的 API key
}

response = requests.request("GET", url, headers=headers, data=payload)
print(response.status_code)
print(response.text)

输出

200
{"object":"list","data":[{"id":"deepseek-chat","object":"model","owned_by":"deepseek"},{"id":"deepseek-reasoner","object":"model","owned_by":"deepseek"}]}

二、/user/balance 查询余额接口

https://api-docs.deepseek.com/zh-cn/api/get-user-balance

image-1.png

代码

import requests

url = "https://api.deepseek.com/user/balance"

payload={}
headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

输出

{"is_available":true,"balance_infos":[{"currency":"CNY","total_balance":"0.37","granted_balance":"0.00","topped_up_balance":"0.37"}]}

三、/chat/completions 对话补全接口 - 基本使用

https://api-docs.deepseek.com/zh-cn/api/create-chat-completion

image-2.png

代码

import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    # {
    #   "content": "你是一个小学物理老师,尽量长篇大论",
    #   "role": "system" # 系统设置 角色 回复风格
    # },
    {
      "content": "你好",
      "role": "user"
    }
  ],
  "model": "deepseek-chat",
  "n": 1
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.status_code)
print(response.text)
print(response.json()['choices'][0]['message']['content'])

输出

200
{"id":"22ea3b12-b06c-4068-8704-9ec8acf430d1","object":"chat.completion","created":1745549540,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"你好!😊 很高兴见到你~有什么我可以帮你的吗?"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":4,"completion_tokens":15,"total_tokens":19,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":4},"system_fingerprint":"fp_8802369eaa_prod0225"}
你好!😊 很高兴见到你~有什么我可以帮你的吗?

四、/chat/completions 对话补全接口 多轮对话

https://api-docs.deepseek.com/zh-cn/guides/multi_round_chat

image-3.png

代码

import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    {
      "content": "最高的山峰?",
      "role": "user"
    },
    {
      "role": "assistant",
      "content": "地球上最高的山峰是**珠穆朗玛峰(Mount Everest)**,其 海拔高度为**8,848.86米**(2020年中国与尼泊尔共同公布的最新测量数据)。……"
    },
    {
      "content": "风景怎么样?",
      "role": "user"
    },

  ],
  "model": "deepseek-chat",
  "n": 1
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.status_code)
print(response.text)
print(response.json()['choices'][0]['message']['content'])

输出

200
{"id":"fc573b83-f777-4e2b-b591-cb1bf79de1aa","object":"chat.completion","created":1745563374,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"珠穆朗玛峰的风景堪称 **“极致而震撼”**,但它的美 与危险并存,是一种 **荒凉、壮阔、神圣** 的视觉冲击。……"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":367,"completion_tokens":773,"total_tokens":1140,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":367},"system_fingerprint":"fp_8802369eaa_prod0225"}
珠穆朗玛峰的风景堪称 **“极致而震撼”**,但它的美与危险并存,是一种 **荒凉、壮阔、神圣** 的视觉冲击。……

五、/chat/completions 对话补全接口 流式响应

代码

import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    {
      "content": "你好",
      "role": "user"
    }
  ],
  "model": "deepseek-chat",
  "n": 1,
  "stream": True
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("POST", url, headers=headers, data=payload, stream=True)
print(response.status_code)
print(response.headers['content-type'])

for line in response.iter_lines():
    # print(line)
    if line.startswith(b'data: {'):
        chunk = json.loads(line[6:])['choices'][0]['delta']['content']
        print(chunk, end="", flush=True) 
    elif line == b'data: [DONE]':
        print("[DONE]")   

输出

200
text/event-stream; charset=utf-8
你好!😊 很高兴见到你~有什么我可以帮你的吗?[DONE]

六、/chat/completions 对话补全接口 深度思考

代码

import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    {
      "content": "你好",
      "role": "user"
    }
  ],
  "model": "deepseek-reasoner",
  "n": 1,
  "stream": True
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("POST", url, headers=headers, data=payload, stream=True)
print(response.status_code)
print(response.headers['content-type'])
has_printed = False
for line in response.iter_lines():
    # print(line.decode())
    if line.startswith(b'data: {'):
        delta = json.loads(line[6:])['choices'][0]['delta']
        # if delta['reasoning_content'] is not None:
        #     chunk = delta['reasoning_content']
        # else:
        #     chunk = delta['content']
        if delta['content'] is not None and not has_printed:
            print('\n------------------------')
            has_printed = True
        chunk = delta['reasoning_content'] if delta['reasoning_content'] is not None else delta['content']
        print(chunk, end="", flush=True) 
    elif line == b'data: [DONE]':
        print("[DONE]")        

输出

200
text/event-stream; charset=utf-8
嗯,用户发来的是“你好”,这是很常见的问候。首先,我需要回应一个友好的问候,比如“你好!有什么我可以帮助你的吗?” 这样既礼貌又开放,让用户知道他们可以继续提问。不过,可能用户只是打个招呼,或者接下来会有具体的问题。我需要保持 回复简洁,同时鼓励他们进一步交流。还要注意用中文,避免使用任何Markdown格式。另外,检查有没有拼写错误,确保回复 流畅自然。好,这样应该可以了。
------------------------
你好!有什么我可以帮助你的吗?[DONE]

七、/chat/completions 对话补全接口 输出控制

代码

import requests
import json

url = "https://api.deepseek.com/chat/completions"

payload = json.dumps({
  "messages": [
    {
      "content": "最高的山峰?",
      "role": "user"
    }
  ],
  "model": "deepseek-chat",
  "n": 1,
  "max_tokens": 100,
  "stop": ["\n", "。"]
})
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Authorization': 'Bearer <TOKEN>'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.status_code)
print(response.text)
print(response.json()['choices'][0]['message']['content'])

输出

200
{"id":"f71ed8cc-a79e-4205-9c29-e318658be353","object":"chat.completion","created":1745563803,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"地球上最高的山峰是**珠穆朗玛峰(Mount Everest)**,其海拔高度为**8,848.86米**(2020年中国和尼泊尔共同宣布的最新测量数据)"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":6,"completion_tokens":42,"total_tokens":48,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":6},"system_fingerprint":"fp_8802369eaa_prod0225"}
地球上最高的山峰是**珠穆朗玛峰(Mount Everest)**,其海拔高度为**8,848.86米**(2020年中国和尼泊尔共同宣布的最新测量数据)

华健课堂
1 声望0 粉丝