跳至主要內容

Vertex AI 嵌入

用法 - Embedding

import litellm
from litellm import embedding
litellm.vertex_project = "hardy-device-38811" # Your Project ID
litellm.vertex_location = "us-central1" # proj location

response = embedding(
model="vertex_ai/textembedding-gecko",
input=["good morning from litellm"],
)
print(response)

支援的 Embedding 模型

這裡 列出的所有模型都受支援

模型名稱函式呼叫
text-embedding-004embedding(model="vertex_ai/text-embedding-004", input)
text-multilingual-embedding-002embedding(model="vertex_ai/text-multilingual-embedding-002", input)
textembedding-geckoembedding(model="vertex_ai/textembedding-gecko", input)
textembedding-gecko-multilingualembedding(model="vertex_ai/textembedding-gecko-multilingual", input)
textembedding-gecko-multilingual@001embedding(model="vertex_ai/textembedding-gecko-multilingual@001", input)
textembedding-gecko@001embedding(model="vertex_ai/textembedding-gecko@001", input)
textembedding-gecko@003embedding(model="vertex_ai/textembedding-gecko@003", input)
text-embedding-preview-0409embedding(model="vertex_ai/text-embedding-preview-0409", input)
text-multilingual-embedding-preview-0409embedding(model="vertex_ai/text-multilingual-embedding-preview-0409", input)
gemini-embedding-2-previewembedding(model="vertex_ai/gemini-embedding-2-preview", input)
gemini-embedding-2 (GA)embedding(model="vertex_ai/gemini-embedding-2", input)
Fine-tuned OR Custom Embedding modelsembedding(model="vertex_ai/<your-model-id>", input)

支援的 OpenAI(Unified)參數

參數型別vertex 對應項目
inputstring or List[string]instances
dimensionsintoutput_dimensionality
input_typeLiteral["RETRIEVAL_QUERY","RETRIEVAL_DOCUMENT", "SEMANTIC_SIMILARITY", "CLASSIFICATION", "CLUSTERING", "QUESTION_ANSWERING", "FACT_VERIFICATION"]task_type

使用 OpenAI(Unified)參數

response = litellm.embedding(
model="vertex_ai/text-embedding-004",
input=["good morning from litellm", "gm"]
input_type = "RETRIEVAL_DOCUMENT",
dimensions=1,
)

支援的 Vertex 特定參數

參數型別
auto_truncatebool
task_typeLiteral["RETRIEVAL_QUERY","RETRIEVAL_DOCUMENT", "SEMANTIC_SIMILARITY", "CLASSIFICATION", "CLUSTERING", "QUESTION_ANSWERING", "FACT_VERIFICATION"]
titlestr

使用 Vertex 特定參數(使用 task_typetitle

您可以將任何 Vertex 特定參數傳遞給 embedding 模型。只要像這樣將它們傳遞給 embedding 函式:

相關的 Vertex AI 文件,內含所有 embedding 參數

response = litellm.embedding(
model="vertex_ai/text-embedding-004",
input=["good morning from litellm", "gm"]
task_type = "RETRIEVAL_DOCUMENT",
title = "test",
dimensions=1,
auto_truncate=True,
)

BGE 嵌入

使用部署在 Vertex AI 上的 BGE(Baidu General Embedding)模型。

用法

Using BGE on Vertex AI
import litellm

response = litellm.embedding(
model="vertex_ai/bge/<your-endpoint-id>",
input=["Hello", "World"],
vertex_project="your-project-id",
vertex_location="your-location"
)

print(response)

多模態 Embeddings

Gemini Embedding 2 預覽(多模態)

gemini-embedding-2-preview 支援 統一多模態 embeddings——可在單一請求中處理文字、圖片、音訊、影片和 PDF。詳情請參閱 部落格文章。GA 模型 ID gemini-embedding-2 提供相同行為——可在下方任何範例中直接替換模型名稱。請參閱 GA 部落格 以了解 cost-map 涵蓋範圍與價格說明。

回應格式 — Vertex 只會回傳一個合併向量

Vertex AI 的 Gemini embedding 端點只提供單一內容 embedContent(沒有 batchEmbedContents),因此在 input=[...] 中傳入 N 個項目時,會回傳 1 個統一 embedding,將所有部分融合在一起,而不是 N 個獨立向量。若要每個項目各取得一個向量,請對每個輸入各呼叫一次 embedding(...)

這與 Gemini API 路徑(gemini/gemini-embedding-2-preview)不同,後者會針對每個輸入元素回傳一個 embedding(OpenAI 相容)。請參閱 Gemini embedding 文件

輸入格式:

  • Data URI: data:image/png;base64,<encoded_data>
  • GCS URL: gs://bucket/path/to/file.png(MIME 類型會根據副檔名推斷)

支援的 MIME 類型: image/pngimage/jpegaudio/mpegaudio/wavvideo/mp4video/quicktimeapplication/pdf

import litellm
from litellm import embedding

litellm.vertex_project = "your-project-id"
litellm.vertex_location = "us-central1"

# Text + Image (GCS URL)
response = embedding(
model="vertex_ai/gemini-embedding-2-preview",
input=[
"Describe this image",
"gs://my-bucket/images/photo.png"
],
)

# Text + Image (base64)
response = embedding(
model="vertex_ai/gemini-embedding-2-preview",
input=[
"The food was delicious",
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII"
],
)

multimodalembedding@001(舊版)

已知限制:

  • 每個請求只支援 1 張圖片 / 影片 / 圖片
  • 僅支援 GCS 或 base64 編碼的圖片 / 影片

用法

使用 GCS 圖片

response = await litellm.aembedding(
model="vertex_ai/multimodalembedding@001",
input="gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png" # will be sent as a gcs image
)

使用 base 64 編碼的圖片

response = await litellm.aembedding(
model="vertex_ai/multimodalembedding@001",
input="data:image/jpeg;base64,..." # will be sent as a base64 encoded image
)

文字 + 圖片 + 影片 Embeddings

文字 + 圖片

response = await litellm.aembedding(
model="vertex_ai/multimodalembedding@001",
input=["hey", "gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png"] # will be sent as a gcs image
)

文字 + 影片

response = await litellm.aembedding(
model="vertex_ai/multimodalembedding@001",
input=["hey", "gs://my-bucket/embeddings/supermarket-video.mp4"] # will be sent as a gcs image
)

圖片 + 影片

response = await litellm.aembedding(
model="vertex_ai/multimodalembedding@001",
input=["gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png", "gs://my-bucket/embeddings/supermarket-video.mp4"] # will be sent as a gcs image
)