API 参考

OpenAI Chat Completions API

使用 messages 发送对话,从 choices 中读取模型回复。

POST https://api.aigocode.dev/v1/chat/completions

Chat Completions 是 OpenAI 兼容协议的另一种调用入口,适合已有应用、SDK 和第三方客户端采用的 messages 格式。GPT、Grok 等支持 OpenAI 协议的模型均可使用,鉴权为 Authorization: Bearer YOUR_API_KEY。

AIGoCode 的 OpenAI 协议模型同时支持本接口和 Responses,可以按现有代码选择。

请求参数

字段类型必填说明
modelstring是当前分组中的模型 ID
messagesarray是对话消息列表,每项包含角色与内容
streamboolean否设置为 true 时使用 SSE 流式返回

文本对话中,user 表示用户输入,assistant 表示模型回复;系统提示使用模型支持的 system 或 developer 消息。多轮对话时,按顺序传入历史消息与本轮输入。

请求示例

下面使用 gpt-6-astra,Key 选择支持该模型的可外接分组。

export AIGOCODE_API_KEY="填写支持 OpenAI 协议且可外接分组的 API Key"

curl "https://api.aigocode.dev/v1/chat/completions" \
  -H "Authorization: Bearer $AIGOCODE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-6-astra",
  "messages": [
    {
      "role": "user",
      "content": "Reply with exactly: Hello from AIGoCode!"
    }
  ]
}'
$env:AIGOCODE_API_KEY = "填写支持 OpenAI 协议且可外接分组的 API Key"

$body = @{
  model = "gpt-6-astra"
  messages = @(
    @{
      role = "user"
      content = "Reply with exactly: Hello from AIGoCode!"
    }
  )
} | ConvertTo-Json -Depth 6

$response = Invoke-RestMethod `
  -Method Post `
  -Uri "https://api.aigocode.dev/v1/chat/completions" `
  -Headers @{ Authorization = "Bearer $env:AIGOCODE_API_KEY" } `
  -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

$response | ConvertTo-Json -Depth 20
set "AIGOCODE_API_KEY=填写支持 OpenAI 协议且可外接分组的 API Key"

curl.exe "https://api.aigocode.dev/v1/chat/completions" ^
  -H "Authorization: Bearer %AIGOCODE_API_KEY%" ^
  -H "Content-Type: application/json" ^
  -d "{\"model\":\"gpt-6-astra\",\"messages\":[{\"role\":\"user\",\"content\":\"Reply with exactly: Hello from AIGoCode!\"}]}"

响应

{
  "id": "chatcmpl_example",
  "object": "chat.completion",
  "model": "gpt-6-astra",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello from AIGoCode!"
      },
      "finish_reason": "stop"
    }
  ]
}

普通文本回复读取 choices[0].message.content,结束原因位于 finish_reason。工具调用的返回内容应按 tool_calls 处理。

用量信息位于 usage,常见字段为 prompt_tokens、completion_tokens 和 total_tokens。消费明细可在「使用统计」中核对。

流式响应

请求中设置 "stream": true,从每个事件的 choices[].delta 中读取新增内容;data: [DONE] 表示流结束。

Responses 使用的是另一套事件结构。程序使用本接口时,应按 Chat Completions 的 choices 与 delta 字段解析。

相关入口

本页目录