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

# POST /v1/chat/completions — OpenAI 对话格式

> 发送对话历史到任意模型并获取补全响应。Anyone 完全兼容 OpenAI Chat Completions API 格式。

`/v1/chat/completions` 端点接收一组消息并返回模型的回复。Anyone 完全实现了 OpenAI Chat Completions 格式，你只需修改 base URL 和 API key，就能将任何现有的 OpenAI SDK 或客户端指向 Anyone，无需其他代码改动。

## 端点

```
POST /v1/chat/completions
```

## 认证

在 `Authorization` 头中传入你的 Anyone API key：

```
Authorization: Bearer YOUR_TOKEN
```

## 请求参数

<ParamField body="model" type="string" required>
  用于生成补全的模型标识符。Anyone 会将请求路由到该模型对应的上游渠道。

  对于 OpenAI o 系列推理模型，可使用后缀变体控制推理强度：`o3-mini-high`、`o3-mini-medium` 或 `o3-mini-low`，分别对应 `reasoning_effort` 的 `high`、`medium` 和 `low`。
</ParamField>

<ParamField body="messages" type="object[]" required>
  按顺序排列的消息数组，表示对话历史。每条消息必须包含 `role` 和 `content`。

  <Expandable title="消息属性">
    <ParamField body="messages[].role" type="string" required>
      消息作者的角色。可选值：`system`、`user`、`assistant` 或 `tool`。对于较新的 OpenAI 推理模型（o3 及更高、gpt-5 及更高），请用 `developer` 代替 `system`。
    </ParamField>

    <ParamField body="messages[].content" type="string | object[]" required>
      消息内容。纯文本消息传字符串，多模态消息（文本、图片、音频、文件）传内容块数组。
    </ParamField>

    <ParamField body="messages[].name" type="string">
      参与者名称（可选）。同一角色多次出现时有助于区分。
    </ParamField>

    <ParamField body="messages[].tool_calls" type="object[]">
      模型在上一轮生成的 tool calling 请求。仅出现在调用了工具的 `assistant` 消息中。
    </ParamField>

    <ParamField body="messages[].tool_call_id" type="string">
      此消息所回应的 tool call ID。`tool` 角色的消息必填。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  设为 `true` 时，响应以 SSE（服务端推送事件）流式返回。每个事件包含一个 `ChatCompletionChunk` 部分对象。流以 `data: [DONE]` 结束。
</ParamField>

<ParamField body="stream_options" type="object">
  仅在 `stream` 为 `true` 时生效的选项。

  <Expandable title="stream_options 属性">
    <ParamField body="stream_options.include_usage" type="boolean" default="false">
      设为 `true` 时，最后一个 SSE 块中包含 `usage` 字段（token 用量统计）。
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="temperature" type="number">
  采样温度，范围 `0` 到 `2`。值越高输出越随机，值越低输出越确定。与 `top_p` 互斥——只用其中一个。
</ParamField>

<ParamField body="top_p" type="number">
  核采样概率质量。模型只考虑累积概率达到 `top_p` 的 token。取值 `0` 到 `1`。与 `temperature` 互斥。
</ParamField>

<ParamField body="max_tokens" type="integer">
  模型可生成的最大 token 数量。未指定时使用模型默认限制。较新的 OpenAI 模型请用 `max_completion_tokens`。
</ParamField>

<ParamField body="max_completion_tokens" type="integer">
  补全中可生成的最大 token 数量，包含推理 token。同时提供时优先于 `max_tokens`。
</ParamField>

<ParamField body="reasoning_effort" type="string">
  控制模型在回复前的推理程度。可选值：`low`、`medium`、`high`。适用于 OpenAI 推理模型（o 系列）。也可直接在模型名中编码推理强度：`o3-mini-high`、`o3-mini-medium`、`o3-mini-low`。
</ParamField>

<ParamField body="stop" type="string | string[]">
  一个或多个停止序列。模型遇到这些序列时停止生成，输出中不包含停止序列本身。
</ParamField>

<ParamField body="n" type="integer" default="1">
  为每条消息生成多少个补全选项。
</ParamField>

<ParamField body="frequency_penalty" type="number" default="0">
  取值 `-2.0` 到 `2.0`。正值会惩罚在已有文本中频繁出现的 token，减少重复。
</ParamField>

