跳转至

天狐大模型平台-DeepSeek 调用 API 指南

1、获取 API 密钥

API 调用要获得授权才能调用,因此首先要获得 API 密钥:

  1. 打开 https://deepseek.shanhaiengine.com/ 注册用户并登录。

  2. 点击左下角的用户名位置进入“设置”,如下图:
    85673e701a49b3a2a9589fab750f615b.png

  3. 在设置界面中找到“账号”,点击“API 密钥”的显示按钮,可以看到“API 密钥”下面有个“创建新安全密钥”,创建后复制该密钥,密钥是以 sk 开头的一串字符,如“sk-xxxxxxxxxxxxxxxxxxx”。
    1cf38e0ceb51fd5e3f3d861d40370e9f.png

2、核心接口

API 接口地址:https://deepseek.shanhaiengine.com/ 目前支持的模型:

  • 满血版:DeepSeek-R1-bf16
  • 蒸馏版:deepseek-r1:70b
  • 蒸馏版:deepseek-r1:32b

由于 DeepSeek-R1 都是有思考过程的,所以整体返回时间会比较长,在调用时注意设置超时时间长一点。

2.1 获取模型列表

  • API:GET /api/models
  • Curl 示例:(YOUR_API_KEY 换成自己的 key,以下其他接口同理)
curl -H "Authorization: Bearer YOUR_API_KEY" https://deepseek.shanhaiengine.com/api/models

2.2 对话

  • API:POST /api/chat/completions
  • Curl 示例:
curl -X POST https://deepseek.shanhaiengine.com/api/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
      "model": "DeepSeek-R1-bf16",
      "messages": [
        {
          "role": "user",
          "content": "Why is the sky blue?"
        }
      ]
    }'

2.3 上传文件:

  • API: POST /api/v1/files/
  • Curl 示例:(YOUR_API_KEY 换成自己的 key)
curl -X POST -H "Authorization: Bearer YOUR_API_KEY" -H "Accept: application/json" \
-F "file=@/Users/chen/test.pdf" https://deepseek.shanhaiengine.com/api/v1/files/
  • Python 示例:
curl -X POST -H "Authorization: Bearer YOUR_API_KEY" -H "Accept: application/json" \
-F "file=@/Users/chen/test.pdf" https://deepseek.shanhaiengine.com/api/v1/files/

2.4 添加文件到知识库

  • API: POST /api/v1/knowledge/{id}/file/add
  • Curl 示例:
curl -X POST https://deepseek.shanhaiengine.com/api/v1/knowledge/{knowledge_id}/file/add \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"file_id": "your-file-id-here"}'
  • Python 实例:
import requests

def add_file_to_knowledge(token, knowledge_id, file_id):
    url = f'https://deepseek.shanhaiengine.com/api/v1/knowledge/{knowledge_id}/file/add'
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    data = {'file_id': file_id}
    response = requests.post(url, headers=headers, json=data)
    return response.json()

2.5 在对话中使用文件

  • 接口: POST /api/chat/completions
  • Curl 示例: 首先要调用文件上传接口获得文件 ID,然后再调用对话接口:
curl -X POST 'https://deepseek.shanhaiengine.com/api/chat/completions' \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
      "model": "DeepSeek-R1-bf16",
      "messages": [
        {"role": "user", "content": "Explain the concepts in this document."}
      ],
      "files": [
        {"type": "file", "id": "your-file-id-here"}
      ],
      "stream":false
    }'
  • Python 示例:
import requests

def chat_with_file(token, model, query, file_id):
    url = 'https://deepseek.shanhaiengine.com/api/chat/completions'
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    payload = {
        'model': model,
        'messages': [{'role': 'user', 'content': query}],
        'files': [{'type': 'file', 'id': file_id}]
    }
    response = requests.post(url, headers=headers, json=payload)
    return response.json()

2.6 使用多文件知识库对话

  • 接口: POST /api/chat/completions
  • Curl 示例: 如果使用知识库的话,需要获取知识库文件集合 ID,然后调用:
curl -X POST 'https://deepseek.shanhaiengine.com/api/chat/completions' \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
      "model": "DeepSeek-R1-bf16",
      "messages": [
        {"role": "user", "content": "Explain the concepts in this document."}
      ],
      "files": [
        {"type": "collection", "id": "your-collection-id-here"}
      ],
      "stream":false
    }'
  • Python 示例:
import requests

def chat_with_collection(token, model, query, collection_id):
    url = 'https://deepseek.shanhaiengine.com/api/chat/completions'
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    payload = {
        'model': model,
        'messages': [{'role': 'user', 'content': query}],
        'files': [{'type': 'collection', 'id': collection_id}]
    }
    response = requests.post(url, headers=headers, json=payload)
    return response.json()