RAG Engine API

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

構文の例

このセクションでは、RAG コーパスを作成するための構文について説明します。

curl

PROJECT_ID: Your project ID.
LOCATION: The region to process the request.
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" : "...",
  "description": "..."
}'

Python

corpus = rag.create_corpus(display_name=..., description=...)
print(corpus)

パラメータ リスト

このセクションでは、次について説明します。

パラメータ
コーパス管理パラメータをご覧ください。 コーパス管理の例をご覧ください。
ファイル管理パラメータをご覧ください。 ファイル管理の例をご覧ください。

コーパス管理パラメータ

RAG コーパスの詳細については、コーパス管理をご覧ください。

RAG コーパスを作成する

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

本文リクエスト
パラメータ

display_name

必須: string

RAG コーパスの表示名。

description

省略可: string

RAG コーパスの説明。

vector_db_config

省略可: 変更不可: RagVectorDbConfig

ベクトル DB の構成。

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}

RagVectorDbConfig
パラメータ

rag_managed_db

oneof vector_db: RagVectorDbConfig.RagManagedDb

ベクトル データベースが指定されていない場合、rag_managed_db がデフォルトのベクトル データベースになります。

weaviate

oneof vector_db: RagVectorDbConfig.Weaviate

Weaviate インスタンスを指定します。

weaviate.http_endpoint

string

Weaviate インスタンスの HTTP エンドポイント。

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

weaviate.collection_name

string

RAG コーパスがマッピングされる Weaviate コレクション。

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

pinecone

oneof vector_db: RagVectorDbConfig.Pinecone

Pinecone インスタンスを指定します。

pinecone.index_name

string

これは、RAG コーパスで使用する Pinecone インデックスの作成に使用される名前です。

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

vertex_feature_store

oneof vector_db: RagVectorDbConfig.VertexFeatureStore

Vertex AI Feature Store インスタンスを指定します。

vertex_feature_store.feature_view_resource_name

string

RAG コーパスがマッピングされる Vertex AI Feature Store FeatureView

形式: projects/{project}/locations/{location}/featureOnlineStores/{feature_online_store}/featureViews/{feature_view}

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

vertex_vector_search

oneof vector_db: RagVectorDbConfig.VertexVectorSearch

Vertex Vector Search インスタンスを指定します。

vertex_vector_search.index

string

これは、RAG コーパスで使用するベクトル検索インデックスのリソース名です。

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

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

vertex_vector_search.index_endpoint

string

これは、RAG コーパスで使用するベクトル検索インデックス エンドポイントのリソース名です。

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

この値を設定後に変更することはできません。CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

api_auth.api_key_config.api_key_secret_version

string

これは、Secret Manager に保存されている Secret の完全なリソース名です。ここには、ベクトル データベースの選択に応じて Weaviate または Pinecone API キーが含まれます。

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

CreateRagCorpus API 呼び出しでは空のままにして、続く UpdateRagCorpus API 呼び出しで空でない値を設定できます。

rag_embedding_model_config.vertex_prediction_endpoint.endpoint

省略可: 変更不可: string

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

RAG コーパスを更新する

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

本文リクエスト
パラメータ

display_name

省略可: string

RAG コーパスの表示名。

description

省略可: string

RAG コーパスの説明。

rag_vector_db.weaviate.http_endpoint

string

Weaviate インスタンスの HTTP エンドポイント。

RagCorpusWeaviate 構成で作成され、このフィールドがこれまで設定されたことがない場合は、Weaviate インスタンスの HTTP エンドポイントを更新できます。

rag_vector_db.weaviate.collection_name

string

RAG コーパスがマッピングされる Weaviate コレクション。

RagCorpusWeaviate 構成で作成され、このフィールドがこれまで設定されたことがない場合は、Weaviate インスタンスのコレクション名を更新できます。

rag_vector_db.pinecone.index_name

string

これは、RAG コーパスで使用する Pinecone インデックスの作成に使用される名前です。

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

rag_vector_db.vertex_feature_store.feature_view_resource_name

string

RAG コーパスがマッピングされる Vertex AI Feature Store FeatureView

形式: projects/{project}/locations/{location}/featureOnlineStores/{feature_online_store}/featureViews/{feature_view}

RagCorpusVertex AI Feature Store 構成で作成され、このフィールドがこれまで設定されたことがない場合は、更新できます。

rag_vector_db.vertex_vector_search.index

string

これは、RAG コーパスで使用するベクトル検索インデックスのリソース名です。

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

RagCorpusVector Search 構成で作成され、このフィールドがこれまで設定されたことがない場合は、更新できます。

