Skip to main content
POST
Chat Completion

聊天完成

AI 聊天完成的主要端点,具有可选的知识库检索 (RAG)、模型冗余/故障转移、每次调用安全策略和流。

端点

说明

具有可选知识库检索 (RAG) 的 AI 聊天完成的主要端点。它支持:
  • 两种输入形式 — 单个 prompt 字符串(旧版)或 OpenAI 样式的 messages 数组。
  • 模型冗余 — 调用者定义的故障转移链(主要 + 最多 2 个后备)。请参见冗余与故障转移。
  • 每次呼叫安全性 — SMLTP 策略选择和内联提示屏蔽覆盖。
  • 流式传输 — 服务器发送的事件 (SSE)。
  • 签名收据 — 通过网关路由的响应的 SMLTP 合规收据参考。
OpenAI SDK 兼容性如果您想将 SecureAI 放入现有的 OpenAI 集成中并实现零代码更改,请改用 /api/external/v1/chat/completions 处的 OpenAI 兼容端点。这一经典端点是唯一支持 RAG 的端点。

身份验证

必需:API 密钥

标题

请求正文

输入参数

提供**prompt 或 messages — 不能同时提供。

模型和冗余参数

检索和生成参数

请求示例

回应

成功响应 (200)

元数据对象

流媒体

设置 "stream": true 以接收服务器发送的事件。每条 SSE 行都是 data: <json>,流以 data: [DONE] 结尾。帧通过 type 字段输入:

错误响应

400 错误请求

401 未经授权

403 禁止

429 / 502 — 冗余链已耗尽

当冗余链中的每个模型都失败时,响应会报告每次尝试。如果所有故障都是速率限制,则状态为 429,否则为 502。

500 内部服务器错误

用法示例

JavaScript/Node.js

###Python

注释

  • index 是必需的。发送 index: "Zero-Knowledge" 进行直接 AI 响应,无需 RAG。
  • user_id 参数将请求记入不同的用户帐户(管理员控制)。
  • 温度固定在0-2; max_tokens 的上限为 4000。
  • 要在不调用模型或消费点的情况下验证每个策略的请求,请使用策略检查。
  • 有关故障转移链语义(触发器、超时、流行为、耗尽状态代码),请参阅冗余和故障转移。

授权

Authorization
string
header
必填

API key authentication using Bearer token format. Example: Authorization: Bearer sk-your-api-key-here

请求体

application/json
prompt
string
必填

The user's message/prompt

示例:

"What is the company's policy on remote work?"

model
string

The AI model to use for completion

示例:

"openai/gpt-4.1-mini"

index
string

Knowledge base to search for context (use 'Zero-Knowledge' for direct AI responses)

示例:

"my-knowledge-base"

smltp_policy
string

Security policy to apply

示例:

"internal"

temperature
number<float>
默认值:0.7

Controls randomness in the response (0 = deterministic, 2 = very random)

必填范围: 0 <= x <= 2
示例:

0.7

max_tokens
integer
默认值:1000

Maximum number of tokens in the response

必填范围: 1 <= x <= 4000
示例:

1000

stream
boolean
默认值:false

Whether to stream the response

示例:

false

conversation_id
string

Optional conversation ID for tracking

示例:

"conv-123"

system_message
string

Optional custom system message

示例:

"You are a helpful assistant."

use_rag
boolean
默认值:true

Whether to use RAG (knowledge base retrieval)

示例:

true

user_id
string

MongoDB ObjectId of the user to bill for this request. If not provided, the API key owner will be billed

示例:

"60a7c8f5e8b4f5001f7a8c23"

响应

Successful chat completion

success
boolean
示例:

true

id
string
示例:

"req-abc123"

object
string
示例:

"chat.completion"

created
integer
示例:

1705312200

model
string
示例:

"openai/gpt-4.1-mini"

choices
object[]
usage
object
metadata
object