> For AI agents: the complete documentation index is available at https://docs.reelway.ai/llms.txt, the full documentation bundle is available at https://docs.reelway.ai/llms-full.txt.

# 对话补全

本页是 Intelligence 视角的**构建指南**;端点字段级细节见 [API 参考 → 对话补全](https://docs.reelway.ai/api-reference/chat-completions.md)。

## 基本用法

```js
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://api.reelway.ai/v1',
  apiKey: process.env.REELWAY_API_KEY,
});

const completion = await client.chat.completions.create({
  model: 'qwen3.6-35b-a3b-gguf-iq4xs',
  messages: [
    { role: 'system', content: '你是短视频口播稿改写助手。' },
    { role: 'user', content: '把这段简介改写成视频口播稿:…' }
  ],
});
console.log(completion.choices[0].message.content);
```

## 流式

```js
const stream = await client.chat.completions.create({ model, messages, stream: true });
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}
```

网关已配置 `flush_interval -1`:流式响应不被代理缓冲,SSE 分片即时到达。

## 函数工具调用

标准 OpenAI `tools` 数组与 `tool_choice`;模型返回 `tool_calls` 后由你的代码执行工具并回填 `role: "tool"` 消息。

## 计费

按输入/输出 token 计费,统一余额扣费(受理时预估、完成时校正)。单价以模型卡片与价格规则为准。

## 陷阱与建议

- `developer` 角色被归一为 `system`;不要依赖两者的区分。
- 对话历史由调用方维护;需要服务端可恢复的长对话请改用[持久对话运行](https://docs.reelway.ai/intelligence/durable-chat-runs.md)。
- 限流退避、`429`/`Retry-After` 处理与[生产集成](https://docs.reelway.ai/api-reference/production-integrations.md)一致。
