APIGPT.Cloud - DeepSeek 开发文档
你可以通过任何语言的 HTTP 请求与 API 进行交互,还可以使用OpenAI的官方 Python 库来调用 DeepSeek 模型。
要安装 OpenAI 库,请运行以下命令:
pip install openai
本节示例所使用的 openai 库版本为 1.65.2。
当您在API中需要开启使用stream模式时,推荐用OpenAI官方的SDK来调用接口,避免繁琐的数据解析问题。
01 认证
DeepSeek API 使用 API 密钥进行身份验证。请访问你的 App 页面,以获取你在请求中使用的 API 密钥。
请记住,你的 API 密钥是一个秘密!不要与他人分享它,也不要在任何客户端代码(浏览器、应用程序)中公开它。生产请求必须通过你自己的后端服务器路由,你的 API 密钥可以从环境变量或密钥管理服务中安全加载。
所有 API 请求都应该在 Authorization
HTTP 头中包含你的 API 密钥,如下所示:
Authorization: Bearer <替换成从APIGPT.CLOUD创建的OpenAI APP Key>
02 发送请求
你可以将下面的命令粘贴到终端中以运行你的第一个 API 请求。请确保将
发送你的第一个 API 请求
curl https://openai.pgpt.cloud/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API_KEY>" \
-d '{
"model": "deepseek-v3",
"messages": [{"role": "user", "content": "Say this message is from apigpt.cloud!"}],
"temperature": 0.7
}'
#你也可以用Python代码来发送第一个 API 请求
from openai import OpenAI
client = OpenAI(
api_key='<API_KEY>',
base_url='https://openai.pgpt.cloud/v1'
)
stream = False
messages = [{"role": "user", "content": "Say this message is from apigpt.cloud!"}]
response = client.chat.completions.create(
model="deepseek-v3",
messages=messages,
stream=stream
)
if stream:
for chunk in response:
if chunk.choices:
delta = chunk.choices[0].delta
print(delta)
else:
print(response.to_dict())
这个请求查询了 deepseek-v3 模型,以完成以 "Say this message is from apigpt.cloud!" 为提示开始的文本。
如果你会收到类下面JSON格式的数据响应,这说明你的请求成功了
{
"id": "021740967590681d10720ce972e21dad49b43df6863a94e8b7a25",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "Sure! Here's how you can phrase it:\n\n\"Hello! This message is from apigpt.cloud!\" \n\nLet me know if you'd like to customize it further!",
"role": "assistant"
}
}
],
"created": 1740967592,
"model": "deepseek-v3-241226",
"object": "chat.completion",
"service_tier": "default",
"usage": {
"completion_tokens": 35,
"prompt_tokens": 13,
"total_tokens": 48,
"completion_tokens_details": {
"reasoning_tokens": 0
},
"prompt_tokens_details": {
"cached_tokens": 0
}
我们可以在收到的请求里看到 finish_reason
是 stop
,这意味着 API 返回了模型生成的完整完成。在上面的请求中,我们只生成了一条消息,但是你可以设置 n 参数来生成多个消息选项。
03 Chat APIs
给定一个包含对话的消息列表,模型将返回一个响应。
API - Create chat completion
POST https://openai.pgpt.cloud/v1/chat/completions
为给定的聊天对话创建一个模型响应。
Request body
Create chat completion 请求示范
from openai import OpenAI
client = OpenAI(
api_key='<API_KEY>',
base_url='https://openai.pgpt.cloud/v1'
)
response = client.chat.completions.create(
model="deepseek-r1",
messages=messages,
stream=stream
)
print(response.to_dict())
curl https://openai.pgpt.cloud/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API_KEY>" \
-d '{
"model": "deepseek-r1",
"messages": [{"role": "user", "content": "You are a helpful assistant."}],
"temperature": 0.7
}'
Create chat completion 请求示范打印结果
{
"id": "021740969997728ad511188aa801ef7d22624728e6fa2c9d2c28d",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "\n\nHello! How can I assist you today?",
"role": "assistant",
"reasoning_content": "Okay, the user greeted me with \"Hello!\". I should respond in a friendly and welcoming manner. Maybe say something like, \"Hello! How can I assist you today?\" to invite them to ask for help. Keep it open-ended so they feel comfortable to ask anything they need. Let me make sure there are no typos and the tone is right.\n"
}
}
],
"created": 1740970000,
"model": "deepseek-r1-250120",
"object": "chat.completion",
"service_tier": "default",
"usage": {
"completion_tokens": 83,
"prompt_tokens": 13,
"total_tokens": 96,
"completion_tokens_details": {
"reasoning_tokens": 73
},
"prompt_tokens_details": {
"cached_tokens": 0
}
}
}
参数 - model string
Required
要使用的模型ID。目前我们支持并推荐用 deepseek-v3
, deepseek-r1
.
其中 deepseek-r1
是推理模型,返回的消息响应中会包含推理思维数据,包含在 response.choices[0].message.reasoning_content
字段中。
参数 - messages array
Required
到目前为止,对话包含的消息列表
消息 message
的数据结构:
参数 | 类型 | 是否必须 | 描述 |
---|---|---|---|
role | string |
Required |
消息作者的角色。其中之一是system 、user 、assistant 。 |
content | string |
Optional |
消息的内容。除了带有函数调用的assistant ,所有消息都需要 content 。 |
name | string |
Optional |
此 content 作者的姓名。姓名可以包含a-z、A-Z、0-9和下划线,最长长度为64个字符。 |
参数 - temperature number
Optional Defaults to 1
要使用的采样温度,介于0和2之间。较高的值(如0.8)会使输出更加随机,而较低的值(如0.2)会使输出更加集中和确定性。
参数 stream boolean
Optional Defaults to false
如果设置了此选项,将发送部分消息增量,就像在 ChatGPT 中一样。令牌将作为数据类型的服务器发送的事件逐步发送,一旦可用,流将以 data: [DONE] 消息终止。
参数 max_tokens integer
Optional Defaults to inf
在聊天补全中生成的最大令牌数。 输入令牌和生成令牌的总长度受模型上下文长度的限制。
错误代码
Error Code | Meaning |
---|---|
400 | Invalid request: there was an issue with the format or content of your request. |
401 | Unauthorized: there's an issue with your API key. |
403 | Forbidden: your API key does not have permission to use the specified resource. |
404 | Not found: the requested resource was not found. |
429 | Your account has hit a rate limit. |
500 | An unexpected error has occurred internal to Anthropic's systems. |
529 | Your API is temporarily overloaded. |
When receiving a streaming response via SSE, it's possible that an error can occur after returning a 200 response, in which case error handling wouldn't follow these standard mechanisms.