本頁面說明嵌入模型選項,並示範如何使用嵌入模型建立 RAG 語料庫。在 RAG 語料庫的生命週期內,嵌入模型與 RAG 語料庫的關聯會維持不變。
嵌入簡介
嵌入是指以數值表示輸入內容,您可以在應用程式中使用嵌入,辨識複雜的意義和語意關係,以及處理和生成語言。
嵌入技術會將文字、圖片和影片轉換為浮點數陣列,也就是向量。兩個向量在嵌入空間中的距離越近,輸入內容的相似度就越高。
嵌入模型是語意檢索系統的重要元件。檢索系統的效能取決於嵌入模型對資料中關係的對應程度。
嵌入模型選項
Vertex AI RAG Engine 會實作檢索增強生成 (RAG),並提供下列嵌入模型供您選擇,以便在 RAG 語料庫中使用:
嵌入模型類型 | 說明 |
---|---|
Vertex AI 文字嵌入模型 | 由發布商 (例如 Google) 訓練的模型。這些模型以大量文字資料集訓練而成,可為許多工作提供強大的基準。 |
微調 Vertex AI 文字嵌入模型 | Vertex AI 文字嵌入模型經過微調,具備專業知識或高度客製化的效能。 |
OSS 嵌入模型 | 第三方開放原始碼嵌入模型,僅提供英文和多語言變體。 |
支援的嵌入模型
嵌入模型可用於建立語料庫,以及在生成回覆期間搜尋和擷取資料。本節列出支援的嵌入模型。
模型版本 | 說明 |
---|---|
text-embedding-005 |
預設嵌入模型。 建議搭配 RAG 語料庫使用。 |
text-embedding-004 |
|
text-multilingual-embedding-002 |
建議搭配 RAG 語料庫使用。 |
開放原始碼嵌入模型
系統也支援下列開放式嵌入模型。您可以在Model Garden 中找到這些模型。
e5-base-v2
e5-large-v2
e5-small-v2
multilingual-e5-large
multilingual-e5-small
使用 Vertex AI 文字嵌入模型
Vertex AI 文字嵌入 API 使用 Gecko 嵌入模型,可產生 768 維度的密集嵌入向量。密集嵌入會儲存文字的意義,這與稀疏向量不同,後者傾向於直接將字詞對應至數字。在生成式 AI 中使用密集向量嵌入的好處是,您不必搜尋直接相符的字詞或語法,而是可以更有效地搜尋與查詢意義一致的段落,即使這些段落使用的語言不同也沒關係。
Gecko 模型
Gecko 模型提供僅支援英文和多語言的版本。與微調的 Gecko 模型不同,發布者 Gecko 模型不需要部署,因此是搭配 Vertex AI RAG 引擎使用的首選模型。
如要找出使用的預設嵌入模型,或需要建議搭配 RAG 語料庫使用的 Gecko 模型清單,請參閱「支援的嵌入模型」。
Gecko 模型停用後
發布商 Gecko 模型可能已停用。如果發生這種情況,即使是停用前建立的 RAG 語料庫,也無法搭配 Vertex AI RAG 引擎使用發布商 Gecko 模型。Gecko 模型停用後,您必須遷移 RAG 語料庫,也就是建立新的 RAG 語料庫並重新匯入資料。您也可以使用微調的 Gecko 模型或自行部署的 OSS 嵌入模型,這些模型在停用後仍可使用。
使用發布者 Gecko 模型建立 RAG 語料庫
這些程式碼範例示範如何使用發布商 Gecko 模型建立 RAG 語料庫。
curl
ENDPOINT=us-central1-aiplatform.googleapis.com
PROJECT_ID=YOUR_PROJECT_ID
// Set this to your choice of publisher Gecko model. Note that the full resource name of the publisher model is required.
// Example: projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/text-embedding-004
ENDPOINT_NAME=YOUR_ENDPOINT_NAME
// Set a display name for your corpus.
// For example, "my test corpus"
CORPUS_DISPLAY_NAME=YOUR_CORPUS_DISPLAY_NAME
// CreateRagCorpus
// Input: ENDPOINT, PROJECT_ID, ENDPOINT_NAME, CORPUS_DISPLAY_NAME
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${ENDPOINT}/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
-d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"rag_embedding_model_config" : {
"vertex_prediction_endpoint": {
"endpoint": '\""${ENDPOINT_NAME}"\"'
}
}
}'
Python 適用的 Vertex AI SDK
import vertexai
from vertexai import rag
# Set Project
PROJECT_ID = "YOUR_PROJECT_ID"
vertexai.init(project=${PROJECT_ID}, location="us-central1")
# Configure a Google first-party embedding model
embedding_model_config = rag.RagEmbeddingModelConfig(
publisher_model="publishers/google/models/text-embedding-004"
)
# Name your corpus
DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
rag_corpus = rag.create_corpus(
display_name=DISPLAY_NAME, rag_embedding_model_config=embedding_model_config
)
使用微調的 Vertex AI 文字嵌入模型
雖然基礎發布商模型是以大量文字資料集訓練而成,且能為許多工作提供強大的基準,但有時您可能需要模型具備專業知識或高度客製化的效能。在這種情況下,您可以透過模型微調,使用相關資料微調模型的表示方式。這種做法的另一個好處是,模型經過微調後,產生的圖片會歸您所有,且不會受到 Gecko 模型淘汰的影響。所有微調後的 Gecko 嵌入模型都會生成包含 768 維向量的嵌入。如要進一步瞭解這些模型,請參閱「取得文字嵌入」。
如要進一步瞭解如何調整嵌入模型,請參閱「調整文字嵌入」。
這些程式碼範例說明如何使用已部署的微調 Gecko 模型建立 RAG 語料庫。
curl
ENDPOINT=us-central1-aiplatform.googleapis.com
PROJECT_ID=YOUR_PROJECT_ID
// Your Vertex AI endpoint resource with the deployed fine-tuned Gecko model
// Example: projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}
ENDPOINT_NAME=YOUR_ENDPOINT_NAME
// Set a display name for your corpus.
// For example, "my test corpus"
CORPUS_DISPLAY_NAME=YOUR_CORPUS_DISPLAY_NAME
// CreateRagCorpus
// Input: ENDPOINT, PROJECT_ID, ENDPOINT_NAME, CORPUS_DISPLAY_NAME
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${ENDPOINT}/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
-d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"rag_embedding_model_config" : {
"vertex_prediction_endpoint": {
"endpoint": '\""${ENDPOINT_NAME}"\"'
}
}
}'
Python 適用的 Vertex AI SDK
import vertexai
from vertexai import rag
# Set Project
PROJECT_ID = "YOUR_PROJECT_ID"
vertexai.init(project=${PROJECT_ID}, location="us-central1")
# Your Vertex Endpoint resource with the deployed fine-tuned Gecko model
ENDPOINT_ID = "YOUR_MODEL_ENDPOINT_ID"
MODEL_ENDPOINT = "projects/${PROJECT_ID}/locations/us-central1/endpoints/${ENDPOINT_ID}"
embedding_model_config = rag.RagEmbeddingModelConfig(
endpoint=${MODEL_ENDPOINT},
)
# Name your corpus
DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
rag_corpus = rag.create_corpus(
display_name=${DISPLAY_NAME}, rag_embedding_model_config=embedding_model_config
)
使用 OSS 嵌入模型
Vertex AI RAG Engine 支援第三方開放原始碼嵌入模型,包括僅支援英文和支援多種語言的版本。下表列出支援的 E5 型號。
模型版本 | 基礎模型 | 參數 | 嵌入維度 | 僅支援英文 |
---|---|---|---|---|
e5-base-v2 |
MiniLM |
1.09 億 | 768 | ✔ |
e5-large-v2 |
MiniLM |
3.35 億 | 1,024 | ✔ |
e5-small-v2 |
MiniLM |
3,300 萬 | 384 | ✔ |
multilingual-e5-large |
xlm-roberta-large |
5.6 億 | 1,024 | ✗ |
multilingual-e5-small |
microsoft/Multilingual-MiniLM-L12-H384 |
1.18 億 | 384 | ✗ |
如要搭配 Vertex AI RAG 引擎使用 E5 模型,必須從 Model Garden 部署 E5 模型。如要部署 E5 模型,請參閱 Google Cloud 控制台中的「E5 文字嵌入」。
這些程式碼範例示範如何使用已部署的 E5 模型建立 RAG 語料庫。
curl
ENDPOINT=us-central1-aiplatform.googleapis.com
PROJECT_ID=YOUR_PROJECT_ID
// Your Vertex Endpoint resource with the deployed E5 model
// Example: projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}
ENDPOINT_NAME=YOUR_ENDPOINT_NAME
// Set a display name for your corpus.
// For example, "my test corpus"
CORPUS_DISPLAY_NAME=YOUR_CORPUS_DISPLAY_NAME
// CreateRagCorpus
// Input: ENDPOINT, PROJECT_ID, ENDPOINT_NAME, CORPUS_DISPLAY_NAME
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${ENDPOINT}/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
-d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME</var>}"\"',
"rag_embedding_model_config" : {
"vertex_prediction_endpoint": {
"endpoint": '\""${ENDPOINT_NAME}"\"'
}
}
}'
Python 適用的 Vertex AI SDK
import vertexai
from vertexai import rag
# Set Project
PROJECT_ID = "YOUR_PROJECT_ID"
vertexai.init(project=PROJECT_ID, location="us-central1")
# Your Vertex Endpoint resource with the deployed E5 model
ENDPOINT_ID = "YOUR_MODEL_ENDPOINT_ID"
MODEL_ENDPOINT = "projects/{PROJECT_ID}/locations/us-central1/endpoints/{ENDPOINT_ID}"
embedding_model_config = rag.RagEmbeddingModelConfig(
endpoint=MODEL_ENDPOINT,
)
# Name your corpus
DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
rag_corpus = rag.create_corpus(
display_name=DISPLAY_NAME, rag_embedding_model_config=embedding_model_config
)