מדריך למתחילים בנושא שימוש במחשב

בדף הזה מוסבר איך לבצע קריאות ישירות ל-API כדי ליצור סביבת ארגז חול (sandbox) לשימוש במחשב ב-Agent Platform ולהשתמש בה. במדריך למתחילים הזה תבצעו את הפעולות הבאות:

  • כדי לגשת לארגז החול, צריך ליצור מכונה של Agent Platform.
  • יוצרים ארגז חול לשימוש במחשב.
  • יוצרים טוקן גישה לארגז החול.
  • שולחים בקשה לבדיקת הסטטוס.
  • מחיקת המשאבים.

לפני שמתחילים

מגדירים את הפרויקט והסביבה.

הגדרת הפרויקט

  1. 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

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

  3. 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.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

התפקידים הנדרשים

כדי להשתמש בארגז החול וליצור טוקנים, אתם צריכים את התפקידים הבאים:

  • משתמש ב-Agent Platform‏ (roles/aiplatform.user) בפרויקט.
  • יצירת אסימונים בחשבון שירות (roles/iam.serviceAccountTokenCreator) בחשבון השירות שמשמש ליצירת אסימונים.
  • לחשבון השירות שמשמש ליצירת טוקנים צריך להיות גם התפקיד "משתמש בפלטפורמת הסוכן" (roles/aiplatform.user) בפרויקט.

התקנת ספריות

מתקינים את Agent Platform SDK:

pip install google-cloud-agentplatform>=2.0.1

יצירת מכונה של Agent 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: מזהה הפרויקט ב- Google Cloud .
  • ‫LOCATION: האזור של המכונה (למשל us-central1).

כדי להצפין את הנתונים בסביבת ה-Sandbox באמצעות מפתחות הצפנה בניהול הלקוח (CMEK), צריך להגדיר את CMEK כשיוצרים את המכונה של Agent Platform. מידע נוסף זמין במאמר הגדרת מפתחות הצפנה בניהול הלקוח (CMEK).

יצירת תבנית לשימוש במחשב

יוצרים תבנית לארגז חול שתשמש אתכם ליצירת ארגז חול לשימוש במחשב.

# 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}")

יצירת סביבת פיתוח (sandbox) לשימוש במחשב

יוצרים סביבת ארגז חול מהתבנית.

# 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 Web Token ‏ (JWT) באמצעות חשבון שירות.

service_account_email = "SERVICE_ACCOUNT_EMAIL"
access_token = client.sandboxes.generate_access_token(
    service_account_email=service_account_email,
)

מחליפים את SERVICE_ACCOUNT_EMAIL בכתובת האימייל של חשבון השירות עם התפקיד "יצירת אסימונים בחשבון שירות".

שליחת בקשה לארגז החול (sandbox)

שולחים בקשת HTTP GET לשרת ה-API של ארגז החול כדי לבדוק את הסטטוס שלו.

response = client.sandboxes.send_command(
    http_method="GET",
    access_token=access_token,
    sandbox_environment=sandbox
)
print(f"Sandbox response: {response.body}")

שליחת בקשת 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
)

בטבלה הבאה מפורטות שיטות ונתיבים נוספים שאפשר לשלוח לארגז החול:

‏Method נתיב תיאור
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()

המאמרים הבאים