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

# OpenAI Embeddings API 申請及使用

> OpenAI generation 集成指南 - XHuoAPI

OpenAI 詞向量服務，用於生成表示輸入文本的詞向量結果。

本文檔主要介紹 OpenAI Embeddings API 操作的使用流程，利用它我們可以創建表示輸入文本的嵌入向量。。

## 申請流程

要使用 OpenAI Embeddings API，首先可以到 [OpenAI Embeddings API](https://api.xhuoapi.ai/documents/0f2e63fa-5890-4bdd-84f0-1706b5c9a387) 頁面點擊「Acquire」按鈕，獲取請求所需要的憑證：

![](https://cdn.xhuoapi.ai/nyq0xz.png)

如果你尚未登入或註冊，會自動跳轉到登入頁面邀請您來註冊和登入，登入註冊之後會自動返回當前頁面。

在首次申請時會有免費額度贈送，可以免費使用該 API。

## 基本使用

接下來就可以在介面上填寫對應的內容，如圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/4qtbvr.png" width="500" className="m-auto" />
</p>

在第一次使用該接口時，我們至少需要填寫三個內容，一個是 `authorization`，直接在下拉列表裡面選擇即可。另一個參數是 `model`， `model` 就是我們選擇使用 OpenAI 官網模型類別，這裡我們主要有 3 種模型，詳情可以看我們提供的模型。最後一個參數是`input`，`input` 是我們需要轉換的詞向量文本。

同時您可以注意到右側有對應的調用代碼生成，您可以複製代碼直接運行，也可以直接點擊「Try」按鈕進行測試。

可選參數：

* `dimensions`：裁剪向量維度，默認輸出完整維度。
* `encoding_format`：返回格式，可選 `float` 或 `base64`。

<p>
  <img src="https://cdn.xhuoapi.ai/gqulm9.png" width="500" className="m-auto" />
</p>

Python 樣例調用代碼：

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/openai/embeddings"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002",
    "encoding_format": "float"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
```

調用之後，我們發現返回結果如下：

```json theme={null}
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [
        0.0022756963,
        -0.009305916,
        0.015742613,
        -0.0077253063,
        -0.0047450014,
        0.014917395,
        -0.009807394,
        -0.038264707,
        -0.0069127847,
        -0.028590616,
        0.025251659,
        ....
        -0.014079482,
        -0.015425222,
        0.0040753055,
        0.002727979,
        -0.03138366,
        0.041159317,
        -0.017608874,
        -0.018637223,
        0.014587308,
        0.010486611,
        -0.015387135,
        -0.019424353,
        -0.002800979
      ]
    }
  ],
  "model": "text-embedding-ada-002",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}
```

返回結果一共有多個字段，介紹如下：

* `model`，此次文本轉詞向量所採用的模型。
* `usage`，此次文本轉詞向量所使用的token信息。
* `data`，文本轉化後的詞向量結果。

其中 `data` 是包含了文本對應的詞向量的具體信息，它裡面的 `embedding` 是生成的詞向量具體結果。

## 錯誤處理

在調用 API 時，如果遇到錯誤，API 會返回相應的錯誤代碼和信息。例如：

* `400 token_mismatched`：Bad request, possibly due to missing or invalid parameters.
* `400 api_not_implemented`：Bad request, possibly due to missing or invalid parameters.
* `401 invalid_token`：Unauthorized, invalid or missing authorization token.
* `429 too_many_requests`：Too many requests, you have exceeded the rate limit.
* `500 api_error`：Internal server error, something went wrong on the server.

### 錯誤響應示例

```json theme={null}
{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
```

## 結論

通過本文檔，您已經了解了如何使用 OpenAI Embeddings API 輕鬆使用官方 OpenAI 的詞向量生成的功能。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題，請隨時聯繫我們的技術支持團隊。
