管理沙箱

本頁說明如何使用 Python SDK 管理沙箱環境的生命週期。您可以列出現有沙箱、查看詳細資料、暫停及繼續沙箱,或在不再需要時刪除沙箱。

事前準備

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

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

管理沙箱

您可以列出、查看詳細資料、暫停、繼續及刪除與 Agent Platform 執行個體相關聯的沙箱環境。暫停和繼續功能可讓您取消閒置沙箱的運算資源排程,並在稍後重新上線,且不會遺失檔案系統狀態或連線身分。如要變更現有沙箱的設定,請刪除沙箱並重新建立。

列出沙箱

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

import agentplatform

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

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

for sandbox in sandboxes:
    print(sandbox.name)

更改下列內容:

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

查看沙箱詳細資料

如要查看特定沙箱的設定和狀態,請按照下列步驟操作:

sandbox_name = 'projects/PROJECT_ID/locations/LOCATION/reasoningEngines/INSTANCE_ID/sandboxEnvironments/SANDBOX_ID'

sandbox = client.sandboxes.get(name=sandbox_name)
print(sandbox)

更改下列內容:

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

輸出內容包含建立時間、狀態 (例如 STATE_RUNNING) 和沙箱規格等詳細資料。

暫停沙箱

暫停沙箱會釋出運算資源,但保留磁碟狀態和連線中繼資料。暫停的沙箱比執行中的沙箱便宜許多,而且可以在幾秒內恢復。如果沙箱閒置時間較長,但您想保留工作階段狀態和連線身分,以供日後使用,請使用暫停功能。

已暫停的沙箱:

  • 保留沙箱 ID、連線中繼資料和任何 Private Service Connect 端點。
  • 保留暫停時容器內的所有檔案系統狀態。
  • 轉換至 STATE_PAUSED。在您恢復之前,對其資料平面端點提出的要求都會傳回錯誤。
import agentplatform

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

pause_operation = client.sandboxes.pause(
    name=sandbox_name,
    config={
        "wait_for_completion": True,  # Optional. Blocks until the sandbox reaches STATE_PAUSED.
    },
)
paused_sandbox = pause_operation.response
print(f"Sandbox {paused_sandbox.name} state: {paused_sandbox.state}")  # STATE_PAUSED

更改下列內容:

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

繼續使用沙箱

恢復暫停的沙箱後,運算資源就會重新上線。沙箱會保留暫停時的 ID、連線端點 (包括任何 Private Service Connect 服務連結) 和檔案系統狀態。恢復作業完成後,即可再次向沙箱發出資料平面要求。

sandbox_name = 'projects/PROJECT_ID/locations/LOCATION/reasoningEngines/INSTANCE_ID/sandboxEnvironments/SANDBOX_ID'

resume_operation = client.sandboxes.resume(
    name=sandbox_name,
    config={
        "wait_for_completion": True,  # Optional. Blocks until the sandbox reaches STATE_RUNNING.
    },
)
resumed_sandbox = resume_operation.response
print(f"Sandbox {resumed_sandbox.name} state: {resumed_sandbox.state}")  # STATE_RUNNING

更改下列內容:

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

刪除沙箱

如要明確刪除沙箱環境並釋出資源,請按照下列步驟操作:

client.sandboxes.delete(name=sandbox_name)
print("Sandbox deleted.")