1. Concept

Interpreters are tools that empower agents to execute given code snippets on the local machine or in isolated environments. CAMEL supports several types of interpreters to cater to different needs and security considerations.

1.1. Internal Python Interpreter

The Internal Python Interpreter executes Python code directly within the same process as the agent. This is the simplest interpreter and is suitable for trusted code execution. In its default (safe) mode, it evaluates expressions and executes statements in a controlled environment; print() is not directly available as a side effect, so code should evaluate to a string for output.

Example Usage:

from camel.interpreters import InternalPythonInterpreter

# Initialize the interpreter
interpreter = InternalPythonInterpreter()

# Code to execute (must be an expression that evaluates to a string in safe mode)
python_code = "'Hello from InternalPythonInterpreter!'"

# Execute the code
result_str = interpreter.run(code=python_code, code_type="python")
print(f"Result: {result_str}")

1.2. Subprocess Interpreter

The Subprocess Interpreter runs code in a separate subprocess. This provides a degree of isolation from the main agent process. It can execute various shell commands and scripts in addition to Python code.

Example Usage:

from camel.interpreters import SubprocessInterpreter

# Initialize the interpreter
interpreter = SubprocessInterpreter()

# Command to execute (e.g., bash)
shell_command = "echo 'Hello from SubprocessInterpreter!'"

# Execute the command
result_str = interpreter.run(code=shell_command, code_type="bash")
print(f"Result: {result_str.strip()}")

1.3. Docker Interpreter

The Docker Interpreter executes code snippets within a Docker container. This offers a high level of isolation, ensuring that code runs in a controlled and sandboxed environment. This is particularly useful for running untrusted code or code with specific dependencies.

To utilize this interpreter, you need to have Docker installed on your system. For more information on Docker installation, visit Docker’s official website.

Example Usage:

from camel.interpreters import DockerInterpreter

# Initialize the interpreter (assumes a basic python image is available)
interpreter = DockerInterpreter()

# Code to execute in Docker
python_code_in_docker = "print('Hello from DockerInterpreter!')"

# Execute the code
result_str = interpreter.run(code=python_code_in_docker, code_type="python")
print(f"Result: {result_str.strip()}")

1.4. IPython Interpreter

The IPython Interpreter (JupyterKernelInterpreter) leverages an IPython kernel to execute code. This allows for a rich, interactive execution environment, similar to using a Jupyter notebook. It supports features like state persistence across code executions within a session.

Example Usage:

from camel.interpreters import JupyterKernelInterpreter

interpreter = JupyterKernelInterpreter(
    require_confirm=False, print_stdout=True, print_stderr=True
)


python_code_in_jupyter_kernel = "print('Hello from JupyterKernelInterpreter!')"
result = interpreter.run(code=python_code_in_jupyter_kernel, code_type="python")
print(result)

1.5. E2B Interpreter

The E2B Interpreter utilizes the E2B cloud-based sandboxed environments for code execution. This provides a secure and managed environment for running code without requiring local setup of complex dependencies or risking local system integrity.

For more information, visit the E2B official documentation.

Example Usage:

from camel.interpreters import E2BInterpreter

interpreter = E2BInterpreter()


python_code_in_e2b = "print('Hello from E2BInterpreter!')"
result = interpreter.run(code=python_code_in_e2b, code_type="python")
print(result)