このページでは、Computer Use サンドボックス環境を作成して使用するための直接 API 呼び出しを行う方法について説明します。 このクイックスタートでは、次のタスクを行います。
- サンドボックスにアクセスする Agent Platform インスタンスを作成する。
- Computer Use サンドボックスを作成する。
- サンドボックスのアクセス トークンを生成する。
- ステータスを確認するリクエストを送信する。
- リソースをクリーンアップする。
始める前に
プロジェクトと環境を設定します。
プロジェクトを設定する
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the Gemini Enterprise Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
必要なロールを取得する
サンドボックスを使用してトークンを生成するには、次のロールが必要です。
- プロジェクトに対する Agent Platform ユーザー(
roles/aiplatform.user)。 - トークン生成に使用されるサービス アカウントに対するサービス アカウント トークン作成者(
roles/iam.serviceAccountTokenCreator)。 - トークン生成に使用されるサービス アカウントには、プロジェクトに対する Agent Platform ユーザー(
roles/aiplatform.user)ロールも必要です。
ライブラリをインストールする
Agent Platform SDK をインストールします。
pip install google-cloud-agentplatform>=2.0.1Agent Platform インスタンスを作成する
サンドボックスを使用するには、まず Agent Platform インスタンスを作成します。
import agentplatform
client = agentplatform.Client(
project='PROJECT_ID',
location='LOCATION',
http_options={
"api_version": "v1beta1",
}
)
remote_agent = client.runtimes.create()
remote_agent_name = remote_agent.api_resource.name
次のように置き換えます。
PROJECT_ID: 実際の Cloud de Confiance プロジェクト ID。LOCATION: インスタンスのリージョン(us-central1など)。
顧客管理の暗号鍵(CMEK)を使用してサンドボックス データを暗号化するには、Agent Platform インスタンスの作成時に CMEK を構成する必要があります。詳細については、顧客管理の暗号鍵(CMEK)を構成するをご覧ください。
Computer Use のテンプレートを作成する
Computer Use サンドボックスの作成時に使用するサンドボックス テンプレートを作成します。
# Create a default Computer Use sandbox template
templates_client = client.sandboxes.templates
tmplt_operation = templates_client.create(
name=remote_agent_name,
display_name='DISPLAY_NAME',
config={
"default_container_environment": {
"default_container_category": "DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE",
},
"egress_control_config": {
"internet_access": True,
},
},
)
template_name = tmplt_operation.response.name
print(f"Created template: {template_name}")
Computer Use サンドボックスを作成する
テンプレートからサンドボックス環境を作成します。
# Create a sandbox environment referencing the template.
# To customize the lifetime, set `ttl` to a duration string in seconds (for example, "3600s" for 1 hour).
create_operation = client.sandboxes.create(
name=remote_agent_name,
config={
"sandbox_environment_template": template_name,
"display_name": 'DISPLAY_NAME',
"ttl": "3600s", # Optional. Overrides the default 2-hour sandbox lifetime.
}
)
sandbox = create_operation.response
print(f"Created sandbox environment: {sandbox.name}")
print(f"Sandbox will expire at: {sandbox.expire_time}")
アクセス トークンを生成する
サンドボックスを操作するには、サービス アカウントを使用して JSON ウェブトークン(JWT)アクセス トークンを生成します。
service_account_email = "SERVICE_ACCOUNT_EMAIL"
access_token = client.sandboxes.generate_access_token(
service_account_email=service_account_email,
)
SERVICE_ACCOUNT_EMAIL は、サービス アカウント トークン作成者ロールを持つサービス アカウントのメールアドレスに置き換えます。
サンドボックスにリクエストを送信する
HTTP GET リクエストをサンドボックス API サーバーに送信して、ステータスを確認します。
response = client.sandboxes.send_command(
http_method="GET",
access_token=access_token,
sandbox_environment=sandbox
)
print(f"Sandbox response: {response.body}")
特定のページに移動するには、HTTP POST リクエストを送信します。
data = {"command": "Page.navigate", "params": {"url": "https://example.com"}}
response = client.sandboxes.send_command(
http_method="POST",
path="cdp",
access_token=access_token,
request_dict=data,
sandbox_environment=sandbox
)
次の表に、サンドボックスに送信できる追加のメソッドとパスを示します。
| メソッド | パス | 説明 |
|---|---|---|
| GET | / | サンドボックスのヘルスチェックを取得します。 |
| POST | /tabs | 新しいタブを作成し、その詳細を返します。 |
| GET | /tabs | 開いているすべてのタブの詳細を一覧表示します。 |
| POST | /tabs/{tab_id}/activate | タブをアクティブに設定し、フォアグラウンドに表示します。 |
| 削除 | /tabs/{tab_id} | 特定のタブを閉じます。 |
| GET | /cdp_ws_endpoint | CDP WebSocket エンドポイント パスを返します。 |
| POST | /cdp | アクティブなタブで CDP コマンドを実行します。 |
| POST | /cdps | アクティブなタブで複数の CDP コマンドを実行します。 |
サポートされている CDP コマンドと形式の詳細については、 CDP サイトをご覧ください。
クリーンアップ
課金されないようにするには、このクイックスタートで作成したリソースを削除します。
client.sandboxes.delete(name=sandbox.name)
remote_agent.delete()