RAG Engine API

Vertex AI RAG Engine は、検索拡張生成(RAG)用の Vertex AI プラットフォームのコンポーネントです。RAG Engine を使用すると、大規模言語モデル(LLM)はドキュメントやデータベースなどの外部ナレッジソースからデータにアクセスして使用し、より正確で有益なレスポンスを生成できます。

このドキュメントでは、RAG Engine API の使用に関する情報と例を示し、次のトピックについて説明します。

  • コーパス管理: コーパスの管理に使用する API パラメータについて説明し、RAG コーパスの作成、更新、一覧表示、取得、削除の例を示します。
  • ファイル管理: ファイルを管理するための API パラメータについて説明し、RAG コーパス内のファイルのアップロード、インポート、管理の例を示します。
  • 取得と生成: コンテキストを取得してグラウンディングされたレスポンスを生成するための API パラメータについて、例を挙げて説明します。
  • プロジェクト管理: RAG エンジンのプロジェクト レベルの設定を構成する方法について、例を挙げて説明します。

次の図は、RAG Engine API を使用するワークフローの概要を示しています。

コーパス管理パラメータ

このセクションでは、RAG コーパスの管理に使用するパラメータについて説明します。詳細については、コーパス管理をご覧ください。

RAG コーパスを作成する

次の表に、RAG コーパスの作成に使用するパラメータを示します。

ベクトル データベース オプション

RAG コーパスには、次のいずれかのベクトル データベース オプションを選択できます。

ベクトル データベース オプション 説明 ユースケース
rag_managed_db Vertex AI が提供するフルマネージドのサーバーレス ベクトル データベース。これはデフォルトのオプションです。 独自のベクトル データベース インフラストラクチャを管理せずに、シンプルで統合されたソリューションが必要な場合におすすめします。
pinecone セルフマネージド Pinecone ベクトル データベースとの統合。Pinecone インデックス名と API キーを指定する必要があります。 既存の Pinecone 設定がある場合や、特定の機能が必要な場合は、このオプションを使用します。
vertex_vector_search ベクトル検索との統合。インデックスとインデックス エンドポイントのリソース名の指定が必要です。 Google Cloud エコシステム内で高パフォーマンスでスケーラブルなベクトル検索ソリューションが必要な場合は、このオプションを使用します。

リクエストの本文

パラメータ
display_name

必須: string

RAG コーパスの表示名。

description

省略可: string

RAG コーパスの説明。

encryption_spec

省略可: 変更不可: string

RAG コーパスに関連する保存データの暗号化に使用される CMEK 鍵の名前。このキーは、ベクトル データベースの RagManaged オプションにのみ適用されます。このフィールドは、コーパスの作成時にのみ設定できます。

形式: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{key_name}

vector_db_config

省略可: 変更不可: vectorDbConfig

ベクトル DB の構成。このフィールドは oneof オブジェクトです。次のいずれかを選択します。

  • rag_managed_db: デフォルトのフルマネージド ベクトル データベース。
  • pinecone: Pinecone インスタンスを指定します。
    • index_namestring): Pinecone インデックスの名前。これは、後で UpdateRagCorpus 呼び出しで設定できます。
    • api_auth.api_key_config.api_key_secret_versionstring): Pinecone API キーを含む Secret Manager のシークレットの完全なリソース名。形式: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}。これは後で設定できます。
  • vertex_vector_search: ベクトル検索インスタンスを指定します。
    • indexstring): ベクトル検索インデックスのリソース名。形式: projects/{project}/locations/{location}/indexes/{index}。これは後で設定できます。
    • index_endpointstring): ベクトル検索インデックス エンドポイントのリソース名。形式: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}。これは後で設定できます。
vertex_ai_search_config.serving_config

省略可: string

Vertex AI Search の構成。

形式: projects/{project}/locations/{location}/collections/{collection}/engines/{engine}/servingConfigs/{serving_config} または projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store}/servingConfigs/{serving_config}

rag_embedding_model_config.vertex_prediction_endpoint.endpoint

省略可: 変更不可: string

RAG コーパスに使用するエンベディング モデル。この値を設定後に変更することはできません。空白のままにすると、デフォルトのエンベディング モデルとして text-embedding-005 が使用されます。

RAG コーパスを更新する

次の表では、RAG コーパスの更新に使用するパラメータの一覧を確認できます。

リクエストの本文

パラメータ
display_name

省略可: string

RAG コーパスの新しい表示名。

description

省略可: string

