Agent Engine でエージェントを開発してデプロイする
このページでは、指定した日付の 2 つの通貨間の為替レートを返すエージェントを作成し、デプロイする方法について説明します。
始める前に
- 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.
-
Make sure 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
Agent Engine の使用に必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。
-
Vertex AI ユーザー(
roles/aiplatform.user
) -
ストレージ管理者(
roles/storage.admin
)
ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。
Vertex AI SDK for Python をインストールして初期化する
次のコマンドを実行して、Vertex AI SDK for Python とその他の必要なパッケージをインストールします。
pip install --upgrade --quiet \ "google-cloud-aiplatform[langchain,reasoningengine]" \ cloudpickle==3.0.0 \ "pydantic>=2.10" \ requests
ユーザーとして認証します。
Colab
次のコードを実行します。
from google.colab import auth auth.authenticate_user()
Cloud Shell
対応は不要です。
ローカルシェル
次のコマンドを実行します。
gcloud auth application-default login
次のコードを実行して、Agent Engine をインポートし、SDK を初期化します。
import vertexai from vertexai import agent_engines vertexai.init( project="PROJECT_ID", # Your project ID. location="LOCATION", # Your cloud region. staging_bucket="gs://BUCKET_NAME", # Your staging bucket. )
エージェントを開発する
LangchainAgent
テンプレートを使用してエージェントをインスタンス化します。def get_exchange_rate( currency_from: str = "USD", currency_to: str = "EUR", currency_date: str = "latest", ): """Retrieves the exchange rate between two currencies on a specified date.""" import requests response = requests.get( f"https://api.frankfurter.app/{currency_date}", params={"from": currency_from, "to": currency_to}, ) return response.json() agent = agent_engines.LangchainAgent( model="gemini-1.5-pro", tools=[get_exchange_rate], # Optional. model_kwargs={ "temperature": 0.28, "max_output_tokens": 1000, "top_p": 0.95, }, )
ローカルでエージェントをテストします。
agent.query( input="What's the exchange rate from US dollars to Swedish currency today?" )
エージェントをデプロイする
エージェントを Agent Engine マネージド ランタイムにデプロイします。
remote_agent = agent_engines.create( agent, requirements=[ "google-cloud-aiplatform[agent_engines,langchain]", "cloudpickle==3.0.0", "pydantic>=2.10", "requests", ], )
これにより、Vertex AI に
reasoningEngine
リソースが作成されます。クエリを送信してリモート エージェントをテストします。
remote_agent.query( input="What's the exchange rate from US dollars to Swedish currency today?" )
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。
remote_agent.delete()