Appearance
聊天对话
InfiFlow 提供标准聊天对话接口,兼容 OpenAI API 格式,支持流式(SSE)和非流式响应。通过该接口可统一调用 DeepSeek、GLM 等主流 AI 模型。
1. 请求地址
POSThttps://llm.infiflow.cn/v1/chat/completions
注意
大部分兼容 OpenAI 接口标准的 AI 工具在使用服务接入地址时,可能需要在地址后添加 /v1 或 /v1/chat/completions,具体以工具要求为准,具体请参考工具文档或联系客服获取支持。
2. 请求参数
| 参数名 | 类型 | 必填 | 默认值 | 描述 |
|---|---|---|---|---|
| model | string | 是 | 无 | 要使用的模型 ID,如 deepseek-v3.2、glm-4 等,可在模型广场查看完整列表 |
| messages | array[object] | 是 | 无 | 对话消息列表,每条消息包含 role 和 content(详见下方 messages 参数说明) |
| temperature | number | 否 | 1.0 | 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。建议改变此参数或 top_p,但不要两者都改 |
| top_p | number | 否 | 1.0 | 核采样参数,模型考虑具有 top_p 概率质量的标记的结果。0.1 意味着只考虑构成前 10% 概率质量的标记。建议改变此参数或 temperature,但不要两者都改 |
| n | integer | 否 | 1 | 为每个输入消息生成多少个聊天完成选项 |
| stream | boolean | 否 | false | 如果设置,将发送部分消息增量(SSE)。当令牌可用时,令牌将作为纯数据服务器发送事件 data: 发送,流由消息 data: [DONE] 终止 |
| stop | string/array | 否 | 无 | API 将停止生成更多令牌的最多 4 个序列 |
| max_tokens | integer | 否 | 无 | 聊天完成时生成的最大令牌数。输入标记和生成标记的总长度受模型上下文长度的限制 |
| presence_penalty | number | 否 | 0 | -2.0 到 2.0 之间的数字。正值会根据到目前为止是否出现在文本中来惩罚新标记,从而增加模型谈论新主题的可能性 |
| frequency_penalty | number | 否 | 0 | -2.0 到 2.0 之间的数字。正值会根据新标记在文本中的现有频率对其进行惩罚,从而降低模型逐字重复同一行的可能性 |
| logit_bias | object | 否 | 无 | 修改指定标记出现在完成中的可能性。接受一个 JSON 对象,该对象将标记 ID 映射到从 -100 到 100 的关联偏差值。-1 到 1 之间的值会减少或增加选择的可能性;-100 或 100 会导致相关令牌的禁止或独占选择 |
| user | string | 否 | 无 | 代表您的最终用户的唯一标识符,用于监控和检测滥用行为 |
2.1. messages 参数说明
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
| role | string | 是 | 消息角色,可选值:system(系统设定)、user(用户)、assistant(助手) |
| content | string | 是 | 消息内容 |
3. 请求示例
3.1. OpenAI 兼容格式
bash
curl https://llm.infiflow.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxx" \
-d '{
"model": "deepseek-v3.2",
"messages":[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello"}
],
"temperature": 0.7,
"stream": true
}'powershell
$body = @{
model = "deepseek-v3.2"
messages = @(
@{role = "system"; content = "You are a helpful assistant."}
@{role = "user"; content = "Hello"}
)
temperature = 0.7
stream = $true
} | ConvertTo-Json
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer sk-xxxxxx"
}
$response = Invoke-RestMethod -Uri "https://llm.infiflow.cn/v1/chat/completions" `
-Method Post -Headers $headers -Body $body -ContentType "application/json"
$response.choices[0].message.contentpython
import requests
import json
url = "https://llm.infiflow.cn/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxxx"
}
data = {
"model": "deepseek-v3.2",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello"}
],
"temperature": 0.7,
"stream": True
}
response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result["choices"][0]["message"]["content"])java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class ChatCompletion {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
JsonObject message1 = new JsonObject();
message1.addProperty("role", "system");
message1.addProperty("content", "You are a helpful assistant.");
JsonObject message2 = new JsonObject();
message2.addProperty("role", "user");
message2.addProperty("content", "Hello");
JsonArray messages = new JsonArray();
messages.add(message1);
messages.add(message2);
JsonObject requestBody = new JsonObject();
requestBody.addProperty("model", "deepseek-v3.2");
requestBody.add("messages", messages);
requestBody.addProperty("temperature", 0.7);
requestBody.addProperty("stream", true);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://llm.infiflow.cn/v1/chat/completions"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer sk-xxxxxx")
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}vue
<template>
<div>
<p>{{ response }}</p>
</div>
</template>
<script>
import axios from "axios";
export default {
data() {
return {
response: ""
};
},
methods: {
async sendMessage() {
try {
const res = await axios.post(
"https://llm.infiflow.cn/v1/chat/completions",
{
model: "deepseek-v3.2",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello" }
],
temperature: 0.7,
stream: false
},
{
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxxx"
}
}
);
this.response = res.data.choices[0].message.content;
} catch (error) {
console.error("请求失败", error);
}
}
}
};
</script>javascript
const url = "https://llm.infiflow.cn/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxxx"
};
const body = {
model: "deepseek-v3.2",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello" }
],
temperature: 0.7,
stream: false
};
fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(body)
})
.then(response => response.json())
.then(data => {
console.log(data.choices[0].message.content);
})
.catch(error => {
console.error("请求失败", error);
});csharp
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class ChatCompletion
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer sk-xxxxxx");
JObject requestBody = new JObject
{
{ "model", "deepseek-v3.2" },
{ "messages", new JArray
{
new JObject { { "role", "system" }, { "content", "You are a helpful assistant." } },
new JObject { { "role", "user" }, { "content", "Hello" } }
}
},
{ "temperature", 0.7 },
{ "stream", true }
};
string json = JsonConvert.SerializeObject(requestBody);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(
"https://llm.infiflow.cn/v1/chat/completions", content);
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
}
}vb
Imports System.Net.Http
Imports System.Text
Imports Newtonsoft.Json.Linq
Module ChatCompletion
Sub Main()
Dim client As New HttpClient()
client.DefaultRequestHeaders.Add("Authorization", "Bearer sk-xxxxxx")
Dim requestBody As New JObject()
requestBody("model") = "deepseek-v3.2"
Dim messages As New JArray()
Dim msg1 As New JObject()
msg1("role") = "system"
msg1("content") = "You are a helpful assistant."
messages.Add(msg1)
Dim msg2 As New JObject()
msg2("role") = "user"
msg2("content") = "Hello"
messages.Add(msg2)
requestBody("messages") = messages
requestBody("temperature") = 0.7
requestBody("stream") = True
Dim json As String = requestBody.ToString()
Dim content As New StringContent(json, Encoding.UTF8, "application/json")
Dim response As HttpResponseMessage = client.PostAsync(
"https://llm.infiflow.cn/v1/chat/completions", content).Result
Dim result As String = response.Content.ReadAsStringAsync().Result
Console.WriteLine(result)
End Sub
End Modulejava
import System.*;
import System.Net.*;
import System.IO.*;
import System.Text.*;
public class ChatCompletion
{
public static void main(String[] args)
{
try
{
String url = "https://llm.infiflow.cn/v1/chat/completions";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.set_Method("POST");
request.set_ContentType("application/json");
request.get_Headers().Add("Authorization", "Bearer sk-xxxxxx");
String jsonBody = "{\"model\":\"deepseek-v3.2\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a helpful assistant.\"},{\"role\":\"user\",\"content\":\"Hello\"}],\"temperature\":0.7,\"stream\":true}";
byte[] byteArray = Encoding.get_UTF8().GetBytes(jsonBody);
request.set_ContentLength(byteArray.get_Length());
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.get_Length());
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String result = reader.ReadToEnd();
Console.WriteLine(result);
reader.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("请求失败: " + ex.getMessage());
}
}
}3.2. Claude 原生格式
支持 Anthropic 官方的消息流格式。
POST/v1/messages
bash
curl https://llm.infiflow.cn/v1/messages \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxx" \
-d '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello"}
]
}'3.3. Gemini 原生格式
兼容 Google AI Studio 的 API 结构。
POST/v1beta/models/{model}:generateContent
bash
curl https://llm.infiflow.cn/v1beta/models/{model}:generateContent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxx" \
-d '{
"contents": [{
"parts": [{"text": "Hello"}]
}]
}'4. 响应示例
4.1. 非流式响应
json
{
"id": "chatcmpl-9a8b7c6d5e4f3g2h1i0j",
"object": "chat.completion",
"created": 1698864000,
"model": "deepseek-v3.2",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 18,
"completion_tokens": 10,
"total_tokens": 28
}
}4.2. 流式响应(SSE)
text
data: {"id":"chatcmpl-9a8b7c6d","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"id":"chatcmpl-9a8b7c6d","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]}
data: {"id":"chatcmpl-9a8b7c6d","object":"chat.completion.chunk","choices":[{"delta":{"content":"! How can I help you today?"},"index":0}]}
data: {"id":"chatcmpl-9a8b7c6d","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop","index":0}]}
data: [DONE]5. 响应参数说明
| 参数名 | 类型 | 描述 |
|---|---|---|
| id | string | 对话的唯一标识符 |
| object | string | 对象类型,非流式时为 chat.completion;流式时为 chat.completion.chunk |
| created | integer | 创建时间戳(Unix 时间戳) |
| model | string | 使用的模型 ID |
| choices | array | 对话完成选项列表 |
| choices[].index | integer | 选项索引 |
| choices[].message | object | 非流式响应下的消息对象,包含 role 和 content |
| choices[].delta | object | 流式响应下的增量消息对象,包含 role 或 content |
| choices[].finish_reason | string | 完成原因,可选值:stop(正常结束)、length(达到长度限制)、null(流式传输中) |
| usage.prompt_tokens | integer | 提示词消耗的 token 数量 |
| usage.completion_tokens | integer | 补全消耗的 token 数量 |
| usage.total_tokens | integer | 总消耗 token 数量 |
6. 常见错误码
| 状态码 | 描述 | 排查建议 |
|---|---|---|
| 400 | 请求参数错误 | 检查 model 和 messages 参数是否正确 |
| 401 | 认证失败 | 检查 API 密钥是否有效,可在控制中心的API密钥页面重新生成 |
| 429 | 请求频率过高 | 降低请求频率,或前往账户充值提升调用配额 |
| 500 | 服务端内部错误 | 请稍后重试,如持续出现请联系技术支持 |
注意事项:
- 流式响应(stream=true)使用 Server-Sent Events(SSE)协议,客户端需逐行解析
data:前缀的数据 - 建议设置合理的
temperature值,对话任务推荐 0.7 左右,代码生成任务推荐 0.2 左右 - 所有请求均需在 Header 中携带有效的 Bearer Token,请妥善保管您的 API 密钥
- 可在费用明细页面查看 API 调用的详细消耗记录