RAG コーパスの新しい説明。

rag_vector_db.pinecone.index_name

string

Pinecone インデックスの名前。RagCorpusPinecone 構成で作成され、インデックス名がこれまで設定されていない場合は、このフィールドを設定できます。

rag_vector_db.vertex_vector_search.index

string

ベクトル検索インデックスのリソース名。RagCorpusVector Search 構成で作成され、インデックスがこれまで設定されていない場合は、このフィールドを設定できます。

形式: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}

rag_vector_db.vertex_vector_search.index_endpoint

string

ベクトル検索インデックス エンドポイントのリソース名。RagCorpusVector Search 構成で作成され、インデックス エンドポイントがこれまで設定されていない場合は、このフィールドを設定できます。

形式: projects/{project}/locations/{location}/indexes/{index}

rag_vector_db.api_auth.api_key_config.api_key_secret_version

string

Pinecone API キーを含む Secret Manager のシークレットの完全なリソース名。

形式: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

RAG コーパスをリストする

次の表では、RAG コーパスをリストするために使用するパラメータの一覧を確認できます。

パラメータ

page_size

省略可: int

ページごとに返すコーパスの最大数。

page_token

省略可: string

標準的なリストのページトークン。通常は、前の [VertexRagDataService.ListRagCorpora][] 呼び出しの [ListRagCorporaResponse.next_page_token][] から取得します。

RAG コーパスを取得する

次の表では、RAG コーパスの取得に使用するパラメータの一覧を確認できます。

パラメータ
name

必須: string

RagCorpus リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

RAG コーパスを削除する

次の表では、RAG コーパスの削除に使用するパラメータの一覧を確認できます。

パラメータ
name

必須: string

削除する RagCorpus リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

ファイル管理パラメータ

このセクションでは、RAG コーパス内のファイルを管理するためのパラメータについて説明します。詳しくは、ファイル管理をご覧ください。

RAG ファイルをアップロードする

次の表では、RAG ファイルのアップロードに使用するパラメータの一覧を確認できます。

リクエストの本文

パラメータ
parent

必須: string

ファイルをアップロードする RagCorpus リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

rag_file

必須: RagFile

アップロードするファイル。次のフィールドが含まれています。

  • display_name(必須、string): RAG ファイルの表示名。
  • description(省略可、string): RAG ファイルの説明。
upload_rag_file_config

必須: UploadRagFileConfig

RagFile の構成。次のフィールドが含まれています。

  • rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_sizeint32): 各チャンク内のトークンの数。
  • rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_overlapint32): チャンク間のトークンの重複。

RAG ファイルをインポートする

次の表では、RAG ファイルのインポートに使用するパラメータの一覧を確認できます。

パラメータ

parent

必須: string

RagCorpus リソースの名前。

形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}

gcs_source

oneof import_source: GcsSource

Cloud Storage のロケーション。

個別のファイルだけでなく、Cloud Storage ディレクトリ全体のインポートもサポートしています。

gcs_source.uris

list/string

アップロード ファイルを含む Cloud Storage URI。

google_drive_source

oneof import_source: GoogleDriveSource

Google ドライブのロケーション。

個別のファイルと Google ドライブ フォルダのインポートをサポートしています。

slack_source

oneof import_source: SlackSource

ファイルがアップロードされる Slack チャネル。

jira_source

oneof import_source: JiraSource

ファイルがアップロードされる Jira クエリ。

share_point_sources

oneof import_source: SharePointSources

ファイルがアップロードされる SharePoint ソース。

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_size

int32

各チャンクに付与されているトークンの数。

rag_file_transformation_config.rag_file_chunking_config.fixed_length_chunking.chunk_overlap

int32

チャンク間の重複。

rag_file_parsing_config

省略可: RagFileParsingConfig

RagFiles の解析構成を指定します。

このフィールドが設定されていない場合、RAG はデフォルトのパーサーを使用します。

max_embedding_requests_per_min

省略可: int32

このジョブがコーパスで指定されたエンベディング モデルに対して 1 分間に実行できるクエリの最大数。この値は、このジョブに固有のものであり、他のインポート ジョブと共有されません。プロジェクトの [割り当て] ページで適切な値を設定します。

指定しない場合、デフォルト値の 1,000 QPM が使用されます。

GoogleDriveSource

resource_ids.resource_id

必須: string

Google ドライブのリソースの ID。

resource_ids.resource_type

必須: string

Google ドライブのリソースのタイプ。

