---
title: OpenAI Chat Completions
url: https://runapi.ai/zh-CN/docs/api/openai/chat-completions.md
canonical: https://runapi.ai/zh-CN/docs/api/openai/chat-completions
locale: zh-CN
---

# OpenAI Chat Completions

## 概览

使用兼容模型和对应协议的请求、响应结构调用 RunAPI 的 OpenAI 兼容 Chat Completions operation。

### 快速开始

1. 创建 API Key，并将其设置为 RUNAPI_API_KEY。
2. 选择兼容模型，然后发送文档所列的路径参数和 JSON 请求体。
3. 读取此 operation 展示的 JSON 响应或服务器发送事件，并处理协议错误。

## 端点

POST /v1/chat/completions

基础 URL: https://runapi.ai

Contract 版本: v1

首选身份验证: Authorization: Bearer YOUR_API_TOKEN

## 身份验证载体

RunAPI 接受下列每种载体；生成的示例使用该协议的首选请求头。

- `Authorization: Bearer YOUR_API_TOKEN` (首选)
- `x-api-key: YOUR_API_TOKEN`
- `x-goog-api-key: YOUR_API_TOKEN`
- `?key=YOUR_API_TOKEN`

## 兼容模型

打开模型页可查看当前价格、限流和商业使用详情。

- [claude-fable-5](/models/claude)
- [claude-sonnet-5](/models/claude)
- [claude-opus-5](/models/claude)
- [claude-opus-4-8](/models/claude)
- [claude-opus-4-7](/models/claude)
- [claude-opus-4-6](/models/claude)
- [claude-sonnet-4-6](/models/claude)
- [claude-opus-4-5-20251101](/models/claude)
- [claude-sonnet-4-5-20250929](/models/claude)
- [claude-haiku-4-5-20251001](/models/claude)
- [claude-opus-4-1-20250805](/models/claude)
- [gpt-5.5](/models/gpt)
- [gpt-5.5-pro](/models/gpt)
- [gpt-5.4](/models/gpt)
- [gpt-5.4-mini](/models/gpt)
- [gpt-5.4-nano](/models/gpt)
- [gpt-5.4-pro](/models/gpt)
- [gpt-5.3-codex](/models/gpt)
- [gpt-5.3-codex-spark](/models/gpt)
- [gpt-5.2](/models/gpt)
- [gpt-5.2-pro](/models/gpt)
- [gpt-5.6-luna](/models/gpt)
- [gpt-5.6-sol](/models/gpt)
- [gpt-5.6-terra](/models/gpt)
- [gemini-3.5-flash](/models/gemini)
- [gemini-3.1-pro-preview](/models/gemini)
- [gemini-3-flash-preview](/models/gemini)
- [gemini-2.5-pro](/models/gemini)
- [gemini-2.5-flash](/models/gemini)
- [deepseek-v4-pro](/models/deepseek)
- [deepseek-v4-flash](/models/deepseek)
- [glm-5.2](/models/glm)
- [glm-5.1](/models/glm)
- [glm-5-turbo](/models/glm)
- [glm-5](/models/glm)
- [glm-4.7](/models/glm)
- [glm-4.6](/models/glm)
- [glm-4.5](/models/glm)
- [glm-4.5-air](/models/glm)
- [kimi-k3](/models/kimi)
- [kimi-k2.7-code](/models/kimi)
- [kimi-k2.6](/models/kimi)
- [kimi-k2.5](/models/kimi)
- [MiniMax-M3](/models/minimax)
- [MiniMax-M2.7](/models/minimax)
- [MiniMax-M2.7-highspeed](/models/minimax)
- [MiniMax-M2.5](/models/minimax)
- [MiniMax-M2.5-highspeed](/models/minimax)
- [MiniMax-M2.1](/models/minimax)
- [MiniMax-M2](/models/minimax)
- [grok-4.20-0309-non-reasoning](/models/grok)
- [grok-4.5](/models/grok)
- [grok-4.3](/models/grok)

## 请求 Contract

这里只列出同时具有公共协议与 RunAPI 支持证据的字段；其他 JSON 请求体字段会原样传递。

### JSON 请求体

必填: `model`, `messages`

```json
{
  "max_tokens": {
    "description": "Compatibility generation token limit retained for supported models; model support can vary.",
    "type": [
      "integer",
      "null"
    ]
  },
  "messages": {
    "description": "Conversation messages in OpenAI Chat Completions format.",
    "items": {
      "additionalProperties": true,
      "properties": {
        "content": {
          "type": [
            "string",
            "array"
          ]
        },
        "role": {
          "type": "string"
        }
      },
      "required": [
        "role"
      ],
      "type": "object"
    },
    "type": "array"
  },
  "model": {
    "description": "RunAPI model identifier returned by the compatible model catalog.",
    "type": "string"
  },
  "stream": {
    "default": false,
    "description": "Return Chat Completions chunks as server-sent events.",
    "type": [
      "boolean",
      "null"
    ]
  },
  "temperature": {
    "description": "Sampling temperature; model support can vary.",
    "type": [
      "number",
      "null"
    ]
  },
  "tools": {
    "description": "Tools the model may call.",
    "type": "array"
  },
  "top_p": {
    "description": "Nucleus sampling probability; model support can vary.",
    "type": [
      "number",
      "null"
    ]
  }
}
```