<ParamField body="presence_penalty" type="number" default="0">
  取值 `-2.0` 到 `2.0`。正值会惩罚在已有文本中出现过的 token，增加话题多样性。
</ParamField>

<ParamField body="seed" type="integer">
  设置后模型尝试产生确定性输出。不保证跨模型版本的可重复性。
</ParamField>

<ParamField body="logprobs" type="boolean" default="false">
  是否返回输出 token 的对数概率。
</ParamField>

<ParamField body="top_logprobs" type="integer">
  在每个 API key 位置返回最可能的 token 数量及其对数概率。需要 `logprobs` 为 `true`。取值 `0` 到 `20`。
</ParamField>

<ParamField body="tools" type="object[]">
  模型可调用的工具列表。每个工具定义一个模型可调用的函数。

  <Expandable title="工具属性">
    <ParamField body="tools[].type" type="string" required>
      工具类型。目前仅支持 `"function"`。
    </ParamField>

    <ParamField body="tools[].function" type="object" required>
      函数定义。

      <Expandable title="函数属性">
        <ParamField body="tools[].function.name" type="string" required>
          要调用的函数名。
        </ParamField>

        <ParamField body="tools[].function.description" type="string">
          函数功能描述。模型根据此描述决定何时调用。
        </ParamField>

        <ParamField body="tools[].function.parameters" type="object">
          函数参数，JSON Schema 格式。
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="tool_choice" type="string | object" default="auto">
  控制模型如何选择工具。传 `"none"` 禁用 tool calling，`"auto"` 由模型决定，`"required"` 强制调用工具，或传对象 `{"type": "function", "function": {"name": "..."}}` 强制调用指定函数。
</ParamField>

<ParamField body="parallel_tool_calls" type="boolean" default="true">
  是否允许模型在单轮中调用多个工具。
</ParamField>

<ParamField body="response_format" type="object">
  指定输出格式。传 `{"type": "json_object"}` 启用 JSON 模式。传 `{"type": "json_schema", "json_schema": {...}}` 强制遵循指定的 JSON Schema。

  <Expandable title="response_format 属性">
    <ParamField body="response_format.type" type="string" required>
      输出格式。可选 `"text"`、`"json_object"` 或 `"json_schema"`。
    </ParamField>

    <ParamField body="response_format.json_schema" type="object">
      当 `type` 为 `"json_schema"` 时必填。定义输出必须遵循的 schema。
    </ParamField>
  </Expandable>
</ParamField>

## 响应字段

<ResponseField name="id" type="string">
  此次补全的唯一标识符，格式为 `chatcmpl-...`。
</ResponseField>

<ResponseField name="object" type="string">
  非流式响应始终为 `"chat.completion"`，流式块为 `"chat.completion.chunk"`。
</ResponseField>

<ResponseField name="created" type="integer">
  补全创建时间的 Unix 时间戳（秒）。
</ResponseField>

<ResponseField name="model" type="string">
  生成此补全所用的模型标识符。
</ResponseField>

<ResponseField name="choices" type="object[]">
  补全选项数组。大多数请求返回一个选项（`n=1`）。

  <Expandable title="choice 属性">
    <ResponseField name="choices[].index" type="integer">
      此选项的索引，从 `0` 开始。
    </ResponseField>

    <ResponseField name="choices[].message" type="object">
      生成的消息。

      <Expandable title="消息属性">
        <ResponseField name="choices[].message.role" type="string">
          生成消息的角色，始终为 `"assistant"`。
        </ResponseField>

        <ResponseField name="choices[].message.content" type="string | null">
          消息的文本内容。当模型调用工具而非生成文本时为 `null`。
        </ResponseField>

        <ResponseField name="choices[].message.tool_calls" type="object[]">
          模型请求的 tool calling（如有）。每项包含 `id`、`type` 和 `function`（含 `name` 和 `arguments`）。
        </ResponseField>

        <ResponseField name="choices[].message.reasoning_content" type="string">
          模型的内部推理内容（上游返回时可见，如支持思考的模型）。
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="choices[].finish_reason" type="string">
      模型停止生成的原因。可选值：`"stop"`（自然结束）、`"length"`（达到 token 限制）、`"tool_calls"`（调用了工具）或 `"content_filter"`（内容过滤）。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  此次请求的 token 用量统计。

  <Expandable title="usage 属性">
    <ResponseField name="usage.prompt_tokens" type="integer">
      输入消息中的 token 数量。
    </ResponseField>

    <ResponseField name="usage.completion_tokens" type="integer">
      生成输出中的 token 数量。
    </ResponseField>

    <ResponseField name="usage.total_tokens" type="integer">
      `prompt_tokens` 与 `completion_tokens` 之和。
    </ResponseField>
  </Expandable>
