> For AI agents: the complete documentation index is available at https://little-butterfly-9bfe.sentia389.workers.dev/llms.txt, the full documentation bundle is available at https://little-butterfly-9bfe.sentia389.workers.dev/llms-full.txt.

# OpenAI Chat Completions

向 routerlio 的 `POST /v1/chat/completions` 发送 `messages`，从 `choices` 读取回答。鉴权使用 `Authorization: Bearer <ROUTERLIO_API_KEY>`。先按[快速接入](https://little-butterfly-9bfe.sentia389.workers.dev/start/index.md)设置密钥、网关根地址和模型 ID。

各厂商手册列出支持的型号与参数：[OpenAI](https://little-butterfly-9bfe.sentia389.workers.dev/models/openai/index.md)、[Anthropic Claude](https://little-butterfly-9bfe.sentia389.workers.dev/models/anthropic/index.md)、[Google Gemini](https://little-butterfly-9bfe.sentia389.workers.dev/models/google/index.md)、[Moonshot Kimi](https://little-butterfly-9bfe.sentia389.workers.dev/models/moonshot/index.md)。

## 最小请求

在 Bash 中运行以下命令。`jq` 将 `ROUTERLIO_MODEL` 的值写入请求体。

```bash
payload=$(jq -n \
  --arg model "${ROUTERLIO_MODEL:?请设置 ROUTERLIO_MODEL}" \
  '{
    model: $model,
    messages: [
      {
        role: "user",
        content: "请说你好。"
      }
    ]
  }') &&
curl -sS \
  --url "${ROUTERLIO_BASE_URL:?请设置 ROUTERLIO_BASE_URL}/v1/chat/completions" \
  --header "Authorization: Bearer ${ROUTERLIO_API_KEY:?请设置 ROUTERLIO_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data-binary "$payload"
```

在 API 客户端中可直接使用以下 JSON 请求体。将 `gpt-6.2-sol` 换成令牌可见且支持此入口的模型 ID。

```json
{
  "model": "gpt-6.2-sol",
  "messages": [
    {
      "role": "user",
      "content": "请说你好。"
    }
  ]
}
```

| 请求字段                           | 类型        | 必填     | 说明                                                                         |
| ------------------------------ | --------- | ------ | -------------------------------------------------------------------------- |
| `model`                        | 字符串       | 是      | 令牌在 routerlio 上可用的完整模型 ID。                                                 |
| `messages`                     | 对象数组      | 普通对话必填 | 按顺序传递对话消息。纯文本用 `role` 和字符串 `content`，角色及其它内容类型的支持范围取决于模型。                  |
| `stream`                       | 布尔值       | 否      | 设为 `true` 请求 SSE。                                                          |
| `max_completion_tokens`        | 整数        | 否      | 生成 token 上限，推理模型还可能把推理计入。部分兼容渠道使用 `max_tokens`，按所选型号填写。                    |
| `stream_options.include_usage` | 布尔值       | 否      | 流式用量选项。渠道支持时，用量块的 `choices` 可以为空。                                          |
| `reasoning_effort`             | 字符串       | 否      | 推理强度，可用枚举和默认值查具体系列页。                                                       |
| `temperature`、`top_p`          | 数字        | 否      | 采样设置。能否传入、允许范围及其和推理强度的关系取决于目标模型。                                           |
| `tools`、`tool_choice`          | 数组、字符串或对象 | 否      | 描述工具和选择规则。常见函数工具用 `tools[].function.name` 和 `tools[].function.parameters`。 |
| `response_format`              | 对象        | 否      | 结构化输出配置，常见类型为 `json_object` 或 `json_schema`。使用 `json_schema` 时提供对应结构定义。    |

## 读取 JSON 响应

| 响应字段                        | 说明                                                           |
| --------------------------- | ------------------------------------------------------------ |
| `id`、`model`                | 响应 ID 和响应报告的模型。                                              |
| `choices[].message.content` | 文本输出。若模型返回工具调用，检查 `choices[].message.tool_calls`。            |
| `choices[].finish_reason`   | 结束原因，例如 `stop`、`length` 或 `tool_calls`。                      |
| `usage`                     | 返回时可读取 `prompt_tokens`、`completion_tokens` 和 `total_tokens`。 |

非流式响应示例数据。

```json
{
  "id": "chatcmpl_example",
  "object": "chat.completion",
  "created": 1795900800,
  "model": "gpt-6.2-sol",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好。"
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 3,
    "total_tokens": 11
  }
}
```

## 读取流式响应

在命令的 `jq` 对象中加入 `stream: true`，并给 cURL 加 `-N`。按各个 `choice.index` 拼接 `delta.content`，允许个别数据块只有角色、结束原因或用量。用 `finish_reason` 判断生成结果，收到 `data: [DONE]` 时结束解析。若使用函数工具，按 `choice.index` 和 `delta.tool_calls[].index` 累积参数片段，完整拼接后再解析参数 JSON。

SSE 示例片段，已省略其它数据块。

```text
data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1795900800,"model":"gpt-6.2-sol","choices":[{"index":0,"delta":{"content":"你好。"},"finish_reason":null}]}

data: {"id":"chatcmpl_example","object":"chat.completion.chunk","created":1795900800,"model":"gpt-6.2-sol","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

## 常见失败

| 现象                                | 处理方式                                                                                                                                        |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| 请求提示 `field messages is required` | 普通对话请传非空 `messages` 数组。Responses 的 `input` 用在 [Responses 入口](https://little-butterfly-9bfe.sentia389.workers.dev/start/responses/index.md)。 |
| HTTP 成功但没有可读文本                    | 检查 `finish_reason` 和 `message.tool_calls`。流式请求检查 `delta`，并允许 `choices` 为空。                                                                  |
| `stream_options` 或 token 上限字段被拒绝  | 先用最小请求确认模型可用，再按该渠道支持的字段逐项添加。                                                                                                                |

其它状态码及请求 ID 的定位方式见[故障排查](https://little-butterfly-9bfe.sentia389.workers.dev/troubleshooting/index.md)。

## 官方定义

- [OpenAI Chat Completions 创建接口](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create)
