跳转到内容
EN

批量生成向量嵌入

POST/google/v1beta/models/{modelsId}:batchEmbedContents● 支持

Gemini API 官方文档 ↗

请求参数

  • modelstring必填

    path: Required. The model's resource name. This serves as an ID for the Model to use. This name should match a model name returned by the ListModels method. Format: models/{model}

  • requestsarray必填

    Required. Embed requests for the batch. The model in each of these requests must match the model specified BatchEmbedContentsRequest.model.

    支持 1–100 项,返回顺序与输入一致;维度必须一致。

    展开子字段 (6)
    • contentobject可选◐ 部分支持

      Required. The content to embed. Only the parts.text fields will be counted.

      仅文本部件生效;图片(含多模态嵌入模型)返回 400。

    • embedContentConfigobject可选◐ 部分支持

      Optional. Configuration for the EmbedContent request.

      仅 outputDimensionality 可用(受模型限制);autoTruncate、documentOcr、audioTrackExtraction 等其余配置返回 400。

      展开子字段 (6)
      • audioTrackExtractionboolean可选○ 未支持

        Optional. Whether to extract audio from video content.

        传入返回 400;此嵌入配置当前不支持。

      • autoTruncateboolean可选○ 未支持

        Optional. Whether to silently truncate the input content if it's longer than the maximum sequence length.

        传入返回 400;此嵌入配置当前不支持。

      • documentOcrboolean可选○ 未支持

        Optional. Whether to enable OCR for document content.

        传入返回 400;此嵌入配置当前不支持。

      • outputDimensionalityinteger可选◐ 部分支持

        Optional. Reduced dimension for the output embedding. If set, excessive values in the output embedding are truncated from the end.

        text-embedding-3-small、qwen3-vl-embedding 支持已测维度;doubao-embedding-vision 需至少 1024;bge-m3、qwen3-embedding-0.6b 报错。

      • taskTypestring可选○ 未支持

        Optional. The task type of the embedding.

        传入返回 400;此嵌入配置当前不支持。

        取值 CLASSIFICATIONCLUSTERINGCODE_RETRIEVAL_QUERYFACT_VERIFICATIONQUESTION_ANSWERINGRETRIEVAL_DOCUMENTRETRIEVAL_QUERYSEMANTIC_SIMILARITYTASK_TYPE_UNSPECIFIED
      • titlestring可选○ 未支持

        Optional. The title for the text.

        传入返回 400;此嵌入配置当前不支持。

    • modelstring可选

      Required. The model's resource name. This serves as an ID for the Model to use. This name should match a model name returned by the ListModels method. Format: models/{model}

      必须与 URL 路径中的模型一致,否则返回 400。

    • outputDimensionalityinteger可选◐ 部分支持

      Optional. Deprecated: Please use EmbedContentConfig.output_dimensionality instead. Optional reduced dimension for the output embedding. If set, excessive values in the output embedding are truncated from the end. Supported by newer models since 2024 only. You cannot set this value if using the earlier model (models/embedding-001).

      text-embedding-3-small、qwen3-vl-embedding 支持已测维度;doubao-embedding-vision 需至少 1024;bge-m3、qwen3-embedding-0.6b 报错。

    • taskTypestring可选○ 未支持

      Optional. Deprecated: Please use EmbedContentConfig.task_type instead. Optional task type for which the embeddings will be used. Not supported on earlier models (models/embedding-001).

      传入返回 400。

      取值 CLASSIFICATIONCLUSTERINGCODE_RETRIEVAL_QUERYFACT_VERIFICATIONQUESTION_ANSWERINGRETRIEVAL_DOCUMENTRETRIEVAL_QUERYSEMANTIC_SIMILARITYTASK_TYPE_UNSPECIFIED
    • titlestring可选○ 未支持

      Optional. Deprecated: Please use EmbedContentConfig.title instead. An optional title for the text. Only applicable when TaskType is RETRIEVAL_DOCUMENT. Note: Specifying a title for RETRIEVAL_DOCUMENT provides better quality embeddings for retrieval.

      传入返回 400。

返回

200

requests 中每一项的约束与 embedContent 相同(文本部件、同上拒绝规则);批量内 所有条目的 outputDimensionality 必须一致。当前官方 JS 与 Python SDK 的 embedContent 调用实际走的就是这个批量端点。

错误按 Gemini API 格式返回,参见 错误码

请求示例

curl
curl https://api.deepraxis.com/google/v1beta/models/text-embedding-3-small:batchEmbedContents \
  -H "x-goog-api-key: $PRAXIS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"requests":[{"content":{"parts":[{"text":"第一条"}]}},{"content":{"parts":[{"text":"第二条"}]}}]}'
Python
from google import genai
from google.genai import types

client = genai.Client(
    api_key="$PRAXIS_API_KEY",
    http_options=types.HttpOptions(base_url="https://api.deepraxis.com/google"),
)

resp = client.models.embed_content(
    model="text-embedding-3-small",
    contents=["第一条", "第二条"],
)
for embedding in resp.embeddings:
    print(embedding.values)
TypeScript
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "$PRAXIS_API_KEY",
  httpOptions: { baseUrl: "https://api.deepraxis.com/google" },
});

const resp = await ai.models.embedContent({
  model: "text-embedding-3-small",
  contents: ["第一条", "第二条"],
});
for (const embedding of resp.embeddings ?? []) console.log(embedding.values);

响应示例 200

{
  "embeddings": [
    {
      "values": [
        -0.026688681915402412,
        -0.037877585738897324,
        0.08132540434598923,
        -0.004256384912878275,
        "… (1024 项)"
      ]
    },
    {
      "values": [
        -0.0032429357524961233,
        -0.041793204843997955,
        0.05447297915816307,
        0.021508345380425453,
        "… (1024 项)"
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 5
  }
}

响应

HTTP 200

Successful response

application/json

名称 类型 说明
embeddings array<object> Output only. The embeddings for each request, in the same order as provided in the batch request.
embeddings[].shape array<integer> This field stores the soft tokens tensor frame shape (e.g. [1, 1, 256, 2048]).
embeddings[].values array<number> The embedding values. This is for 3P users only and will not be populated for 1P calls.
usageMetadata object Output only. The usage metadata for the request.
usageMetadata.promptTokenCount integer Output only. Number of tokens in the prompt.
usageMetadata.promptTokenDetails array<object> Output only. List of modalities that were processed in the request input.