# Tạo một chat completion

**API:** Model API

**Endpoint:** `POST /api/v1/chat/completions`

API Chat Completions tương thích OpenAI. Đặt stream=true để nhận Server-Sent Events. Phản hồi và cấu trúc lỗi tuân theo cấu trúc Chat Completions/tương thích OpenAI.

## Base URL

- `https://openapi.felo.ai` — Môi trường production

## Authentication

- `FeloBearerAuth` (http bearer) — Khóa API Felo được gửi dưới dạng `Authorization: Bearer <key>`.
- `FeloApiKey` (apiKey in header `X-API-Key`) — Khóa API Felo được gửi dưới dạng `X-API-Key: <key>`.

## Parameters

| Name | In | Required | Type | Description |
| --- | --- | --- | --- | --- |
| `X-Felo-Channel` | header | no | string, default: "default" | Mã kênh công khai được phân tách bằng dấu phẩy, có thứ tự. Bỏ trống header này để dùng default; default phải được dùng một mình. Ở chế độ ví, default quét thứ tự kênh công khai hiệu dụng của mô hình theo chiều ngược, bỏ qua các kênh chưa xuất bản hoặc không khả dụng, và chọn nhà cung cấp của kênh khả dụng đầu tiên. Nó bao gồm các endpoint khả dụng của nhà cung cấp đó có cùng ID mô hình upstream và cùng hạng dịch vụ. Các kênh chỉ định rõ được kiểm tra theo thứ tự do client chỉ định; chỉ nhóm nhà cung cấp của kênh đầu tiên được gửi đi. Không tự động dự phòng sang nhà cung cấp khác. Header nhà cung cấp kiểu cũ không thể ghi đè định tuyến kênh. |

## Request body

Required: **yes**

### application/json

Schema: `ChatCompletionRequest` (`#/components/schemas/ChatCompletionRequest`)

```json
{
  "type": "object",
  "required": [
    "model",
    "messages"
  ],
  "properties": {
    "model": {
      "type": "string"
    },
    "messages": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/components/schemas/ChatMessage"
      }
    },
    "stream": {
      "type": "boolean",
      "default": false
    },
    "temperature": {
      "type": "number",
      "minimum": 0
    },
    "top_p": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "n": {
      "type": "integer",
      "minimum": 1
    },
    "max_tokens": {
      "type": "integer",
      "minimum": 1
    },
    "max_completion_tokens": {
      "type": "integer",
      "minimum": 1
    },
    "stop": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "tools": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": true
      }
    },
    "tool_choice": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "additionalProperties": true
        }
      ]
    },
    "response_format": {
      "type": "object",
      "additionalProperties": true
    },
    "user": {
      "type": "string"
    },
    "service_tier": {
      "type": [
        "string",
        "null"
      ],
      "default": "default",
      "description": "Hiện chỉ hỗ trợ default (tiêu chuẩn). Giá trị bị bỏ trống, null, các bí danh và chuỗi không được hỗ trợ đều quay về default; các loại khác trả về HTTP 400. Các kênh và giá đã cấu hình không thay đổi. API văn bản gửi giá trị tiêu chuẩn của nhà cung cấp; API Decisions và tạo nội dung bỏ qua trường upstream."
    }
  },
  "additionalProperties": true
}
```

Example:

```json
{
  "model": "gpt-5.5",
  "messages": [
    {
      "role": "user",
      "content": "Write a one-sentence greeting."
    }
  ],
  "stream": false
}
```

## Responses

### 200 — Chat completion hoặc phản hồi streaming

**application/json**

Schema: `ChatCompletionResponse` (`#/components/schemas/ChatCompletionResponse`)

**text/event-stream**

Schema: string

```json
{
  "type": "string",
  "description": "Các khối SSE chứa sự kiện dữ liệu chat.completion.chunk."
}
```

### 400 — Yêu cầu, mô hình hoặc ánh xạ nhà cung cấp không hợp lệ

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 401 — Khóa API Felo không hợp lệ hoặc bị thiếu

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 402 — Số dư không đủ

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 403 — Tài khoản không được phép sử dụng mô hình. Các mô hình đã bị vô hiệu hóa cũng bị từ chối trước khi gọi upstream. Các yêu cầu và tác vụ đã được tiếp nhận vẫn tiếp tục.

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 429 — Đã đạt giới hạn chi tiêu hoặc số yêu cầu

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 502 — Yêu cầu tới nhà cung cấp upstream thất bại

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

### 503 — Nhà cung cấp hoặc dịch vụ thanh toán không khả dụng

**application/json**

Schema: `OpenAIErrorResponse` (`#/components/schemas/OpenAIErrorResponse`)

## Schema definitions

### ChatChoice

`#/components/schemas/ChatChoice`

```json
{
  "type": "object",
  "properties": {
    "index": {
      "type": "integer"
    },
    "message": {
      "$ref": "#/components/schemas/ChatMessage"
    },
    "finish_reason": {
      "type": [
        "string",
        "null"
      ]
    },
    "logprobs": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
```

### ChatCompletionResponse

`#/components/schemas/ChatCompletionResponse`

```json
{
  "type": "object",
  "required": [
    "id",
    "object",
    "choices"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "object": {
      "type": "string",
      "description": "Thường là chat.completion với phản hồi không streaming."
    },
    "created": {
      "type": "integer",
      "format": "int64"
    },
    "model": {
      "type": "string"
    },
    "choices": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/ChatChoice"
      }
    },
    "usage": {
      "$ref": "#/components/schemas/Usage"
    },
    "system_fingerprint": {
      "type": "string"
    }
  },
  "additionalProperties": true
}
```

### ChatMessage

`#/components/schemas/ChatMessage`

```json
{
  "type": "object",
  "required": [
    "role",
    "content"
  ],
  "properties": {
    "role": {
      "type": "string",
      "enum": [
        "system",
        "developer",
        "user",
        "assistant",
        "tool"
      ]
    },
    "content": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "name": {
      "type": "string"
    },
    "tool_call_id": {
      "type": "string"
    },
    "tool_calls": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": true
      }
    }
  },
  "additionalProperties": true
}
```

### OpenAIErrorResponse

`#/components/schemas/OpenAIErrorResponse`

```json
{
  "type": "object",
  "description": "Phong bì lỗi tương thích OpenAI. Router có thể bao gồm code và message mà không có đầy đủ các trường upstream tùy chọn.",
  "required": [
    "error"
  ],
  "properties": {
    "error": {
      "type": "object",
      "required": [
        "message"
      ],
      "properties": {
        "message": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "description": "Loại lỗi tương thích OpenAI khi được cung cấp."
        },
        "param": {
          "type": [
            "string",
            "null"
          ]
        },
        "code": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
```

### Usage

`#/components/schemas/Usage`

```json
{
  "type": "object",
  "description": "Đối tượng mức sử dụng của nhà cung cấp. Các trường thay đổi tùy theo endpoint và nhà cung cấp.",
  "additionalProperties": true
}
```
