tencent cloud

腾讯云 DeepSeek OpenAI 对话接口
最后更新时间:2025-11-25 11:51:09
腾讯云 DeepSeek OpenAI 对话接口
最后更新时间: 2025-11-25 11:51:09
腾讯云知识引擎原子能力 DeepSeek OpenAI 对话接口兼容了 OpenAI 的接口规范,这意味着您可以直接使用 OpenAI 官方提供的 SDK 来调用。您仅需要将 base_url 和 api_key 替换成相关配置,不需要对应用做额外修改,即可无缝将您的应用切换到相应的大模型。
base_url:https://api.lkeap.tencentcloud.com/v1
api_key:需在控制台 API KEY页面 进行创建,操作步骤请参考 快速入门
接口请求地址完整路径:https://api.lkeap.tencentcloud.com/v1/chat/completions
调用情况可在 控制台 > 数据报表 中查看。计费详情请参见 计费概述

在线体验

如您希望在网页内直接体验 DeepSeek 模型对话,推荐您前往 腾讯云智能体开发平台,创建对话应用。

已支持的模型

DeepSeek R1模型

模型
参数量
最大上下文长度
最大输入长度
最大输出长度
DeepSeek-R1
671B
48k
16k
32k
DeepSeek-R1-0528
671B
48k
16k
32k

DeepSeek V3模型

模型
参数量
最大上下文长度
最大输入长度
最大输出长度
DeepSeek-V3
671B
48k
16k
32k

DeepSeek V3.1-Terminus 模型

模型
参数量
最大上下文长度
最大输入长度
最大输出长度
DeepSeek-V3.1-Terminus
685B
128k
96k
32k

DeepSeek-R1(model 参数值为 deepseek-r1)

DeepSeek-R1 为671B 模型,使用强化学习训练,推理过程包含大量反思和验证,思维链长度可达数万字。 该系列模型在数学、代码以及各种复杂逻辑推理任务上推理效果优异,并为用户展现了完整的思考过程。

DeepSeek-R1-0528(model 参数值为 deepseek-r1-0528)

DeepSeek-R1-0528为671B 模型,架构优化与训练策略升级后,相比上一版本在代码生成、长文本处理和复杂推理领域提升明显。

DeepSeek-V3(model 参数值为 deepseek-v3)

DeepSeek-V3 模型为 0324 发布的最新版本。DeepSeek-V3 为671B 参数 MoE 模型,在百科知识、数学推理等多项任务上优势突出。

DeepSeek-V3.1-Terminus(model 参数值为 deepseek-v3.1-terminus)

DeepSeek-V3.1-Terminus 为685B 参数 MoE 模型,在保持模型原有能力的基础上,优化了语言一致性,Agent 能力等问题,输出效果相比前一版本更加稳定。

快速开始

API 使用前提:已在腾讯云控制台 API Key管理 开通知识引擎原子能力并创建 API Key。
如果您首次使用知识引擎原子能力,请参考 快速入门 进行知识引擎原子能力的开通,并将示例代码中的 model 参数修改为上表中您需要调用的模型名称。
由于 deepseek-r1 模型的思考过程可能较长,可能导致响应慢或超时,建议您优先使用流式输出方式调用。

安装SDK

您需要确保已安装 Python 3.8或以上版本。
安装或更新 OpenAI Python SDK
运行以下命令:
pip install -U openai
如果运行失败,请将 pip 改为pip3。

示例代码片段

非流式请求

Python
cURL

import os
from openai import OpenAI

client = OpenAI(
api_key="LKEAP_API_KEY",
base_url="https://api.lkeap.tencentcloud.com/v1",
)

completion = client.chat.completions.create(
model="deepseek-r1",
messages=[
{'role': 'user', 'content': 'Which is greater, 9.9 or 9.11?'}
]
)

print("reasoning_content:")
print(completion.choices[0].message.reasoning_content)
print("content:")
print(completion.choices[0].message.content)
curl https://api.lkeap.tencentcloud.com/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer sk-xxxxxxxxxxx" \\
-d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "hello"
}
],
"stream": false
}'

多轮对话

大模型知识引擎提供的 DeepSeek API 默认不会记录您的历史对话信息。多轮对话功能可以让大模型“拥有记忆”,满足如追问、信息采集等需要连续交流的场景。如果您使用 deepseek-r1 模型,会收到 reasoning_content 字段(思考过程)与 content(回复内容),您可以将 content 字段通过{'role': 'assistant', 'content':API 返回的 content} 添加到上下文,无需添加 reasoning_content 字段。
Python
cURL
import os
from openai import OpenAI
client = OpenAI(
api_key="LKEAP_API_KEY",
base_url="https://api.lkeap.tencentcloud.com/v1",
)

