接口说明
对话补全(Chat)
POST https://api.scnet.cn/api/llm/v1/chat/completions
1.功能介绍
根据输入的上下文,来让模型补全对话内容。
2.请求参数
Header 参数
名称 | 类型 | 必需 | 示例值 |
---|---|---|---|
Content-Type | string | 是 | application/json |
Authorization | string | 是 | Bearer |
Body 参数
名称 | 类型 | 必需 | 默认值 | 描述 |
---|---|---|---|---|
model | string | 是 | \ | 使用的模型的 ID。您可以使用DeepSeek-R1-Distill-Qwen-7B和DeepSeek-R1-Distill-Qwen-32B。 |
messages | array | 是 | \ | 表示聊天上下文信息。 您必须在消息数组中提供一个或多个消息。每个消息必须与system、user或assistant中的一个角色相关联。 角色:表示消息作者的角色。根据角色,模型了解它应该如何行为以及谁在发起调用。 System:这个角色通常负责设定模型的行为和规则。它可以提供上下文信息,定义任务的范围,或者设定对话的主题。 User:用户角色代表与模型进行交互的实际用户。用户提出问题、请求信息或寻求帮助,模型根据用户的输入进行响应。 Assistant:助手角色是模型本身,负责理解用户的输入并生成相应的输出。 其中,system 角色可选,如存在则必须位于列表的最开始。user 和 assistant 需交替出现,以 user 提问开始,user 提问结束,Content 不能为空。Role 的顺序示例:[system(可选) user assistant user assistant user ...]。 |
max_tokens | integer | 否 | 4096 | 介于 1 到 8192 间的整数,限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制。如未指定 max_tokens参数,默认使用 4096。 |
temperature | number | 否 | 1 | 采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2) 将使输出更加集中和确定。 对于DeepSeek-R1系列模型,官方给出使用建议如下: temperature 设置在 0.5-0.7 之间,推荐 0.6,防止输出重复或不连贯。 |
top_p | number | 否 | 1 | top的值,介于 0 和 1 之间。作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果。所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改。 不支持以下模型(为了兼容已有软件,设置该参数后不会报错,但也不会生效): DeepSeek-R1-Distill系列 |
stop | object | 否 | null | 生成停止标识,当模型生成结果以stop中某个元素结尾时,停止文本生成。 |
stream | boolean | 否 | false | 流式响应的选项,当字段stream为true时,该字段生效。 |
response_format | object | 否 | text | 指定模型必须输出的格式。设置为 { "type": "json_object" } 以启用 JSON 模式,该模式保证模型生成的消息是有效的 JSON。 注意: 使用 JSON 模式时,你还必须通过系统或用户消息指示模型生成 JSON。否则,模型可能会生成不断的空白字符,直到生成达到令牌限制,从而导致请求长时间运行并显得“卡住”。此外,如果 finish_reason="length",这表示生成超过了 max_tokens 或对话超过了最大上下文长度,消息内容可能会被部分截断。 |
frequency_penalty | number | 否 | 0 | 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性。 不支持以下模型(为了兼容已有软件,设置该参数后不会报错,但也不会生效): DeepSeek-R1-Distill系列 |
presence_penalty | number | 否 | 0 | 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。 不支持以下模型(为了兼容已有软件,设置该参数后不会报错,但也不会生效): DeepSeek-R1-Distill系列 |
3.响应参数
名称 | 类型 | 必需 | 描述 |
---|---|---|---|
id | string | 是 | 该对话的唯一标识符。 |
choices | object[] | 是 | 模型生成的 completion 的选择列表,object[]包含以下三个参数: ● finish_reason(string),模型停止生成 token 的原因。必需。 ○ stop:模型自然停止生成,或遇到 stop 序列中列出的字符串。 ○ length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制。 ○ content_filter:输出内容因触发过滤策略而被过滤。 ○ insufficient_system_resource:系统推理资源不足,生成被打断。 ● index(integer),该 completion 在模型生成的 completion 的选择列表中的索引。必需。 ● message(object),模型生成的 completion 消息。必需。 ○ content(string),该 completion 的内容。必需,可为空值。 ○ reasoning_content(string),仅适用于 deepseek-reasoner 模型。内容为 assistant 消息中在最终答案之前的推理内容。非必需。 ○ role(string),生成这条消息的角色。必需。 |
created | integer | 是 | 创建聊天完成时的 Unix 时间戳(以秒为单位)。 |
model | string | 是 | 生成该 completion 的模型名。 |
system_fingerprint | string | 是 | 代表模型运行的后端配置。 |
object | string | 是 | 对象的类型, 其值为 chat.completion。 |
usage | object | 是 | Token 统计信息。 |
4.请求示例
cURL请求示例
curl -L -X POST 'https://api.scnet.cn/api/llm/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <API Key>' \
--data-raw '{
"messages": [
{
"content": "You are a helpful assistant",
"role": "system"
},
{
"content": "Hi",
"role": "user"
}
],
"model": "DeepSeek-R1-Distill-Qwen-7B",
"frequency_penalty": 0,
"max_tokens": 2048,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"stop": null,
"stream": false,
"temperature": 1,
"top_p": 1
}'
Python请求示例
- OpenAI SDK
from openai import OpenAI
client = OpenAI(api_key="<API Key>", base_url="https://api.scnet.cn/api/llm/v1")
response = client.chat.completions.create(
model="DeepSeek-R1-Distill-Qwen-7B",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
max_tokens=1024,
temperature=0.7,
stream=False
)
print(response.choices[0].message.content)
- REQUESTS
import requests
import json
url = "https://api.scnet.cn/api/llm/v1/chat/completions"
payload = json.dumps({
"messages": [
{
"content": "You are a helpful assistant",
"role": "system"
},
{
"content": "Hi",
"role": "user"
}
],
"model": "DeepSeek-R1-Distill-Qwen-7B",
"frequency_penalty": 0,
"max_tokens": 2048,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"stop": None,
"stream": False,
"temperature": 1,
"top_p": 1
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <API Key>'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
- HTTP.CLIENT
import http.client
import json
conn = http.client.HTTPSConnection("api.scnet.cn")
payload = json.dumps({
"messages": [
{
"content": "You are a helpful assistant",
"role": "system"
},
{
"content": "Hi",
"role": "user"
}
],
"model": "DeepSeek-R1-Distill-Qwen-7B",
"frequency_penalty": 0,
"max_tokens": 2048,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"stop": None,
"stream": False,
"temperature": 1,
"top_p": 1
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer <API Key>'
}
conn.request("POST", "/api/llm/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Go请求示例
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.scnet.cn/api/llm/v1/chat/completions"
method := "POST"
payload := strings.NewReader(`{
"messages": [
{
"content": "You are a helpful assistant",
"role": "system"
},
{
"content": "Hi",
"role": "user"
}
],
"model": "DeepSeek-R1-Distill-Qwen-7B",
"frequency_penalty": 0,
"max_tokens": 2048,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"stop": null,
"stream": false,
"temperature": 1,
"top_p": 1
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer <API Key>")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
Nodejs请求示例
- OpenAI SDK
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: 'https://api.scnet.cn/api/llm/v1',
apiKey: '<API Key>'
});
async function main() {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "DeepSeek-R1-Distill-Qwen-7B",
});
console.log(completion.choices[0].message.content);
}
main();
5.响应示例
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Hello! How can I help you today?",
"role": "assistant"
}
}
],
"created": 1740036498,
"id": "39bff8f688377b9e0c73564ab2fae566",
"model": "DeepSeek-R1-Distill-Qwen-7B",
"object": "chat.completion",
"usage": {
"completion_tokens": 10,
"prompt_tokens": 16,
"total_tokens": 26
}
}