管理沙箱範本

每個沙箱都以沙箱環境範本為基礎,其中說明要執行的容器映像檔。沙箱範本可做為自訂容器沙箱的藍圖。本頁面說明如何使用 Agent Platform SDK 管理沙箱範本的生命週期。您可以列出現有範本、查看詳細資料,或在不再需要時刪除範本。

如要修改範本的設定 (例如變更容器映像檔),請刪除現有範本,然後使用更新後的設定建立新範本。

事前準備

如要管理沙箱範本,您必須具備專案的下列角色:

  • Agent Platform 使用者 (roles/aiplatform.user)

在多個沙箱中重複使用範本

如果是電腦用途和 Shell 沙箱,如未指定範本,系統會為沙箱建立預設範本 (DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE 或 DEFAULT_CONTAINER_CATEGORY_SHELL_SANDBOX)。

每次呼叫 create() 時,如果沒有指定範本名稱,系統就會建立新範本。刪除沙箱不會刪除範本,因此您必須分別刪除範本。如果您建立許多沙箱,建議重複使用範本,以節省資源。

建立範本:

from agentplatform._genai import types

template_op = client.agent_engines.sandboxes.templates.create(
    name=instance_name,
    display_name="shell-sandbox-template",
    config=types.CreateSandboxEnvironmentTemplateConfig(
        default_container_environment=types.SandboxEnvironmentTemplateDefaultContainerEnvironment(
            default_container_category=types.DefaultContainerCategory.DEFAULT_CONTAINER_CATEGORY_SHELL_SANDBOX,
        ),
    ),
)
template = template_op.response.name

設定 sandbox_environment_template,將每個沙箱與範本建立關聯:

operation = client.sandboxes.create(
    name=instance_name,
    spec={"shell_environment": {}},
    config={
        "sandbox_environment_template": template,
        "wait_for_completion": True,
    },
)

列出範本

如要列出與 Agent Platform 執行個體相關聯的所有沙箱範本:

import agentplatform

client = agentplatform.Client(project='PROJECT_ID', location='LOCATION')
instance_name = 'projects/PROJECT_ID/locations/LOCATION/reasoningEngines/INSTANCE_ID'

templates_response = client.sandboxes.templates.list(name=instance_name)

for template in templates_response.sandbox_environment_templates:
    print(template.name)

更改下列內容:

  • PROJECT_ID: Cloud de Confiance 專案 ID。
  • LOCATION:Agent Platform 執行個體的 Cloud de Confiance 區域。請參閱「支援的區域」。
  • INSTANCE_ID:Agent Platform 執行個體 ID。

查看範本詳細資料

如要查看特定沙箱範本的設定:

template_name = 'projects/PROJECT_ID/locations/LOCATION/reasoningEngines/INSTANCE_ID/SandboxEnvironmentTemplate/TEMPLATE_ID'

template = client.sandboxes.templates.get(name=template_name)
print(template)

更改下列內容:

  • PROJECT_ID: Cloud de Confiance 專案 ID。
  • LOCATION:Agent Platform 執行個體的 Cloud de Confiance 區域。請參閱「支援的區域」。
  • INSTANCE_ID:包含沙箱範本的 Agent Platform 執行個體 ID。
  • TEMPLATE_ID:沙箱範本 ID。

查看沙箱的範本

如要查看與沙箱相關聯的沙箱範本,請執行下列指令:

print(sandbox.sandbox_environment_template)

刪除範本

如要刪除沙箱範本:

client.sandboxes.templates.delete(name=template_name)
print("Template deleted.")