messages = [
{'role': 'user', 'content': 'hello'},
{'role': 'assistant', 'content': 'hi, how can I assist you?'},
{'role': 'user', 'content': 'Which is greater, 9.9 or 9.11?'}
]

completion = client.chat.completions.create(
model="deepseek-r1",
messages=messages
)

print("="*20+"first-round dialogue"+"="*20)
print("="*20+"reasoning_content"+"="*20)
print(completion.choices[0].message.reasoning_content)
print("="*20+"content"+"="*20)
print(completion.choices[0].message.content)

messages.append({'role': 'assistant', 'content': completion.choices[0].message.content})
messages.append({'role': 'user', 'content': 'who are you'})
print("="*20+"second-round dialogue"+"="*20)
completion = client.chat.completions.create(
model="deepseek-r1",
messages=messages
)
print("="*20+"reasoning_content"+"="*20)
print(completion.choices[0].message.reasoning_content)
print("="*20+"content"+"="*20)
print(completion.choices[0].message.content)
curl https://api.lkeap.tencentcloud.com/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer sk-xxxxxxxxxxx" \\
-d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "hello"
},
{
"role": "assistant",
"content": "hi, how can I assist you?"
},
{
"role": "user",
"content": "Which is greater, 9.9 or 9.11?"
}
],
"stream": true
}'

流式输出

deepseek-r1 模型可能会输出较长的思考过程,为了降低超时风险,建议您使用流式输出方式调用 deepseek-r1 模型。
Python
cURL
from openai import OpenAI
import os

client = OpenAI(
api_key="LKEAP_API_KEY",
base_url="https://api.lkeap.tencentcloud.com/v1",
)

def main():
reasoning_content = ""
answer_content = ""
is_answering = False
# Create chat completion request
stream = client.chat.completions.create(
model="deepseek-r1",
messages=[
{"role": "user", "content": "Which is greater, 9.9 or 9.11?"}
],
stream=True
)

print("\\n" + "=" * 20 + "reasoning processes" + "=" * 20 + "\\n")

for chunk in stream:
# Process Usage Information
if not getattr(chunk, 'choices', None):
print("\\n" + "=" * 20 + "Token usage" + "=" * 20 + "\\n")
print(chunk.usage)
continue

delta = chunk.choices[0].delta

if not getattr(delta, 'reasoning_content', None) and not getattr(delta, 'content', None):
continue

if not getattr(delta, 'reasoning_content', None) and not is_answering:
print("\\n" + "=" * 20 + "reasoning_content" + "=" * 20 + "\\n")
is_answering = True

if getattr(delta, 'reasoning_content', None):
print(delta.reasoning_content, end='', flush=True)
reasoning_content += delta.reasoning_content
elif getattr(delta, 'content', None):
print(delta.content, end='', flush=True)
answer_content += delta.content

if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"error:{e}")
curl https://api.lkeap.tencentcloud.com/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer sk-xxxxxxxxxxx" \\
-d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "hello"
}
],
"stream": true
}'

Function Calling

DeepSeek-V3 现已支持 Function Calling,调用流程图如下(以获取当地天气为例):




使用指南:

Function call 是指模型在回答用户问题时,调用外部函数或API来获得信息或与外部系统交互的能力。
Function call 使用流程(以获取某地温度举例):
1. 定义函数 get_weather。
2. 第一次请求对话接口:传递用户问题和函数定义(方法名称、描述、参数列表),由模型选择合适的函数并填充函数的参数。
3. 第一次接收模型的响应:在客户侧调用函数获得结果(函数需要由客户的代码来调用,模型只会给出要调用的函数和参数而不会执行调用)。
4. 第二次请求对话接口:在第一次的请求基础上,附加第一次的响应和函数调用的结果,放到多轮上下文中,再次请求模型。
5. 第二次接收模型的响应:模型生成最终结果。
第一次请求示例:

```python
curl --location 'https://api.lkeap.tencentcloud.com/v1/chat/completions' \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer $API_KEY" \\
--data '{
"model": "deepseek-v3",
"messages": [
{
"role": "user",
"content": "What's the weather like in Paris today?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
}

},
"required": [
"latitude","longitude"
]
}
}
}
]
}'
```

第一次响应示例:

JSON

