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

# Create embeddings

從文字或媒體輸入建立 embedding 向量。純文字請求每條輸入回傳一個向量，適用於檢索、語義搜尋、聚類、分類和 RAG。包含媒體（以 base64 data URI 形式傳入的圖片、音訊或影片）的請求會將**所有**輸入融合為**單一**跨模態向量——如需每個資產各得一個向量，請將每個資產放在單獨的請求中進行 embedding。相容 OpenAI Embeddings API 格式。此端點不支援串流。

## 認證

<ParamField header="Authorization" type="string" required>
  Bearer 令牌。在 Authorization 請求標頭中使用您的 API 金鑰作為 Bearer 令牌。

  Format: `Bearer <SUNRA_KEY>`
</ParamField>

## 請求

此端點接受一個 JSON 物件。

<ParamField body="model" type="string" required>
  要使用的 embedding 模型 ID，例如 `google/gemini-embedding-2`。可用的 embedding 模型見[模型頁面](https://sunra.ai/models)。
</ParamField>

<ParamField body="input" type="string | string[]" required>
  要進行 embedding 的輸入，可以是單一字串或字串陣列。純文字輸入獨立進行 embedding，按輸入順序每條回傳一個向量。媒體輸入以 base64 data URI 形式傳入（`data:image/png;base64,…`、`data:audio/mpeg;base64,…`、`data:video/mp4;base64,…`），可與文字混合——但任何包含媒體的請求都會為全部輸入回傳一個融合後的單一向量。限制（來自上游）：每個請求最多 6 張圖片，影片最長 120 秒，所有模態合計不超過 8,192 個 token（按文字 token 計；圖片每張 258 個 token，音訊每秒 25 個 token，影片每秒 66 個 token）。
</ParamField>

<ParamField body="dimensions" type="integer">
  輸出 embedding 的維度數。僅支援具有靈活輸出維度（Matryoshka Representation Learning）的模型。`google/gemini-embedding-2` 預設為 `3072`，支援 `128` 到 `3072` 之間的值。較小的值會截斷向量，同時保留大部分語義品質。
</ParamField>

<ParamField body="encoding_format" type="string">
  回傳 embedding 的格式。`float`（預設）或 `base64`。
</ParamField>

## 回應

<ResponseField name="object" type="string">
  始終為 `list`。
</ResponseField>

<ResponseField name="data" type="object[]">
  embedding 物件列表，每條輸入對應一個，按輸入順序排列。

  <Expandable title="embedding 物件">
    <ResponseField name="object" type="string">
      始終為 `embedding`。
    </ResponseField>

    <ResponseField name="index" type="integer">
      對應輸入的索引。
    </ResponseField>

    <ResponseField name="embedding" type="number[]">
      embedding 向量。
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="model" type="string">
  用於建立 embedding 的模型。
</ResponseField>

<ResponseField name="usage" type="object">
  本次請求的 token 用量。embedding 僅按輸入 token 計費，各模態費率不同（定價見模型頁面）。包含媒體的請求會回傳按模態細分的 token 數。

  <Expandable title="usage 物件">
    <ResponseField name="prompt_tokens" type="integer">
      文字輸入的 token 數。
    </ResponseField>

    <ResponseField name="input_image_tokens" type="integer">
      圖片輸入的 token 數（每張 258 個）。僅當請求包含圖片時回傳。
    </ResponseField>

    <ResponseField name="input_audio_tokens" type="integer">
      音訊輸入的 token 數（每秒 25 個）。僅當請求包含音訊時回傳。
    </ResponseField>

    <ResponseField name="input_video_tokens" type="integer">
      影片輸入的 token 數（每秒 66 個）。僅當請求包含影片時回傳。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      所有模態的輸入 token 總數（embedding 不產生輸出 token）。
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api-llm.sunra.ai/v1/embeddings \
    -H "Authorization: Bearer <SUNRA_KEY>" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "google/gemini-embedding-2",
      "input": [
        "The quick brown fox jumps over the lazy dog.",
        "Sunra is a platform for AI models."
      ],
      "dimensions": 768
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://api-llm.sunra.ai/v1/embeddings",
      headers={
          "Authorization": "Bearer <SUNRA_KEY>",
          "Content-Type": "application/json"
      },
      json={
          "model": "google/gemini-embedding-2",
          "input": [
              "The quick brown fox jumps over the lazy dog.",
              "Sunra is a platform for AI models."
          ],
          "dimensions": 768
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api-llm.sunra.ai/v1/embeddings", {
    method: "POST",
    headers: {
      "Authorization": "Bearer <SUNRA_KEY>",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "google/gemini-embedding-2",
      input: [
        "The quick brown fox jumps over the lazy dog.",
        "Sunra is a platform for AI models."
      ],
      dimensions: 768
    })
  });
  const data = await response.json();
  console.log(data);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "object": "list",
    "data": [
      {
        "object": "embedding",
        "index": 0,
        "embedding": [0.011253, -0.020551, 0.049236, "..."]
      },
      {
        "object": "embedding",
        "index": 1,
        "embedding": [0.031744, 0.008122, -0.014307, "..."]
      }
    ],
    "model": "google/gemini-embedding-2",
    "usage": {
      "prompt_tokens": 18,
      "total_tokens": 18
    }
  }
  ```
</ResponseExample>
