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

# 对话补全

> 使用 `POST /v1/chat/completions` 调用对话模型，并支持流式输出。

<Info>
  你可以用 OpenAI 兼容接口调用 `POST /v1/chat/completions`。
  你先在 **Console** 创建 **API key**。
</Info>

<CardGroup cols={2}>
  <Card title="获取 API key" icon="key" href="/faq/token-management">
    在 **Console** 复制 key。
  </Card>

  <Card title="模型列表" icon="list" href="/api-reference/models">
    先确认 `model` 该填哪个 id。
  </Card>
</CardGroup>

## 接口信息

* **接口地址**：`POST https://api.yelinai.com/v1/chat/completions`
* **鉴权方式**：`Authorization: Bearer YOUR_API_KEY`
* **兼容性**：兼容 OpenAI Chat Completions

## 最小请求示例

### cURL

```bash theme={"system"}
curl https://api.yelinai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "system", "content": "你是一个有用的 AI 助手。"},
      {"role": "user", "content": "用一句话介绍 YeLin AI。"}
    ]
  }'
```

### Python (OpenAI SDK)

```python theme={"system"}
from openai import OpenAI

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

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "你是一个有用的 AI 助手。"},
        {"role": "user", "content": "用一句话介绍 YeLin AI。"},
    ],
)

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

## 请求参数

### 必填参数

| 参数         | 类型     | 说明                                         |
| ---------- | ------ | ------------------------------------------ |
| `model`    | string | 模型 id。你可以先看 [模型列表](/api-reference/models)。 |
| `messages` | array  | 对话消息数组。                                    |

### 常用可选参数

| 参数            | 类型      | 说明              |
| ------------- | ------- | --------------- |
| `temperature` | number  | 控制随机性。常用范围 0-2。 |
| `max_tokens`  | integer | 最大输出 token 数。   |
| `stream`      | boolean | 是否启用流式输出。       |

## 消息格式

```json theme={"system"}
{"role":"user","content":"你好"}
```

支持的 `role`：`system`、`user`、`assistant`。

## 流式输出示例

```bash theme={"system"}
curl https://api.yelinai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "写一句广告语"}],
    "stream": true
  }'
```

<Tip>
  你在调试阶段先用 `gpt-4o-mini`。
  你确认流程没问题后再换更强模型。
</Tip>
