> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/api-reference/cn/compute/aig/embedding/openai-embedding.md).

# OpenAI

## 1. 概述

OpenAI 的文本嵌入（Text Embeddings）用于衡量文本字符串之间的相关性。嵌入通常用于以下场景：

* 搜索（Search）：根据查询字符串的相关性对搜索结果进行排序。
* 聚类（Clustering）：将相似的文本字符串分组。
* 推荐（Recommendations）：推荐具有相关文本字符串的项目。
* 异常检测（Anomaly Detection）：识别与其他数据关联较低的异常文本。
* 多样性测量（Diversity Measurement）：分析文本的相似性分布。
* 分类（Classification）：根据最相似的标签对文本字符串进行分类。

**模型列表：**

* `text-embedding-3-small`
* `text-embedding-3-large`
* `text-embedding-ada-002`

## 2. 请求说明

* **请求方法**:`POST`
* **请求地址**:

  > `https://gateway.theturbo.ai/v1/embeddings`

***

## 3. 请求参数

### 3.1 Header 参数

| 参数名称            | 类型     | 必填 | 说明                                         | 示例值                    |
| --------------- | ------ | -- | ------------------------------------------ | ---------------------- |
| `Content-Type`  | string | 是  | 设置请求头类型，必须为 `application/json`             | `application/json`     |
| `Accept`        | string | 是  | 设置响应类型，建议统一为 `application/json`            | `application/json`     |
| `Authorization` | string | 是  | 身份验证所需的 API\_KEY，格式 `Bearer $YOUR_API_KEY` | `Bearer $YOUR_API_KEY` |

***

### 3.2 Body 参数 (application/json)

| 参数名称             | 类型           | 必填 | 说明                                                         | 示例                       |
| ---------------- | ------------ | -- | ---------------------------------------------------------- | ------------------------ |
| **model**        | string       | 是  | 要使用的模型 ID。详见[概述](#1-概述)列出的可用版本，如 `text-embedding-ada-002`。 | `text-embedding-ada-002` |
| **input**        | string/array | 是  | 输入内容。数组维度不得超过`2048`。输入token数不得超过`8192`。                    | `你好，请给我讲个笑话。`            |
| encoding\_format | string       | 否  | 返回向量的格式。支付`float`和`base64`。                                | `float`                  |

***

## 4. 请求示例

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/embeddings
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "text-embedding-ada-002",
	"input": "今天天气怎么样"
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/embeddings \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"text-embedding-ada-002\",
	\"input\": \"今天天气怎么样\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	YOUR_API_KEY    = "sk-123456789012345678901234567890123456789012345678"
	REQUEST_PAYLOAD = `{
	"model": "text-embedding-ada-002",
	"input": "今天天气怎么样"
}`
)

func main() {

	requestURL := "https://gateway.theturbo.ai/v1/embeddings"
	requestMethod := "POST"
	requestPayload := strings.NewReader(REQUEST_PAYLOAD)

	req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
	if err != nil {
		fmt.Println("Create request failed, err: ", err)
		return
	}

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")
	req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Do request failed, err: ", err)
		return
	}
	defer resp.Body.Close()

	respBodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Read response body failed, err: ", err)
		return
	}
	fmt.Println(string(respBodyBytes))
}
```

{% endtab %}
{% endtabs %}

## 5. 响应示例

```json
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [
        0.0023064255,
        -0.009327292,
        .... (1536 floats total for ada-002)
        -0.0028842222,
      ],
      "index": 0
    }
  ],
  "model": "text-embedding-ada-002",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}
```
