-
Notifications
You must be signed in to change notification settings - Fork 53
add example on aio-sandbox #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
|
|
||
| # AIO Sandbox Example | ||
|
|
||
| This example demonstrates how to create and access an [AIO Sandbox](https://github.com/agent-infra/sandbox) via Agent-Sandbox. | ||
|
|
||
| ## Create an AIO Sandbox | ||
|
|
||
| Apply the sandbox manifest with AIO Sandbox runtime. | ||
|
|
||
| ```sh | ||
| kubectl apply -k base | ||
| # sandbox.agents.x-k8s.io/aio-sandbox-example created | ||
| ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instructions assume that the |
||
|
|
||
| They can then check the status of the applied resource. | ||
| Verify sandbox and pod are running: | ||
|
|
||
| ```sh | ||
| # wait until the sandbox is ready | ||
| kubectl wait --for=condition=Ready sandbox aio-sandbox-example | ||
|
|
||
| kubectl get sandbox | ||
lizzzcai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # NAME AGE | ||
| # aio-sandbox-example 41s | ||
| kubectl get pod aio-sandbox-example | ||
lizzzcai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # NAME READY STATUS RESTARTS AGE | ||
| # aio-sandbox-example 1/1 Running 0 49s | ||
| ``` | ||
|
|
||
| ## Accessing the AIO Sandbox Server | ||
|
|
||
| Port forward the aio-sandbox server port. | ||
|
|
||
| ```sh | ||
| kubectl port-forward --address 0.0.0.0 pod/aio-sandbox-example 8080 | ||
| ``` | ||
|
|
||
| Connect to the aio-sandbox on a browser via http://localhost:8080 or <machine-dns>:8080 | ||
|
|
||
|
|
||
| ## Access the AIO Sandbox via Python SDK | ||
|
|
||
| ```sh | ||
| # set up a virtual environment if needed | ||
| python3 -m venv venv | ||
| source venv/bin/activate | ||
|
|
||
| # install the agent-sandbox package | ||
| pip install agent-sandbox | ||
| ``` | ||
|
|
||
| Run the basic python example: | ||
| ```sh | ||
| python3 main.py | ||
| ``` | ||
|
|
||
| Run the site to markdown example: | ||
|
|
||
| ```sh | ||
| # install the playwright package | ||
| pip install playwright | ||
|
|
||
| python3 site_to_markdown.py | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| apiVersion: agents.x-k8s.io/v1alpha1 | ||
| kind: Sandbox | ||
| metadata: | ||
| name: aio-sandbox-example | ||
| spec: | ||
| podTemplate: | ||
| metadata: | ||
| labels: | ||
| sandbox: aio-sandbox-example | ||
| spec: | ||
| containers: | ||
| - name: aio-sandbox | ||
| image: ghcr.io/agent-infra/sandbox:latest | ||
| ports: | ||
| - containerPort: 8080 | ||
| resources: | ||
| limits: | ||
| memory: "2Gi" | ||
| cpu: "1000m" | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| apiVersion: kustomize.config.k8s.io/v1beta1 | ||
| kind: Kustomization | ||
|
|
||
lizzzcai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| resources: | ||
| - aio-sandbox.yaml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from agent_sandbox import Sandbox | ||
lizzzcai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Initialize client | ||
| client = Sandbox(base_url="http://localhost:8080") | ||
| home_dir = client.sandbox.get_context().home_dir | ||
|
|
||
| # Execute shell commands | ||
| result = client.shell.exec_command(command="ls -la") | ||
| print(result.data.output) | ||
|
|
||
| # File operations | ||
lizzzcai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| content = client.file.read_file(file=f"{home_dir}/.bashrc") | ||
| print(content.data.content) | ||
|
|
||
| # Browser automation | ||
| screenshot = client.browser.screenshot() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import asyncio | ||
| import base64 | ||
| from playwright.async_api import async_playwright | ||
| from agent_sandbox import Sandbox | ||
|
|
||
| async def site_to_markdown(): | ||
| # Initialize sandbox client | ||
| c = Sandbox(base_url="http://localhost:8080") | ||
| home_dir = c.sandbox.get_context().home_dir | ||
|
|
||
| # Browser: Automation to download HTML | ||
| async with async_playwright() as p: | ||
| browser_info = c.browser.get_info().data | ||
| page = await (await p.chromium.connect_over_cdp(browser_info.cdp_url)).new_page() | ||
lizzzcai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await page.goto("https://example.com", wait_until="networkidle") | ||
| html = await page.content() | ||
lizzzcai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| screenshot_b64 = base64.b64encode(await page.screenshot()).decode('utf-8') | ||
|
|
||
| # Jupyter: Convert HTML to markdown in sandbox | ||
| c.jupyter.execute_code(code=f""" | ||
| from markdownify import markdownify | ||
|
||
| html = '''{html}''' | ||
| screenshot_b64 = "{screenshot_b64}" | ||
| md = f"{{markdownify(html)}}\\n\\n" | ||
| with open('{home_dir}/site.md', 'w') as f: | ||
| f.write(md) | ||
| print("Done!") | ||
|
||
| """) | ||
|
||
|
|
||
| # Shell: List files in sandbox | ||
| list_result = c.shell.exec_command(command=f"ls -lh {home_dir}") | ||
| print(f"Files in sandbox: {list_result.data.output}") | ||
|
|
||
| # File: Read the generated markdown | ||
| return c.file.read_file(file=f"{home_dir}/site.md").data.content | ||
|
|
||
| if __name__ == "__main__": | ||
| result = asyncio.run(site_to_markdown()) | ||
| print(f"Markdown saved successfully!") | ||
lizzzcai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.