SlackSource

channels.channels

繰り返し: SlackSource.SlackChannels.SlackChannel

Slack チャンネル情報(ID とインポートする期間を含む)。

channels.channels.channel_id

必須: string

Slack チャンネル ID。

channels.channels.start_time

省略可: google.protobuf.Timestamp

インポートするメッセージの開始タイムスタンプ。

channels.channels.end_time

省略可: google.protobuf.Timestamp

インポートするメッセージの終了タイムスタンプ。

channels.api_key_config.api_key_secret_version

必須: string

Secret Manager に保存されている Secret の完全なリソース名。ここには、Slack チャネル ID にアクセスできる Slack チャネル アクセス トークンが含まれています。
: https://api.slack.com/tutorials/tracks/getting-a-token をご覧ください。

形式: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

JiraSource

jira_queries.projects

繰り返し: string

すべてインポートする Jira プロジェクトのリスト。

jira_queries.custom_queries

繰り返し: string

インポートするカスタム Jira クエリのリスト。JQL(Jira Query Language)の詳細については、
Jira のサポートをご覧ください。

jira_queries.email

必須: string

Jira のメールアドレス。

jira_queries.server_uri

必須: string

Jira のサーバー URI。

jira_queries.api_key_config.api_key_secret_version

必須: string

Secret Manager に保存されている Secret の完全なリソース名。ここには、Slack チャネル ID にアクセスできる Jira API キーが含まれます。
参照: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/

形式: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

SharePointSources

share_point_sources.sharepoint_folder_path

folder_sourceoneof: string

ダウンロード元の SharePoint フォルダのパス。

share_point_sources.sharepoint_folder_id

folder_sourceoneof: string

ダウンロード元の SharePoint フォルダの ID。

share_point_sources.drive_name

drive_sourceoneof: string

ダウンロード元のドライブの名前。

share_point_sources.drive_id

drive_sourceoneof: string

ダウンロード元のドライブの ID。

share_point_sources.client_id

string

Microsoft Azure ポータルに登録されているアプリのアプリケーション ID。
また、MS Graph の権限「Files.ReadAll」、「Sites.ReadAll」、「BrowserSiteLists.Read.All」でアプリケーションを構成する必要があります。

share_point_sources.client_secret.api_key_secret_version

必須: string

Secret Manager に保存されているシークレットの完全なリソース名。Azure に登録されているアプリのアプリケーション シークレットを含みます。

形式: projects/{PROJECT_NUMBER}/secrets/{SECRET_ID}/versions/{VERSION_ID}

share_point_sources.tenant_id

string

Azure Active Directory インスタンスの固有識別子。

share_point_sources.sharepoint_site_name

string

ダウンロード元の SharePoint サイトの名前。サイト名またはサイト ID を指定できます。

RagFileParsingConfig

layout_parser

oneof parser: RagFileParsingConfig.LayoutParser

RagFile に使用するレイアウト パーサー。

layout_parser.processor_name

string

Document AI プロセッサまたはプロセッサ バージョンの完全なリソース名。

形式:
projects/{project_id}/locations/{location}/processors/{processor_id}
projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}

layout_parser.max_parsing_requests_per_min

string

ジョブが Document AI プロセッサに対して 1 分あたりに送信できるリクエストの最大数。

https://cloud.google.com/document-ai/quotas と、プロジェクトの [割り当て] ページを参照して、ここで適切な値を設定します。指定しない場合、デフォルト値の 120 QPM が使用されます。

llm_parser

oneof parser: RagFileParsingConfig.LlmParser

RagFile に使用する LLM パーサー。

llm_parser.model_name

string

LLM モデルのリソース名。

形式:
projects/{project_id}/locations/{location}/publishers/{publisher}/models/{model}

llm_parser.max_parsing_requests_per_min

string

ジョブが LLM モデルに対して 1 分あたりに送信できるリクエストの最大数。

プロジェクトに適切な値を設定するには、モデルの割り当てセクションと、プロジェクトの [割り当て] ページを参照して、ここで適切な値を設定します。指定しない場合、デフォルト値の 5, 000 QPM が使用されます。

RAG ファイルを取得する

次の表では、RAG ファイルの取得に使用するパラメータの一覧を確認できます。

パラメータ
name

必須: string

RagFile リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

RAG ファイルを削除する

次の表では、RAG ファイルの削除に使用するパラメータの一覧を確認できます。

パラメータ
name

必須: string

削除する RagFile リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_file_id}

