腾讯云deepseek调用API
时间: 2025-03-02 21:50:08 浏览: 56
### 调用腾讯云 DeepSeek API 的方法
为了调用腾讯云 DeepSeek API,开发者需遵循特定流程来设置环境并编写代码以实现所需功能。通常情况下,API 请求涉及获取访问凭证、构建请求头以及发送 HTTP 请求到指定端点。
#### 获取访问凭证
在发起任何 API 请求之前,必须先获得有效的 SecretId 和 SecretKey。这些信息可以从腾讯云控制台的安全管理页面找到[^1]。
#### 构建请求头部
每次向 DeepSeek 发送请求时都需要携带认证信息和其他必要参数作为 HTTP 头部的一部分。这包括但不限于 `Authorization` 字段用于身份验证;`Content-Type` 设置为 application/json 表明正文格式为 JSON 数据。
#### 编写 Python 示例代码
下面是一个简单的 Python 程序片段展示如何通过 requests 库来进行一次基本的 POST 请求:
```python
import json
import time
from hashlib import sha1
import hmac
import base64
import requests
def create_auth_header(secret_id, secret_key, host='deepseek.api.qcloud.com'):
"""创建签名"""
timestamp = str(int(time.time()))
date = timestamp[:8]
string_to_sign = f'POST{host}/v2/index/search\n'
sign_key = hmac.new(
(secret_key).encode('utf-8'),
(date).encode('utf-8'),
sha1
).digest()
signature = hmac.new(sign_key, string_to_sign.encode('utf-8'), sha1).hexdigest()
auth_header = {
'Host': host,
'X-TC-Timestamp': timestamp,
'X-TC-Signature': signature,
'X-TC-Version': '2021-09-22',
'X-TC-Action': 'Search',
'X-TC-Region': 'ap-guangzhou', # 更改为实际区域
'Authorization': f'TC3-HMAC-SHA256 Credential={secret_id}/{date}, SignedHeaders=content-type;host;x-tc-action;x-tc-region;x-tc-timestamp, Signature={signature}'
}
return auth_header
if __name__ == '__main__':
SECRET_ID = "your_secret_id"
SECRET_KEY = "your_secret_key"
headers = create_auth_header(SECRET_ID, SECRET_KEY)
payload = {"Query": "example query"}
response = requests.post(
url="https://deepseek.api.qcloud.com/v2/index/search",
data=json.dumps(payload),
headers=headers
)
print(response.json())
```
此脚本展示了怎样利用 HMAC 加密算法生成授权字符串,并将其加入至 HTTP 请求头中以便成功调用 Tencent Cloud DeepSeek API 接口。
阅读全文
相关推荐


















