本页面介绍了如何将 Vertex AI RAG 引擎连接到 Vertex AI Vector Search。
您也可以使用此笔记本 Vertex AI RAG 引擎搭配 Vertex AI Vector Search 进行操作。
Vertex AI RAG 引擎是一款强大的工具,使用由 Spanner 提供技术支持的内置向量数据库来存储和管理文本文档的向量表示法。向量数据库可根据文档与给定查询的语义相似度高效检索相关文档。通过将 Vertex AI Vector Search 集成为额外的向量数据库,并与 Vertex AI RAG 引擎搭配使用,您可以使用 Vector Search 的功能以低延迟处理大量数据,从而提高 RAG 应用的性能和可伸缩性。
Vertex AI Vector Search 设置
Vertex AI Vector Search 基于 Google 研究开发的 Vector Search 技术。借助 Vector Search,您可以利用为 Google 搜索、YouTube 和 Google Play 等 Google 产品奠定基础的基础设施。
如需与 Vertex AI RAG Engine 集成,您需要有一个空的 Vector Search 索引。
设置 Vertex AI SDK
如需设置 Vertex AI SDK,请参阅设置。
创建 Vector Search 索引
如需创建与 RAG 语料库兼容的 Vector Search 索引,该索引必须满足以下条件:
IndexUpdateMethod
必须为STREAM_UPDATE
,请参阅创建流索引。距离衡量类型必须明确设置为以下其中一种类型:
DOT_PRODUCT_DISTANCE
COSINE_DISTANCE
向量的维度必须与您计划在 RAG 语料库中使用的嵌入模型一致。其他参数可以根据您的选择进行调整,您的选择决定了是否可以调整其他参数。
Python 版 Vertex AI SDK
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。
创建 Vector Search 索引端点
Vertex AI RAG 引擎支持公共端点。
Python 版 Vertex AI SDK
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。
将索引部署到索引端点
在执行最近邻搜索之前,必须将索引部署到索引端点。
Python 版 Vertex AI SDK
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。
如果这是您首次将索引部署到索引端点,则大约需要 30 分钟才能自动构建并启动后端,然后才能存储索引。首次部署后,索引会在几秒钟内就绪。如需查看索引部署的状态,请打开 Vector Search 控制台,选择索引端点标签页,然后选择您的索引端点。
确定索引和索引端点的资源名称,其格式如下:
projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}
。
在 Vertex AI RAG 引擎中使用 Vertex AI Vector Search
设置 Vector Search 实例后,按照本部分中的步骤将 Vector Search 实例设置为 RAG 应用的向量数据库。
设置向量数据库以创建 RAG 语料库
创建 RAG 语料库时,仅指定完整的 INDEX_ENDPOINT_NAME
和 INDEX_NAME
。确保索引和索引端点资源名称都使用数字 ID。RAG 语料库会进行创建,并自动与 Vector Search 索引关联。系统会对条件执行验证。如果不符合任何要求,请求会被拒绝。
Python
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
REST
# TODO(developer): Update and un-comment the following lines:
# CORPUS_DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
# Full index/indexEndpoint resource name
# Index: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
# IndexEndpoint: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}
# INDEX_RESOURCE_NAME = "YOUR_INDEX_ENDPOINT_RESOURCE_NAME"
# INDEX_NAME = "YOUR_INDEX_RESOURCE_NAME"
# Call CreateRagCorpus API to create a new RagCorpus
curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" https://${LOCATION_ID}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/ragCorpora -d '{
"display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
"rag_vector_db_config" : {
"vertex_vector_search": {
"index":'\""${INDEX_NAME}"\"'
"index_endpoint":'\""${INDEX_ENDPOINT_NAME}"\"'
}
}
}'
# Call ListRagCorpora API to verify the RagCorpus is created successfully
curl -sS -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://${LOCATION_ID}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/ragCorpora"
使用 RAG API 导入文件
使用 ImportRagFiles
API 将文件从 Cloud Storage 或 Google 云端硬盘导入到 Vector Search 索引。这些文件会嵌入到 Vector Search 索引并存储在其中。
REST
# TODO(developer): Update and uncomment the following lines:
# RAG_CORPUS_ID = "your-rag-corpus-id"
#
# Google Cloud Storage bucket/file location.
# For example, "gs://rag-fos-test/"
# GCS_URIS= "your-gcs-uris"
# Call ImportRagFiles API to embed files and store in the BigQuery table
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}/ragFiles:import \
-d '{
"import_rag_files_config": {
"gcs_source": {
"uris": '\""${GCS_URIS}"\"'
},
"rag_file_chunking_config": {
"chunk_size": 512
}
}
}'
# Call ListRagFiles API to verify the files are imported successfully
curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}/ragFiles
Vertex AI SDK for Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。
使用 RAG API 检索相关上下文
文件导入完成后,您可以使用 RetrieveContexts
API 从 Vector Search 索引检索相关上下文。
REST
# TODO(developer): Update and uncomment the following lines:
# RETRIEVAL_QUERY="your-retrieval-query"
#
# Full RAG corpus resource name
# Format:
# "projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}"
# RAG_CORPUS_RESOURCE="your-rag-corpus-resource"
# Call RetrieveContexts API to retrieve relevant contexts
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1:retrieveContexts \
-d '{
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
},
},
"query": {
"text": '\""${RETRIEVAL_QUERY}"\"',
"similarity_top_k": 10
}
}'
Vertex AI SDK for Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。
使用 Vertex AI Gemini API 生成内容
如需使用 Gemini 模型生成内容,请调用 Vertex AI GenerateContent
API。通过在请求中指定 RAG_CORPUS_RESOURCE
,该 API 会自动从 Vector Search 索引中检索数据。
REST
# TODO(developer): Update and uncomment the following lines:
# MODEL_ID=gemini-1.5-flash-001
# GENERATE_CONTENT_PROMPT="your-generate-content-prompt"
# GenerateContent with contexts retrieved from the FeatureStoreOnline index
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:generateContent \
-d '{
"contents": {
"role": "user",
"parts": {
"text": '\""${GENERATE_CONTENT_PROMPT}"\"'
}
},
"tools": {
"retrieval": {
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
},
"similarity_top_k": 8,
"vector_distance_threshold": 0.32
}
}
}
}'
Vertex AI SDK for Python
如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python 版 Vertex AI SDK API 参考文档。