取得と予測のパラメータ

このセクションでは、取得パラメータと予測パラメータの一覧を確認できます。

取得パラメータ

次の表に、retrieveContexts API のパラメータを示します。

パラメータ

parent

必須: string

RagContexts を取得する Location のリソース名。
ユーザーには、プロジェクト内で呼び出しを行う権限が必要です。

形式: projects/{project}/locations/{location}

vertex_rag_store

VertexRagStore

Vertex RagStore のデータソース。

query

必須: RagQuery

単一の RAG 取得クエリ。

VertexRagStore
VertexRagStore

rag_resources

list: RagResource

RAG ソースの表現。コーパスのみまたは RagFile を指定できます。1 つのコーパスまたは 1 つのコーパスの複数のファイルのみをサポートします。

rag_resources.rag_corpus

省略可: string

RagCorpora リソースの名前。

形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}

rag_resources.rag_file_ids

list: string

RagFile リソースのリスト。

形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file}

RagQuery

text

string

関連するコンテキストを取得するテキスト形式のクエリ。

rag_retrieval_config

省略可: RagRetrievalConfig

クエリの取得構成。

RagRetrievalConfig

top_k

省略可: int32

取得するコンテキストの数。

filter.vector_distance_threshold

oneof vector_db_threshold: double

ベクトル距離がしきい値より小さいコンテキストのみが返されます。

filter.vector_similarity_threshold

oneof vector_db_threshold: double

ベクトル類似度がしきい値より大きいコンテキストのみを返します。

ranking.rank_service.model_name

省略可: string

ランクサービスのモデル名。

例: semantic-ranker-512@latest

ranking.llm_ranker.model_name

省略可: string

ランキングに使用されるモデル名。

例: gemini-2.5-flash

予測パラメータ

次の表では、予測パラメータの一覧を確認できます。

GenerateContentRequest

tools.retrieval.vertex_rag_store

VertexRagStore

Vertex AI RAG ストアで使用されるデータソースを使用するように設定します。

詳細については、VertexRagStore をご覧ください。

プロジェクト管理パラメータ

次の表に、RAG エンジンのマネージド データベースのプロジェクト レベルの階層構成を示します。

階層 説明 ユースケース
RagManagedDbConfig.scaled マネージド ベクトル データベースに高パフォーマンスと自動スケーリング機能を提供する本番環境規模の階層。 クエリ負荷が高い本番環境アプリケーションやデータ量が多い本番環境アプリケーションに推奨されます。
RagManagedDbConfig.basic マネージド ベクトル データベースの費用対効果の高い低コンピューティング ティア。 トラフィックの少ない開発、テスト、小規模なアプリケーションに使用します。
RagManagedDbConfig.unprovisioned マネージド ベクトル データベースとその基盤となるリソースを削除します。これにより、プロジェクトのマネージド DB が事実上無効になります。 費用管理のために、マネージド データベース インフラストラクチャが不要になったときに破棄するために使用します。

コーパス管理の例

このセクションでは、API を使用して RAG コーパスを管理する方法の例を示します。

RAG コーパスの作成の例

次のコードサンプルは、RAG コーパスを作成する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • CORPUS_DISPLAY_NAME: RAG コーパスの表示名。
  • CORPUS_DESCRIPTION: RAG コーパスの説明。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora

リクエストの本文(JSON):

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION",
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora"

