> 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/introduction/anthropic-compatibility.md).

# Tương thích Anthropic

Trỏ SDK `@anthropic-ai/sdk` chính thức hoặc cấu hình endpoint tùy chỉnh của Claude Code/Claude Desktop đến Avis. Đặt Avis API key trong `apiKey` (xem phần Xác thực).

### Base URL

```
https://api.avis.net/api/anthropic
```

### Chuyển đổi một ứng dụng hiện có

```typescript
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  apiKey: process.env.AVIS_API_KEY,
  baseURL: 'https://api.avis.net/api/anthropic',
})
```

Các lệnh gọi Messages API hiện có không cần thay đổi thêm. Để tạo embedding, hãy dùng phần mở rộng của Avis được mô tả bên dưới, vì API và SDK của Anthropic không định nghĩa tài nguyên embedding.

### Endpoints

| Method | Path                           | Streaming | Mô tả                                                          |
| ------ | ------------------------------ | --------- | -------------------------------------------------------------- |
| `HEAD` | `/api/anthropic`               | —         | Kiểm tra kết nối (không cần xác thực)                          |
| `GET`  | `/api/anthropic/v1/models`     | —         | Liệt kê các mô hình khả dụng                                   |
| `POST` | `/api/anthropic/v1/messages`   | ✅         | Messages API                                                   |
| `POST` | `/api/anthropic/v1/embeddings` | —         | Phần mở rộng embeddings của Avis; chỉ hỗ trợ vector dạng float |

Nếu request có các header `anthropic-version` và `anthropic-beta`, chúng sẽ được chuyển tiếp đến nhà cung cấp upstream. Xem phần Xác thực.

#### `POST /messages`

```typescript
const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello!' }],
})
console.log(response.content[0].text)
```

#### `POST /embeddings` (phần mở rộng của Avis)

Anthropic SDK không cung cấp tài nguyên `client.embeddings`. Dùng phương thức cấp thấp `post` để request tiếp tục sử dụng base URL, API key, cơ chế thử lại và các header đã cấu hình cho Anthropic:

```typescript
const response = await client.post<{ data: Array<{ embedding: number[] }> }>(
  '/v1/embeddings',
  {
    body: {
      model: 'text-embedding-3-small',
      input: ['first document', 'second document'],
      dimensions: 256,
      encoding_format: 'float',
    },
  },
)

console.log(response.data[0].embedding)
```

Phần mở rộng này trả về cấu trúc danh sách embedding `{ object, data, model, usage }` giống endpoint tương thích OpenAI. Endpoint nhận văn bản và các content-block đa phương thức đã được Avis chuẩn hóa: `imageBase64`/`imageUrl`, `audioBase64`/`audioUrl`, `fileBase64` và `videoBase64`.

```typescript
import { readFileSync } from 'node:fs'

const response = await client.post<{ data: Array<{ embedding: number[] }> }>('/v1/embeddings', {
  body: {
    model: 'voyage-multimodal-3-5',
    input: [
      {
        content: [
          { type: 'text', text: 'What is in this image?' },
          {
            type: 'imageBase64',
            data: readFileSync('image.png').toString('base64'),
            mediaType: 'image/png',
          },
        ],
      },
    ],
  },
})

console.log(response.data[0].embedding)
```

Hoặc dùng URL, qua `imageUrl`:

```typescript
const response = await client.post<{ data: Array<{ embedding: number[] }> }>('/v1/embeddings', {
  body: {
    model: 'voyage-multimodal-3-5',
    input: [
      {
        content: [
          { type: 'text', text: 'What is in this image?' },
          { type: 'imageUrl', url: 'https://example.com/image.png' },
        ],
      },
    ],
  },
})

console.log(response.data[0].embedding)
```

Âm thanh chỉ dành riêng cho Gemini (chỉ `gemini-embedding-2`/`gemini-embedding-2-preview`), và chấp nhận base64 hoặc tham chiếu URL:

