サンドボックスの作成と使用
アクティブな生成AIプロジェクトにサンドボックスを作成し、コマンドを実行して、終了したらランタイムを停止します。
OCI CLIまたはSDKを使用して、サンドボックスを作成します。コンソールには、プロジェクト構成、サンドボックスの詳細、監視およびライフサイクル・アクションが用意されています。
始める前に
- 「サンドボックスのAPIレベルの権限」で権限を構成します。
- サポートされているリージョンを選択してください。
generative-ai-sandboxコマンド・グループまたはoci.generative_ai_sandboxパッケージを含むOCI CLIまたはSDKバージョンをインストールします。OCI CLIおよびSDK for Pythonを参照してください。
例では、示されている変数構文およびOCIセッション認証をサポートするシェルを使用しています。oci session authenticateを実行し、作成したプロファイルにprofile_nameを設定します。CLIでのトークンベースの認証を参照してください。
CLIにコマンドが含まれていることを確認するには、次を実行します:
oci generative-ai-sandbox --helpCLIの使用
COMPARTMENT_OCIDおよびPROJECT_OCIDをリソース識別子に置き換えます。
Python SDKの使用
この例では、サンドボックスが有効で、セッション認証プロファイルが以前に準備された既存のアクティブなプロジェクトを使用します。サンドボックスを作成し、それを待機し、コマンドを実行し、終了時に停止を要求します。
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}")