Powershell

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

  $cred = gcloud auth print-access-token
  $headers = @{ "Authorization" = "Bearer $cred" }

  Invoke-WebRequest `
      -Method POST `
      -Headers $headers `
      -ContentType: "application/json; charset=utf-8" `
      -InFile request.json `
      -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" | Select-Object -Expand Content

成功を示すステータス コード(2xx)が返されます。

次の例は、REST API を使用して RAG コーパスを作成する方法を示しています。

  // CreateRagCorpus
  // Input: LOCATION, PROJECT_ID, CORPUS_DISPLAY_NAME
  // Output: CreateRagCorpusOperationMetadata
  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" : "CORPUS_DISPLAY_NAME"
    }'

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

# Configure backend_config
backend_config = rag.RagVectorDbConfig(
    rag_embedding_model_config=rag.RagEmbeddingModelConfig(
        vertex_prediction_endpoint=rag.VertexPredictionEndpoint(
            publisher_model="publishers/google/models/text-embedding-005"
        )
    )
)

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

RAG コーパスの更新の例

RAG コーパスの表示名、説明、ベクトル データベースの構成を更新できます。ただし、RAG コーパスの次の不変パラメータは変更できません。

  • ベクトル データベースの種類。たとえば、ベクトル データベースを Pinecone からベクトル検索に変更することはできません。
  • マネージド データベース オプションを使用している場合、ベクトル データベースの構成を更新することはできません。

次の例は、RAG コーパスの更新方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • CORPUS_ID: RAG コーパスのコーパス ID。
  • CORPUS_DISPLAY_NAME: RAG コーパスの表示名。
  • CORPUS_DESCRIPTION: RAG コーパスの説明。
  • INDEX_NAME: ベクトル検索インデックスのリソース名。形式: projects/{project}/locations/{location}/indexes/{index}
  • INDEX_ENDPOINT_NAME: ベクトル検索インデックス エンドポイントのリソース名。形式: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}

HTTP メソッドと URL:

PATCH https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID

リクエストの本文(JSON):

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION",
  "vector_db_config": {
    "vertex_vector_search": {
        "index": "INDEX_NAME",
        "index_endpoint": "INDEX_ENDPOINT_NAME",
    }
  }
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID"

Powershell

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method PATCH `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID" | Select-Object -Expand Content

リクエストが成功すると、2xx ステータス コードが返されます。

RAG コーパスのリストの例

次のコードサンプルは、すべての RAG コーパスをリストする方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • PAGE_SIZE: ページごとに返す RAG コーパスの最大数。
  • PAGE_TOKEN: 結果の次のページを取得するための、前の ListRagCorpora レスポンスのページトークン。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content

リクエストが成功すると、2xx ステータス コードと、指定されたプロジェクトの RAG コーパスのリストが返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

corpora = rag.list_corpora()
print(corpora)
# Example response:
# ListRagCorporaPager<rag_corpora {
#   name: "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/2305843009213693952"
#   display_name: "test_corpus"
#   create_time {
# ...

RAG コーパスの取得の例

次のコードサンプルは、RAG コーパスを取得する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RAG コーパス リソースの ID。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content

成功すると、RagCorpus リソースが返されます。

get コマンドと list コマンドは、RagCorpus が選択したエンベディング モデルを指す vector_db_config 内の rag_embedding_model_config フィールドをどのように使用するのかを示す例で使用されています。

    PROJECT_ID: Your project ID.
    LOCATION: The region to process the request.
    RAG_CORPUS_ID: The corpus ID of your RAG corpus.
  ```

```sh
  // GetRagCorpus
  // Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID
  // Output: RagCorpus
  curl -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

  // ListRagCorpora
  curl -sS -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/
  ```

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# 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 Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

corpus = rag.get_corpus(name=corpus_name)
print(corpus)
# Example response:
# RagCorpus(name='projects/[PROJECT_ID]/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description',
# ...

RAG コーパスの削除の例

次のコードサンプルは、RAG コーパスを削除する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。

HTTP メソッドと URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method DELETE `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content

成功すると、DeleteOperationMetadata が返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# 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 Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag.delete_corpus(name=corpus_name)
print(f"Corpus {corpus_name} deleted.")
# Example response:
# Successfully deleted the RagCorpus.
# Corpus projects/[PROJECT_ID]/locations/us-central1/ragCorpora/123456789012345 deleted.

ファイル管理の例

このセクションでは、API を使用して RAG ファイルを管理する方法の例を示します。

RAG ファイルのアップロードの例

次のコードサンプルは、RAG ファイルをアップロードする方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RAG コーパスのコーパス ID。
  • LOCAL_FILE_PATH: アップロードするファイルのローカルパス。
  • DISPLAY_NAME: RAG ファイルの表示名。
  • DESCRIPTION: RAG ファイルの説明。

リクエストを送信するには、次のコマンドを使用します。

curl -X POST \
-H "X-Goog-Upload-Protocol: multipart" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-F metadata="{'rag_file': {'display_name':' DISPLAY_NAME', 'description':'DESCRIPTION'}}" \
-F file=@LOCAL_FILE_PATH \
"https://LOCATION-aiplatform.googleapis.com/upload/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload"

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# 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}"
# path = "path/to/local/file.txt"
# display_name = "file_display_name"
# description = "file description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.upload_file(
    corpus_name=corpus_name,
    path=path,
    display_name=display_name,
    description=description,
)
print(rag_file)
# RagFile(name='projects/[PROJECT_ID]/locations/us-central1/ragCorpora/1234567890/ragFiles/09876543',
#  display_name='file_display_name', description='file description')