</ResponseField>

## 示例

<Tabs>
  <Tab title="非流式">
    <CodeGroup>
      ```bash cURL theme={null}
      curl https://api.anyone.ai/v1/chat/completions \
        -H "Authorization: Bearer YOUR_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "gpt-5.4",
          "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What is the capital of France?"}
          ]
        }'
      ```

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

      client = OpenAI(
          api_key="YOUR_TOKEN",
          base_url="https://api.anyone.ai/v1",
      )

      response = client.chat.completions.create(
          model="gpt-5.4",
          messages=[
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "What is the capital of France?"},
          ],
      )

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

      ```javascript JavaScript (openai SDK) theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "YOUR_TOKEN",
        baseURL: "https://api.anyone.ai/v1",
      });

      const response = await client.chat.completions.create({
        model: "gpt-5.4",
        messages: [
          { role: "system", content: "You are a helpful assistant." },
          { role: "user", content: "What is the capital of France?" },
        ],
      });

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

    **响应示例：**

    ```json theme={null}
    {
      "id": "chatcmpl-abc123",
      "object": "chat.completion",
      "created": 1714000000,
      "model": "gpt-5.4",
      "choices": [
        {
          "index": 0,
          "message": {
            "role": "assistant",
            "content": "The capital of France is Paris."
          },
          "finish_reason": "stop"
        }
      ],
      "usage": {
        "prompt_tokens": 25,
        "completion_tokens": 9,
        "total_tokens": 34
      }
    }
    ```
  </Tab>

  <Tab title="流式">
    <CodeGroup>
      ```bash cURL theme={null}
      curl https://api.anyone.ai/v1/chat/completions \
        -H "Authorization: Bearer YOUR_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "gpt-5.4",
          "stream": true,
          "stream_options": {"include_usage": true},
          "messages": [
            {"role": "user", "content": "Tell me a short joke."}
          ]
        }'
      ```

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

      client = OpenAI(
          api_key="YOUR_TOKEN",
          base_url="https://api.anyone.ai/v1",
      )

      stream = client.chat.completions.create(
          model="gpt-5.4",
          stream=True,
          messages=[
              {"role": "user", "content": "Tell me a short joke."},
          ],
      )

      for chunk in stream:
          delta = chunk.choices[0].delta
          if delta.content:
              print(delta.content, end="", flush=True)
      ```

      ```javascript JavaScript (openai SDK) theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "YOUR_TOKEN",
        baseURL: "https://api.anyone.ai/v1",
      });

      const stream = await client.chat.completions.create({
        model: "gpt-5.4",
        stream: true,
        messages: [{ role: "user", content: "Tell me a short joke." }],
      });

      for await (const chunk of stream) {
        const delta = chunk.choices[0]?.delta;
        if (delta?.content) process.stdout.write(delta.content);
      }
      ```
    </CodeGroup>

    服务端以如下格式发送事件。每行以 `data: ` 开头，后接 JSON 对象：

    ```
    data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1714000000,"model":"gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":"Why"},"finish_reason":null}]}

    data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1714000000,"model":"gpt-5.4","choices":[{"index":0,"delta":{"content":" don't"},"finish_reason":null}]}

    data: [DONE]
    ```
  </Tab>
</Tabs>

## 推理模型

使用 OpenAI 推理模型时，将 `model` 设为 o 系列标识符。可通过 `reasoning_effort` 参数或直接在模型名中编码来控制推理强度：

```json theme={null}
{"model": "o3-mini-high", "messages": [...]}
```

等价于：

```json theme={null}
{"model": "o3-mini", "reasoning_effort": "high", "messages": [...]}
```

Claude 的思考模式使用 `-thinking` 后缀模型名——例如 `claude-sonnet-4-6-thinking`。Gemini 思考模式在模型名后追加 `-thinking`，或使用 `-low`、`-medium`、`-high` 控制推理强度。