rag_vector_db.vertex_vector_search.index_endpoint

string

これは、RAG コーパスで使用するベクトル検索インデックス エンドポイントのリソース名です。

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

RagCorpusVector Search 構成で作成され、このフィールドがこれまで設定されたことがない場合は、更新できます。

rag_vector_db.api_auth.api_key_config.api_key_secret_version

string

これは、Secret Manager に保存されている Secret の完全なリソース名です。ここには、ベクトル データベースの選択に応じて Weaviate または Pinecone API キーが含まれます。

形式: 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

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

upload_rag_file_config

必須: UploadRagFileConfig

RagCorpus にアップロードされる RagFile の構成。

RagFile

display_name

必須: string

RAG ファイルの表示名。

description

省略可: string

RAG ファイルの説明。

UploadRagFileConfig

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 ファイルをインポートする

次の表では、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 が使用されます。

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

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

hybrid_search.alpha

省略可: float

アルファ値は、密ベクトルの検索結果とスパース ベクトルの検索結果の重みを制御します。範囲は [0, 1] です。ここで、0 はスパース ベクトル検索のみ、1 は密ベクトル検索のみを意味します。デフォルト値は 0.5 で、スパース ベクトル検索と密ベクトル検索のバランスが取れています。

ハイブリッド検索は Weaviate でのみ使用できます。

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-1.5-pro

予測パラメータ

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

GenerateContentRequest

tools.retrieval.vertex_rag_store

VertexRagStore

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

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

コーパス管理の例

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

RAG コーパスの作成の例

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

REST

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

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

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" | Select-Object -Expand Content
成功を示すステータス コード(2xx)が返されます。

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

  PROJECT_ID: Your project ID.
  LOCATION: The region to process the request.
  CORPUS_DISPLAY_NAME: The display name of the <code>RagCorpus</code>.
    // 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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora \
    -d '{
          "display_name" : "CORPUS_DISPLAY_NAME"
      }'

Vertex AI SDK for Python

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


from vertexai.preview 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 embedding model (Optional)
embedding_model_config = rag.EmbeddingModelConfig(
    publisher_model="publishers/google/models/text-embedding-004"
)

corpus = rag.create_corpus(
    display_name=display_name,
    description=description,
    embedding_model_config=embedding_model_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 コーパスの次のパラメータは変更できません。

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

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

REST

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

  • PROJECT_ID: 実際のプロジェクト ID
  • LOCATION: リクエストを処理するリージョン。
  • CORPUS_ID: RAG コーパスのコーパス ID。
  • CORPUS_DISPLAY_NAME: RagCorpus の表示名。
  • CORPUS_DESCRIPTION: RagCorpus の説明。
  • INDEX_NAME: Vector Search Index のリソース名。形式: projects/{project}/locations/{location}/indexes/{index}
  • INDEX_ENDPOINT_NAME: Vector Search Index Endpoint のリソース名。形式: projects/{project}/locations/{location}/indexEndpoints/{index_endpoint}

HTTP メソッドと URL:

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

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

{
  "display_name" : "CORPUS_DISPLAY_NAME",
  "description": "CORPUS_DESCRIPTION",
  "rag_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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/CORPUS_ID" | Select-Object -Expand Content
成功を示すステータス コード(2xx)が返されます。

RAG コーパスのリストの例

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

REST

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

  • PROJECT_ID: 実際のプロジェクト ID
  • LOCATION: リクエストを処理するリージョン。
  • PAGE_SIZE: 標準的なリストのページサイズ。page_size パラメータを更新して、ページごとに返される RagCorpora の数を調整できます。
  • PAGE_TOKEN: 標準的なリストのページトークン。通常は、前の VertexRagDataService.ListRagCorpora 呼び出しの ListRagCorporaResponse.next_page_token を使用して取得します。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content
指定された PROJECT_ID で、成功ステータス コード(`2xx`)と RagCorpora のリストが返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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 コーパスの取得の例

REST

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

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

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/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.
// 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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/

Vertex AI SDK for Python

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


from vertexai.preview 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 コーパスの削除の例

REST

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

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

HTTP メソッドと URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID" | Select-Object -Expand Content
成功すると、DeleteOperationMetadata が返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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 ファイルのアップロードの例

REST

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

  PROJECT_ID: Your project ID.
  LOCATION: The region to process the request.
  RAG_CORPUS_ID: The corpus ID of your RAG corpus.
  LOCAL_FILE_PATH: The local path to the file to be uploaded.
  DISPLAY_NAME: The display name of the RAG file.
  DESCRIPTION: The description of the RAG file.

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

  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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload"

Vertex AI SDK for Python

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


from vertexai.preview 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.skipped_rag_files_count は、インポート中にスキップされたファイルの数です。ファイルは、次の条件が満たされるとスキップされます。

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

REST

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

  • PROJECT_ID: 実際のプロジェクト ID
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。
  • GCS_URIS: Cloud Storage ロケーションのリスト。例: gs://my-bucket1, gs://my-bucket2
  • CHUNK_SIZE: 省略可。各チャンクに必要なトークンの数。
  • CHUNK_OVERLAP: 省略可: チャンク間で重複するトークンの数。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/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: Your project ID.
  LOCATION: The region to process the request.
  RAG_CORPUS_ID: The corpus ID of your RAG corpus.
  GCS_URIS: A list of Cloud Storage locations. Example: gs://my-bucket1.
  CHUNK_SIZE: Number of tokens each chunk should have.
  CHUNK_OVERLAP: Number of tokens overlap between chunks.
  EMBEDDING_MODEL_QPM_RATE: The QPM rate to limit RAGs access to your embedding model. Example: 1000.
// 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/v1beta1/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
  }
}'

