Questa pagina mostra come effettuare chiamate API dirette per creare e utilizzare un ambiente sandbox di utilizzo del computer. In questa guida rapida, svolgi le seguenti attività:
- Crea un'istanza di Agent Platform per accedere alla sandbox.
- Crea una sandbox di utilizzo del computer.
- Genera un token di accesso per la sandbox.
- Invia una richiesta per controllare lo stato.
- Libera spazio.
Prima di iniziare
Configura il progetto e l'ambiente.
Configura il progetto
- Accedi al tuo Google Cloud account. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei carichi di lavoro.
-
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 Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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 Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
Ottieni i ruoli richiesti
Per utilizzare la sandbox e generare token, devi disporre dei seguenti ruoli:
- Agent Platform User (
roles/aiplatform.user) sul progetto. - Creatore token account di servizio (
roles/iam.serviceAccountTokenCreator) sul service account utilizzato per la generazione dei token. - Il account di servizio utilizzato per la generazione dei token deve disporre anche del ruolo Agent Platform User (
roles/aiplatform.user) sul progetto.
Installa le librerie
Installa l'SDK Agent Platform:
posix-terminal
pip install google-cloud-aiplatform>=1.112.0
Crea un'istanza di Agent Platform
Per utilizzare la sandbox, crea prima un'istanza di Agent Platform.
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
Sostituisci quanto segue:
- 'PROJECT_ID': l'ID Google Cloud progetto.
- 'LOCATION': la regione per l'istanza (ad esempio
us-central1).
Crea un modello per l'utilizzo del computer
Crea un modello di sandbox da utilizzare quando crei una sandbox di utilizzo del computer.
# 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}")
Crea una sandbox di utilizzo del computer
Crea un ambiente sandbox dal modello.
# 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}")
Genera un token di accesso
Per interagire con la sandbox, genera un token di accesso JSON Web Token (JWT) utilizzando un account di servizio.
service_account_email = "SERVICE_ACCOUNT_EMAIL"
access_token = client.agent_engines.sandboxes.generate_access_token(
service_account_email=service_account_email,
)
Sostituisci SERVICE_ACCOUNT_EMAIL con l'indirizzo email del service account che ha il ruolo Creatore token account di servizio.
Invia una richiesta alla sandbox
Invia una richiesta GET HTTP al server API della sandbox per verificarne lo stato.
response = client.agent_engines.sandboxes.send_command(
http_method="GET",
access_token=access_token,
sandbox_environment=sandbox
)
print(f"Sandbox response: {response.body}")
Libera spazio
Per evitare che ti vengano addebitati dei costi, elimina le risorse create in questa guida rapida.
client.agent_engines.sandboxes.delete(name=sandbox.name)
agent_instance.delete()
Passaggi successivi
- Esplora snapshot per la gestione del ciclo di vita della sandbox.