本指南說明如何調整文字嵌入模型,提升模型在特定工作上的效能。本指南涵蓋下列主題: 基礎嵌入模型是以大量文字資料集預先訓練而成,可為許多工作提供強大的基準。如果需要專業知識或高度客製化的效能,您可以使用模型調整功能,透過自有資料微調模型的表示方式。 文字嵌入模型支援監督式調整。監督式微調會使用標記範例,在推論期間展示您希望文字嵌入模型提供的輸出類型。 如要進一步瞭解模型調整作業,請參閱「模型調整作業的運作方式」。 Vertex AI 會使用高效參數調整方法進行自訂。在公開檢索基準資料集上執行的實驗中,這個方法顯示出顯著的品質提升,最高可達 41% (平均 12%)。 調整文字嵌入模型有助於讓模型適應特定領域或工作。如果預先訓練的模型不符合需求,這項功能就非常實用。舉例來說,如果根據貴公司的客戶服務單微調嵌入模型,聊天機器人就能更瞭解常見問題,並更有效地回答問題。未經過微調的模型缺乏支援單和產品解決方案的相關知識。 在 Vertex AI 上調整 調整作業完成後,調整後的模型會新增至 Model Registry。與其他模型微調工作不同,文字嵌入微調工作不會自動將模型部署至端點。您必須手動部署微調模型。 用於微調嵌入模型的資料集包含與您希望模型執行的工作相符的資料。 訓練資料集包含下列檔案,這些檔案必須位於 Cloud Storage。啟動微調管線時,您可以使用參數定義每個檔案的路徑。 語料庫檔案:路徑由 查詢檔案:路徑由 訓練標籤:路徑由 以下是 測試標籤 (選用):測試標籤的格式與訓練標籤相同。請使用 驗證標籤 (選用):驗證標籤的格式與訓練標籤相同。請使用 提供的資料集檔案必須符合下列限制: 您可以使用 Vertex AI Pipelines 平台,在專案中執行調整作業。 設定權限 管道會在兩個服務代理程式下執行訓練程式碼。您必須授予這些服務代理程式特定角色,才能使用專案和資料集執行訓練。 Compute Engine 預設服務帳戶
您可以指定自訂服務帳戶,而非 Compute Engine 預設服務帳戶。詳情請參閱「設定具有精細權限的服務帳戶」。 Vertex AI 調整用服務代理
如要進一步瞭解如何設定 Cloud Storage 資料集權限,請參閱「設定管道構件的 Cloud Storage 值區」。 微調需要 GPU 加速器。您可以為文字嵌入調整管道使用下列任一加速器: 如要啟動微調作業,您必須為所選的加速器類型和區域 (例如 部分地區不提供某些加速器。詳情請參閱「在 Vertex AI 中使用加速器」。 您可以使用 Google Cloud 控制台、REST API 或用戶端程式庫,建立嵌入模型微調工作。 如要建立嵌入模型微調工作,請使用
使用任何要求資料之前,請先替換以下項目:
HTTP 方法和網址: JSON 要求主體: 如要傳送要求,請展開以下其中一個選項: 您應該會收到如下的 JSON 回應: 啟動管道後,請透過 Google Cloud 控制台追蹤微調工作的進度。
如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。
詳情請參閱
Python API 參考說明文件。
在試用這個範例之前,請先按照Java使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。
詳情請參閱 Vertex AI Java API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
在試用這個範例之前,請先按照Node.js使用用戶端程式庫的 Vertex AI 快速入門中的操作說明進行設定。
詳情請參閱 Vertex AI Node.js API 參考說明文件。
如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。 如要使用 Google Cloud 控制台微調文字嵌入模型,請按照下列步驟啟動自訂管道: 文字嵌入調整作業支援 VPC Service Controls。如要在虛擬私有雲 (VPC) 中執行微調作業,請在建立 如要使用客戶自行管理的加密金鑰 (CMEK),請在建立 模型調整工作完成後,系統不會自動將調整後的模型部署至端點。您可以在 Model Registry 中找到調整後的模型,做為模型資源。您可以使用 Google Cloud 控制台,查看目前專案中的模型清單,包括微調模型。 如要在 Google Cloud 控制台中查看經過微調的模型,請前往 Vertex AI Model Registry 頁面。 微調嵌入模型後,您需要部署模型資源。如要部署調整後的嵌入模型,請參閱「將模型部署至端點」。 與基礎模型不同,您需要管理微調後的文字嵌入模型。包括管理服務資源,例如機器類型和加速器。為避免預測期間發生記憶體不足錯誤,建議您使用 與 部署微調模型後,您可以使用下列任一指令,將要求傳送至微調模型端點。 微調 如要從微調版 其他模型的 cURL 指令範例 其他模型的微調版本 (例如 輸出內容範例 無論版本為何,這項輸出內容都適用於
textembedding-gecko
和 textembedding-gecko-multilingual
模型穩定版支援微調。預期品質提升幅度
微調嵌入模型的用途
調整工作流程
textembedding-gecko
和 textembedding-gecko-multilingual
的模型時,工作流程包含下列步驟:
準備嵌入資料集
調整嵌入模型時的資料集格式
corpus_path
參數定義。這是 JSONL 檔案,每行都有 _id
、title
和 text
欄位,且值為字串。_id
和 text
為必填欄位,title
則為選填欄位。以下是 corpus.jsonl
檔案範例:{"_id": "doc1", "title": "Get an introduction to generative AI on Vertex AI", "text": "Vertex AI Studio offers a Google Cloud console tool for rapidly prototyping and testing generative AI models. Learn how you can use Vertex AI Studio to test models using prompt samples, design and save prompts, tune a foundation model, and convert between speech and text."}
{"_id": "doc2", "title": "Use gen AI for summarization, classification, and extraction", "text": "Learn how to create text prompts for handling any number of tasks with Vertex AI's generative AI support. Some of the most common tasks are classification, summarization, and extraction. Vertex AI's PaLM API for text lets you design prompts with flexibility in terms of their structure and format."}
{"_id": "doc3", "title": "Custom ML training overview and documentation", "text": "Get an overview of the custom training workflow in Vertex AI, the benefits of custom training, and the various training options that are available. This page also details every step involved in the ML training workflow from preparing data to predictions."}
{"_id": "doc4", "text": "Text embeddings are useful for clustering, information retrieval, retrieval-augmented generation (RAG), and more."}
{"_id": "doc5", "title": "Text embedding tuning", "text": "Google's text embedding models can be tuned on Vertex AI."}
queries_path
參數定義。查詢檔案包含查詢範例,採用 JSONL 格式,且與語料庫檔案具有相同欄位。以下是 queries.jsonl
檔案範例:{"_id": "query1", "text": "Does Vertex support generative AI?"}
{"_id": "query2", "text": "What can I do with Vertex GenAI offerings?"}
{"_id": "query3", "text": "How do I train my models using Vertex?"}
{"_id": "query4", "text": "What is a text embedding?"}
{"_id": "query5", "text": "Can text embedding models be tuned on Vertex?"}
{"_id": "query6", "text": "embeddings"}
{"_id": "query7", "text": "embeddings for rag"}
{"_id": "query8", "text": "custom model training"}
{"_id": "query9", "text": "Google Cloud PaLM API"}
train_label_path
參數定義,這是訓練標籤資料的 Cloud Storage URI。標籤必須位於含有標題的 TSV 檔案中。訓練標籤檔案必須包含查詢和語料庫的子集。檔案必須包含 query-id
、corpus-id
和 score
欄。
query-id
:與查詢檔案中的 _id
鍵相符的字串。corpus-id
:與語料庫檔案中的 _id
鍵相符的字串。score
:非負整數。如果分數大於 0,表示文件與查詢相關。分數越高,代表關聯性越高。如未填寫分數,預設值為 1。如果查詢和文件不相關,您可以從標籤檔案中省略該查詢和文件,也可以納入該查詢和文件,但分數為 0。train_labels.tsv
檔案範例:query-id corpus-id score
query1 doc1 1
query2 doc2 1
query3 doc3 2
query3 doc5 1
query4 doc4 1
query4 doc5 1
query5 doc5 2
query6 doc4 1
query6 doc5 1
query7 doc4 1
query8 doc3 1
query9 doc2 1
test_label_path
參數指定這些目標。如未提供 test_label_path
,系統會自動從訓練標籤中分割測試標籤。validation_label_path
參數指定這些目標。如未提供 validation_label_path
,系統會自動從訓練標籤中分割驗證標籤。資料集大小規定
為 Vertex AI Pipelines 設定專案
PROJECT_NUMBER-compute@developer.gserviceaccount.com
這個服務帳戶需要下列權限:
Storage Object Viewer
存取您在 Cloud Storage 中建立的每個資料集檔案。Storage Object User
存取管道的輸出 Cloud Storage 目錄,PIPELINE_OUTPUT_DIRECTORY。Vertex AI User
存取專案。service-PROJECT_NUMBER@gcp-sa-aiplatform-ft.iam.gserviceaccount.com
這個服務帳戶需要下列權限:
Storage Object Viewer
存取您在 Cloud Storage 中建立的每個資料集檔案。Storage Object User
存取管道的輸出 Cloud Storage 目錄,PIPELINE_OUTPUT_DIRECTORY。使用加速器
NVIDIA_L4
NVIDIA_TESLA_A100
NVIDIA_TESLA_T4
NVIDIA_TESLA_V100
NVIDIA_TESLA_P100
Restricted image training Nvidia V100 GPUs per region
) 擁有足夠的 Restricted image training GPUs
配額。如要提高專案配額,請參閱「申請更多配額」。建立嵌入模型微調工作
REST
projects.locations.pipelineJobs.create
方法。
PROJECT_ID
:您的 Google Cloud 專案 ID。PIPELINE_OUTPUT_DIRECTORY
:管道輸出構件的路徑,開頭為「gs://」。POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/pipelineJobs
{
"displayName": "tune_text_embeddings_model_sample",
"runtimeConfig": {
"gcsOutputDirectory": "PIPELINE_OUTPUT_DIRECTORY",
"parameterValues": {
"corpus_path": "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/corpus.jsonl",
"queries_path": "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/queries.jsonl",
"train_label_path": "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/train.tsv",
"test_label_path": "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/test.tsv",
"base_model_version_id":"text-embedding-004",
"task_type": "DEFAULT",
"batch_size": "128",
"train_steps": "1000",
"output_dimensionality": "768",
"learning_rate_multiplier": "1.0"
}
},
"templateUri": "https://us-kfp.pkg.dev/ml-pipeline/llm-text-embedding/tune-text-embedding-model/v1.1.3"
}
Python
Java
Node.js
控制台
corpus_path
、queries_path
和 train_label_path
,格式請參閱「準備嵌入資料集」。如要進一步瞭解各個參數,請參閱本節的「REST」分頁。其他支援的功能
PipelineJob
時傳遞 network
參數。PipelineJob
時,將金鑰傳遞至 parameterValues.encryption_spec_key_name
管道參數和 encryptionSpec.kmsKeyName
參數。使用微調後的模型
在 Model Registry 中查看調整後的模型
部署模型
NVIDIA_TESLA_A100
GPU 類型進行部署,這類 GPU 可支援任何輸入長度的批次大小 (最多 5 個)。textembedding-gecko
基礎模型類似,微調模型最多支援 3,072 個權杖,並可截斷較長的輸入內容。取得已部署模型的預測結果
textembedding-gecko@001
模型的 cURL 指令範例textembedding-gecko@001
取得預測結果,請使用下列 curl 指令。PROJECT_ID=PROJECT_ID
LOCATION=LOCATION
ENDPOINT_URI=https://${LOCATION}-aiplatform.googleapis.com
MODEL_ENDPOINT=TUNED_MODEL_ENDPOINT_ID
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
${ENDPOINT_URI}/v1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${MODEL_ENDPOINT}:predict \
-d '{
"instances": [
{
"content": "Dining in New York City"
},
{
"content": "Best resorts on the east coast"
}
]
}'
textembedding-gecko@003
和 textembedding-gecko-multilingual@001
) 則需要另外兩個輸入內容:task_type
和 title
。如要進一步瞭解這些參數,請參閱 curl 指令。PROJECT_ID=PROJECT_ID
LOCATION=LOCATION
ENDPOINT_URI=https://${LOCATION}-aiplatform.googleapis.com
MODEL_ENDPOINT=TUNED_MODEL_ENDPOINT_ID
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
${ENDPOINT_URI}/v1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${MODEL_ENDPOINT}:predict \
-d '{
"instances": [
{
"content": "Dining in New York City",
"task_type": "DEFAULT",
"title": ""
},
{
"content": "There are many resorts to choose from on the East coast...",
"task_type": "RETRIEVAL_DOCUMENT",
"title": "East Coast Resorts"
}
]
}'
textembedding-gecko
和 textembedding-gecko-multilingual
模型。textembedding-gecko
{
"predictions": [
[ ... ],
[ ... ],
...
],
"deployedModelId": "...",
"model": "projects/.../locations/.../models/...",
"modelDisplayName": "tuned-text-embedding-model",
"modelVersionId": "1"
}
後續步驟
調整文字嵌入
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-19 (世界標準時間)。