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

# 文本嵌入

> 将文本转换为向量表示

同步接口 — 请求完成后直接返回向量结果。

<RequestExample>
  ```bash cURL theme={"system"}
  curl https://www.qingbo.dev/v1/embeddings \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "text-embedding-3-small",
      "input": "清波 API 是统一的 AI 模型网关"
    }'
  ```

  ```python Python theme={"system"}
  from openai import OpenAI

  client = OpenAI(
      base_url="https://www.qingbo.dev/v1",
      api_key="YOUR_API_KEY"
  )

  response = client.embeddings.create(
      model="text-embedding-3-small",
      input="清波 API 是统一的 AI 模型网关"
  )

  vector = response.data[0].embedding
  print(f"向量维度: {len(vector)}")
  ```

  ```javascript JavaScript theme={"system"}
  import OpenAI from 'openai';

  const client = new OpenAI({
    baseURL: 'https://www.qingbo.dev/v1',
    apiKey: 'YOUR_API_KEY'
  });

  const response = await client.embeddings.create({
    model: 'text-embedding-3-small',
    input: '清波 API 是统一的 AI 模型网关'
  });

  console.log(`向量维度: ${response.data[0].embedding.length}`);
  ```

  ```go Go theme={"system"}
  package main

  import (
      "bytes"
      "encoding/json"
      "fmt"
      "io"
      "net/http"
  )

  func main() {
      payload := map[string]string{
          "model": "text-embedding-3-small",
          "input": "清波 API 是统一的 AI 模型网关",
      }

      body, _ := json.Marshal(payload)
      req, _ := http.NewRequest("POST", "https://www.qingbo.dev/v1/embeddings", bytes.NewBuffer(body))
      req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
      req.Header.Set("Content-Type", "application/json")

      resp, _ := http.DefaultClient.Do(req)
      defer resp.Body.Close()
      result, _ := io.ReadAll(resp.Body)
      fmt.Println(string(result))
  }
  ```

  ```java Java theme={"system"}
  import java.net.http.*;
  import java.net.URI;

  public class Main {
      public static void main(String[] args) throws Exception {
          String payload = """
          {
            "model": "text-embedding-3-small",
            "input": "清波 API 是统一的 AI 模型网关"
          }
          """;

          HttpClient client = HttpClient.newHttpClient();
          HttpRequest request = HttpRequest.newBuilder()
              .uri(URI.create("https://www.qingbo.dev/v1/embeddings"))
              .header("Authorization", "Bearer YOUR_API_KEY")
              .header("Content-Type", "application/json")
              .POST(HttpRequest.BodyPublishers.ofString(payload))
              .build();

          HttpResponse<String> response = client.send(request,
              HttpResponse.BodyHandlers.ofString());
          System.out.println(response.body());
      }
  }
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={"system"}
  {
    "object": "list",
    "model": "text-embedding-3-small",
    "data": [
      {
        "object": "embedding",
        "index": 0,
        "embedding": [0.0023, -0.0091, 0.0152, ...]
      }
    ],
    "usage": {
      "prompt_tokens": 8,
      "total_tokens": 8
    }
  }
  ```
</ResponseExample>

## 请求参数

<ParamField body="model" type="string" required>
  嵌入模型 ID，如 `text-embedding-3-small`、`text-embedding-3-large`
</ParamField>

<ParamField body="input" type="string or array" required>
  要嵌入的文本，支持字符串或字符串数组（批量嵌入）
</ParamField>

## 响应

<ResponseField name="object" type="string">
  固定为 `"list"`
</ResponseField>

<ResponseField name="model" type="string">
  使用的模型
</ResponseField>

<ResponseField name="data" type="array">
  嵌入结果数组

  <Expandable title="属性">
    <ResponseField name="object" type="string">
      固定为 `"embedding"`
    </ResponseField>

    <ResponseField name="index" type="integer">
      输入文本的索引
    </ResponseField>

    <ResponseField name="embedding" type="array">
      浮点数向量
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  Token 用量

  <Expandable title="属性">
    <ResponseField name="prompt_tokens" type="integer">
      输入 token 数
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      总 token 数
    </ResponseField>
  </Expandable>
</ResponseField>
