Vertex AI で LangChain を使用する前に、環境が設定されていることを確認する必要があります。Google Cloud プロジェクトを用意して課金を有効にする必要があります。また、必要な権限を持ち、Cloud Storage バケットを設定し、Vertex AI SDK for Python をインストールする必要があります。以下のトピックを使用して、Vertex AI で LangChain を使い始めるための準備を整えてください。
Google Cloud プロジェクトを設定する
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
必要なロールを取得する
推論エンジンの使用に必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。
-
Vertex AI ユーザー(
roles/aiplatform.user
) -
ストレージ管理者(
roles/storage.admin
)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。
必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。
サービス エージェントの権限を設定する
推論エンジンにデプロイするアプリケーションは、AI Platform Reasoning Engine サービス エージェントのサービス アカウントとして実行されます。このアカウントには、推論エンジン アプリケーションに必要な基本的な権限を付与する Vertex AI Reasoning Engine サービス エージェント ロールが割り当てられています。基本的な権限の一覧については、IAM のドキュメントをご覧ください。
追加の権限が必要な場合は、次の手順を実行することで、このサービス エージェントに追加のロールを付与できます。
[IAM] ページに移動し、[Google 提供のロール付与を含める] チェックボックスをオンにします。
service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com
に一致するプリンシパルを見つけます。編集ボタンをクリックして必要なロールをプリンシパルに追加した後、保存ボタンをクリックします。
Reasoning Engine サービス エージェントを手動で生成する
Reasoning Engine サービス エージェントは、推論エンジンのデプロイ中に自動的にプロビジョニングされますが、事前に手動で生成することが必要になる場合があります。これは、デプロイ プロセスに必要な権限を付与してデプロイの失敗を避けるために、Reasoning Engine サービス エージェントに特定のロールを割り当てる必要がある場合に特に重要です。
Reasoning Engine サービス エージェントを手動で生成する手順は次のとおりです。
Google Cloud CLI を使用して Reasoning Engine サービス エージェントを生成します。
gcloud beta services identity create --service=aiplatform.googleapis.com --project=PROJECT-ID-OR-PROJECT-NUMBER
[IAM] ページに移動し、[アクセスを許可] をクリックします。
[プリンシパルの追加] セクションの [新しいプリンシパル] フィールドに「
service-PROJECT_NUMBER@gcp-sa-aiplatform-re.iam.gserviceaccount.com
」と入力します。[ロールを割り当てる] セクションで、必要なロールを見つけて選択します。
[保存] をクリックします。
Cloud Storage バケットを作成する
推論エンジンは、デプロイ プロセスの一環として、アプリケーションのアーティファクトを Cloud Storage バケットにステージします。Vertex AI の使用を認証されたプリンシパル(ご自身またはサービス アカウント)に、このバケットへの Storage Admin
アクセス権があることを確認してください。このアクセス権は、Vertex AI SDK for Python がコードをパッケージ化してこのバケットに書き込むために必要になります。
Google Cloud コンソール
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
-
In the Get started section, do the following:
- Enter a globally unique name that meets the bucket naming requirements.
- To add a
bucket label,
expand the Labels section ( ),
click add_box
Add label, and specify a
key
and avalue
for your label.
-
In the Choose where to store your data section, do the following:
- Select a Location type.
- Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
- If you select the dual-region location type, you can also choose to enable turbo replication by using the relevant checkbox.
- To set up cross-bucket replication, select
Add cross-bucket replication via Storage Transfer Service and
follow these steps:
Set up cross-bucket replication
- In the Bucket menu, select a bucket.
In the Replication settings section, click Configure to configure settings for the replication job.
The Configure cross-bucket replication pane appears.
- To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
- To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
- Click Done.
-
In the Choose how to store your data section, do the following:
- Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
- To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
- In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
-
In the Choose how to protect object data section, do the
following:
- Select any of the options under Data protection that you
want to set for your bucket.
- To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
- To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
- To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
- To enable Object Retention Lock, click the Enable object retention checkbox.
- To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
- To choose how your object data will be encrypted, expand the Data encryption section (Data encryption method. ), and select a
- Select any of the options under Data protection that you
want to set for your bucket.
-
In the Get started section, do the following:
- Click Create.
コマンドライン
Vertex AI SDK for Python をインストールして初期化する
次のコマンドを実行して、Vertex AI SDK for Python の推論エンジン パッケージをインストールします。
pip install google-cloud-aiplatform[reasoningengine,langchain]
次のコードを実行して、Reasoning Engine の SDK のインポートと初期化を行います。
import vertexai
from vertexai.preview import reasoning_engines
vertexai.init(
project="PROJECT_ID",
location="LOCATION",
staging_bucket="gs://BUCKET_NAME",
)
- PROJECT_ID: 実際のプロジェクト ID。
- LOCATION: リージョン。現在のところ、
us-central1
のみがサポートされています。 - BUCKET_NAME: 使用中の Google Cloud バケット。