### Install Project Dependencies Source: https://github.com/vndee/llm-sandbox/blob/main/CRUSH.md Use 'make install' for a simplified installation or 'uv sync && uv run pre-commit install' for detailed dependency management and pre-commit hook setup. ```bash make install ``` ```bash uv sync && uv run pre-commit install ``` -------------------------------- ### Library Installation Behavior with Skip Setup Source: https://github.com/vndee/llm-sandbox/blob/main/docs/configuration.md Demonstrates that dynamic library installation is disabled when `skip_environment_setup` is true. Use `execute_command` for manual installation if needed. ```python # This will fail with skip_environment_setup=True result = session.run( "import requests; print('OK')", libraries=["requests"] # ❌ Will raise LibraryInstallationNotSupportedError ) # Instead, use execute_command for manual installation if needed session.execute_command("pip install requests") result = session.run("import requests; print('OK')") # ✅ Works ``` -------------------------------- ### Install C++ Libraries with Boost Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Shows how to install system libraries, specifically Boost, for C++ development within the sandbox. This example demonstrates using Boost's `to_upper` algorithm. ```python # Install system libraries with SandboxSession(lang="cpp") as session: # Install Boost session.run(""" #include #include int main() { std::string text = "Hello, World!"; boost::to_upper(text); std::cout << text << std::endl; return 0; } ", libraries=["libboost-all-dev"]) ``` -------------------------------- ### Python: Basic Usage with Library Installation Source: https://context7.com/vndee/llm-sandbox/llms.txt Use SandboxSession for one-shot code execution. It handles container setup and language-specific environment configuration. Libraries can be installed on-the-fly. ```python from llm_sandbox import SandboxSession, SandboxBackend, SupportedLanguage from llm_sandbox.exceptions import SandboxTimeoutError, SecurityViolationError # --- Python: basic usage with library installation --- with SandboxSession(lang="python", verbose=True) as session: result = session.run( "import numpy as np\nprint(np.linspace(0, 1, 5))", libraries=["numpy"], ) print(result.stdout) # [0. 0.25 0.5 0.75 1. ] print(result.exit_code) # 0 assert result.success() ``` -------------------------------- ### Example Dockerfile with Pre-installed Python Libraries Source: https://github.com/vndee/llm-sandbox/blob/main/docs/custom-images.md A sample Dockerfile demonstrating how to install system dependencies and pre-install common Python packages like numpy, pandas, and matplotlib using pip. ```dockerfile FROM python:3.11-slim # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ && rm -rf /var/lib/apt/lists/* # Pre-install Python packages RUN pip install \ numpy \ pandas \ matplotlib \ scikit-learn \ requests \ fastapi # Create working directory WORKDIR /sandbox # Optional: Create directories for output RUN mkdir -p /tmp/sandbox_output /tmp/sandbox_plots ``` -------------------------------- ### install() and execute_command() Source: https://context7.com/vndee/llm-sandbox/llms.txt The `install()` method installs libraries into the sandbox, while `execute_command()` runs arbitrary shell commands. Both require an open session. ```APIDOC ## install() ### Description Installs libraries into the sandbox using the language-specific package manager (pip for Python, npm for JavaScript, go get for Go). It is also called implicitly by `run(libraries=[...])`. ### Parameters - **libraries** (list[str]) - Required - A list of library names to install. ## execute_command() ### Description Runs an arbitrary shell command in the container and returns a `ConsoleOutput`. Requires an open session. ### Parameters - **command** (str) - Required - The shell command to execute. ### Returns - **ConsoleOutput** - An object containing stdout, stderr, and exit_code. ## execute_commands() ### Description Runs a sequence of shell commands in the container. The sequence stops on the first failure. ### Parameters - **commands** (list[tuple[str, str] | str]) - Required - A list of commands to execute. Each command can be a string or a tuple of (command, working_directory). ### Returns - **ConsoleOutput** - An object containing stdout, stderr, and exit_code of the last executed command. ### Example ```python from llm_sandbox import SandboxSession with SandboxSession(lang="python", verbose=True) as session: # Explicit library installation session.install(["pandas", "scikit-learn"]) # Arbitrary shell commands env_result = session.execute_command("python --version") print(env_result.stdout) # Python 3.11.x disk_result = session.execute_command("df -h /sandbox") print(disk_result.stdout) # Multi-command sequence (stops on first failure) output = session.execute_commands([ "mkdir -p /sandbox/results", ("echo 'done' > /sandbox/results/status.txt", "/sandbox"), "cat /sandbox/results/status.txt", ]) print(output.stdout) # done ``` ``` -------------------------------- ### Troubleshoot Podman Rootless Setup and Socket Issues Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Provides commands for migrating Podman storage for rootless mode and starting the user's Podman socket service when it's not found. ```bash # Rootless setup podman system migrate podman unshare cat /etc/subuid # Socket not found systemctl --user start podman.socket ``` -------------------------------- ### Example Dockerfile Source: https://github.com/vndee/llm-sandbox/blob/main/docs/configuration.md An example Dockerfile demonstrating how to set up a Python environment with system packages and Python libraries. ```dockerfile FROM python:3.11-slim # Install additional system packages RUN apt-get update && apt-get install -y \ build-essential \ && rm -rf /var/lib/apt/lists/* # Pre-install Python packages RUN pip install numpy pandas matplotlib WORKDIR /sandbox ``` -------------------------------- ### Install Docker and LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Installs Docker using a script and then installs the LLM Sandbox with Docker support via pip. ```bash curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh pip install 'llm-sandbox[docker]' ``` -------------------------------- ### Install Dependencies Source: https://github.com/vndee/llm-sandbox/blob/main/CONTRIBUTING.md Install project dependencies and pre-commit hooks using the provided make command. ```bash make install ``` -------------------------------- ### Install LLM Sandbox with Docker Support Source: https://context7.com/vndee/llm-sandbox/llms.txt Install the library with minimal dependencies, or include support for specific container backends like Docker, Kubernetes, or Podman. All container backends can be installed together. ```bash # Minimal install pip install llm-sandbox # With Docker support (most common) pip install 'llm-sandbox[docker]' # With Kubernetes support pip install 'llm-sandbox[k8s]' # With Podman support pip install 'llm-sandbox[podman]' # All container backends pip install 'llm-sandbox[docker,k8s,podman]' # MCP server with Docker pip install 'llm-sandbox[mcp-docker]' ``` -------------------------------- ### Install Kubernetes Tools and LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Instructions for installing `kubectl` and the LLM Sandbox package with Kubernetes support using pip. ```bash # Install kubectl curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl # Install LLM Sandbox with Kubernetes support pip install 'llm-sandbox[k8s]' ``` -------------------------------- ### Installing Libraries with %pip Source: https://github.com/vndee/llm-sandbox/blob/main/docs/interactive-sessions.md Shows how to install external Python libraries within an interactive session using the `%pip install` magic command, followed by their usage. ```python from llm_sandbox import InteractiveSandboxSession with InteractiveSandboxSession(lang="python") as session: # Install libraries in the first run result = session.run("%pip install matplotlib numpy") result = session.run(""" import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title("Sine Wave") print("Plot created") """ ) print(result.stdout) ``` -------------------------------- ### JavaScript: Express.js Server Setup Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Sets up a basic Express.js web server that listens for GET requests on the root path and responds with a JSON message. Includes a note that the server is not externally accessible. ```javascript const express = require('express'); const app = express(); app.get('/', (req, res) => { res.json({ message: 'Hello from Express!' }); }); // Note: Server won't actually be accessible from outside the container const PORT = 3000; app.listen(PORT, () => { console.log(`Server would run on port ${PORT}`); console.log('(Not accessible from outside the sandbox)'); // Gracefully exit after setup process.exit(0); }); ``` -------------------------------- ### Verify Installation Source: https://github.com/vndee/llm-sandbox/blob/main/CONTRIBUTING.md Run the test suite to verify that the development environment is set up correctly. ```bash make test ``` -------------------------------- ### Google-Style Docstring Example Source: https://github.com/vndee/llm-sandbox/blob/main/CONTRIBUTING.md Demonstrates the Google-style docstring format including Args, Returns, Raises, and Example sections. ```python def execute_code(code: str, language: str = "python") -> ConsoleOutput: """Execute code in a sandboxed environment. This function creates an isolated container environment and executes the provided code safely. Args: code: The source code to execute language: Programming language identifier Returns: ConsoleOutput containing stdout, stderr, and exit code Raises: SecurityError: If code violates security policy NotOpenSessionError: If session is not initialized Example: >>> result = execute_code("print('Hello')") >>> print(result.stdout) Hello """ ``` -------------------------------- ### Install Podman and LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Commands to install Podman on Ubuntu/Debian systems and then install the LLM Sandbox package with Podman support using pip. ```bash sudo apt-get update sudo apt-get install -y podman pip install 'llm-sandbox[podman]' ``` -------------------------------- ### Generic Integration Pattern with Async Example Source: https://github.com/vndee/llm-sandbox/blob/main/docs/integrations.md Illustrates a generic integration pattern for the LLM Sandbox, referencing an asynchronous example file. ```python --8<-- "examples/async_example.py" ``` -------------------------------- ### Multi-stage Docker Build for Smaller Images Source: https://github.com/vndee/llm-sandbox/blob/main/docs/custom-images.md An example of a multi-stage Dockerfile. It uses a 'builder' stage to install dependencies and then copies only the necessary artifacts to a slim runtime stage, resulting in a smaller final image. ```dockerfile # Build stage FROM python:3.11 as builder COPY requirements.txt . RUN pip install --user -r requirements.txt # Runtime stage FROM python:3.11-slim COPY --from=builder /root/.local /root/.local ENV PATH=/root/.local/bin:$PATH WORKDIR /sandbox ``` -------------------------------- ### Multi-Language Support Example Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Example of a user prompt requesting code execution for a JavaScript sorting function. ```text "Write a JavaScript function to sort an array of numbers and demonstrate it" ``` -------------------------------- ### Conventional Commit Examples Source: https://github.com/vndee/llm-sandbox/blob/main/docs/contributing.md Examples demonstrating the correct format for commit messages using the Conventional Commits specification. ```bash # Good git commit -m "feat: add support for Ruby language handler" git commit -m "fix: resolve memory leak in Docker session cleanup" git commit -m "docs: update security policy examples" git commit -m "test: add unit tests for Kubernetes backend" git commit -m "refactor: simplify session factory logic" # With scope git commit -m "feat(security): add new pattern detection for SQL injection" git commit -m "fix(docker): handle container cleanup on session timeout" # Breaking changes git commit -m "feat!: remove deprecated session.execute() method" git commit -m "feat(api)!: change SecurityPolicy constructor signature" # Bad git commit -m "fixed stuff" git commit -m "updates" ``` -------------------------------- ### Skip Environment Setup Source: https://github.com/vndee/llm-sandbox/blob/main/docs/configuration.md Bypass automatic language-specific environment setup for faster container startup. Recommended for production or custom images. ```python from llm_sandbox import SandboxSession, SandboxBackend # Skip environment setup for faster container startup with SandboxSession( lang="python", skip_environment_setup=True, # Skip pip upgrades and venv creation verbose=True ) as session: result = session.run("print('Hello from pre-configured environment!')") ``` -------------------------------- ### Hybrid Library Installation in Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/custom-images.md Demonstrates how to use both pre-installed libraries (pandas, numpy) and libraries installed at runtime (requests) within a single SandboxSession. Only the runtime-installed libraries need to be specified in the 'libraries' parameter. ```python # Pre-installed: pandas, numpy # Runtime-installed: requests result = session.run(""" import pandas as pd # Pre-installed import numpy as np # Pre-installed import requests # Will be installed at runtime data = pd.DataFrame({'x': np.random.randn(10)}) response = requests.get('https://api.github.com') print(f"Data shape: {data.shape}, API status: {response.status_code}") """, libraries=["requests"]) # Only need to specify requests ``` -------------------------------- ### Install Libraries with SandboxSession Source: https://github.com/vndee/llm-sandbox/blob/main/CLAUDE.md Libraries can be automatically installed when specified in the `libraries` argument of the `session.run()` method. This is useful for ad-hoc dependency management. ```python # Libraries installed automatically when specified session.run(code, libraries=["numpy", "pandas"]) ``` -------------------------------- ### Install and Use Python Libraries in Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Demonstrates how to install and utilize Python packages like NumPy dynamically within a sandbox session by specifying them in the `libraries` argument. ```python from llm_sandbox import SandboxSession with SandboxSession(lang="python") as session: # Run code with numpy result = session.run(""" import numpy as np # Create an array arr = np.array([1, 2, 3, 4, 5]) print(f"Array: {arr}") print(f"Mean: {np.mean(arr)}") print(f"Sum: {np.sum(arr)}") """, libraries=["numpy"]) print(result.stdout) ``` -------------------------------- ### Language-Specific Image Optimization Examples Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Provides examples of using optimized Docker images for different languages, such as slim images for Python and JRE for Java, and mentions multi-stage builds for Go. ```dockerfile # Python: Use slim images image="python:3.11-slim" ``` ```dockerfile # Java: Use JDK vs JRE based on needs image="openjdk:11-jre-slim" # For running only ``` ```dockerfile # Go: Use multi-stage builds # Build in one stage, run in minimal image ``` -------------------------------- ### Configure Podman Client for Rootless Mode Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Example of initializing the PodmanClient to use the rootless socket, ensuring it runs as a regular user. ```python # Run as regular user client = PodmanClient( base_url=f"unix:///run/user/{os.getuid()}/podman/podman.sock" ) ``` -------------------------------- ### Basic Code Execution Example Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Example of a user prompt requesting basic code execution for a Python factorial function. ```text "Write a Python function to calculate the factorial of a number and test it with n=5" ``` -------------------------------- ### Skip Environment Setup for Production Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Set `skip_environment_setup=True` when using pre-configured images or in production to bypass automatic environment setup and accelerate container startup. ```python from llm_sandbox import SandboxSession # Skip environment setup when using custom images with SandboxSession( lang="python", image="my-registry.com/python-ml:latest", # Pre-configured image skip_environment_setup=True # Skip pip upgrades and venv creation ) as session: result = session.run("import numpy; print('Ready!')") ``` -------------------------------- ### Check Podman Installation Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Verify if Podman is installed and accessible by running `podman --version` in your terminal. ```bash podman --version ``` -------------------------------- ### Install LLM Sandbox with MCP Support (Podman) Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Install the LLM Sandbox package with MCP support for the Podman backend using pip. ```bash pip install 'llm-sandbox[mcp-podman]' ``` -------------------------------- ### Clone Repository and Install LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/README.md Clone the LLM Sandbox repository and install it in development mode. Run pre-commit hooks and tests to ensure the development environment is set up correctly. ```bash git clone https://github.com/vndee/llm-sandbox.git cd llm-sandbox make install uv run pre-commit run -a make test ``` -------------------------------- ### Install LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/index.md Install the llm-sandbox package using pip. For specific backend support, use the optional extras. ```bash pip install llm-sandbox ``` ```bash # For Docker support pip install 'llm-sandbox[docker]' # For Kubernetes support pip install 'llm-sandbox[k8s]' # For Podman support pip install 'llm-sandbox[podman]' ``` -------------------------------- ### Install LLM Sandbox with MCP Support (Kubernetes) Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Install the LLM Sandbox package with MCP support for the Kubernetes backend using pip. ```bash pip install 'llm-sandbox[mcp-k8s]' ``` -------------------------------- ### Install LLM Sandbox with Podman Backend Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs LLM Sandbox with support for the Podman backend. This option is for users who prefer Podman as their container runtime. ```bash pip install 'llm-sandbox[podman]' ``` -------------------------------- ### Install LLM Sandbox with MCP Support (Bash) Source: https://github.com/vndee/llm-sandbox/blob/main/README.md Commands to install LLM Sandbox with Model Context Protocol (MCP) support for different backends (Docker, Podman, Kubernetes). ```bash # For Docker backend pip install 'llm-sandbox[mcp-docker]' # For Podman backend pip install 'llm-sandbox[mcp-podman]' # For Kubernetes backend pip install 'llm-sandbox[mcp-k8s]' ``` -------------------------------- ### Install LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/README.md Install the llm-sandbox package using pip. For specific backend support, append the desired extra, e.g., '[docker]'. ```bash pip install llm-sandbox ``` ```bash # For Docker support (most common) pip install 'llm-sandbox[docker]' # For Kubernetes support pip install 'llm-sandbox[k8s]' # For Podman support pip install 'llm-sandbox[podman]' # All backends pip install 'llm-sandbox[docker,k8s,podman]' ``` ```bash git clone https://github.com/vndee/llm-sandbox.git cd llm-sandbox pip install -e '.[dev]' ``` -------------------------------- ### Install LLM Sandbox with MCP Support (Docker) Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Install the LLM Sandbox package with MCP support for the Docker backend using pip. ```bash pip install 'llm-sandbox[mcp-docker]' ``` -------------------------------- ### Pre-install Libraries in Pooled Containers Source: https://github.com/vndee/llm-sandbox/blob/main/docs/container-pooling.md Install specified libraries in all pooled containers by passing the `libraries` parameter when creating the pool manager. ```python pool = create_pool_manager( backend="docker", config=PoolConfig(max_pool_size=10), lang="python", libraries=["requests", "numpy", "pandas"], # Installed in all containers ) ``` -------------------------------- ### Production Deployment with Podman Source: https://github.com/vndee/llm-sandbox/blob/main/docs/configuration.md Utilize Podman for rootless containers, skipping environment setup with a custom image for secure and efficient deployments. ```python with SandboxSession( lang="python", backend=SandboxBackend.PODMAN, skip_environment_setup=True, image="my-registry.com/python-secure:latest" ) as session: result = session.run("print('Secure environment ready!')") ``` -------------------------------- ### Install LLM Sandbox with All Backends Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs LLM Sandbox with support for Docker, Kubernetes, and Podman backends, providing flexibility across different container environments. ```bash pip install 'llm-sandbox[docker,k8s,podman]' ``` -------------------------------- ### Install LLM Sandbox with Docker Backend Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs LLM Sandbox with support for the Docker backend, commonly used for containerized execution. Requires a container runtime like Docker. ```bash pip install 'llm-sandbox[docker]' ``` -------------------------------- ### MCP Server Configuration (JSON) Source: https://github.com/vndee/llm-sandbox/blob/main/README.md Example JSON configuration for an MCP client, specifying the LLM Sandbox server command and arguments. ```json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], } } } ``` -------------------------------- ### Build and Preview Documentation Source: https://github.com/vndee/llm-sandbox/blob/main/CONTRIBUTING.md Build the project documentation and preview it locally. ```bash make docs # Visit http://localhost:8000 ``` -------------------------------- ### Caching Pip Dependencies in Dockerfile Source: https://github.com/vndee/llm-sandbox/blob/main/docs/custom-images.md Configures the pip cache directory within a Dockerfile to speed up package installations during image builds. This example sets the PIP_CACHE_DIR environment variable and uses it with pip install. ```dockerfile ENV PIP_CACHE_DIR=/tmp/pip_cache RUN mkdir -p /tmp/pip_cache RUN pip install --cache-dir /tmp/pip_cache numpy pandas ``` -------------------------------- ### Basic Unit Test Structure Source: https://github.com/vndee/llm-sandbox/blob/main/docs/contributing.md Example of a basic unit test using pytest, including setup, action, and assertion. ```python # tests/test_your_feature.py import pytest from llm_sandbox import YourFeature def test_your_feature(): """Test description""" # Arrange feature = YourFeature() # Act result = feature.do_something() # Assert assert result == expected_value ``` -------------------------------- ### Build Project Source: https://github.com/vndee/llm-sandbox/blob/main/CRUSH.md Build the project using 'make build' for a quick build or 'uvx --from build pyproject-build --installer uv' for a more explicit build process with uv. ```bash make build ``` ```bash uvx --from build pyproject-build --installer uv ``` -------------------------------- ### Running Code on Docker Backend Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Example of executing Python code using the default Docker backend. Ensure Docker is installed and running. ```python from llm_sandbox import SandboxSession, SandboxBackend with SandboxSession( backend=SandboxBackend.DOCKER, lang="python" ) as session: result = session.run("print('Running on Docker!')") print(result.stdout) ``` -------------------------------- ### Running Code on Podman Backend Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Example of executing Python code using the Podman backend. Requires Podman to be installed and accessible via the specified socket. ```python from podman import PodmanClient client = PodmanClient(base_url="unix:///run/podman/podman.sock") with SandboxSession( backend=SandboxBackend.PODMAN, client=client, lang="python" ) as session: result = session.run("print('Running on Podman!')") print(result.stdout) ``` -------------------------------- ### Multi-Stage Docker Builds Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Utilize multi-stage builds in Dockerfiles to create smaller, more efficient custom images. This example installs dependencies in a builder stage and copies them to the final image. ```dockerfile # Dockerfile FROM python:3.11-slim as builder RUN pip install --user numpy pandas FROM python:3.11-slim COPY --from=builder /root/.local /root/.local ``` -------------------------------- ### MCP Server Configuration for Docker Source: https://context7.com/vndee/llm-sandbox/llms.txt Example JSON configuration for the MCP server using Docker as the backend. This setup hardens security by dropping all capabilities and setting specific resource limits. ```json { "mcpServers": { "llm-sandbox": { "command": "python3", "args": ["-m", "llm_sandbox.mcp_server.server"], "env": { "BACKEND": "docker", "DOCKER_HOST": "unix:///var/run/docker.sock", "SANDBOX_NETWORK_MODE": "none", "SANDBOX_READ_ONLY": "true", "SANDBOX_CAP_DROP": "ALL", "SANDBOX_SECURITY_OPT": "no-new-privileges:true", "SANDBOX_MEMORY": "512m", "SANDBOX_CPU_COUNT": "1", "KEEP_TEMPLATE": "true", "COMMIT_CONTAINER": "false" } } } } ``` -------------------------------- ### JavaScript: Execute Node.js Code with npm Packages Source: https://context7.com/vndee/llm-sandbox/llms.txt Run JavaScript code, including installing and using npm packages like lodash. The sandbox handles the Node.js environment setup. ```javascript # --- JavaScript with npm packages --- with SandboxSession(lang="javascript") as session: result = session.run( "const _ = require('lodash'); console.log(_.sum([1,2,3,4]));", libraries=["lodash"], ) print(result.stdout) # 10 ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/vndee/llm-sandbox/blob/main/CONTRIBUTING.md Clone the LLM Sandbox repository and navigate into the project directory. ```bash git clone https://github.com/YOUR_USERNAME/llm-sandbox.git cd llm-sandbox ``` -------------------------------- ### Run Basic Go Program in Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Demonstrates basic execution of a Go program within the sandbox, printing version information and a simple sum calculation. ```python with SandboxSession(lang="go") as session: result = session.run(""" package main import ( "fmt" "runtime" ) func main() { fmt.Printf("Go version: %s\n", runtime.Version()) fmt.Println("Hello from Go!") // Go features numbers := []int{1, 2, 3, 4, 5} sum := 0 for _, n := range numbers { sum += n } fmt.Printf("Sum: %d\n", sum) } """) print(result.stdout) ``` -------------------------------- ### Run Go Concurrency Example in Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Shows how to execute a Go program that utilizes concurrency with goroutines and wait groups to manage multiple worker tasks. ```python with SandboxSession(lang="go") as session: result = session.run(""" package main import ( "fmt" "sync" "time" ) func worker(id int, wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("Worker %d starting\n", id) time.Sleep(time.Millisecond * 100) fmt.Printf("Worker %d done\n", id) } func main() { var wg sync.WaitGroup for i := 1; i <= 5; i++ { wg.Add(1) go worker(i, &wg) } wg.Wait() fmt.Println("All workers completed") } """) ``` -------------------------------- ### Development Installation of LLM Sandbox Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs LLM Sandbox in editable mode for development purposes. This requires cloning the repository and installing development dependencies. ```bash git clone https://github.com/vndee/llm-sandbox.git cd llm-sandbox pip install -e '.[dev]' ``` -------------------------------- ### Complete Example for Existing Container Demo Source: https://github.com/vndee/llm-sandbox/blob/main/docs/existing-container-support.md This snippet is a placeholder for a complete demonstration script located at 'examples/existing_container_demo.py'. It likely showcases various aspects of using existing containers. ```python --8<-- "examples/existing_container_demo.py" ``` -------------------------------- ### Install LLM Sandbox Core Package Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs the fundamental LLM Sandbox package using pip. Ensure Python 3.10 or higher is installed. ```bash pip install llm-sandbox ``` -------------------------------- ### Check Kubernetes Installation Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Verify if Kubernetes is installed and accessible by running `kubectl version` in your terminal. ```bash kubectl version ``` -------------------------------- ### Check Docker Installation Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Verify if Docker is installed and accessible by running `docker --version` in your terminal. ```bash docker --version ``` -------------------------------- ### Basic LLM Sandbox Session with Podman Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Demonstrates creating a Podman client and initiating a basic LLM Sandbox session to run a simple Python command. Ensure the Podman socket is accessible. ```python from podman import PodmanClient from llm_sandbox import SandboxSession, SandboxBackend # Create Podman client client = PodmanClient( base_url="unix:///run/user/1000/podman/podman.sock" ) with SandboxSession( backend=SandboxBackend.PODMAN, client=client, lang="python" ) as session: result = session.run("print('Hello from Podman!')") print(result.stdout) ``` -------------------------------- ### Basic Kubernetes Backend Usage Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Example of using the Kubernetes backend with LLM Sandbox. Specify the backend and Kubernetes namespace when creating a SandboxSession. ```python from llm_sandbox import SandboxSession, SandboxBackend with SandboxSession( backend=SandboxBackend.KUBERNETES, lang="python", kube_namespace="default" ) as session: result = session.run("print('Hello from Kubernetes!')") print(result.stdout) ``` -------------------------------- ### Install Python Packages During Execution Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Install and use Python packages like NumPy, Pandas, and Matplotlib directly within a sandbox session by specifying them in the `libraries` argument. Alternatively, use `session.install()` to install packages separately before running code. ```python # Install packages during code execution with SandboxSession(lang="python") as session: result = session.run(""" import numpy as np import pandas as pd import matplotlib.pyplot as plt print(f"NumPy version: {np.__version__}") print(f"Pandas version: {pd.__version__}") """, libraries=["numpy", "pandas", "matplotlib"]) # Install packages separately with SandboxSession(lang="python") as session: session.install(["scikit-learn", "seaborn"]) result = session.run(""" from sklearn import __version__ print(f"Scikit-learn version: {__version__}") """) ``` -------------------------------- ### Basic Docker Session Usage Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Demonstrates creating a sandbox session with the default Docker backend and running a simple Python command. ```python from llm_sandbox import SandboxSession, SandboxBackend # Default Docker backend with SandboxSession(lang="python") as session: result = session.run("print('Hello from Docker!')") print(result.stdout) # Explicit Docker backend with SandboxSession( backend=SandboxBackend.DOCKER, lang="python" ) as session: pass ``` -------------------------------- ### Data Visualization Example Source: https://github.com/vndee/llm-sandbox/blob/main/docs/mcp-integration.md Example of a user prompt requesting code execution for data visualization using matplotlib. ```text "Create a scatter plot showing the relationship between x and y data points using matplotlib" ``` -------------------------------- ### Using Different Backend Support Source: https://github.com/vndee/llm-sandbox/blob/main/docs/interactive-sessions.md Illustrates how to configure InteractiveSandboxSession to use different containerization backends like Docker, Podman, or Kubernetes. ```python from llm_sandbox import InteractiveSandboxSession from llm_sandbox.const import SandboxBackend # Docker (default) with InteractiveSandboxSession(backend=SandboxBackend.DOCKER) as session: session.run("print('Using Docker')") # Podman with InteractiveSandboxSession(backend=SandboxBackend.PODMAN) as session: session.run("print('Using Podman')") # Kubernetes with InteractiveSandboxSession( backend=SandboxBackend.KUBERNETES, kube_namespace="default" ) as session: session.run("print('Using Kubernetes')") ``` -------------------------------- ### Dockerfile for Pre-installed Packages Source: https://github.com/vndee/llm-sandbox/blob/main/docs/languages.md Example Dockerfile to build an image with common Python data science packages pre-installed, speeding up sandbox initialization. ```dockerfile # Build image with pre-installed packages FROM python:3.11 RUN pip install numpy pandas matplotlib scikit-learn ``` -------------------------------- ### Install LLM Sandbox with Kubernetes Backend Source: https://github.com/vndee/llm-sandbox/blob/main/docs/getting-started.md Installs LLM Sandbox with support for the Kubernetes backend. This is suitable for environments with a Kubernetes cluster. ```bash pip install 'llm-sandbox[k8s]' ``` -------------------------------- ### Initialize Interactive Sandbox Session with Settings Source: https://github.com/vndee/llm-sandbox/blob/main/docs/interactive-sessions.md Configure resource limits like memory and timeout, and history size for an interactive session. Adjust these based on your specific workload requirements. ```python from llm_sandbox import InteractiveSandboxSession, InteractiveSettings # Set appropriate limits for your use case settings = InteractiveSettings( max_memory="4GB", # Adjust based on your data size timeout=600, # Longer timeout for complex operations history_size=100 # Keep history manageable ) with InteractiveSandboxSession( lang="python", interactive_settings=settings ) as session: # Your code here pass ``` -------------------------------- ### Troubleshoot Docker Daemon Connection Issues Source: https://github.com/vndee/llm-sandbox/blob/main/docs/backends.md Offers commands to start the Docker service and set the default context, useful when unable to connect to the Docker daemon. ```bash # Cannot connect to daemon sudo systemctl start docker docker context use default ``` -------------------------------- ### Run IPython Magic Commands in Python Source: https://github.com/vndee/llm-sandbox/blob/main/docs/interactive-sessions.md Demonstrates how to use common IPython magic commands like %who, %pwd, and !ls within an interactive Python session. Also shows how to time code execution with %%timeit. ```python from llm_sandbox import InteractiveSandboxSession with InteractiveSandboxSession(lang="python") as session: # List variables result = session.run("%who") print(result.stdout) # Check current directory result = session.run("%pwd") print(result.stdout) # Execute shell commands result = session.run("!ls -la /sandbox") print(result.stdout) # Time code execution result = session.run(""" %%timeit sum(range(1000)) """) print(result.stdout) ``` -------------------------------- ### Install Libraries and Execute Python Code in Sandbox Source: https://context7.com/vndee/llm-sandbox/llms.txt Use `session.install()` to add Python packages like pandas and scikit-learn to the sandbox. Then, use `session.run()` to execute Python code that utilizes these libraries. `session.execute_command()` can run any shell command, such as checking the Python version or disk space. ```python from llm_sandbox import SandboxSession with SandboxSession(lang="python", verbose=True) as session: # Explicit library installation session.install(["pandas", "scikit-learn"]) # Run code that uses the installed libraries result = session.run(""" from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier import numpy as np X, y = load_iris(return_X_y=True) clf = RandomForestClassifier(n_estimators=10, random_state=42) clf.fit(X, y) preds = clf.predict(X[:5]) print("Predictions:", preds) print("Accuracy:", np.mean(clf.predict(X) == y)) """) print(result.stdout) # Predictions: [0 0 0 0 0] # Accuracy: 1.0 # Arbitrary shell commands env_result = session.execute_command("python --version") print(env_result.stdout) # Python 3.11.x disk_result = session.execute_command("df -h /sandbox") print(disk_result.stdout) # Multi-command sequence (stops on first failure) output = session.execute_commands([ "mkdir -p /sandbox/results", ("echo 'done' > /sandbox/results/status.txt", "/sandbox"), "cat /sandbox/results/status.txt", ]) print(output.stdout) # done ``` -------------------------------- ### Check Installed Python Packages Source: https://github.com/vndee/llm-sandbox/blob/main/docs/custom-images.md Use this Python snippet to list all installed packages within your environment, useful for debugging conflicting libraries. ```python # Check what's installed result = session.run(""" import pkg_resources installed = [str(d) for d in pkg_resources.working_set] for package in sorted(installed): print(package) """) ```