Panduan memulai Penggunaan Komputer

Halaman ini menunjukkan cara melakukan panggilan API langsung untuk membuat dan menggunakan lingkungan sandbox Penggunaan Komputer. Dalam panduan memulai ini, Anda akan melakukan tugas-tugas berikut:

  • Buat instance Agent Platform untuk mengakses sandbox.
  • Buat sandbox Penggunaan Komputer.
  • Buat token akses untuk sandbox.
  • Kirim permintaan untuk memeriksa status.
  • Bersihkan resource.

Sebelum memulai

Menyiapkan project dan lingkungan Anda.

Menyiapkan project

  1. Login ke akun Google Cloud Anda. 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.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Gemini Enterprise Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Gemini Enterprise Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

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 Google Cloud Anda.
  • LOCATION: Region untuk instance Anda (seperti us-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.
# To customize the lifetime, set `ttl` to a duration string in seconds (for example, "3600s" for 1 hour).
create_operation = client.agent_engines.sandboxes.create(
    name=agent_instance_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}")

Membuat token akses

Untuk berinteraksi dengan sandbox, buat token akses JSON Web Token (JWT) dengan 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 POST HTTP 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
DAPATKAN / Mendapatkan health check sandbox
POST /tabs Membuat tab baru dan menampilkan detailnya.
DAPATKAN /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.
DAPATKAN /cdp_ws_endpoint Menampilkan jalur endpoint WebSocket CDP.
POST /cdp Menjalankan perintah CDP di tab aktif.
POST /cdps Menjalankan beberapa perintah CDP di tab yang aktif.

Untuk mengetahui informasi selengkapnya tentang perintah dan format CDP yang didukung, lihat situs CDP.

Pembersihan

Agar tidak menimbulkan biaya, 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.