生成向量嵌入
把一段文本变成向量嵌入,供检索与相似度使用。
/google/v1beta/models/{modelsId}:embedContent● 支持请求参数
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}`
contentobject必填◐ 部分支持Required. The content to embed. Only the `parts.text` fields will be counted.
仅文本部件生效;图片(含多模态嵌入模型)返回 400。
▸ 展开子字段(2)
partsarray可选Ordered `Parts` that constitute a single message. Parts may have different MIME types.
rolestring可选Optional. The producer of the content. Must be either 'user' or 'model'. Useful to set for multi-turn conversations, otherwise can be left blank or unset.
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;此嵌入配置当前不支持。
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。
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
只接受文本部件;图像等多模态部件会被拒绝。taskType、title 以及 autoTruncate / documentOcr / audioTrackExtraction 等检索配置在平台上没有等价语义, 请求携带时会收到 400(INVALID_ARGUMENT),不会静默忽略——因为不同 taskType 生成的向量行为不同,静默丢弃会悄悄改变检索质量。outputDimensionality 映射为 返回向量的维度。
错误按 Gemini API 格式返回,参见 错误码
响应
HTTP 200
Successful response
application/json
| 名称 | 类型 | 说明 |
|---|---|---|
| embedding | object | Output only. The embedding generated from the input content. |
| embedding.shape | array<integer> | This field stores the soft tokens tensor frame shape (e.g. [1, 1, 256, 2048]). |
| embedding.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. |