Creating and Using a Sandbox
Create a sandbox in an active Generative AI project, run a command, and stop the runtime when finished.
Use the OCI CLI or SDK to create sandboxes. The Console provides project configuration, sandbox details, monitoring, and lifecycle actions.
Before You Begin
- Configure the permissions in API-Level Permissions for Sandboxes.
- Select a supported region.
- Install an OCI CLI or SDK version that includes the
generative-ai-sandboxcommand group oroci.generative_ai_sandboxpackage. See OCI CLI and SDK for Python.
The examples use a shell that supports the shown variable syntax and OCI session authentication. Run oci session authenticate, then set profile_name to the profile you created. See Token-based Authentication for the CLI.
To check that your CLI includes the commands, run:
oci generative-ai-sandbox --helpUsing the CLI
Replace COMPARTMENT_OCID and PROJECT_OCID with your resource identifiers.
Using the Python SDK
This example uses an existing active project with sandboxes enabled and the session-authentication profile prepared earlier. It creates a sandbox, waits for it, runs a command, and requests a stop when finished.
from pathlib import Path
import time
import warnings
import oci
from oci.generative_ai_sandbox import GenerativeAiSandboxClient
from oci.generative_ai_sandbox.models import (
CreateSandboxDetails,
RunSandboxCommandDetails,
StopSandboxDetails,
)
profile_name = "DEFAULT"
project_id = "PROJECT_OCID"
region = "us-chicago-1"
config = oci.config.from_file(profile_name=profile_name)
key = oci.signer.load_private_key_from_file(config["key_file"])
token = Path(config["security_token_file"]).expanduser().read_text().strip()
signer = oci.auth.signers.SecurityTokenSigner(token, key)
client = GenerativeAiSandboxClient(
config=config,
signer=signer,
service_endpoint=f"https://inference.generativeai.{region}.oci.oraclecloud.com",
)
created = client.create_sandbox(
project_id,
CreateSandboxDetails(
display_name="my-sandbox",
runtime="python-3.11",
shape="SMALL",
expiration_duration="PT30M",
),
).data
sandbox_id = created.id
print(sandbox_id)
state = created.lifecycle_state
try:
# This example waits up to five minutes for creation.
deadline = time.monotonic() + 300
while time.monotonic() < deadline:
sandbox = client.get_sandbox(sandbox_id).data
state = sandbox.lifecycle_state
if state == "RUNNING":
break
if state in {"FAILED", "STOPPING", "STOPPED", "DELETING", "DELETED"}:
raise RuntimeError(f"Sandbox state: {sandbox.lifecycle_state}")
time.sleep(2)
else:
raise TimeoutError("Sandbox didn't reach RUNNING within five minutes")
result = client.run_sandbox_command_and_wait(
sandbox_id,
RunSandboxCommandDetails(command="python3 --version", timeout="PT1M"),
).data
print(result.output.stdout)
print(result.output.stderr)
print(result.output.exit_code)
finally:
# Attempt cleanup without masking an earlier error.
if state not in {"FAILED", "STOPPING", "STOPPED", "DELETING", "DELETED"}:
try:
client.stop_sandbox(sandbox_id, StopSandboxDetails(is_force=False))
except Exception as cleanup_error:
warnings.warn(f"Check sandbox {sandbox_id}: stop failed: {cleanup_error}")