> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Embeddings API

> Sử dụng các route embeddings của CometAPI để tạo vector cho tìm kiếm ngữ nghĩa, phân cụm, hệ thống gợi ý và quy trình retrieval.

Sử dụng embeddings của CometAPI khi ứng dụng của bạn cần vector cho tìm kiếm ngữ nghĩa, phân cụm, hệ thống gợi ý hoặc retrieval. Gửi văn bản đến `/v1/embeddings`, lưu vector được trả về, rồi tìm kiếm trên đó bằng cơ sở dữ liệu vector của bạn.

## Tạo một embedding

Sử dụng model ID có hỗ trợ embedding từ [trang Models](/vi/overview/models) hoặc [model directory](https://www.cometapi.com/models/). Các ví dụ bên dưới gọi Embeddings API tương thích với OpenAI.

<Note>
  Các ví dụ này sử dụng placeholder `your-embedding-model-id`. Hãy thay nó bằng một model ID embedding khả dụng từ [trang Models](/vi/overview/models) hoặc [model directory](https://www.cometapi.com/models/) trước khi bạn chạy request.
</Note>

<Tip>
  Mở [Create embeddings](/api/text/embeddings) để sử dụng playground và schema của endpoint.
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.cometapi.com/v1/embeddings",
      headers={
          "Authorization": "Bearer " + os.environ["COMETAPI_KEY"],
          "Content-Type": "application/json",
      },
      json={
          "model": "your-embedding-model-id",
          "input": "CometAPI lets developers use many model providers.",
      },
      timeout=30,
  )

  response.raise_for_status()
  result = response.json()
  print(len(result["data"][0]["embedding"]))
  ```

  ```javascript Node.js theme={null}
  const response = await fetch("https://api.cometapi.com/v1/embeddings", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "your-embedding-model-id",
      input: "CometAPI lets developers use many model providers.",
    }),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const result = await response.json();
  console.log(result.data[0].embedding.length);
  ```

  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/embeddings \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "your-embedding-model-id",
      "input": "CometAPI lets developers use many model providers."
    }'
  ```
</CodeGroup>

## Ví dụ phản hồi

Một phản hồi thành công có thể trông như sau. Phản hồi bao gồm một vector cho mỗi mục input; vector bên dưới đã được rút gọn để dễ đọc hơn:

```json theme={null}
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [
        -0.0021,
        -0.0491,
        0.0209
      ]
    }
  ],
  "model": "your-embedding-model-id",
  "usage": {
    "prompt_tokens": 10,
    "total_tokens": 10
  }
}
```

## Input hàng loạt

Gửi một mảng chuỗi khi bạn muốn nhận nhiều vector từ một request:

```bash cURL theme={null}
curl https://api.cometapi.com/v1/embeddings \
  -H "Authorization: Bearer $COMETAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-embedding-model-id",
    "input": [
      "Create an API key",
      "Change the base URL",
      "Retry after a rate limit"
    ]
  }'
```

## Ví dụ về bản ghi model

<Info>
  Phản hồi danh mục model ví dụ này cho thấy envelope `/api/models` và hình dạng của một bản ghi model embedding tương thích OpenAI. Một số bản ghi embedding sử dụng `model_type` rỗng; hãy chọn model embedding theo ID và hỗ trợ endpoint thay vì chỉ dựa vào riêng trường đó.
</Info>

```bash cURL theme={null}
curl https://api.cometapi.com/api/models
```

```json theme={null}
{
  "success": true,
  "page": 1,
  "page_size": 20,
  "total": 302,
  "data": [
    {
      "created": 1757904564,
      "id": "your-embedding-model-id",
      "code": "your-embedding-model-id",
      "provider": "ExampleProvider",
      "provider_code": "example",
      "name": "Example embedding model",
      "model_type": "embedding",
      "features": [
        "text-embedding"
      ],
      "endpoints": [
        "openai"
      ],
      "pricing": {
        "currency": "USD / M Tokens",
        "input": 0.1,
        "output": null,
        "per_request": null,
        "per_second": null
      }
    }
  ]
}
```

## Các lỗi thường gặp

<AccordionGroup>
  <Accordion title="Input too long">
    Chia các tài liệu dài thành từng phần trước khi tạo embedding.
  </Accordion>

  <Accordion title="Wrong model type">
    Chọn một model có khả năng embedding từ danh mục model.
  </Accordion>

  <Accordion title="Vector dimensions mismatch">
    Giữ nguyên cùng model và số chiều cho một chỉ mục vector.
  </Accordion>

  <Accordion title="Missing API key">
    Gửi `Authorization: Bearer $COMETAPI_KEY`.
  </Accordion>
</AccordionGroup>

## Mã lỗi và chiến lược retry

<AccordionGroup>
  <Accordion title="400">
    Không retry cho đến khi input, model ID hoặc thiết lập dimensions được sửa.
  </Accordion>

  <Accordion title="401">
    Không retry cho đến khi API key tồn tại và hợp lệ.
  </Accordion>

  <Accordion title="404">
    Kiểm tra base URL, path và model ID trước khi retry.
  </Accordion>

  <Accordion title="429">
    Retry với exponential backoff và giảm kích thước batch hoặc mức độ đồng thời.
  </Accordion>

  <Accordion title="500 or 503">
    Retry với backoff cho các lỗi provider hoặc dịch vụ tạm thời.
  </Accordion>
</AccordionGroup>

<Tip>
  Để biết các mẫu triển khai, xem [Mã lỗi và chiến lược retry](/vi/guides/error-codes-and-retry-strategy) và [Giới hạn tốc độ và mức độ đồng thời](/vi/guides/rate-limits-and-concurrency).
</Tip>

## Giá và danh mục model

<CardGroup cols={3}>
  <Card title="Trang Models" icon="list" href="/overview/models">
    Tìm hiểu cách CometAPI hiển thị model ID trong tài liệu.
  </Card>

  <Card title="Danh mục model" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    Xem tình trạng khả dụng và khả năng của model.
  </Card>

  <Card title="Giá" icon="tag" href="https://www.cometapi.com/pricing/">
    Kiểm tra giá trước khi bạn gọi một model.
  </Card>
</CardGroup>
