sysbox


Namesysbox JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/vndee/sysbox
SummaryLightweight and portable sandbox runtime (code interpreter) Python library
upload_time2024-09-01 20:33:05
maintainerNone
docs_urlNone
authorMark Powers
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Sysbox v0.1.2

*Securely Execute LLM-Generated Code with Ease*

Sysbox is a lightweight and portable sandbox environment designed to run large language model (LLM) generated code in a safe and isolated manner using Docker containers or Kubernetes. This project provides an easy-to-use interface for setting up, managing, and executing code in a controlled environment, simplifying the process of running code generated by LLMs.

![Sysbox](https://blog.duy.dev/content/images/size/w2000/2024/07/sysbox--6--1.png)
### Features

- **Easy Setup:** Quickly create sandbox environments with minimal configuration.
- **Isolation:** Run your code in isolated Docker containers to prevent interference with your host system.
- **Flexibility:** Support for multiple programming languages.
- **Portability:** Use predefined Docker images or custom Dockerfiles.
- **Scalability:** Support Kubernetes and remote Docker host.

### Installation

#### Using pip

1. Ensure you have [pip](https://pip.pypa.io/en/stable/installation/) installed.
2. Install the package:

```sh
pip install sysbox
```

### Usage

#### Session Lifecycle

The `SandboxSession` class manages the lifecycle of the sandbox environment, including the creation and destruction of Docker containers. Here’s a typical lifecycle:

1. **Initialization:** Create a `SandboxSession` object with the desired configuration.
2. **Open Session:** Call the `open()` method to build/pull the Docker image and start the Docker container.
3. **Run Code:** Use the `run()` method to execute code inside the sandbox. Currently, it supports Python, Java, JavaScript, C++, Go, and Ruby. See [examples](examples) for more details.
4. **Close Session:** Call the `close()` method to stop and remove the Docker container. If the `keep_template` flag is set to `True`, the Docker image will not be removed, and the last container state will be committed to the image.

### Example

Here's a simple example to demonstrate how to use sysbox:

```python
from sysbox import SandboxSession

# Create a new sandbox session
with SandboxSession(image="python:3.9.19-bullseye", keep_template=True, lang="python") as session:
    result = session.run("print('Hello, World!')")
    print(result)

# With custom Dockerfile
with SandboxSession(dockerfile="Dockerfile", keep_template=True, lang="python") as session:
    result = session.run("print('Hello, World!')")
    print(result)

# Or default image
with SandboxSession(lang="python", keep_template=True) as session:
    result = session.run("print('Hello, World!')")
    print(result)
```


sysbox also supports copying files between the host and the sandbox:

```python
from sysbox import SandboxSession

with SandboxSession(lang="python", keep_template=True) as session:
    # Copy a file from the host to the sandbox
    session.copy_to_runtime("test.py", "/sandbox/test.py")

    # Run the copied Python code in the sandbox
    result = session.run("python /sandbox/test.py")
    print(result)

    # Copy a file from the sandbox to the host
    session.copy_from_runtime("/sandbox/output.txt", "output.txt")
```

For other languages usage, please refer to the [examples](examples/code_runner_docker.py).

You can also use [remote Docker host](https://docs.docker.com/config/daemon/remote-access/) as below:

```python
import docker
from sysbox import SandboxSession

tls_config = docker.tls.TLSConfig(
    client_cert=("path/to/cert.pem", "path/to/key.pem"),
    ca_cert="path/to/ca.pem",
    verify=True
)
docker_client = docker.DockerClient(base_url="tcp://<your_host>:<port>", tls=tls_config)

with SandboxSession(
    client=docker_client,
    image="python:3.9.19-bullseye",
    keep_template=True,
    lang="python",
) as session:
    result = session.run("print('Hello, World!')")
    print(result)
```

For Kubernetes usage, please refer to the examples. Essentially, you just need to set the use_kubernetes flag to True and provide the Kubernetes client, or leave it as the default for the local context.

#### Integration
With Langchain integration, you can easily run the generated code in a safe and isolated environment. Here's an example of how to use sysbox with Langchain:

```python
from typing import Optional, List
from sysbox import SandboxSession
from langchain import hub
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from langchain.agents import AgentExecutor, create_tool_calling_agent


@tool
def run_code(lang: str, code: str, libraries: Optional[List] = None) -> str:
    """
    Run code in a sandboxed environment.
    :param lang: The language of the code.
    :param code: The code to run.
    :param libraries: The libraries to use, it is optional.
    :return: The output of the code.
    """
    with SandboxSession(lang=lang, verbose=False) as session:  # type: ignore[attr-defined]
        return session.run(code, libraries).text


if __name__ == "__main__":
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    prompt = hub.pull("hwchase17/openai-functions-agent")
    tools = [run_code]

    agent = create_tool_calling_agent(llm, tools, prompt)
    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
    output = agent_executor.invoke(
        {
            "input": "Write python code to calculate Pi number by Monte Carlo method then run it."
        }
    )
    print(output)

    output = agent_executor.invoke(
        {
            "input": "Write python code to calculate the factorial of a number then run it."
        }
    )
    print(output)

    output = agent_executor.invoke(
        {"input": "Write python code to calculate the Fibonacci sequence then run it."}
    )
    print(output)
```

For Llama-Index:
```python
from typing import Optional, List
from sysbox import SandboxSession

from llama_index.llms.openai import OpenAI
from llama_index.core.tools import FunctionTool
from llama_index.core.agent import FunctionCallingAgentWorker


import nest_asyncio

nest_asyncio.apply()


def run_code(lang: str, code: str, libraries: Optional[List] = None) -> str:
    """
    Run code in a sandboxed environment.
    :param lang: The language of the code, must be one of ['python', 'java', 'javascript', 'cpp', 'go', 'ruby'].
    :param code: The code to run.
    :param libraries: The libraries to use, it is optional.
    :return: The output of the code.
    """
    with SandboxSession(lang=lang, verbose=False) as session:  # type: ignore[attr-defined]
        return session.run(code, libraries).text


if __name__ == "__main__":
    llm = OpenAI(model="gpt-4o", temperature=0)
    code_execution_tool = FunctionTool.from_defaults(fn=run_code)

    agent_worker = FunctionCallingAgentWorker.from_tools(
        [code_execution_tool],
        llm=llm,
        verbose=True,
        allow_parallel_tool_calls=False,
    )
    agent = agent_worker.as_agent()

    response = agent.chat(
        "Write python code to calculate Pi number by Monte Carlo method then run it."
    )
    print(response)

    response = agent.chat(
        "Write python code to calculate the factorial of a number then run it."
    )
    print(response)

    response = agent.chat(
        "Write python code to calculate the Fibonacci sequence then run it."
    )
    print(response)

    response = agent.chat("Calculate the sum of the first 10000 numbers.")
    print(response)
```

### Contributing

We welcome contributions to improve sysbox! Since I am a Python developer, I am not familiar with other languages. If you are interested in adding better support for other languages, please feel free to submit a pull request.

Here is a list of things you can do to contribute:
- [ ] Add Java maven support.
- [x] Add support for JavaScript.
- [x] Add support for C++.
- [x] Add support for Go.
- [ ] Add support for Ruby.
- [x] Add remote Docker host support.
- [x] Add remote Kubernetes cluster support.
- [x] Langchain integration.
- [x] LlamaIndex integration.
- [ ] Commit the last container state to the image before closing kubernetes session.

### License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/vndee/sysbox",
    "name": "sysbox",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "Mark Powers",
    "author_email": "mpoweru@lifsys.com",
    "download_url": "https://files.pythonhosted.org/packages/18/2c/5a3ca97c69af829e791d04b3690710802a7ed236bdb9944584eb2e731137/sysbox-0.1.2.tar.gz",
    "platform": null,
    "description": "# Sysbox v0.1.2\n\n*Securely Execute LLM-Generated Code with Ease*\n\nSysbox is a lightweight and portable sandbox environment designed to run large language model (LLM) generated code in a safe and isolated manner using Docker containers or Kubernetes. This project provides an easy-to-use interface for setting up, managing, and executing code in a controlled environment, simplifying the process of running code generated by LLMs.\n\n![Sysbox](https://blog.duy.dev/content/images/size/w2000/2024/07/sysbox--6--1.png)\n### Features\n\n- **Easy Setup:** Quickly create sandbox environments with minimal configuration.\n- **Isolation:** Run your code in isolated Docker containers to prevent interference with your host system.\n- **Flexibility:** Support for multiple programming languages.\n- **Portability:** Use predefined Docker images or custom Dockerfiles.\n- **Scalability:** Support Kubernetes and remote Docker host.\n\n### Installation\n\n#### Using pip\n\n1. Ensure you have [pip](https://pip.pypa.io/en/stable/installation/) installed.\n2. Install the package:\n\n```sh\npip install sysbox\n```\n\n### Usage\n\n#### Session Lifecycle\n\nThe `SandboxSession` class manages the lifecycle of the sandbox environment, including the creation and destruction of Docker containers. Here\u2019s a typical lifecycle:\n\n1. **Initialization:** Create a `SandboxSession` object with the desired configuration.\n2. **Open Session:** Call the `open()` method to build/pull the Docker image and start the Docker container.\n3. **Run Code:** Use the `run()` method to execute code inside the sandbox. Currently, it supports Python, Java, JavaScript, C++, Go, and Ruby. See [examples](examples) for more details.\n4. **Close Session:** Call the `close()` method to stop and remove the Docker container. If the `keep_template` flag is set to `True`, the Docker image will not be removed, and the last container state will be committed to the image.\n\n### Example\n\nHere's a simple example to demonstrate how to use sysbox:\n\n```python\nfrom sysbox import SandboxSession\n\n# Create a new sandbox session\nwith SandboxSession(image=\"python:3.9.19-bullseye\", keep_template=True, lang=\"python\") as session:\n    result = session.run(\"print('Hello, World!')\")\n    print(result)\n\n# With custom Dockerfile\nwith SandboxSession(dockerfile=\"Dockerfile\", keep_template=True, lang=\"python\") as session:\n    result = session.run(\"print('Hello, World!')\")\n    print(result)\n\n# Or default image\nwith SandboxSession(lang=\"python\", keep_template=True) as session:\n    result = session.run(\"print('Hello, World!')\")\n    print(result)\n```\n\n\nsysbox also supports copying files between the host and the sandbox:\n\n```python\nfrom sysbox import SandboxSession\n\nwith SandboxSession(lang=\"python\", keep_template=True) as session:\n    # Copy a file from the host to the sandbox\n    session.copy_to_runtime(\"test.py\", \"/sandbox/test.py\")\n\n    # Run the copied Python code in the sandbox\n    result = session.run(\"python /sandbox/test.py\")\n    print(result)\n\n    # Copy a file from the sandbox to the host\n    session.copy_from_runtime(\"/sandbox/output.txt\", \"output.txt\")\n```\n\nFor other languages usage, please refer to the [examples](examples/code_runner_docker.py).\n\nYou can also use [remote Docker host](https://docs.docker.com/config/daemon/remote-access/) as below:\n\n```python\nimport docker\nfrom sysbox import SandboxSession\n\ntls_config = docker.tls.TLSConfig(\n    client_cert=(\"path/to/cert.pem\", \"path/to/key.pem\"),\n    ca_cert=\"path/to/ca.pem\",\n    verify=True\n)\ndocker_client = docker.DockerClient(base_url=\"tcp://<your_host>:<port>\", tls=tls_config)\n\nwith SandboxSession(\n    client=docker_client,\n    image=\"python:3.9.19-bullseye\",\n    keep_template=True,\n    lang=\"python\",\n) as session:\n    result = session.run(\"print('Hello, World!')\")\n    print(result)\n```\n\nFor Kubernetes usage, please refer to the examples. Essentially, you just need to set the use_kubernetes flag to True and provide the Kubernetes client, or leave it as the default for the local context.\n\n#### Integration\nWith Langchain integration, you can easily run the generated code in a safe and isolated environment. Here's an example of how to use sysbox with Langchain:\n\n```python\nfrom typing import Optional, List\nfrom sysbox import SandboxSession\nfrom langchain import hub\nfrom langchain_openai import ChatOpenAI\nfrom langchain.tools import tool\nfrom langchain.agents import AgentExecutor, create_tool_calling_agent\n\n\n@tool\ndef run_code(lang: str, code: str, libraries: Optional[List] = None) -> str:\n    \"\"\"\n    Run code in a sandboxed environment.\n    :param lang: The language of the code.\n    :param code: The code to run.\n    :param libraries: The libraries to use, it is optional.\n    :return: The output of the code.\n    \"\"\"\n    with SandboxSession(lang=lang, verbose=False) as session:  # type: ignore[attr-defined]\n        return session.run(code, libraries).text\n\n\nif __name__ == \"__main__\":\n    llm = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n    prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n    tools = [run_code]\n\n    agent = create_tool_calling_agent(llm, tools, prompt)\n    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n    output = agent_executor.invoke(\n        {\n            \"input\": \"Write python code to calculate Pi number by Monte Carlo method then run it.\"\n        }\n    )\n    print(output)\n\n    output = agent_executor.invoke(\n        {\n            \"input\": \"Write python code to calculate the factorial of a number then run it.\"\n        }\n    )\n    print(output)\n\n    output = agent_executor.invoke(\n        {\"input\": \"Write python code to calculate the Fibonacci sequence then run it.\"}\n    )\n    print(output)\n```\n\nFor Llama-Index:\n```python\nfrom typing import Optional, List\nfrom sysbox import SandboxSession\n\nfrom llama_index.llms.openai import OpenAI\nfrom llama_index.core.tools import FunctionTool\nfrom llama_index.core.agent import FunctionCallingAgentWorker\n\n\nimport nest_asyncio\n\nnest_asyncio.apply()\n\n\ndef run_code(lang: str, code: str, libraries: Optional[List] = None) -> str:\n    \"\"\"\n    Run code in a sandboxed environment.\n    :param lang: The language of the code, must be one of ['python', 'java', 'javascript', 'cpp', 'go', 'ruby'].\n    :param code: The code to run.\n    :param libraries: The libraries to use, it is optional.\n    :return: The output of the code.\n    \"\"\"\n    with SandboxSession(lang=lang, verbose=False) as session:  # type: ignore[attr-defined]\n        return session.run(code, libraries).text\n\n\nif __name__ == \"__main__\":\n    llm = OpenAI(model=\"gpt-4o\", temperature=0)\n    code_execution_tool = FunctionTool.from_defaults(fn=run_code)\n\n    agent_worker = FunctionCallingAgentWorker.from_tools(\n        [code_execution_tool],\n        llm=llm,\n        verbose=True,\n        allow_parallel_tool_calls=False,\n    )\n    agent = agent_worker.as_agent()\n\n    response = agent.chat(\n        \"Write python code to calculate Pi number by Monte Carlo method then run it.\"\n    )\n    print(response)\n\n    response = agent.chat(\n        \"Write python code to calculate the factorial of a number then run it.\"\n    )\n    print(response)\n\n    response = agent.chat(\n        \"Write python code to calculate the Fibonacci sequence then run it.\"\n    )\n    print(response)\n\n    response = agent.chat(\"Calculate the sum of the first 10000 numbers.\")\n    print(response)\n```\n\n### Contributing\n\nWe welcome contributions to improve sysbox! Since I am a Python developer, I am not familiar with other languages. If you are interested in adding better support for other languages, please feel free to submit a pull request.\n\nHere is a list of things you can do to contribute:\n- [ ] Add Java maven support.\n- [x] Add support for JavaScript.\n- [x] Add support for C++.\n- [x] Add support for Go.\n- [ ] Add support for Ruby.\n- [x] Add remote Docker host support.\n- [x] Add remote Kubernetes cluster support.\n- [x] Langchain integration.\n- [x] LlamaIndex integration.\n- [ ] Commit the last container state to the image before closing kubernetes session.\n\n### License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Lightweight and portable sandbox runtime (code interpreter) Python library",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/vndee/sysbox"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "51ea641216921cb41a021930766537cfd7f55ff1c0b8d0a5e5ab195f86403f3c",
                "md5": "ba5b49cb1b0296759c6544ec34d8a6db",
                "sha256": "39189909a68351d29bf35e5f74cb943b3abe9ee525891a25121f4321a71dd61f"
            },
            "downloads": -1,
            "filename": "sysbox-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ba5b49cb1b0296759c6544ec34d8a6db",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 14087,
            "upload_time": "2024-09-01T20:33:03",
            "upload_time_iso_8601": "2024-09-01T20:33:03.788661Z",
            "url": "https://files.pythonhosted.org/packages/51/ea/641216921cb41a021930766537cfd7f55ff1c0b8d0a5e5ab195f86403f3c/sysbox-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "182c5a3ca97c69af829e791d04b3690710802a7ed236bdb9944584eb2e731137",
                "md5": "a2beb06e3ba7e9569eee9fcc47356d76",
                "sha256": "1787146ab944b1bc2ceb176ff68e3e3b5984c9760f59a3f85a80a6e2521129fd"
            },
            "downloads": -1,
            "filename": "sysbox-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a2beb06e3ba7e9569eee9fcc47356d76",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 14254,
            "upload_time": "2024-09-01T20:33:05",
            "upload_time_iso_8601": "2024-09-01T20:33:05.596757Z",
            "url": "https://files.pythonhosted.org/packages/18/2c/5a3ca97c69af829e791d04b3690710802a7ed236bdb9944584eb2e731137/sysbox-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-01 20:33:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vndee",
    "github_project": "sysbox",
    "github_not_found": true,
    "lcname": "sysbox"
}
        
Elapsed time: 9.70943s