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 Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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 Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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 Agent Platform User (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': ID project 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
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 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}")

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

  • Jelajahi Snapshot untuk pengelolaan siklus proses sandbox.