API 参考

POST /v1/chat/completions

LLMs Text

POST /v1/chat/completions

OpenAI 兼容协议,字段与官方一致。用于:

  • OpenAI 官方 SDK(openai-python / openai npm)
  • 任何改 base_url 即可对接的 OpenAI 兼容客户端
  • Cursor / Cline / Continue 等 IDE 插件
  • LangChain / LlamaIndex 等框架

端点

POST https://relay.4aicode.net/v1/chat/completions

请求

POST /v1/chat/completions HTTP/1.1
Host: relay.4aicode.net
Authorization: Bearer sk-4ai-xxxxxxxxxxxxxxxx
content-type: application/json

{
  "model": "gpt-5.6-terra",
  "stream": true,
  "messages": [
    { "role": "system", "content": "You are helpful." },
    { "role": "user",   "content": "Write fizzbuzz in Go." }
  ]
}

支持的模型

GPT-6 系列:

模型适用
gpt-6-astra新一代旗舰模型;短上下文输入 $1.50/M、输出 $7.50/M(官方价 1.5 折)
gpt-6.1-solGPT-6.1 旗舰,复杂编码和长任务;输入 $0.30/M、输出 $1.50/M(官方价 1.5 折)
gpt-6-solGPT-6 旗舰,复杂编码和长任务;输入 $0.30/M、输出 $1.50/M(官方价 1.5 折)
gpt-6-lunaGPT-6 轻量模型,快速日常任务;输入 $0.015/M、输出 $0.075/M(官方价 1.5 折)

GPT-5 系列:

模型适用
gpt-5.6-sol旗舰,复杂编码和长任务优先,支持 max 推理强度
gpt-5.6-terra均衡档,性价比高
gpt-5.6-luna便宜 + 快,日常够用
gpt-5.5上一代通用模型
gpt-5.4通用
gpt-5.4-mini便宜 + 快
gpt-5.3-codex代码优化

Google Gemini:

模型适用
google/gemini-3.1-pro-previewGoogle Gemini Pro 预览
google/gemini-3-flash-previewGoogle Gemini Flash 预览
google/gemini-2.5-flashGoogle Gemini Flash 稳定款
google/gemini-2.5-proGoogle Gemini Pro 稳定款

GPT-5.2 及以下型号已被上游废弃。其他模型(gpt-4o、gpt-4 等)会 400 拒绝,错误信息里带可用清单。

模型可用性取决于上游支持范围和服务状态;上游返回模型不支持时,relay 会直接返回明确的 400。

被忽略的参数

接受但不生效(兼容 SDK 不报错):

  • logit_bias
  • user
  • functions(用新版 tools)

响应

与 OpenAI 官方一致:

  • choices[].message.content
  • choices[].finish_reason
  • usage(prompt_tokens / completion_tokens / total_tokens)

流式格式:

data: {"choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[{"delta":{"content":", world"}}]}
data: [DONE]

完整例子

Python

from openai import OpenAI

client = OpenAI(
    base_url="https://relay.4aicode.net/v1",
    api_key="sk-4ai-xxxxxxxxxxxxxxxx",
)

resp = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "用一句话介绍你自己"},
    ],
)
print(resp.choices[0].message.content)

TypeScript

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://relay.4aicode.net/v1",
  apiKey: process.env.OPENAI_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [{ role: "user", content: "hi" }],
});
console.log(resp.choices[0].message.content);

错误

HTTP含义
400字段错 / 模型不在 allowlist
401token 无效
403团队被暂停
429超限流或超配额
502上游 OpenAI 抖动
503上游服务暂时不可用,详见 服务冷却与重试

与 /v1/messages 的差异

维度/v1/messages/v1/chat/completions
协议AnthropicOpenAI
可用模型Claude 全系gpt-5* / google/gemini-*
必填字段max_tokens—
usage 字段input_tokens / output_tokensprompt_tokens / completion_tokens