```typescript
const response = await client.post<{ data: Array<{ embedding: number[] }> }>('/v1/embeddings', {
  body: {
    model: 'gemini-embedding-2',
    input: [
      {
        content: [
          { type: 'text', text: 'Transcribe the sentiment of this clip.' },
          {
            type: 'audioBase64',
            data: readFileSync('clip.wav').toString('base64'),
            mediaType: 'audio/wav',
          },
        ],
      },
    ],
  },
})

console.log(response.data[0].embedding)
```

```typescript
const response = await client.post<{ data: Array<{ embedding: number[] }> }>('/v1/embeddings', {
  body: {
    model: 'gemini-embedding-2',
    input: [
      {
        content: [
          { type: 'text', text: 'Transcribe the sentiment of this clip.' },
          { type: 'audioUrl', url: 'https://example.com/clip.wav' },
        ],
      },
    ],
  },
})

console.log(response.data[0].embedding)
```

Với video (và PDF qua `fileBase64`), không có biến thể dùng URL; chỉ chấp nhận dữ liệu base64 nhúng trực tiếp. Tạo embedding video khả dụng với các mô hình có metadata khả năng cho phép:

```typescript
import { readFileSync } from 'node:fs'

const response = await client.post<{ data: Array<{ embedding: number[] }> }>(
  '/v1/embeddings',
  {
    body: {
      model: 'gemini-embedding-2',
      dimensions: 128,
      input: [
        {
          content: [
            { type: 'text', text: 'Embed this video' },
            {
              type: 'videoBase64',
              data: readFileSync('clip.mp4').toString('base64'),
              mediaType: 'video/mp4',
            },
          ],
        },
      ],
    },
  },
)

console.log(response.data[0].embedding)
```

`videoBase64` là phần mở rộng của Avis, không phải loại content-block của Anthropic. Video chỉ được hỗ trợ với `gemini-embedding-2` và `gemini-embedding-2-preview`; tổ hợp mô hình và đầu vào không được hỗ trợ sẽ trả về `400`.

#### `GET /models`

```typescript
const models = await fetch('https://api.avis.net/api/anthropic/v1/models', {
  headers: {
    Authorization: `Bearer ${process.env.AVIS_API_KEY}`,
  },
}).then((r) => r.json())

console.log(models.data[0])
```

### Hỗ trợ các trường

Proxy này hoạt động như một **lớp chuyển tiếp mỏng**. Các trường request chủ yếu tuân theo định nghĩa upstream tương thích Anthropic và được chuyển tiếp nguyên vẹn. Việc một trường có được áp dụng hay không phụ thuộc vào nhà cung cấp và mô hình được chọn.

### Response của `/models`

