跳转到主要内容
POST
/
chat-messages
cURL
curl --request POST \
  --url 'https://{api_base_url}/chat-messages' \
  --header 'Authorization: Bearer {api_key}' \
  --header 'Content-Type: application/json' \
  --no-buffer \
  --data '{"query": "What are the specs of the iPhone 13 Pro Max?", "inputs": {}, "response_mode": "streaming", "user": "{user}"}'
import requests

url = "https://{api_base_url}/chat-messages"

payload = {
"inputs": { "city": "San Francisco" },
"query": "What are the specs of the iPhone 13 Pro Max?",
"response_mode": "streaming",
"conversation_id": "",
"user": "abc-123",
"files": [
{
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/logo.png"
}
]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
inputs: {city: 'San Francisco'},
query: 'What are the specs of the iPhone 13 Pro Max?',
response_mode: 'streaming',
conversation_id: '',
user: 'abc-123',
files: [
{
type: 'image',
transfer_method: 'remote_url',
url: 'https://example.com/logo.png'
}
]
})
};

fetch('https://{api_base_url}/chat-messages', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://{api_base_url}/chat-messages",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'inputs' => [
'city' => 'San Francisco'
],
'query' => 'What are the specs of the iPhone 13 Pro Max?',
'response_mode' => 'streaming',
'conversation_id' => '',
'user' => 'abc-123',
'files' => [
[
'type' => 'image',
'transfer_method' => 'remote_url',
'url' => 'https://example.com/logo.png'
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://{api_base_url}/chat-messages"

payload := strings.NewReader("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://{api_base_url}/chat-messages")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://{api_base_url}/chat-messages")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"inputs\": {\n \"city\": \"San Francisco\"\n },\n \"query\": \"What are the specs of the iPhone 13 Pro Max?\",\n \"response_mode\": \"streaming\",\n \"conversation_id\": \"\",\n \"user\": \"abc-123\",\n \"files\": [\n {\n \"type\": \"image\",\n \"transfer_method\": \"remote_url\",\n \"url\": \"https://example.com/logo.png\"\n }\n ]\n}"

response = http.request(request)
puts response.read_body
{
  "event": "message",
  "task_id": "c3800678-a077-43df-a102-53f23ed20b88",
  "id": "b01a39de-3480-4f3e-9f1e-4841a80f8e5e",
  "message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  "mode": "chat",
  "answer": "iPhone 13 Pro Max specs are listed here:...",
  "metadata": {
    "usage": {
      "prompt_tokens": 1033,
      "prompt_unit_price": "0.001",
      "prompt_price_unit": "0.001",
      "prompt_price": "0.0010330",
      "completion_tokens": 128,
      "completion_unit_price": "0.002",
      "completion_price_unit": "0.001",
      "completion_price": "0.0002560",
      "total_tokens": 1161,
      "total_price": "0.0012890",
      "currency": "USD",
      "latency": 0.7682376249867957
    },
    "retriever_resources": [
      {
        "position": 1,
        "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
        "dataset_name": "iPhone",
        "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
        "document_name": "iPhone List",
        "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
        "score": 0.98457545,
        "content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\" \"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""
      }
    ]
  },
  "created_at": 1705407629
}

授权

Authorization
string
header
必填

每个请求都通过 API Key 认证:Authorization: Bearer {API_KEY}。应用接口使用应用 API Key,知识库接口使用知识库 API Key(快速开始)。

API Key 应保存在服务端,切勿嵌入客户端代码。缺失或无效的 Key 会返回 HTTP 401unauthorized)。

请求体

application/json

发送对话消息的请求体。

query
string
必填

用户发送的消息。

inputs
object
必填

应用输入变量的值,以变量名为键。变量名称和类型可从 获取应用参数 响应中的 user_input_form 字段获取。

user
string
必填

终端用户标识,由你的应用定义,需在应用内唯一。会话、消息和文件仅对携带相同 user 的请求可见。参见 终端用户身份

response_mode
enum<string>

响应返回模式。省略时默认为阻塞模式。

  • streaming(推荐):回复以 Server-Sent Events 形式返回。
  • blocking:生成完成后一次性返回。生成时间较长时有中断风险:如果 API 前的反向代理在超时时间内未收到响应,就可能中断连接。
  • Agent 应用仅支持 streaming;使用 blocking 会返回 400。
可用选项:
streaming,
blocking
conversation_id
string

要继续的会话 ID。省略此字段或传入空字符串可开始新会话;响应会返回一个 conversation_id,在下一条消息中带上它即可。要恢复较早的会话,可从 获取会话列表 获取其 ID。

files
object[]

要附加到消息的文件。本地文件需先通过 上传文件 上传,再将返回的 id 作为 upload_file_id 引用,并设置 transfer_method: local_file

auto_generate_name
boolean
默认值:true

自动生成会话标题。如果为 false,请使用 重命名会话 API 并设置 auto_generate: true 来异步生成标题。

workflow_id
string

仅适用于 Chatflow 应用。 指定要执行的已发布工作流版本 ID。如果未提供,将使用最新发布的版本。

响应

请求成功。内容类型和结构取决于请求中的 response_mode 参数。

  • 如果 response_modeblocking,返回 application/jsonChatCompletionResponse 对象。
  • 如果 response_modestreaming,返回 text/event-stream 和服务器发送事件流。
event
string

事件类型,固定为 message

task_id
string<uuid>

用于请求追踪和停止响应 API 的任务 ID。

id
string<uuid>

该响应事件的唯一 ID。

message_id
string<uuid>

唯一的消息 ID。调用反馈或推荐问题接口时,将此值作为 message_id 参数使用。

conversation_id
string<uuid>

会话 ID。

mode
string

应用模式。chat 表示聊天助手应用,agent-chat 表示 Agent 应用,advanced-chat 表示 Chatflow 应用。

answer
string

完整的响应内容。

metadata
object

包含用量和检索资源的元数据。

created_at
integer<int64>

消息创建时间戳(Unix 纪元秒)。