RAG ファイルのインポートの例

ファイルとフォルダは、ドライブまたは Cloud Storage からインポートできます。response.metadata を使用すると、SDK の response オブジェクトで部分的な失敗、リクエスト時間、レスポンス時間を確認できます。

response.skipped_rag_files_count フィールドには、インポート中にスキップされたファイルの数が示されます。次の条件が満たされる場合、サービスはファイルをスキップします。

  1. ファイルがすでにインポートされている。
  2. ファイルが変更されていない。
  3. ファイルのチャンク構成が変更されていない。

Python

from vertexai import rag
import vertexai

# 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}"
# paths = ["https://drive.google.com/file/123", "gs://my_bucket/my_files_dir"]  # Supports Cloud Storage and Google Drive Links

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.import_files(
    corpus_name=corpus_name,
    paths=paths,
    transformation_config=rag.TransformationConfig(
        rag.ChunkingConfig(chunk_size=1024, chunk_overlap=256)
    ),
    import_result_sink="gs://sample-existing-folder/sample_import_result_unique.ndjson",  # Optional: This must be an existing Cloud Storage bucket folder, and the filename must be unique (non-existent).
    llm_parser=rag.LlmParserConfig(
      model_name="gemini-2.5-pro-preview-05-06",
      max_parsing_requests_per_min=100,
    ),  # Optional
    max_embedding_requests_per_min=900,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RAG コーパスのコーパス ID。
  • FOLDER_RESOURCE_ID: ドライブ フォルダのリソース ID。
  • GCS_URIS: Cloud Storage ロケーションのリスト。例: gs://my-bucket1
  • CHUNK_SIZE: 各チャンクに付与されている必要があるトークンの数。
  • CHUNK_OVERLAP: 2 つのチャンク間で重複するトークンの数。
  • EMBEDDING_MODEL_QPM_RATE: RAG によるエンベディング モデルへのアクセスを制限する QPM レート。例: 1,000。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import

リクエストの本文(JSON):

{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": "GCS_URIS"
    },
    "rag_file_chunking_config": {
      "chunk_size": "CHUNK_SIZE",
      "chunk_overlap": "CHUNK_OVERLAP"
    }
  }
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import"

Powershell

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import" | Select-Object -Expand Content

成功すると、ImportRagFilesOperationMetadata リソースが返されます。

次のサンプルは、Cloud Storage からファイルをインポートする方法を示しています。max_embedding_requests_per_min 制御フィールドを使用して、RAG Engine が ImportRagFiles インデックス登録プロセス中にエンベディング モデルを呼び出すレートを制限します。このフィールドのデフォルト値は、1 分あたり 1000 回の呼び出しです。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RAG コーパスのコーパス ID。
  • GCS_URIS: Cloud Storage ロケーションのリスト。例: gs://my-bucket1
  • CHUNK_SIZE: 各チャンクに付与されている必要があるトークンの数。
  • CHUNK_OVERLAP: 2 つのチャンク間で重複するトークンの数。
  • EMBEDDING_MODEL_QPM_RATE: RAG によるエンベディング モデルへのアクセスを制限する QPM レート。例: 1,000。
// ImportRagFiles
// Import a single Cloud Storage file or all files in a Cloud Storage bucket.
// Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID, GCS_URIS
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
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/RAG_CORPUS_ID/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": "GCS_URIS"
    },
    "rag_file_chunking_config": {
      "chunk_size": CHUNK_SIZE,
      "chunk_overlap": CHUNK_OVERLAP
    },
    "max_embedding_requests_per_min": EMBEDDING_MODEL_QPM_RATE
  }
}'

次のサンプルは、ドライブからファイルをインポートする方法を示しています。max_embedding_requests_per_min 制御フィールドを使用して、RAG Engine が ImportRagFiles インデックス登録プロセス中にエンベディング モデルを呼び出すレートを制限します。このフィールドのデフォルト値は、1 分あたり 1000 回の呼び出しです。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RAG コーパスのコーパス ID。
  • FOLDER_RESOURCE_ID: ドライブ フォルダのリソース ID。
  • CHUNK_SIZE: 各チャンクに付与されている必要があるトークンの数。
  • CHUNK_OVERLAP: 2 つのチャンク間で重複するトークンの数。
  • EMBEDDING_MODEL_QPM_RATE: RAG によるエンベディング モデルへのアクセスを制限する QPM レート。例: 1,000。
