多模態嵌入 API 會根據您提供的輸入內容 (可包含圖片、文字和影片資料的組合) 生成向量。接著,您可以使用嵌入向量執行後續工作,例如圖片分類或影片內容審查。
如需其他概念資訊,請參閱「多模態嵌入」。
支援的機型:
模型 | 程式碼 |
---|---|
多模態專用的 Embeddings | multimodalembedding@001 |
語法範例
傳送多模態嵌入 API 要求的語法。
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:predict \ -d '{ "instances": [ ... ], }'
Python
from vertexai.vision_models import MultiModalEmbeddingModel model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding") model.get_embeddings(...)
參數清單
如需實作詳情,請參閱範例。
要求內文
{
"instances": [
{
"text": string,
"image": {
// Union field can be only one of the following:
"bytesBase64Encoded": string,
"gcsUri": string,
// End of list of possible types for union field.
"mimeType": string
},
"video": {
// Union field can be only one of the following:
"bytesBase64Encoded": string,
"gcsUri": string,
// End of list of possible types for union field.
"videoSegmentConfig": {
"startOffsetSec": integer,
"endOffsetSec": integer,
"intervalSec": integer
}
},
"parameters": {
"dimension": integer
}
}
]
}
參數 | |
---|---|
|
自由參加: 要生成嵌入項目的圖片。 |
|
自由參加: 要生成嵌入的文字。 |
|
自由參加: 要產生嵌入內容的影片片段。 |
|
自由參加: 回應中包含的嵌入維度。僅適用於文字和圖片輸入內容。可接受的值: |
圖片
參數 | |
---|---|
|
自由參加: 以 Base64 字串編碼的圖片位元組。必須是 |
|
選填。 要執行嵌入作業的圖片 Cloud Storage 位置。請選擇 |
|
選填。 圖片內容的 MIME 類型。支援的值為 |
影片
參數 | |
---|---|
|
自由參加: 以 Base64 字串編碼的影片位元組。請選擇 |
|
自由參加: 要執行嵌入作業的影片 Cloud Storage 位置。請選擇 |
|
自由參加: 影片片段設定。 |
VideoSegmentConfig
參數 | |
---|---|
|
自由參加: 影片片段的起始偏移時間 (以秒為單位)。如未指定,則會以 |
|
自由參加: 影片片段的結束時間偏移量 (以秒為單位)。如未指定,系統會以 |
|
選填。 系統會根據這個間隔生成影片的嵌入內容。 |
回應主體
{
"predictions": [
{
"textEmbedding": [
float,
// array of 128, 256, 512, or 1408 float values
float
],
"imageEmbedding": [
float,
// array of 128, 256, 512, or 1408 float values
float
],
"videoEmbeddings": [
{
"startOffsetSec": integer,
"endOffsetSec": integer,
"embedding": [
float,
// array of 1408 float values
float
]
}
]
}
],
"deployedModelId": string
}
回應元素 | 說明 |
---|---|
imageEmbedding |
128、256、512 或 1408 維度的浮點數清單。 |
textEmbedding |
128、256、512 或 1408 維度的浮點數清單。 |
videoEmbeddings |
1408 個浮點數的維度清單,其中包含生成嵌入內容的影片片段開始和結束時間 (以秒為單位)。 |
範例
基本用途
從圖片生成嵌入
使用下列範例為圖片生成嵌入項目。
REST
使用任何要求資料之前,請先替換以下項目:
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 - PROJECT_ID:您的 Google Cloud 專案 ID。
- TEXT:要取得嵌入的目標文字。例如:
a cat
。 - B64_ENCODED_IMG:要取得嵌入內容的目標圖片。圖片必須指定為採用 Base64 編碼的位元組字串。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict
JSON 要求主體:
{ "instances": [ { "text": "TEXT", "image": { "bytesBase64Encoded": "B64_ENCODED_IMG" } } ] }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict" | Select-Object -Expand Content
{ "predictions": [ { "textEmbedding": [ 0.010477379, -0.00399621, 0.00576670747, [...] -0.00823613815, -0.0169572588, -0.00472954148 ], "imageEmbedding": [ 0.00262696808, -0.00198890246, 0.0152047109, -0.0103145819, [...] 0.0324628279, 0.0284924973, 0.011650892, -0.00452344026 ] } ], "deployedModelId": "DEPLOYED_MODEL_ID" }
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
Node.js
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Node.js API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Java
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Java API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Go API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
從影片生成嵌入
使用下列範例為影片內容生成嵌入。
REST
下列範例使用 Cloud Storage 中的影片。您也可以使用 video.bytesBase64Encoded
欄位,提供影片的 Base64 編碼字串表示法。
使用任何要求資料之前,請先替換以下項目:
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 - PROJECT_ID:您的 Google Cloud 專案 ID。
- VIDEO_URI:要取得嵌入內容的目標影片 Cloud Storage URI。
例如:
gs://my-bucket/embeddings/supermarket-video.mp4
。您也可以提供 Base64 編碼的位元組字串:
[...] "video": { "bytesBase64Encoded": "B64_ENCODED_VIDEO" } [...]
videoSegmentConfig
(START_SECOND、END_SECOND、 INTERVAL_SECONDS)。選用。生成嵌入內容的特定影片片段 (以秒為單位)。例如:
[...] "videoSegmentConfig": { "startOffsetSec": 10, "endOffsetSec": 60, "intervalSec": 10 } [...]
使用這項設定可指定 10 秒到 60 秒的影片資料,並為下列 10 秒的影片間隔生成嵌入內容:[10, 20)、[20, 30)、[30, 40)、[40, 50)、[50, 60)。這個影片間隔 (
"intervalSec": 10
) 屬於標準影片嵌入模式,系統會以標準模式費率向使用者收費。如果省略
videoSegmentConfig
,服務會使用下列預設值:"videoSegmentConfig": { "startOffsetSec": 0, "endOffsetSec": 120, "intervalSec": 16 }
。 這個影片間隔 ("intervalSec": 16
) 屬於基本影片嵌入模式,因此系統會以基本模式價格費率向使用者收費。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict
JSON 要求主體:
{ "instances": [ { "video": { "gcsUri": "VIDEO_URI", "videoSegmentConfig": { "startOffsetSec": START_SECOND, "endOffsetSec": END_SECOND, "intervalSec": INTERVAL_SECONDS } } } ] }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict" | Select-Object -Expand Content
回覆 (7 秒影片,未指定 videoSegmentConfig
):
{ "predictions": [ { "videoEmbeddings": [ { "endOffsetSec": 7, "embedding": [ -0.0045467657, 0.0258095954, 0.0146885719, 0.00945400633, [...] -0.0023291884, -0.00493789, 0.00975185353, 0.0168156829 ], "startOffsetSec": 0 } ] } ], "deployedModelId": "DEPLOYED_MODEL_ID" }
回覆 (59 秒影片,影片片段設定如下:"videoSegmentConfig": { "startOffsetSec": 0, "endOffsetSec": 60, "intervalSec": 10 }
):
{ "predictions": [ { "videoEmbeddings": [ { "endOffsetSec": 10, "startOffsetSec": 0, "embedding": [ -0.00683252793, 0.0390476175, [...] 0.00657121744, 0.013023301 ] }, { "startOffsetSec": 10, "endOffsetSec": 20, "embedding": [ -0.0104404651, 0.0357737206, [...] 0.00509833824, 0.0131902946 ] }, { "startOffsetSec": 20, "embedding": [ -0.0113538112, 0.0305239167, [...] -0.00195809244, 0.00941874553 ], "endOffsetSec": 30 }, { "embedding": [ -0.00299320649, 0.0322436653, [...] -0.00993082579, 0.00968887936 ], "startOffsetSec": 30, "endOffsetSec": 40 }, { "endOffsetSec": 50, "startOffsetSec": 40, "embedding": [ -0.00591270532, 0.0368893594, [...] -0.00219071587, 0.0042470959 ] }, { "embedding": [ -0.00458270218, 0.0368121453, [...] -0.00317760976, 0.00595594104 ], "endOffsetSec": 59, "startOffsetSec": 50 } ] } ], "deployedModelId": "DEPLOYED_MODEL_ID" }
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Go API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
進階用途
請使用下列範例,取得影片、文字和圖片內容的嵌入。
如果是影片嵌入,您可以指定影片片段和嵌入密度。
REST
以下範例使用圖片、文字和影片資料。您可以在要求主體中使用這些資料類型的任意組合。
這個範例會使用 Cloud Storage 中的影片。您也可以使用 video.bytesBase64Encoded
欄位,提供影片的 Base64 編碼字串表示法。
使用任何要求資料之前,請先替換以下項目:
- LOCATION:專案的區域。例如
us-central1
、europe-west2
或asia-northeast3
。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。 - PROJECT_ID:您的 Google Cloud 專案 ID。
- TEXT:要取得嵌入的目標文字。例如:
a cat
。 - IMAGE_URI:要取得嵌入內容的目標圖片 Cloud Storage URI。
例如:
gs://my-bucket/embeddings/supermarket-img.png
。您也可以提供圖片做為 Base64 編碼的位元組字串:
[...] "image": { "bytesBase64Encoded": "B64_ENCODED_IMAGE" } [...]
- VIDEO_URI:要取得嵌入內容的目標影片 Cloud Storage URI。
例如:
gs://my-bucket/embeddings/supermarket-video.mp4
。您也可以提供 Base64 編碼的位元組字串:
[...] "video": { "bytesBase64Encoded": "B64_ENCODED_VIDEO" } [...]
videoSegmentConfig
(START_SECOND、END_SECOND、 INTERVAL_SECONDS)。選用。生成嵌入內容的特定影片片段 (以秒為單位)。例如:
[...] "videoSegmentConfig": { "startOffsetSec": 10, "endOffsetSec": 60, "intervalSec": 10 } [...]
使用這項設定可指定 10 秒到 60 秒的影片資料,並為下列 10 秒的影片間隔生成嵌入內容:[10, 20)、[20, 30)、[30, 40)、[40, 50)、[50, 60)。這個影片間隔 (
"intervalSec": 10
) 屬於標準影片嵌入模式,系統會以標準模式費率向使用者收費。如果省略
videoSegmentConfig
,服務會使用下列預設值:"videoSegmentConfig": { "startOffsetSec": 0, "endOffsetSec": 120, "intervalSec": 16 }
。 這個影片間隔 ("intervalSec": 16
) 屬於基本影片嵌入模式,因此系統會以基本模式價格費率向使用者收費。
HTTP 方法和網址:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict
JSON 要求主體:
{ "instances": [ { "text": "TEXT", "image": { "gcsUri": "IMAGE_URI" }, "video": { "gcsUri": "VIDEO_URI", "videoSegmentConfig": { "startOffsetSec": START_SECOND, "endOffsetSec": END_SECOND, "intervalSec": INTERVAL_SECONDS } } } ] }
如要傳送要求,請選擇以下其中一個選項:
curl
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict"
PowerShell
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/multimodalembedding@001:predict" | Select-Object -Expand Content
{ "predictions": [ { "textEmbedding": [ 0.0105433334, -0.00302835181, 0.00656806398, 0.00603460241, [...] 0.00445805816, 0.0139605571, -0.00170318608, -0.00490092579 ], "videoEmbeddings": [ { "startOffsetSec": 0, "endOffsetSec": 7, "embedding": [ -0.00673126569, 0.0248149596, 0.0128901172, 0.0107588246, [...] -0.00180952181, -0.0054573305, 0.0117037306, 0.0169312079 ] } ], "imageEmbedding": [ -0.00728622358, 0.031021487, -0.00206603738, 0.0273937676, [...] -0.00204976718, 0.00321615417, 0.0121978866, 0.0193375275 ] } ], "deployedModelId": "DEPLOYED_MODEL_ID" }
Python
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Python API 參考說明文件。
Go
在試用這個範例之前,請先按照Go使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。 詳情請參閱 Vertex AI Go API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
後續步驟
如需詳細說明文件,請參閱下列內容: