RAG Engine を使用して Agent Search を検索バックエンドとして使用する

このページでは、Agent Search と RAG Engine のインテグレーションについて説明します。

Agent Search は、Gemini Enterprise Agent Platform RAG アプリケーション内でデータを取得および管理するためのソリューションを提供します。Agent Search を取得バックエンドとして使用することにより、パフォーマンス、スケーラビリティ、インテグレーションの容易さを向上させることができます。

  • パフォーマンスとスケーラビリティの向上: Agent Search は 非常に低いレイテンシで大量のデータを処理するように設計されています。これにより、複雑であったり広範なナレッジベースを扱うときには特に、RAG アプリケーションの応答時間が短縮され、パフォーマンスが向上します。

  • シンプルなデータ マネジメント: ウェブサイト、BigQuery データセット、Cloud Storage バケットなどのさまざまなソースからデータをインポートして、データの取り込みプロセスを効率化できます。

  • シームレスな統合: Agent Platform にはエージェント検索との統合が組み込まれているため、RAG アプリケーションのコーパス バックエンドとしてエージェント検索を選択できます。これにより、インテグレーション プロセスが簡素化され、コンポーネント間の最適な互換性が確保されます。

  • LLM 出力の品質の向上: Agent Search の検索機能を使用することで、RAG アプリケーションがコーパスから最も関連性の高い情報を確実に取得できるようになります。これにより、LLM によって生成される出力の精度と有用性が向上します。

Agent Searchは、詳細な情報検索、自然言語処理、大規模言語モデル(LLM)処理の最新機能を組み合わせて、ユーザーの意図を理解し、ユーザーにとって最も関連性の高い検索結果を返します。

Agent Search を使用すると、管理するデータを使用して Google 品質の検索アプリケーションをビルドできます。

Agent Search を設定するには、次の操作を行います。

  1. 検索データ ストアを作成する。

  2. 検索 アプリケーションを作成する。

Agent Search を RAG Engine の検索バックエンドとして使用する

Agent Search が設定されたら、次の手順に沿って RAG アプリケーションの取得バックエンドとして設定します。

Agent Search を検索バックエンドとして設定して RAG コーパスを作成する

次のコードサンプルは、Agent Search を RAG コーパスの検索バックエンドとして構成する方法を示しています。

REST

コマンドラインを使用して RAG コーパスを作成する手順は次のとおりです。

  1. RAG コーパスを作成する

    コードサンプルで使用されている次の変数を置き換えます。

    • PROJECT_ID: Cloud de Confiance by S3NS プロジェクトの ID。
    • LOCATION: リクエストを処理するリージョン。
    • DISPLAY_NAME: 作成する RAG コーパスの表示名 。
    • ENGINE_NAME: エージェント検索エンジンまたはエージェント検索データストアの完全なリソース名。たとえば

      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: Cloud de Confiance by S3NS プロジェクトの 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 で認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、 ローカル開発環境の認証の設定をご覧ください。

コードサンプルを実行する前に、GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境 変数を s3nsapis.fr に設定します。


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: Cloud de Confiance by S3NS プロジェクトの 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: Cloud de Confiance by S3NS プロジェクトの ID。

  • LOCATION: リクエストを処理するリージョン。

  • MODEL_ID: コンテンツ生成用の LLM モデル。たとえば、 gemini-3.8-flash のようにします。

  • GENERATION_METHOD: コンテンツ生成用の LLM メソッド。 たとえば、generateContent、streamGenerateContent のようにします。

  • 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....
#   ...

次のステップ