将 Vector Search 2.0 与 RAG 搭配使用

本页面介绍了 Gemini Enterprise Agent Platform 上的 RAG 引擎如何使用 Gemini Enterprise Agent Platform Vector Search 2.0,后者是 一 Google Cloud 种产品,可作为全面的存储和检索 系统来存储和管理文档的向量表示法。然后,Gemini Enterprise Agent Platform Vector Search 2.0 会根据文档与给定查询的语义相似度检索相关文档。

RAG 将为您管理 Vector Search 2.0 集合,并且您将拥有对项目中 Vector Search 2.0 集合的完全访问权限。

选择 RAG 后端

在 Gemini Enterprise Agent Platform 上使用 RAG 引擎时,您可以为 RAG 语料库选择不同的后端存储选项。下表总结了主要区别:

后端选项 底层技术 由 RAG 引擎管理 项目中的数据可见性 CMEK 支持
RagManagedVertexVectorSearch Gemini Enterprise Agent Platform Vector Search 2.0 由 Google 全面管理。 在您的 Google Cloud 项目中可见
VertexVectorSearch Gemini Enterprise Agent Platform Vector Search 1.0 用户需要设置、管理和清理 Vector Search 1.0 实例。 在您的 Google Cloud 项目中可见
RagManagedDb Spanner 由 Google 全面管理。 在您的项目中不可直接见

选择 RagManagedVertexVectorSearch 可获得最新功能、自动化管理以及 Vector Search 2.0 数据的透明度。如果 CMEK 支持是严格要求,请考虑 RagManagedDb,但请注意无法直接查看后端数据库。

启用 Vector Search API

如需使用 RAG 托管式 Gemini Enterprise Agent Platform Vector Search,您需要先 启用 Vector Search API

使用 RagManagedVertexVectorSearch 创建 RAG 语料库

此代码示例演示了如何使用 RagManagedVertexVectorSearch 创建 RAG 语料库。

Python

from vertexai.preview import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION
DISPLAY_NAME = YOUR_RAG_CORPUS_DISPLAY_NAME

# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

vector_db = rag.RagManagedVertexVectorSearch()
rag_corpus = rag.create_corpus(
    display_name=DISPLAY_NAME, backend_config=rag.RagVectorDbConfig(vector_db=vector_db))

REST

执行以下变量替换操作:

  • PROJECT_ID:您的项目 ID。
  • LOCATION:处理请求的区域。
  • CORPUS_DISPLAY_NAME:RAG 语料库的显示名称。
PROJECT_ID=PROJECT_ID
LOCATION=LOCATION
CORPUS_DISPLAY_NAME=CORPUS_DISPLAY_NAME

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora \
-d '{
      "display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
      "vector_db_config": {
        "rag_managed_vertex_vector_search": {}
      }
    }'

将数据导入 RagManagedVertexVectorSearch

您可以使用 ImportRagFiles API 或 UploadRagFile API 将数据导入 RagManagedVertexVectorSearch

如需将本地文件上传到 RAG 语料库,请参阅上传 RAG 文件。如需将数据导入 RAG 语料库,请参阅以下代码示例,该示例演示了如何从 Cloud Storage 导入数据。如需了解支持的 数据源,请参阅 RAG 支持的数据源

Python

from vertexai.preview import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION
CORPUS_ID = YOUR_CORPUS_ID
PATHS = ["gs://my_bucket/my_files_dir"]

# Initialize Agent Platform API once per session
vertexai.init(project=PROJECT_ID, location=LOCATION)

corpus_name = f"projects/{PROJECT_ID}/locations/{LOCATION}/ragCorpora/{CORPUS_ID}"
# This is a non blocking call.
response = await rag.import_files_async(
    corpus_name=corpus_name,
    paths=PATHS,
)

# Wait for the import to complete.
await response.result()

REST

GCS_URI=GCS_URI

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora/${CORPUS_ID}/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": '\""${GCS_URI}"\"',
      },
  }
}'

后续步骤