// ImportRagFiles
// Import all files in a Google Drive folder.
// Input: LOCATION, PROJECT_ID, RAG_CORPUS_ID, FOLDER_RESOURCE_ID
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
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/RAG_CORPUS_ID/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "google_drive_source": {
      "resource_ids": {
        "resource_id": "FOLDER_RESOURCE_ID",
        "resource_type": "RESOURCE_TYPE_FOLDER"
      }
    },
    "max_embedding_requests_per_min": EMBEDDING_MODEL_QPM_RATE
  }
}'

RAG ファイルのリストの例

次のコードサンプルは、RAG ファイルを一覧表示する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。
  • PAGE_SIZE: ページごとに返す RagFiles の最大数。
  • PAGE_TOKEN: 結果の次のページを取得するための、前の ListRagFiles レスポンスのページトークン。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content

リクエストが成功すると、2xx ステータス コードと、指定された RAG_CORPUS_IDRagFiles のリストが返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# 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 Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

files = rag.list_files(corpus_name=corpus_name)
for file in files:
    print(file.display_name)
    print(file.name)
# Example response:
# g-drive_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/222222222222
# g_cloud_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/333333333333

RAG ファイルの取得の例

次のコードサンプルは、RAG ファイルを取得する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。
  • RAG_FILE_ID: RagFile リソースの ID。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method GET `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content

成功すると、RagFile リソースが返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

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

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.get_file(name=file_name)
print(rag_file)
# Example response:
# RagFile(name='projects/1234567890/locations/us-central1/ragCorpora/11111111111/ragFiles/22222222222',
# display_name='file_display_name', description='file description')

RAG ファイルの削除の例

次のコードサンプルは、RAG ファイルを削除する方法を示しています。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID>: プロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。
  • RAG_FILE_ID: RagFile リソースの ID。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file_id}

HTTP メソッドと URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

次のコマンドを実行します。

curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

Powershell

次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method DELETE `
    -Headers $headers `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content

成功すると、DeleteOperationMetadata が返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

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

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag.delete_file(name=file_name)
print(f"File {file_name} deleted.")
# Example response:
# Successfully deleted the RagFile.
# File projects/1234567890/locations/us-central1/ragCorpora/1111111111/ragFiles/2222222222 deleted.

取得クエリの例

クエリを指定すると、RAG の検索コンポーネントがナレッジベースを検索し、関連情報を探します。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
import vertexai

# 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 Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=corpus_name,
            # Optional: supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="Hello World!",
    rag_retrieval_config=rag.RagRetrievalConfig(
        top_k=10,
        filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
    ),
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_RESOURCE: RagCorpus リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • VECTOR_DISTANCE_THRESHOLD: ベクトル距離がしきい値より小さいコンテキストのみが返されます。
  • TEXT: 関連するコンテキストを取得するクエリテキスト。
  • SIMILARITY_TOP_K: 取得する上位コンテキストの数。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts

リクエストの本文(JSON):

{
"vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE"
    },
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
  },
  "query": {
  "text": TEXT
  "similarity_top_k": SIMILARITY_TOP_K
  }
}

curl

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts"

Powershell

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" | Select-Object -Expand Content

リクエストが成功すると、2xx ステータス コードと関連するコンテキストのリストが返されます。

生成の例

LLM では、取得したコンテキストを使用してグラウンディングされた回答を生成します。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際のプロジェクト ID。
  • LOCATION: リクエストを処理するリージョン。
  • MODEL_ID: コンテンツ生成用の LLM モデル。例: gemini-2.5-flash
  • GENERATION_METHOD: コンテンツを生成するための LLM メソッド。オプション: generateContentstreamGenerateContent
  • INPUT_PROMPT: コンテンツ生成のために LLM に送信されるテキスト。アップロードされた Rag ファイルに関連するプロンプトを使用するようにしてください。
  • RAG_CORPUS_RESOURCE: RagCorpus リソースの名前。形式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • SIMILARITY_TOP_K: 省略可。取得する上位コンテキストの数。
  • VECTOR_DISTANCE_THRESHOLD: 省略可。ベクトル距離がしきい値より小さいコンテキストが返されます。
  • USER: ユーザー名。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD

リクエストの本文(JSON):

{
"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",
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
  }
  }
}
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD"

Powershell

request.json という名前のファイルにリクエスト本文を保存し、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" | Select-Object -Expand Content

成功すると、生成されたコンテンツと引用が返されます。

Python

Vertex AI SDK for Python のインストールまたは更新の方法については、Vertex AI SDK for Python をインストールするをご覧ください。 詳細については、 Python API リファレンス ドキュメントをご覧ください。


from vertexai import rag
from vertexai.generative_models import GenerativeModel, Tool
import vertexai

# 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 Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=corpus_name,
                    # Optional: supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            rag_retrieval_config=rag.RagRetrievalConfig(
                top_k=10,
                filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
            ),
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-2.0-flash-001", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
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....
#   ...

プロジェクト管理の例

ティアは、RagManagedDb を使用する RAG コーパスに影響する RagEngineConfig リソースのプロジェクト レベルの設定です。階層構成を取得するには、GetRagEngineConfig を使用します。階層構成を更新するには、UpdateRagEngineConfig を使用します。

階層構成の管理の詳細については、階層を管理するをご覧ください。

プロジェクト構成を取得する

次のコードサンプルは、RagEngineConfig を読み取る方法を示しています。

コンソール

  1. Google Cloud コンソールで、[RAG Engine] ページに移動します。

    RAG Engine に移動

  2. RAG Engine が実行されているリージョンを選択します。RAG コーパスのリストが更新されます。
  3. [RAG Engine を構成] をクリックします。[RAG Engine を構成する] ペインが表示されます。RAG Engine に選択されている階層を確認できます。
  4. [キャンセル] をクリックします。

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

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

rag_engine_config = rag.rag_data.get_rag_engine_config(
    name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"
)

print(rag_engine_config)

REST

curl -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig

プロジェクト構成を更新する

このセクションでは、構成を Scaled 階層、Basic 階層、Unprovisioned 階層に変更する方法を示すコードサンプルを示します。

RagEngineConfig をスケーリングされた階層に更新する

次のコードサンプルは、RagEngineConfig をスケーリングされた階層に設定する方法を示しています。

コンソール

  1. Google Cloud コンソールで、[RAG Engine] ページに移動します。

    RAG Engine に移動

  2. RAG Engine が実行されているリージョンを選択します。RAG コーパスのリストが更新されます。
  3. [RAG Engine を構成] をクリックします。[RAG Engine を構成する] ペインが表示されます。
  4. RAG Engine を実行する階層を選択します。
  5. [保存] をクリックします。

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

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

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
name=rag_engine_config_name,
rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Scaled()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'scaled': {}}}"

RagEngineConfig をベーシック ティアに更新する

次のコードサンプルは、RagEngineConfig を Basic 階層に設定する方法を示しています。

コンソール

  1. Google Cloud コンソールで、[RAG Engine] ページに移動します。

    RAG Engine に移動

  2. RAG Engine が実行されているリージョンを選択します。RAG コーパスのリストが更新されます。
  3. [RAG Engine を構成] をクリックします。[RAG Engine を構成する] ペインが表示されます。
  4. RAG Engine を実行する階層を選択します。
  5. [保存] をクリックします。

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

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

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
name=rag_engine_config_name,
rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Basic()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'basic': {}}}"

RagEngineConfig を Unprovisioned ティアに更新する

次のコードサンプルは、RagEngineConfig を未プロビジョニング階層に設定する方法を示しています。

コンソール

  1. Google Cloud コンソールで、[RAG Engine] ページに移動します。

    RAG Engine に移動

  2. RAG Engine が実行されているリージョンを選択します。RAG コーパスのリストが更新されます。
  3. [RAG Engine を構成] をクリックします。[RAG Engine を構成する] ペインが表示されます。
  4. [RAG Engine を削除] をクリックします。確認ダイアログが表示されます。
  5. delete」と入力して、RAG Engine でデータを削除することを確認し、[確認] をクリックします。
  6. [保存] をクリックします。

Python

from vertexai import rag
import vertexai

PROJECT_ID = YOUR_PROJECT_ID
LOCATION = YOUR_RAG_ENGINE_LOCATION

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

rag_engine_config_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/ragEngineConfig"

new_rag_engine_config = rag.RagEngineConfig(
  name=rag_engine_config_name,
  rag_managed_db_config=rag.RagManagedDbConfig(tier=rag.Unprovisioned()),
)

updated_rag_engine_config = rag.rag_data.update_rag_engine_config(
  rag_engine_config=new_rag_engine_config
)

print(updated_rag_engine_config)

REST

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/ragEngineConfig -d "{'ragManagedDbConfig': {'unprovisioned': {}}}"

次のステップ