E2B is a secure, sandboxed environment where you can run arbitrary code.
To build this integration, you just need to replace Open Interpreter’s python
(which runs locally) with a python
that runs on E2B.
First, get an API key here, and set it:
import os
os.environ["E2B_API_KEY"] = "<your_api_key_here>"
Then, define a custom language for Open Interpreter. The class name doesn’t matter, but we’ll call it PythonE2B
:
import e2b
class PythonE2B:
"""
This class contains all requirements for being a custom language in Open Interpreter:
- name (an attribute)
- run (a method)
- stop (a method)
- terminate (a method)
Here, we'll use E2B to power the `run` method.
"""
name = "python"
system_message = "# Follow this rule: Every Python code block MUST contain at least one print statement."
def run(self, code):
"""Generator that yields a dictionary in LMC Format."""
stdout, stderr = e2b.run_code('Python3', code)
yield {
"type": "console", "format": "output",
"content": stdout + stderr
}
def stop(self):
"""Stops the code."""
pass
def terminate(self):
"""Terminates the entire process."""
pass
interpreter.computer.terminate()
interpreter.computer.languages = [PythonE2B]
interpreter.chat("What's 349808*38490739?")