使用 RAG Engine 将 Agent Search 用作检索后端

本页面介绍了 Agent Search 与 RAG 引擎的集成。

Agent Search 提供了一种解决方案,用于在 Gemini Enterprise Agent Platform RAG 应用中检索和管理数据。通过使用 Agent Search 作为检索后端,您可以提高性能、可伸缩性和集成便利性。

  • 增强性能和可伸缩性:Agent Search 旨在以极低的延迟处理大量数据。这意味着 RAG 应用的响应速度更快,性能更出色,尤其是在处理复杂或庞大的知识库时。

  • 简化数据管理:从各种来源(例如 网站、BigQuery 数据集和 Cloud Storage 存储分区)导入数据,从而简化数据注入流程

  • 无缝集成:Agent Platform 提供与 Agent Search 的内置 集成,让您可以选择 Agent Search 作为 RAG 应用的语料库后端。这简化了集成过程,有助于确保组件之间实现最佳兼容性。

  • 提高 LLM 输出质量:通过使用 Agent Search 的检索功能,您可以帮助确保 RAG 应用从语料库中检索到最相关的信息,从而让 LLM 生成更准确、信息更丰富的输出。

Agent Search 汇集了深度信息检索、自然语言处理和大语言模型 (LLM) 处理中的 最新功能,有助于理解用户意图并为用户返回最相关的结果。

借助 Agent Search,您可以使用自己控制的数据构建 Google 品质的搜索应用。

如需设置 Agent Search,请执行以下操作:

  1. 创建搜索数据 存储区

  2. 创建搜索 应用

将 Agent Search 用作 RAG 引擎的检索后端

设置 Agent Search 后,按照以下步骤将其设置为 RAG 应用的检索后端。

将 Agent Search 设置为检索后端以创建 RAG 语料库

以下代码示例展示了如何将 Agent Search 配置为 RAG 语料库的检索后端。

REST

如需使用命令行创建 RAG 语料库,请执行以下操作:

  1. 创建 RAG 语料库

    替换代码示例中使用的以下变量:

    • PROJECT_ID:您的 Google Cloud 项目的 ID。
    • LOCATION:处理请求的区域。
    • DISPLAY_NAME:您要创建的 RAG 语料库 的显示名称。
    • ENGINE_NAME:Agent Search 引擎或 Agent Search 数据存储区的完整资源名称。例如:

      projects/PROJECT_NUMBER/locations/LOCATION/collections/default_collection/engines/ENGINE_NAME/servingConfigs/default_search

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" \
    -d '{
      "display_name" : "DISPLAY_NAME",
      "vertex_ai_search_config" : {
        "serving_config": "ENGINE_NAME/servingConfigs/default_search"
      }
    }'
    
  2. 监控进度

    替换代码示例中使用的以下变量:

    • PROJECT_ID:您的 Google Cloud 项目的 ID。
    • LOCATION:处理请求的区域。
    • OPERATION_ID:RAG 语料库创建 操作的 ID。
    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID"
    

Python

在尝试此示例之前,请按照《Agent Platform 快速入门:使用客户端库》中的 Python 设置说明进行操作。

如需向 Agent Platform 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证


import agentplatform
from agentplatform import types

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# vertex_ai_search_engine_name = "projects/{PROJECT_ID}/locations/{LOCATION}/collections/default_collection/engines/{ENGINE_ID}"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Agent Platform client once per session
client = agentplatform.Client(project=PROJECT_ID, location="us-central1")

# Configure Search
vertex_ai_search_config = types.VertexAiSearchConfig(
    serving_config=f"{vertex_ai_search_engine_name}/servingConfigs/default_search",
)

corpus = client.rag.create_corpus(
    rag_corpus=types.RagCorpus(
        display_name=display_name,
        description=description,
        vertex_ai_search_config=vertex_ai_search_config,
    ),
)
print(corpus)
# Example response:
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description'.
# ...

使用 RAG API 检索上下文

创建 RAG 语料库后,可以通过 RetrieveContexts API 从 Agent Search 检索相关上下文。

REST