### 请求示例

```json
{
  "messages": [
    {
      "content": "Say hello in one sentence.",
      "role": "user"
    }
  ],
  "model": "gpt-5.4",
  "stream": false
}
```

## 同步响应

HTTP 200

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "choices": {
      "type": "array"
    },
    "created": {
      "type": "integer"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "object": {
      "const": "chat.completion",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "completion_tokens": {
          "type": "integer"
        },
        "prompt_tokens": {
          "type": "integer"
        },
        "total_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "prompt_tokens",
        "completion_tokens",
        "total_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "id",
    "object",
    "created",
    "model",
    "choices"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hello! How can I help today?",
        "role": "assistant"
      }
    }
  ],
  "created": 1785196800,
  "id": "chatcmpl_example",
  "model": "gpt-5.4",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 8,
    "prompt_tokens": 12,
    "total_tokens": 20
  }
}
```

## SSE 事件：chunk

媒体类型: `text/event-stream`

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "choices": {
      "type": "array"
    },
    "created": {
      "type": "integer"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "object": {
      "const": "chat.completion.chunk",
      "type": "string"
    },
    "usage": {
      "oneOf": [
        {
          "additionalProperties": true,
          "properties": {
            "completion_tokens": {
              "type": "integer"
            },
            "prompt_tokens": {
              "type": "integer"
            },
            "total_tokens": {
              "type": "integer"
            }
          },
          "required": [
            "prompt_tokens",
            "completion_tokens",
            "total_tokens"
          ],
          "type": "object"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "id",
    "object",
    "choices"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "choices": [
    {
      "delta": {
        "content": "Hello"
      },
      "finish_reason": null,
      "index": 0
    }
  ],
  "id": "chatcmpl_example",
  "object": "chat.completion.chunk",
  "usage": null
}
```

## 协议错误：invalid_request

HTTP 400

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "code": {
          "type": [
            "string",
            "null"
          ]
        },
        "message": {
          "type": "string"
        },
        "param": {
          "type": [
            "string",
            "null"
          ]
        },
        "type": {
          "type": "string"
        }
      },
      "required": [
        "message",
        "type"
      ],
      "type": "object"
    }
  },
  "required": [
    "error"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "error": {
    "code": null,
    "message": "messages is required",
    "param": "messages",
    "type": "invalid_request_error"
  }
}
```

## Usage

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "completion_tokens": {
      "type": "integer"
    },
    "prompt_tokens": {
      "type": "integer"
    },
    "total_tokens": {
      "type": "integer"
    }
  },
  "required": [
    "prompt_tokens",
    "completion_tokens",
    "total_tokens"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "completion_tokens": 8,
  "prompt_tokens": 12,
  "total_tokens": 20
}
```

## 生成的代码示例

每个 cURL、JavaScript 和 Python 示例都会发送此 Contract 中经过验证的请求，无需协议专用 SDK。

### curl

```curl
curl -X POST https://runapi.ai/v1/chat/completions \
  -H Authorization:\ Bearer\ YOUR_API_TOKEN \
  -H Content-Type:\ application/json \
  -d \{\"model\":\"gpt-5.4\",\"messages\":\[\{\"role\":\"user\",\"content\":\"Say\ hello\ in\ one\ sentence.\"\}\],\"stream\":false\}
```

### javascript

```javascript
const response = await fetch("https://runapi.ai/v1/chat/completions", {
  method: "POST",
  headers: {
  "Authorization": "Bearer YOUR_API_TOKEN",
  "Content-Type": "application/json"
},
  body: JSON.stringify({
  "model": "gpt-5.4",
  "messages": [
    {
      "role": "user",
      "content": "Say hello in one sentence."
    }
  ],
  "stream": false
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
console.log(data);
```

### python

#### 安装

```bash
pip install requests
```

```python
import requests

response = requests.post(
    "https://runapi.ai/v1/chat/completions",
    headers={"Authorization": "Bearer YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"model": "gpt-5.4", "messages": [{"role": "user", "content": "Say hello in one sentence."}], "stream": False}
)
response.raise_for_status()
print(response.json())
```

## 相关指南

- [API 身份验证](/docs/guides/authentication)
- [快速开始](/docs/guides/llm-api/quickstart)
