> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sub2api.ruilinlu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sub2API Chat Completions API 接口说明

> Sub2API 完整兼容 OpenAI Chat Completions API，支持多轮对话、流式输出与 Function Calling。

Sub2API 完整兼容 OpenAI Chat Completions API 格式。你可以直接将现有的 OpenAI SDK 客户端指向 Sub2API 的 base URL，无需修改其他代码即可调用各种 LLM 模型。该接口支持多轮对话上下文、流式输出、Function Calling 等完整功能。

## 接口端点

**POST** `https://sub2api.ruilinlu.com/v1/chat/completions`

## 请求参数

<ParamField body="model" type="string" required>
  模型名称标识符，例如 `gpt-4o`、`claude-3-5-sonnet` 等。具体可用模型请参考 <a href="/models">模型列表</a>。
</ParamField>

<ParamField body="messages" type="array" required>
  对话消息数组，按时间顺序排列。每条消息包含以下字段:

  * `role`: 消息角色，可选 `system`、`user`、`assistant`、`tool`
  * `content`: 消息内容(字符串)

  ```json theme={null}
  [
    {"role": "system", "content": "你是一个有帮助的助手。"},
    {"role": "user", "content": "你好！"},
    {"role": "assistant", "content": "你好！有什么我可以帮你的吗？"},
    {"role": "user", "content": "请讲一个笑话。"}
  ]
  ```
</ParamField>

<ParamField body="stream" default="false" type="boolean">
  是否启用流式输出。设为 `true` 时，API 将使用 SSE (Server-Sent Events) 逐字返回内容。详情参考 <a href="/api/streaming">流式输出文档</a>。
</ParamField>

<ParamField body="temperature" default="1" type="number">
  采样温度，控制输出的随机性。取值范围 0 到 2，值越高输出越随机。建议根据任务类型调整，代码生成推荐 0.2，创意写作推荐 0.8 以上。
</ParamField>

<ParamField body="max_tokens" type="integer">
  生成内容的最大 Token 数量限制。实际可用上限取决于所选模型。
</ParamField>

<ParamField body="top_p" default="1" type="number">
  核采样参数。与 temperature 一起使用时建议只调整其中一个。
</ParamField>

## 完整请求示例

以下是一个多轮对话的完整 cURL 请求:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://sub2api.ruilinlu.com/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -d '{
      "model": "gpt-4o",
      "messages": [
        {"role": "system", "content": "你是一个有帮助的助手。"},
        {"role": "user", "content": "你好！"},
        {"role": "assistant", "content": "你好！有什么我可以帮你的吗？"},
        {"role": "user", "content": "请用 Python 写一段快速排序代码。"}
      ],
      "temperature": 0.2
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      base_url="https://sub2api.ruilinlu.com/v1",
      api_key="YOUR_API_KEY"
  )

  response = client.chat.completions.create(
      model="gpt-4o",
      messages=[
          {"role": "system", "content": "你是一个有帮助的助手。"},
          {"role": "user", "content": "你好！"},
          {"role": "assistant", "content": "你好！有什么我可以帮你的吗？"},
          {"role": "user", "content": "请用 Python 写一段快速排序代码。"}
      ],
      temperature=0.2
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://sub2api.ruilinlu.com/v1",
    apiKey: "YOUR_API_KEY"
  });

  const response = await client.chat.completions.create({
    model: "gpt-4o",
    messages: [
      { role: "system", content: "你是一个有帮助的助手。" },
      { role: "user", content: "你好！" },
      { role: "assistant", content: "你好！有什么我可以帮你的吗？" },
      { role: "user", content: "请用 Node.js 写一段快速排序代码。" }
    ],
    temperature: 0.2
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

## 响应结构

````json theme={null}
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1712345678,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "```python\ndef quicksort(arr):\n    ...\n```"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 45,
    "completion_tokens": 120,
    "total_tokens": 165
  }
}
````

## 响应字段

<ResponseField name="id" type="string">
  本次请求的唯一标识符。
</ResponseField>

<ResponseField name="object" type="string">
  对象类型，固定为 `chat.completion`。
</ResponseField>

<ResponseField name="choices" type="array">
  生成的回复列表。通常只包含一个元素，包含:

  * `message`: 包含 `role` 和 `content` 的完整消息对象
  * `finish_reason`: 生成结束原因，如 `stop`、`length`、`content_filter`
</ResponseField>

<ResponseField name="usage.prompt_tokens" type="integer">
  输入提示消耗的 Token 数量。
</ResponseField>

<ResponseField name="usage.completion_tokens" type="integer">
  模型生成的 Token 数量。
</ResponseField>

## 流式输出

如需使用流式输出，将 `stream` 设为 `true`。API 将返回 SSE 数据流，逐块(chunk)返回生成内容。

<Tip>
  详细流式输出用法请参考 <a href="/api/streaming">流式输出文档</a>。
</Tip>

## 相关接口

* [Responses API](/api/responses-api) - OpenAI 新一代有状态 API
* [Anthropic Messages API](/api/anthropic-messages) - Anthropic 原生格式接口
* [流式输出](/api/streaming) - SSE 流式输出详细指南
