Skip to content

Commit

Permalink
added sandboxing support
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkulaga committed Oct 18, 2024
1 parent 5dea00f commit f46ce26
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
11 changes: 6 additions & 5 deletions examples/test_sandbox.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from llm_sandbox import SandboxSession
from just_agents_sandbox.micromamba_session import MicromambaSession

# Create a new sandbox session
def test_sandbox():
with SandboxSession(image="mambaorg/micromamba:latest", keep_template=True) as session:
result = session.run("echo 'Hello world'")
print(result)

# Or default image
with MicromambaSession(image="quay.io/longevity-genie/biosandbox:latest", lang="python", keep_template=True, verbose=True) as session:
session.execute_command("python --version")
result = session.run("print('Hello, World!')")


test_sandbox()
57 changes: 57 additions & 0 deletions just_agents_sandbox/micromamba_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Optional
import docker
from llm_sandbox.session import SandboxDockerSession
from llm_sandbox.docker import ConsoleOutput
from llm_sandbox.const import SupportedLanguage

class MicromambaSession(SandboxDockerSession):
def __init__(self, client: Optional[docker.DockerClient] = None,
image: Optional[str] = None,
dockerfile: Optional[str] = None,
lang: str = SupportedLanguage.PYTHON,
keep_template: bool = False,
verbose: bool = False, environment: str = "base"):
super().__init__(client=client,
image=image,
dockerfile=dockerfile,
lang=lang,
keep_template=keep_template,
verbose=verbose)
self.environment = environment


def execute_command(
self, command: Optional[str], workdir: Optional[str] = None
) -> ConsoleOutput:
if not command:
raise ValueError("Command cannot be empty")

if not self.container:
raise RuntimeError(
"Session is not open. Please call open() method before executing commands."
)
command = f"micromamba run -n {self.environment} {command}"

if self.verbose:
print(f"Executing command: {command}")

if workdir:
exit_code, exec_log = self.container.exec_run(
command, stream=True, tty=True, workdir=workdir
)
else:
exit_code, exec_log = self.container.exec_run(
command, stream=True, tty=True
)

output = ""
if self.verbose:
print("Output:", end=" ")

for chunk in exec_log:
chunk_str = chunk.decode("utf-8")
output += chunk_str
if self.verbose:
print(chunk_str, end="")

return ConsoleOutput(output)
2 changes: 1 addition & 1 deletion publish.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##!/bin/bash
rm -rf dist
python setup.py sdist bdist_wheel --universal
twine upload --verbose dist/* --config-file ~/.pypirc
twine upload --verbose dist/* --config-file .pypirc

0 comments on commit f46ce26

Please sign in to comment.