本页介绍了可供选择的嵌入模型,并展示了如何使用所选模型创建 RAG 语料库。在 RAG 语料库的整个生命周期内,您的嵌入模型与 RAG 语料库之间的关联将保持不变。
嵌入简介
嵌入是输入的数值表示法。您可以在应用中使用嵌入来识别复杂含义和语义关系,以及处理和生成语言。
嵌入的工作原理是将文本、图片和视频转换为浮点数组(称为向量)。嵌入空间中两个向量越接近,它们的输入就越相似。
嵌入模型是语义检索系统的重要组成部分。检索系统的性能取决于嵌入模型映射数据中关系的程度。
嵌入模型选项
Vertex AI RAG 引擎实现了检索增强生成 (RAG),并为您提供了以下可在 RAG 语料库中使用的嵌入模型:
嵌入模型类型 | 说明 |
---|---|
Vertex AI 文本嵌入模型 | 由发布商(例如 Google)训练的模型。这些模型是在大量文本数据集上进行训练的,可为许多任务提供强大的基础。 |
经过微调的 Vertex AI 文本嵌入模型 | Vertex AI 文本嵌入模型经过微调,可提供专业知识或高度量身定制的性能。 |
OSS 嵌入模型 | 仅提供英语版本和多语言版本的第三方开源嵌入模型。 |
支持的嵌入模型
嵌入模型用于创建语料库,并在生成回答期间用于搜索和检索。本部分列出了支持的嵌入模型。
模型版本 | 说明 |
---|---|
text-embedding-005 |
默认的嵌入模型。 建议与 RAG 语料库搭配使用。 |
text-embedding-004 |
|
text-multilingual-embedding-002 |
建议与 RAG 语料库搭配使用。 |
textembedding-gecko@003 |
|
textembedding-gecko-multilingual@001 |
|
textembedding-gecko@002 |
仅限微调后版本。 |
textembedding-gecko@001 |
仅限微调后版本。 |
开源嵌入模型
还支持以下开放嵌入模型。您可以在 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 搭配使用时是首选模型。
如需确定使用的默认嵌入模型,或者需要查看建议与 RAG 语料库搭配使用的 Gecko 模型列表,请参阅支持的嵌入模型。
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.EmbeddingModelConfig(
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, 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.EmbeddingModelConfig(
endpoint=${MODEL_ENDPOINT},
)
# Name your corpus
DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
rag_corpus = rag.create_corpus(
display_name=${DISPLAY_NAME}, 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 亿 | 1024 | ✔ |
e5-small-v2 |
MiniLM |
3300 万 | 384 | ✔ |
multilingual-e5-large |
xlm-roberta-large |
5.6 亿 | 1024 | ✗ |
multilingual-e5-small |
microsoft/Multilingual-MiniLM-L12-H384 |
1.18 亿 | 384 | ✗ |
如需将 E5 模型与 Vertex AI RAG Engine 搭配使用,必须从 Model Garden 部署 E5 模型。如需部署 E5 模型,请参阅 Google Cloud 控制台中的 E5 Text Embedding。
以下代码示例演示了如何使用已部署的 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.EmbeddingModelConfig(
endpoint=MODEL_ENDPOINT,
)
# Name your corpus
DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
rag_corpus = rag.create_corpus(
display_name=DISPLAY_NAME, embedding_model_config=embedding_model_config
)