API 手冊
建立文字對話請求
Content-Type:
application/json
請求參數說明
Authorization
- 類型
string
- 位置
header
- 是否必填是
- 描述使用以下格式進行身份驗證:
Bearer <your api key>
(訪問 iFlow 官網登入獲取 API KEY)。
LLM 模型
參數名 | 類型 | 是否必填 | 預設值 | 描述 |
---|---|---|---|---|
messages | object[] | 是 | - | 構成當前對話的訊息列表。 |
messages.content | string | 是 | 2025年中國大模型行業將會迎來哪些機遇和挑戰? | 訊息的內容。 |
messages.role | enum<string> | 是 | user | 訊息作者的角色。 可選值:user , assistant , system |
model | enum<string> | 是 | deepseek-r1 | 對應的模型名稱。 為更好的提升服務品質,我們將不定期對本服務提供的模型做相關變更,包括但不限於模型上下線、模型服務能力調整,我們會在可行的情況下以公告、訊息推送等適當的方式進行通知。 支援的模型請參考快速開始頁面。 |
frequency_penalty | number | 否 | 0.5 | 調整生成 token 的頻率懲罰,用於控制重複性。 |
max_tokens | integer | 否 | 512 | 生成的最大 token 數量。 取值範圍:1 < x < 8192 |
n | integer | 否 | 1 | 返回的生成結果數量。 |
response_format | object | 否 | - | 指定模型輸出格式的物件。 |
response_format.type | string | 否 | - | 回應格式的類型。 |
stop | string[] | null | 否 | - |
stream | boolean | 否 | false | 如果設定為 true ,token 將作為伺服器發送事件(SSE)逐步返回。 |
temperature | number | 否 | 0.7 | 控制回應的隨機性。值越低,輸出越確定;值越高,輸出越隨機。 |
tools | object[] | 否 | - | 模型可能呼叫的工具列表。目前僅支援函數作為工具。使用此參數提供一個函數列表,模型可能會為其生成 JSON 輸入。最多支援 128 個函數。 |
tools.function | object | 否 | - | 函數物件。 |
tools.function.name | string | 否 | - | 要呼叫的函數名稱。必須由字母、數字、底線或連字號組成,最大長度為 64。 |
tools.function.description | string | 否 | - | 函數的描述,用於模型選擇何時以及如何呼叫該函數。 |
tools.function.parameters | object | 否 | - | 函數接受的參數,描述為 JSON Schema 物件。如果不指定參數,則定義了一個空參數列表的函數。 |
tools.function.strict | boolean | null | 否 | false |
tools.type | enum<string> | 否 | function | 工具的類型。目前僅支援 function 。 |
top_k | number | 否 | 50 | 限制 token 選擇範圍為前 k 個候選。 |
top_p | number | 否 | 0.7 | 核取樣參數,用於根據累積機率動態調整每個預測 token 的選擇範圍。 |
請求舉例
- CURL
- Python
- Javascript
- Java
curl --request POST \
--url https://apis.iflow.cn/v1/chat/completions \
--header 'Authorization: Bearer <iflow API KEY>' \
--header 'Content-Type: application/json' \
--data '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "2025年中國大模型行業將會迎來哪些機遇和挑戰?"
}
],
"stream": false,
"max_tokens": 512,
"stop": [
"null"
],
"temperature": 0.7,
"top_p": 0.7,
"top_k": 50,
"frequency_penalty": 0.5,
"n": 1,
"response_format": {
"type": "text"
},
"tools": [
{
"type": "function",
"function": {
"description": "<string>",
"name": "<string>",
"parameters": {},
"strict": false
}
}
]
}'
import requests
url = "https://apis.iflow.cn/v1/chat/completions"
payload = {
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "2025年中國大模型行業將會迎來哪些機遇和挑戰?"
}
],
"stream": False,
"max_tokens": 512,
"stop": ["null"],
"temperature": 0.7,
"top_p": 0.7,
"top_k": 50,
"frequency_penalty": 0.5,
"n": 1,
"response_format": {"type": "text"},
"tools": [
{
"type": "function",
"function": {
"description": "<string>",
"name": "<string>",
"parameters": {},
"strict": False
}
}
]
}
headers = {
"Authorization": "Bearer <iFlow API KEY>",
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
const url = "https://apis.iflow.cn/v1/chat/completions";
const payload = {
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "2025年中國大模型行業將會迎來哪些機遇和挑戰?"
}
],
"stream": false,
"max_tokens": 512,
"stop": ["null"],
"temperature": 0.7,
"top_p": 0.7,
"top_k": 50,
"frequency_penalty": 0.5,
"n": 1,
"response_format": {"type": "text"},
"tools": [
{
"type": "function",
"function": {
"description": "<string>",
"name": "<string>",
"parameters": {},
"strict": false
}
}
]
};
const headers = {
"Authorization": "Bearer <iFlow API KEY>",
"Content-Type": "application/json"
};
fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => console.log(data));
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
// 建構訊息物件
Map<String, Object> message = new HashMap<>();
message.put("role", "user");
message.put("content", "2025年中國大模型行業將會迎來哪些機遇和挑戰?");
// 建構工具函數物件
Map<String, Object> function = new HashMap<>();
function.put("description", "<string>");
function.put("name", "<string>");
function.put("parameters", new HashMap<>());
function.put("strict", false);
Map<String, Object> tool = new HashMap<>();
tool.put("type", "function");
tool.put("function", function);
// 建構回應格式物件
Map<String, Object> responseFormat = new HashMap<>();
responseFormat.put("type", "text");
// 建構請求參數
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "deepseek-r1");
requestBody.put("messages", Arrays.asList(message));
requestBody.put("stream", false);
requestBody.put("max_tokens", 512);
requestBody.put("stop", Arrays.asList("null"));
requestBody.put("temperature", 0.7);
requestBody.put("top_p", 0.7);
requestBody.put("top_k", 50);
requestBody.put("frequency_penalty", 0.5);
requestBody.put("n", 1);
requestBody.put("response_format", responseFormat);
requestBody.put("tools", Arrays.asList(tool));
// 轉換為JSON字串
ObjectMapper mapper = new ObjectMapper();
String jsonBody = mapper.writeValueAsString(requestBody);
// 發送請求
HttpResponse<String> response = Unirest.post("https://apis.iflow.cn/v1/chat/completions")
.header("Authorization", "Bearer <iFlow API KEY>")
.header("Content-Type", "application/json")
.body(jsonBody)
.asString();
回應參數
- 非串流輸出
- 串流輸出
參數名 | 類型 | 是否必填 | 預設值 | 描述 |
---|---|---|---|---|
choices | object[] | 是 | - | 模型生成的選擇列表。 |
choices.finish_reason | enum<string> | 否 | - | 生成結束的原因。 可選值: - stop : 自然結束。 - eos : 到達句子結束符。 - length : 達到最大 token 長度限制。 - tool_calls : 呼叫了工具(如函數)。 |
choices.message | object | 是 | - | 模型返回的訊息物件。 |
created | integer | 是 | - | 回應生成的時間戳。 |
id | string | 是 | - | 回應的唯一標識符。 |
model | string | 是 | - | 使用的模型名稱。 |
object | enum<string> | 是 | - | 回應類型。 可選值: - chat.completion : 表示這是一個聊天完成回應。 |
tool_calls | object[] | 否 | - | 模型生成的工具呼叫,例如函數呼叫。 |
tool_calls.function | object | 否 | - | 模型呼叫的函數。 |
tool_calls.function.arguments | string | 否 | - | 函數呼叫的參數,由模型以 JSON 格式生成。 注意:模型生成的 JSON 可能無效,或者可能會生成不屬於函數定義的參數。在呼叫函數前,請在程式碼中驗證這些參數。 |
tool_calls.function.name | string | 否 | - | 要呼叫的函數名稱。 |
tool_calls.id | string | 否 | - | 工具呼叫的唯一標識符。 |
tool_calls.type | enum<string> | 否 | - | 工具的類型。 目前僅支援 function 。 可選值: - function : 表示這是一個函數呼叫。 |
usage | object | 是 | - | Token 使用情況統計。 |
usage.completion_tokens | integer | 是 | - | 完成部分使用的 token 數量。 |
usage.prompt_tokens | integer | 是 | - | 提示部分使用的 token 數量。 |
usage.total_tokens | integer | 是 | - | 總共使用的 token 數量。 |
參數名 | 類型 | 是否必填 | 預設值 | 描述 |
---|---|---|---|---|
id | string | 是 | - | 聊天補全的唯一標識符。每個分塊具有相同的 ID。 |
choices | object[] | 是 | - | 模型生成的選擇列表。 |
choices.finish_reason | enum<string> | 否 | - | 生成結束的原因。可選值:stop (自然結束)、eos (到達句子結束符)、length (達到最大 token 長度限制)、tool_calls (呼叫了工具,如函數)。 |
choices.message | object | 是 | - | 模型返回的訊息物件。 |
created | integer | 是 | - | 回應生成的時間戳(Unix 時間戳,單位為秒)。 |
model | string | 是 | - | 使用的模型名稱。 |
object | enum<string> | 是 | - | 回應類型。可選值:chat.completion (表示這是一個聊天完成回應)。 |
tool_calls | object[] | 否 | - | 模型生成的工具呼叫,例如函數呼叫。 |
tool_calls.function | object | 否 | - | 模型呼叫的函數。 |
tool_calls.function.arguments | string | 否 | - | 函數呼叫的參數,由模型以 JSON 格式生成。注意:模型生成的 JSON 可能無效,或者可能會生成不屬於函數定義的參數。在呼叫函數前,請在程式碼中驗證這些參數。 |
tool_calls.function.name | string | 否 | - | 要呼叫的函數名稱。 |
tool_calls.id | string | 否 | - | 工具呼叫的唯一標識符。 |
tool_calls.type | enum<string> | 否 | - | 工具的類型。目前僅支援 function (表示這是一個函數呼叫)。 |
usage | object | 是 | - | Token 使用情況統計。 |
usage.completion_tokens | integer | 是 | - | 完成部分使用的 token 數量。 |
usage.prompt_tokens | integer | 是 | - | 提示部分使用的 token 數量。 |
usage.total_tokens | integer | 是 | - | 總共使用的 token 數量(提示 + 完成)。 |
delta | object | 否 | - | 串流模型回應生成的聊天補全增量。 |
choices.logprobs | object 或 null | 否 | - | 該選項的對數機率資訊。 |
choices.logprobs.content | array 或 null | 否 | - | 包含對數機率資訊的訊息內容標記列表。 |
choices.logprobs.refusal | array 或 null | 否 | - | 包含拒絕訊息的標記列表及其對數機率資訊。 |
choices.logprobs.refusal.token | string | 否 | - | 標記。 |
choices.logprobs.refusal.logprob | number | 否 | - | 如果該標記在最有可能的 20 個標記內,則為其對數機率;否則使用值 -9999.0 表示極不可能。 |
choices.logprobs.refusal.bytes | array 或 null | 否 | - | 表示標記的 UTF-8 位元組表示的整數列表。用於需要組合多個標記位元組表示的情況。 |
choices.logprobs.refusal.top_logprobs | array | 否 | - | 當前標記位置上最有可能的標記及其對數機率列表。在少數情況下,返回的數量可能少於請求的數量。 |
finish_reason | string 或 null | 否 | - | 模型停止生成標記的原因。 |
index | integer | 否 | - | 選項在選項列表中的索引。 |
service_tier | string 或 null | 否 | - | 用於處理請求的服務層級。 |
system_fingerprint | string | 否 | - | 表示模型運行時後端設定的指紋。可與請求參數 seed 結合使用,以了解可能影響確定性的後端變更。 |
回應資訊
- 非串流
- 串流
{
"id": "<string>",
"choices": [
{
"message": {
"role": "assistant",
"content": "<string>",
"reasoning_content": "<string>"
},
"finish_reason": "stop"
}
],
"tool_calls": [
{
"id": "<string>",
"type": "function",
"function": {
"name": "<string>",
"arguments": "<string>"
}
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 123,
"total_tokens": 123
},
"created": 123,
"model": "<string>",
"object": "chat.completion"
}
{"id":"<string>","object":"chat.completion.chunk","created":1694268190,"model":"<string>", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
{"id":"<string>","object":"chat.completion.chunk","created":1694268190,"model":"<string>", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]}
....
{"id":"<string>","object":"chat.completion.chunk","created":1694268190,"model":"<string>", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}