// Poll the operation status.
// The response contains the number of files imported.
OPERATION_ID: The operation ID you get from the response of the previous command.
poll_op_wait OPERATION_ID

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

  PROJECT_ID: Your project ID.
  LOCATION: The region to process the request.
  RAG_CORPUS_ID: The corpus ID of your RAG corpus.
  FOLDER_RESOURCE_ID: The resource ID of your Google Drive folder.
  CHUNK_SIZE: Number of tokens each chunk should have.
  CHUNK_OVERLAP: Number of tokens overlap between chunks.
  EMBEDDING_MODEL_QPM_RATE: The QPM rate to limit RAGs access to your embedding model. Example: 1000.
// 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/v1beta1/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
  }
}'

// Poll the operation status.
// The response contains the number of files imported.
OPERATION_ID: The operation ID you get from the response of the previous command.
poll_op_wait OPERATION_ID

Vertex AI SDK for Python

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


from vertexai.preview 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 Google 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,
    chunk_size=512,  # Optional
    chunk_overlap=100,  # Optional
    max_embedding_requests_per_min=900,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")
# Example response:
# Imported 2 files.

RAG ファイルのリストの例

このコードサンプルは、RAG ファイルをリストする方法を示しています。

REST

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

  • PROJECT_ID: 実際のプロジェクト ID
  • LOCATION: リクエストを処理するリージョン。
  • RAG_CORPUS_ID: RagCorpus リソースの ID。
  • PAGE_SIZE: 標準的なリストのページサイズ。page_size パラメータを更新して、ページごとに返される RagFiles の数を調整できます。
  • PAGE_TOKEN: 標準的なリストのページトークン。通常は、前の VertexRagDataService.ListRagFiles 呼び出しの ListRagFilesResponse.next_page_token を使用して取得します。

HTTP メソッドと URL:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content
指定された RAG_CORPUS_ID の下で、成功ステータス コード(2xx)と RagFiles のリストが返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
成功すると、RagFile リソースが返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
成功すると、DeleteOperationMetadata リソースが返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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 の検索コンポーネントがナレッジベースを検索し、クエリに関連する情報を探します。

REST

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

  • LOCATION: リクエストを処理するリージョン。
  • PROJECT_ID: 実際のプロジェクト ID
  • 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/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" | Select-Object -Expand Content
成功ステータス コード(2xx)と関連する RagFiles のリストが返されます。

Vertex AI SDK for Python

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


from vertexai.preview 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!",
    similarity_top_k=10,  # Optional
    vector_distance_threshold=0.5,  # Optional
    # vector_search_alpha=0.5, # Optional - Only supported for Weaviate
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

生成

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

REST

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

  • PROJECT_ID: 実際のプロジェクト ID
  • LOCATION: リクエストを処理するリージョン。
  • MODEL_ID: コンテンツ生成用の LLM モデル。例: gemini-1.5-pro-002
  • 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: 省略可。ベクトル距離がしきい値より小さいコンテキストが返されます。

HTTP メソッドと URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/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/v1beta1/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/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" | Select-Object -Expand Content
成功すると、生成されたコンテンツと引用が返されます。

Vertex AI SDK for Python

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


from vertexai.preview import rag
from vertexai.preview.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", ...],
                )
            ],
            similarity_top_k=3,  # Optional
            vector_distance_threshold=0.5,  # Optional
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-1.5-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....
#   ...

次のステップ