You can add or edit the programming languages that Open Interpreter’s computer runs.
In this example, we’ll swap out the python
language for a version of python
that runs in the cloud. We’ll use E2B
to do this.
(E2B
is a secure, sandboxed environment where you can run arbitrary code.)
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)
You can use this class to run any language you know how to run, or edit any of the official languages (which also conform to this class).
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?")