> For the complete documentation index, see [llms.txt](https://docs.avis.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.avis.net/vi/tai-lieu-api/endpoints/text-completion.md).

# Tạo văn bản

`POST /api/v1/text/completions`

Trả về các chunk văn bản dạng chat theo cơ chế Server-Sent Events (SSE).

### Các trường request

| Trường               | Kiểu      | Bắt buộc | Ràng buộc                         | Mô tả                                                          |
| -------------------- | --------- | -------- | --------------------------------- | -------------------------------------------------------------- |
| `model`              | string    | Có       | Chuỗi không rỗng                  | Mã định danh mô hình dành cho người dùng, lấy từ `/ai/models`. |
| `messages`           | object\[] | Có       | Mảng có ít nhất một phần tử       | Các tin nhắn chat theo thứ tự hội thoại.                       |
| `messages[].role`    | string    | Có       | `system`, `user` hoặc `assistant` | Vai trò của tin nhắn.                                          |
| `messages[].content` | string    | Có       | Chuỗi không rỗng                  | Nội dung văn bản của tin nhắn.                                 |
| `temperature`        | number    | Không    | Tối thiểu `0`, tối đa `2`         | Nhiệt độ lấy mẫu. Giá trị cao hơn tăng độ đa dạng.             |
| `maxTokens`          | integer   | Không    | Tối thiểu `1`                     | Số token tối đa được tạo ra.                                   |

Các trường tùy chọn bổ sung sau được hỗ trợ và sẽ được chuyển tiếp nếu mô hình áp dụng: `topP`, `stop`, `tools`, `toolChoice`, `responseFormat`, `seed`, `presencePenalty`, `frequencyPenalty`, `topK`, `repetitionPenalty`, `minP`, `topA`, `logprobs`, `topLogprobs`, `logitBias`, `structuredOutputs`, `parallelToolCalls`, `includeReasoning`, `reasoningEffort`, `webSearchOptions`, `verbosity`, `thinking`. Danh sách trường từng mô hình chấp nhận có trong `capabilities.text.supportedParameters` của `GET /ai/models`. Hãy kiểm tra danh sách này theo từng mô hình.

### Ví dụ request

```json
{
  "model": "seed-2-0-lite",
  "messages": [
    { "role": "system", "content": "Answer clearly and briefly." },
    { "role": "user", "content": "Explain UUIDv7 in one paragraph." }
  ],
  "temperature": 0.7,
  "maxTokens": 512
}
```

### Stream response

Header của response:

```http
Content-Type: text/event-stream
Cache-Control: no-cache, no-transform
Connection: keep-alive
X-Accel-Buffering: no
```

Mỗi chunk được phát ra dưới dạng:

```
data: <TextChunk JSON>

```

Stream kết thúc bằng:

```
data: [DONE]

```

### Các trường của TextChunk

| Trường                   | Kiểu   | Bắt buộc | Mô tả                                                                                       |
| ------------------------ | ------ | -------- | ------------------------------------------------------------------------------------------- |
| `delta`                  | string | Có       | Đoạn văn bản tăng dần của chunk này.                                                        |
| `finishReason`           | string | Không    | Lý do dừng, thường có ở chunk cuối cùng của mô hình.                                        |
| `usage`                  | object | Không    | Chi tiết mức sử dụng, thường chỉ có ở chunk cuối cùng khi nhà cung cấp báo cáo mức sử dụng. |
| `usage.promptTokens`     | number | Không    | Số token tiêu thụ bởi prompt.                                                               |
| `usage.completionTokens` | number | Không    | Số token do mô hình tạo ra.                                                                 |
| `usage.totalTokens`      | number | Không    | Tổng token của prompt và completion.                                                        |

Nếu nhà cung cấp gặp lỗi sau khi stream bắt đầu, lỗi sẽ được gửi dưới dạng một đối tượng JSON SSE có trường `error`, rồi stream sẽ đóng.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.avis.net/vi/tai-lieu-api/endpoints/text-completion.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
