---
title: OpenAI Embeddings
url: https://runapi.ai/zh-CN/docs/api/openai/embeddings.md
canonical: https://runapi.ai/zh-CN/docs/api/openai/embeddings
locale: zh-CN
---

# OpenAI Embeddings

## 概览

使用兼容模型和对应协议的请求、响应结构调用 RunAPI 的 OpenAI 兼容 Embeddings operation。

### 快速开始

1. 创建 API Key，并将其设置为 RUNAPI_API_KEY。
2. 选择兼容模型，然后发送文档所列的路径参数和 JSON 请求体。
3. 读取此 operation 展示的 JSON 响应或服务器发送事件，并处理协议错误。

## 端点

POST /v1/embeddings

基础 URL: https://runapi.ai

Contract 版本: v1

首选身份验证: Authorization: Bearer YOUR_API_TOKEN

## 身份验证载体

RunAPI 接受下列每种载体；生成的示例使用该协议的首选请求头。

- `Authorization: Bearer YOUR_API_TOKEN` (首选)
- `x-api-key: YOUR_API_TOKEN`
- `x-goog-api-key: YOUR_API_TOKEN`
- `?key=YOUR_API_TOKEN`

## 兼容模型

打开模型页可查看当前价格、限流和商业使用详情。

- [text-embedding-3-large](/models/gpt)
- [text-embedding-3-small](/models/gpt)
- [text-embedding-ada-002](/models/gpt)

## 请求 Contract

这里只列出同时具有公共协议与 RunAPI 支持证据的字段；其他 JSON 请求体字段会原样传递。

### JSON 请求体

必填: `model`, `input`

```json
{
  "dimensions": {
    "description": "Requested output vector dimensions for models that support it.",
    "type": "integer"
  },
  "encoding_format": {
    "description": "Format used for returned embedding vectors.",
    "type": "string"
  },
  "input": {
    "description": "Text or token arrays to embed.",
    "type": [
      "string",
      "array"
    ]
  },
  "model": {
    "description": "RunAPI embedding model identifier.",
    "type": "string"
  }
}
```

### 请求示例

```json
{
  "input": "The quick brown fox.",
  "model": "text-embedding-3-small"
}
```

## 同步响应

HTTP 200

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "data": {
      "type": "array"
    },
    "model": {
      "type": "string"
    },
    "object": {
      "const": "list",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "prompt_tokens": {
          "type": "integer"
        },
        "total_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "prompt_tokens",
        "total_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "object",
    "data",
    "model",
    "usage"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "data": [
    {
      "embedding": [
        0.0023,
        -0.0093,
        0.0151
      ],
      "index": 0,
      "object": "embedding"
    }
  ],
  "model": "text-embedding-3-small",
  "object": "list",
  "usage": {
    "prompt_tokens": 5,
    "total_tokens": 5
  }
}
```

## 协议错误：invalid_request

HTTP 400

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "code": {
          "type": [
            "string",
            "null"
          ]
        },
        "message": {
          "type": "string"
        },
        "param": {
          "type": [
            "string",
            "null"
          ]
        },
        "type": {
          "type": "string"
        }
      },
      "required": [
        "message",
        "type"
      ],
      "type": "object"
    }
  },
  "required": [
    "error"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "error": {
    "code": null,
    "message": "input is required",
    "param": "input",
    "type": "invalid_request_error"
  }
}
```

## Usage

### Schema

```json
{
  "additionalProperties": true,
  "properties": {
    "prompt_tokens": {
      "type": "integer"
    },
    "total_tokens": {
      "type": "integer"
    }
  },
  "required": [
    "prompt_tokens",
    "total_tokens"
  ],
  "type": "object"
}
```

### 示例

```json
{
  "prompt_tokens": 5,
  "total_tokens": 5
}
```

## 生成的代码示例

每个 cURL、JavaScript 和 Python 示例都会发送此 Contract 中经过验证的请求，无需协议专用 SDK。

### curl

```curl
curl -X POST https://runapi.ai/v1/embeddings \
  -H Authorization:\ Bearer\ YOUR_API_TOKEN \
  -H Content-Type:\ application/json \
  -d \{\"model\":\"text-embedding-3-small\",\"input\":\"The\ quick\ brown\ fox.\"\}
```

### javascript

```javascript
const response = await fetch("https://runapi.ai/v1/embeddings", {
  method: "POST",
  headers: {
  "Authorization": "Bearer YOUR_API_TOKEN",
  "Content-Type": "application/json"
},
  body: JSON.stringify({
  "model": "text-embedding-3-small",
  "input": "The quick brown fox."
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
console.log(data);
```

### python

#### 安装

```bash
pip install requests
```

```python
import requests

response = requests.post(
    "https://runapi.ai/v1/embeddings",
    headers={"Authorization": "Bearer YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"model": "text-embedding-3-small", "input": "The quick brown fox."}
)
response.raise_for_status()
print(response.json())
```

## 相关指南

- [API 身份验证](/docs/guides/authentication)
- [快速开始](/docs/guides/llm-api/quickstart)
