# 嵌入向量

### 创建嵌入向量

```
POST https://api.openai.com/v1/embeddings
```

创建一个嵌入向量来表示输入的文本。

#### 请求体

* model 字符串 必填项 要使用的模型的ID。您可以使用“列出模型API”查看所有可用的模型，或查看我们的模型概述以获取它们的描述。
* input 字符串或数组 必填项 要为其获取嵌入的输入文本，编码为字符串或令牌数组的数组。要在单个请求中获取多个输入的嵌入，传递一个字符串数组或令牌数组的数组。每个输入的长度不能超过8192个令牌。
* user 字符串 可选项 代表您的最终用户的唯一标识符，可以帮助OpenAI监视和检测滥用。了解更多信息。

#### 请求示例

{% tabs %}
{% tab title="curl" %}
{% code lineNumbers="true" %}

```sh
curl https://api.openai.com/v1/embeddings \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002"
  }
```

{% endcode %}
{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Embedding.create(
  model="text-embedding-ada-002",
  input="The food was delicious and the waiter..."
)
```

{% endcode %}
{% endtab %}

{% tab title="node.js" %}
{% code lineNumbers="true" %}

```javascript
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createEmbedding({
  model: "text-embedding-ada-002",
  input: "The food was delicious and the waiter...",
});

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code title="请求参数" lineNumbers="true" %}

```json
{
  "model": "text-embedding-ada-002",
  "input": "The food was delicious and the waiter..."
}

```

{% endcode %}

{% code title="返回结果" lineNumbers="true" %}

```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
  }
}

```

{% endcode %}