| Trường                                  | Kiểu            | Bắt buộc | Ghi chú                                                                                                                                                              |
| --------------------------------------- | --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`                                    | `string`        | ✅        | Model ID dành cho người dùng, dùng trong `model` cho các lệnh gọi tương thích Anthropic                                                                              |
| `name`                                  | `string`        | —        | Tên mô hình dễ đọc                                                                                                                                                   |
| `inputModalities`                       | `array<string>` | —        | Các loại dữ liệu đầu vào được mô hình hỗ trợ                                                                                                                         |
| `outputModalities`                      | `array<string>` | —        | Các loại dữ liệu đầu ra được mô hình hỗ trợ                                                                                                                          |
| `capabilitiesProviderId`                | `string`        | —        | Bản ghi nhà cung cấp được chọn theo độ ưu tiên để xác định khả năng                                                                                                  |
| `capabilities`                          | `object`        | —        | Đối tượng khả năng thực tế của bản ghi nhà cung cấp có độ ưu tiên cao nhất. Nếu không có nghĩa là không bị ràng buộc.                                                |
| `capabilities.text.supportedParameters` | `array<string>` | —        | Với mô hình văn bản: các tham số tạo nội dung được chấp nhận (ví dụ `maxTokens`, `temperature`, `topP`, `tools`, `thinking`).                                        |
| `capabilities.params`                   | `object`        | —        | Giới hạn/ràng buộc của từng tham số, theo tên trường trong request. Với mô hình hình ảnh/video, đọc từ `capabilities.image.params` hoặc `capabilities.video.params`. |
| `capabilities.inputs`                   | `object`        | —        | Các đầu vào dạng content-part được chấp nhận (vai trò, loại media, giới hạn).                                                                                        |
| `capabilities.strictParams`             | `boolean`       | —        | Khi là `true`, các tham số có kiểm soát mà không được hỗ trợ sẽ bị từ chối.                                                                                          |
| `chatPricing`                           | `object`        | —        | Metadata giá chat thực tế của bản ghi nhà cung cấp được chọn                                                                                                         |
| `imagePricing`                          | `object`        | —        | Metadata giá hình ảnh thực tế của bản ghi nhà cung cấp được chọn                                                                                                     |
| `videoPricing`                          | `object`        | —        | Metadata giá video thực tế của bản ghi nhà cung cấp được chọn                                                                                                        |
| `threeDPricing`                         | `object`        | —        | Metadata giá 3D thực tế của bản ghi nhà cung cấp được chọn                                                                                                           |
| `embeddingPricing`                      | `object`        | —        | Metadata giá embedding thực tế của bản ghi nhà cung cấp được chọn                                                                                                    |

#### Cấu trúc giá trị của `capabilities.params`

Với các mô hình hình ảnh/video, hãy dùng `capabilities.image.params` hoặc `capabilities.video.params` của từng mô hình thay vì cố định các giới hạn dùng chung cho mọi mô hình.

| `type`  | Trường bổ sung                                        | Ý nghĩa                                                                                        |
| ------- | ----------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| `enum`  | `values: string[]`, `default?`                        | Giá trị phải là một trong các `values`.                                                        |
| `range` | `min`, `max`, `default?`, `unit?`, `steps?: number[]` | Giá trị phải nằm giữa `min` và `max`. Khi có `steps`, chỉ các giá trị chính xác đó mới hợp lệ. |
| `int`   | `min`, `max`, `default?`                              | Giá trị phải là một số nguyên nằm giữa `min` và `max`.                                         |
| `bool`  | `default?`                                            | Giá trị phải là `true` hoặc `false`.                                                           |

### `/messages`

| Trường           | Kiểu            | Bắt buộc | Ghi chú                                                                                         |
| ---------------- | --------------- | -------- | ----------------------------------------------------------------------------------------------- |
| `model`          | `string`        | ✅        | Anthropic model ID; được phân giải và đổi thành model ID của nhà cung cấp trước khi chuyển tiếp |
| `max_tokens`     | `number`        | ✅        | Số token tối đa được tạo ra                                                                     |
| `messages`       | `array`         | ✅        | Mảng tin nhắn chuẩn của Anthropic                                                               |
| `system`         | `string`        | —        | System prompt                                                                                   |
| `temperature`    | `number`        | —        | Nhiệt độ lấy mẫu                                                                                |
| `top_p`          | `number`        | —        | Nucleus sampling                                                                                |
| `top_k`          | `number`        | —        | Top-K sampling                                                                                  |
| `stream`         | `boolean`       | —        | Bật response dạng stream                                                                        |
| `tools`          | `array`         | —        | Định nghĩa tool                                                                                 |
| `tool_choice`    | `object`        | —        | Chiến lược chọn tool                                                                            |
| `stop_sequences` | `array<string>` | —        | Các chuỗi dừng tùy chỉnh                                                                        |
| `metadata`       | `object`        | —        | Metadata của request                                                                            |
| `thinking`       | `object`        | —        | Cấu hình extended thinking                                                                      |

### Response của `/messages`

Các trường response tuân theo cấu trúc tương thích Anthropic và được chuyển tiếp nguyên vẹn.

#### `stop_reason`

`stop_reason` cho biết vì sao mô hình ngừng tạo nội dung.

| Trường        | Kiểu                   | Bắt buộc | Ghi chú                                                       |
| ------------- | ---------------------- | -------- | ------------------------------------------------------------- |
| `stop_reason` | `enum<string> \| null` | Có       | Giá trị được chuyển tiếp từ upstream; không ánh xạ/chuẩn hóa. |

Các giá trị có thể có:

* `end_turn`
* `max_tokens`
* `stop_sequence`
* `tool_use`
* `pause_turn`
* `refusal`
* `compaction`
* `null`


---

# 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/introduction/anthropic-compatibility.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.