此代码示例演示了如何使用 REST 检索上下文。

替换代码示例中使用的以下变量:

  • PROJECT_ID:您的 Google Cloud 项目的 ID。
  • LOCATION:处理请求的区域。
  • RAG_CORPUS_RESOURCE:RAG 语料库资源的名称。

    格式:projects/{project}/locations/{location}/ragCorpora/{rag_corpus}.

  • TEXT:要获取相关上下文的查询文本。
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" \
  -d '{
    "vertex_rag_store": {
      "rag_resources": {
        "rag_corpus": "RAG_CORPUS_RESOURCE"
      }
    },
    "query": {
      "text": "TEXT"
    }
  }'

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档


import agentplatform

from agentplatform import types
from google.genai import types as genai_types

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/[rag_corpus_id]"

# Initialize Agent Platform client once per session
client = agentplatform.Client(project=PROJECT_ID, location="us-east4")

response = client.rag.retrieve_contexts(
    vertex_rag_store=genai_types.VertexRagStore(
        rag_resources=[
            genai_types.VertexRagStoreRagResource(
                rag_corpus=corpus_name,
                # Optional: supply IDs from `rag.list_files()`.
                # rag_file_ids=["rag-file-1", "rag-file-2", ...],
            )
        ],
    ),
    query=types.RagQuery(
        text="Hello World!",
        rag_retrieval_config=genai_types.RagRetrievalConfig(
            top_k=10,
            filter=genai_types.RagRetrievalConfigFilter(
                vector_distance_threshold=0.5
            ),
        ),
    )
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

使用 Agent Platform Gemini API 生成内容

REST

如需使用 Gemini 模型生成内容,请调用 Agent Platform GenerateContent API。通过在请求中指定 RAG_CORPUS_RESOURCE,它会自动从 Agent Search 中检索数据。

替换示例代码中使用的以下变量:

  • PROJECT_ID:您的 Google Cloud 项目的 ID。

  • LOCATION:处理请求的区域。

  • MODEL_ID:用于内容生成的 LLM 模型。例如: gemini-2.0-flash

  • GENERATION_METHOD:用于生成内容的 LLM 方法。 例如:generateContentstreamGenerateContent

  • INPUT_PROMPT:发送到 LLM 用于生成 内容的文本。尝试使用与 Agent Search 中的文档相关的提示。

  • RAG_CORPUS_RESOURCE:RAG 语料库 资源的名称。格式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}

  • SIMILARITY_TOP_K:(可选)要检索的热门 上下文 数量。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" \
    -d '{
      "contents": {
        "role": "user",
        "parts": {
          "text": "INPUT_PROMPT"
        }
      },
      "tools": {
        "retrieval": {
          "disable_attribution": false,
          "vertex_rag_store": {
            "rag_resources": {
                "rag_corpus": "RAG_CORPUS_RESOURCE"
              },
            "similarity_top_k": SIMILARITY_TOP_K
          }
        }
      }
    }'
    

Python

如需了解如何安装或更新 Vertex AI SDK for Python,请参阅 安装 Vertex AI SDK for Python。 如需了解详情,请参阅 Python API 参考文档


from google import genai
from google.genai import types as genai_types

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

rag_retrieval_tool = genai_types.Tool(
    retrieval=genai_types.Retrieval(
        vertex_rag_store=genai_types.VertexRagStore(
            rag_resources=[
                genai_types.VertexRagStoreRagResource(
                    rag_corpus=corpus_name
                )
            ],
            rag_retrieval_config=genai_types.RagRetrievalConfig(
                top_k=10,
                filter=genai_types.RagRetrievalConfigFilter(
                    vector_distance_threshold=0.5
                ),
            ),
        ),
    )
)

# Create a GenAI SDK client to make a generate_content request
genai_client = genai.Client(enterprise=True, project=PROJECT_ID, location="us-central1")

response = genai_client.models.generate_content(
    model="gemini-2.5-pro",
    contents="Why is the sky blue?",
    config=genai_types.GenerateContentConfig(
        tools=[rag_retrieval_tool]
    )
)
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

后续步骤