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

# OpenAI 聊天

> 创建聊天补全

为给定的聊天对话创建模型响应。有关更多信息，请参阅
[文本生成](https://platform.openai.com/docs/guides/text-generation)、[视觉](https://platform.openai.com/docs/guides/vision)
和[音频](https://platform.openai.com/docs/guides/audio)指南。

支持的参数可能因用于生成响应的模型而异，尤其是较新的推理模型。下文会注明仅推理模型支持的参数。有关推理模型当前不支持的参数，
[请参阅推理指南](https://platform.openai.com/docs/guides/reasoning)。

## 流式响应

设置 `"stream": true`，即可在生成每个词元时接收服务器发送事件（SSE）。这可以缩短首词元响应时间，非常适合聊天界面。

<CodeGroup>
  ```bash curl theme={null}
  curl https://api.aisa.one/v1/chat/completions \
    -H "Authorization: Bearer $AISA_API_KEY" \
    -H "Content-Type: application/json" \
    -N \
    -d '{
      "model": "gpt-5",
      "messages": [{"role": "user", "content": "Write a haiku about APIs."}],
      "stream": true
    }'
  ```

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

  client = OpenAI(base_url="https://api.aisa.one/v1", api_key="sk-aisa-...")

  stream = client.chat.completions.create(
      model="gpt-5",
      messages=[{"role": "user", "content": "Write a haiku about APIs."}],
      stream=True,
  )

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

  ```typescript TypeScript theme={null}
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://api.aisa.one/v1",
    apiKey: process.env.AISA_API_KEY,
  });

  const stream = await client.chat.completions.create({
    model: "gpt-5",
    messages: [{ role: "user", content: "Write a haiku about APIs." }],
    stream: true,
  });

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

### 流的结构

SSE 流中的每一行如下所示：

```
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":"Quiet"},"index":0}]}
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":" packets"},"index":0}]}
...
data: {"id":"chatcmpl-...","choices":[{"delta":{},"finish_reason":"stop","index":0}]}
data: [DONE]
```

* 每个 `data:` 行都是一个 JSON 对象。第一个数据块包含 `role`；后续数据块仅包含 `delta.content`。
* 流以设置了 `finish_reason` 的最终数据块结束，随后是字面量 `data: [DONE]` 行。
* 如果使用工具调用，`delta.tool_calls` 会增量到达，应按 `index` 拼接。

### 处理错误和超时

* **流中错误**会作为普通 SSE 事件到达，其中包含 `error` 键而非 `choices`。请关闭流，并将错误返回给调用方。
* **流断开**（网络短暂故障、客户端超时）无法恢复，请重新发起请求。对于部分响应，不会收取超出已接收词元的费用。
* **空闲超时**：如果流超过 60 秒处于空闲状态（无词元），AIsa 会将其关闭。请将客户端读取超时设置为 120 秒，以留出安全余量。
* **客户端背压**：如果下游消费者处理缓慢，请停止从流中读取；AIsa 会限制传输速率，而不会丢弃词元。

<Tip>
  流式与非流式采用相同的按词元费率。即使流在响应过程中被中断，已传输的词元仍会计费。
</Tip>


## OpenAPI

````yaml openapi/zh/openai-chat.json POST /chat/completions
openapi: 3.0.3
info:
  title: OpenAI Chat API
  version: 1.0.0
  description: OpenAI Chat Completion API，包括图像分析、流式传输、函数调用和 Logprobs。
servers:
  - url: https://api.aisa.one/v1
security:
  - BearerAuth: []
tags:
  - name: Examples
paths:
  /chat/completions:
    post:
      summary: 创建聊天补全
      description: 生成聊天响应，支持图像、流式传输、函数调用和 logprobs。
      operationId: createChatCompletion
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                model:
                  type: string
                  example: gpt-4.1
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                        example: user
                      content:
                        description: 可以是文本字符串或数组（用于图像输入）。
                        oneOf:
                          - type: string
                          - type: array
                            items:
                              type: object
                              properties:
                                type:
                                  type: string
                                text:
                                  type: string
                                image_url:
                                  type: string
                stream:
                  type: boolean
                  example: false
                logprobs:
                  type: boolean
                top_logprobs:
                  type: integer
                functions:
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                      description:
                        type: string
                      parameters:
                        type: object
                        properties: {}
                function_call:
                  oneOf:
                    - type: string
                      example: auto
                    - type: object
                      properties:
                        name:
                          type: string
      responses:
        '200':
          description: 成功完成
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API Key

````