本頁面說明嵌入模型的選項,並說明如何使用嵌入模型建立 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 Engine 使用的理想模型。
如要找出所使用的預設嵌入模型,或需要建議的 Gecko 模型清單,以便搭配 RAG 語料庫使用,請參閱「支援的嵌入模型」。
Gecko 型號停產時
發布商 Gecko 模型可能會停用。如果發生這種情況,發布者 Gecko 模型就無法與 Vertex AI RAG Engine 搭配使用,即使是停用前建立的 RAG 語料庫也是如此。當 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 |
109M | 768 | ✔ |
e5-large-v2 |
MiniLM |
335M | 1,024 | ✔ |
e5-small-v2 |
MiniLM |
33M | 384 | ✔ |
multilingual-e5-large |
xlm-roberta-large |
560 百萬 | 1,024 | ✗ |
multilingual-e5-small |
microsoft/Multilingual-MiniLM-L12-H384 |
1.18 億 | 384 | ✗ |
如要搭配 Vertex AI RAG Engine 使用 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
)