```json
{
"id": "cabe32d2b30dd30ac3d7aad02f235dd4",
"choices": [
{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"content": "调用天气查询工具(get_weather)来获取巴黎的天气信息。\\n\\t\\n\\t用户想要知道今天巴黎的天气。我需要调用天气查询工具(get_weather)来获取巴黎的天气信息。",
"role": "assistant",
"tool_calls": [
{
"id": "call_cvdrgkk2c3mceb26d7sg",
"function": {
"arguments": "{\\"latitude\\":48.8566,\\"longitude\\":2.3522}",
"name": "get_weather"
},
"type": "function",
"index": 0
}
]
}
}
],
"created": 1742452818,
"model": "hunyuan-turbos-latest",
"object": "chat.completion",
"system_fingerprint": "",
"usage": {
"completion_tokens": 48,
"prompt_tokens": 22,
"total_tokens": 70
}
}
```

第二次请求示例:

```python
curl --location 'https://api.lkeap.tencentcloud.com/v1/chat/completions' \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer $API_KEY" \\
--data '{
"model": "deepseek-v3",
"messages": [
{
"role": "user",
"content": "What's the weather like in Paris today?"
},
{
"role": "assistant",
"content": "调用天气查询工具(get_weather)来获取巴黎的天气信息。\\n\\t\\n\\t用户想要知道今天巴黎的天气。我需要调用天气查询工具(get_weather)来获取巴黎的天气信息。",
"tool_calls": [
{
"id": "call_cvdu67s2c3mafqgr1g6g",
"function": {
"arguments": "{\\"latitude\\":48.8566,\\"longitude\\":2.3522}",
"name": "get_weather"
},
"type": "function",
"index": 0
}
]
},
{
"role": "tool",
"tool_call_id": "call_cvdu67s2c3mafqgr1g6g",
"content": "11.7"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
}

},
"required": [
"latitude","longitude"
]
}
}
}
]
}'
```

第二次响应示例:

JSON

```json
{
"id": "b03283653e27bc78a9c095699cfbc123",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The current temperature in Paris is 7.6°C.",
"role": "assistant"
}
}
],
"created": 1742453367,
"model": "hunyuan-turbos-latest",
"object": "chat.completion",
"system_fingerprint": "",
"usage": {
"completion_tokens": 24,
"prompt_tokens": 71,
"total_tokens": 95
},
"note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记"
}
```

之后可以继续进行对话。
注意:
OpenAPI 兼容格式的调用方式下,仅 V3 系列模型支持 Function Calling 功能,R1 模型暂不支持。

深度思考

只要调用 deepseek-r1 模型即代表开启深度思考(深度思考过程通过 reasoning_content 返回)。

注意事项

稳定性

若执行后出现“concurrency exceeded”的响应,则表明您的请求遭遇了限流。这通常是由于服务器资源暂时不足所致。建议您稍后再试,届时服务器负载可能已得到缓解。

DeepSeek-R1

不支持设置的参数和功能
Function Calling、JSON Output、对话前缀续写、上下文硬盘缓存。
不支持的参数
presence_penalty、frequency_penalty、logprobs、top_logprobs。
支持的参数
top_p、temperature、max_tokens。
参数默认值
temperature:0.6(取值范围是[0:2])
top_p:0.6(取值范围是(0:1])
不建议设置 System Prompt(来自官方说明)。

DeepSeek-V3

不支持设置的参数和功能
JSON Output、对话前缀续写、上下文硬盘缓存。
不支持的参数
presence_penalty、frequency_penalty、logprobs、top_logprobs。
支持的参数以及功能
top_p、temperature、max_tokens 参数。
Function Calling 功能
支持 tools 参数。
支持 tool_choice 参数(支持 auto、none、Forced Function(不支持 required 功能))。
参数默认值
temperature:0.6(取值范围是[0:2])
top_p:0.6(取值范围是(0:1])

DeepSeek-V3.1-Terminus

不支持设置的参数和功能
Function Calling、对话前缀续写、上下文硬盘缓存。
不支持的参数
logprobs、top_logprobs。
支持的参数以及功能
top_p、temperature、max_tokens、presence_penalty、frequency_penalty。
JSON Output 功能
支持 json_object 模式。
参数默认值
temperature:0.6(取值范围是[0:2])
top_p:0.6(取值范围是(0:1])
敬请关注后续动态。

错误码

错误码
错误信息
说明
20031
not enough quota
您的账号目前没有可用资源。为了继续使用,请在 后付费设置 打开后付费开关或前往 购买页 购买预付费并发包。
20034
concurrency exceeded
您的请求遭遇了限流。这通常是由于服务器资源暂时不足所致。建议您稍后再试,届时服务器负载可能已得到缓解。
20059
input content too long
输入长度超过上下文长度,请减小输入内容的长度

错误示例

{"error":{"message":"not enough quota","type":"runtime_error","param":null,"code":"20031"}}

本页内容是否解决了您的问题?
您也可以 联系销售 提交工单 以寻求帮助。

文档反馈