Halaman ini menunjukkan cara melakukan panggilan API langsung untuk membuat dan menggunakan lingkungan sandbox Penggunaan Komputer. Dalam panduan memulai ini, Anda akan melakukan tugas berikut:
- Membuat instance Agent Platform untuk mengakses sandbox.
- Membuat sandbox Penggunaan Komputer.
- Membuat token akses untuk sandbox.
- Mengirim permintaan untuk memeriksa status.
- Membersihkan resource.
Sebelum memulai
Menyiapkan project dan lingkungan Anda.
Menyiapkan project
- Login keakun Anda. Google Cloud Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 Google Cloud project.
Enable the Gemini Enterprise Agent Platform API.
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.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 Google Cloud project.
Enable the Gemini Enterprise Agent Platform API.
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.
Mendapatkan peran yang diperlukan
Untuk menggunakan sandbox dan membuat token, Anda memerlukan peran berikut:
- Pengguna Agent Platform (
roles/aiplatform.user) di project. - Service Account Token Creator (
roles/iam.serviceAccountTokenCreator) di akun layanan yang digunakan untuk pembuatan token. - Akun layanan yang digunakan untuk pembuatan token juga harus memiliki peran Pengguna Agent Platform (
roles/aiplatform.user) di project.
Menginstal library
Instal Agent Platform SDK:
posix-terminal
pip install google-cloud-aiplatform>=1.112.0
Membuat instance Agent Platform
Untuk menggunakan sandbox, buat instance Agent Platform terlebih dahulu.
import vertexai
client = vertexai.Client(
project='PROJECT_ID',
location='LOCATION',
http_options={
"api_version": "v1beta1",
}
)
agent_instance = client.agent_engines.create()
agent_instance_name = agent_instance.api_resource.name
Ganti kode berikut:
PROJECT_ID: Project ID Anda Google Cloud .LOCATION: Region untuk instance Anda (sepertius-central1).
Membuat template untuk Penggunaan Komputer
Buat template sandbox yang akan Anda gunakan saat membuat sandbox Penggunaan Komputer.
# Create a default Computer Use sandbox template
templates_client = client.agent_engines.sandboxes.templates
tmplt_operation = templates_client.create(
name=agent_instance_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}")
Membuat sandbox Penggunaan Komputer
Buat lingkungan sandbox dari template.
# Create a sandbox environment referencing the template
create_operation = client.agent_engines.sandboxes.create(
name=agent_instance_name,
config={
"sandbox_environment_template": template_name,
"display_name": 'DISPLAY_NAME',
}
)
sandbox = create_operation.response
print(f"Created sandbox environment: {sandbox.name}")
Membuat token akses
Untuk berinteraksi dengan sandbox, buat token akses token web JSON (JWT) menggunakan akun layanan.
service_account_email = "SERVICE_ACCOUNT_EMAIL"
access_token = client.agent_engines.sandboxes.generate_access_token(
service_account_email=service_account_email,
)
Ganti SERVICE_ACCOUNT_EMAIL dengan email akun layanan yang memiliki peran Service Account Token Creator.
Mengirim permintaan ke sandbox
Kirim permintaan HTTP GET ke server API sandbox untuk memeriksa statusnya.
response = client.agent_engines.sandboxes.send_command(
http_method="GET",
access_token=access_token,
sandbox_environment=sandbox
)
print(f"Sandbox response: {response.body}")
Kirim permintaan HTTP POST untuk membuka halaman tertentu.
data = {"command": "Page.navigate", "params": {"url": "https://example.com"}}
response = client.agent_engines.sandboxes.send_command(
http_method="POST",
path="cdp",
access_token=access_token,
request_dict=data,
sandbox_environment=sandbox
)
Tabel berikut mencantumkan metode dan jalur tambahan yang dapat Anda kirim ke sandbox:
| Metode | Jalur | Deskripsi |
|---|---|---|
| GET | / | Mendapatkan health check sandbox |
| POST | /tabs | Membuat tab baru dan menampilkan detailnya. |
| GET | /tabs | Mencantumkan detail untuk semua tab yang terbuka. |
| POST | /tabs/{tab_id}/activate | Menetapkan tab sebagai aktif dan membawanya ke latar depan. |
| HAPUS | /tabs/{tab_id} | Menutup tab tertentu. |
| GET | /cdp_ws_endpoint | Menampilkan jalur endpoint CDP WebSocket. |
| POST | /cdp | Menjalankan perintah CDP di tab aktif. |
| POST | /cdps | Menjalankan beberapa perintah CDP di tab aktif. |
Untuk mengetahui informasi selengkapnya tentang perintah dan format CDP yang didukung, lihat situs CDP.
Pembersihan
Untuk menghindari tagihan, hapus resource yang dibuat dalam panduan memulai ini.
client.agent_engines.sandboxes.delete(name=sandbox.name)
agent_instance.delete()
Langkah berikutnya
- Pelajari Snapshot untuk pengelolaan siklus proses sandbox.