Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,472 Bytes
886d8e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
---
title: Custom Languages
---
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`](https://e2b.dev/) is a secure, sandboxed environment where you can run arbitrary code.)
First, [get an API key here](https://e2b.dev/), and set it:
```python
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`:
```python
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.
"""
# This is the name that will appear to the LLM.
name = "python"
# Optionally, you can append some information about this language to the system message:
system_message = "# Follow this rule: Every Python code block MUST contain at least one print statement."
# (E2B isn't a Jupyter Notebook, so we added ^ this so it would print things,
# instead of putting variables at the end of code blocks, which is a Jupyter thing.)
def run(self, code):
"""Generator that yields a dictionary in LMC Format."""
# Run the code on E2B
stdout, stderr = e2b.run_code('Python3', code)
# Yield the output
yield {
"type": "console", "format": "output",
"content": stdout + stderr # We combined these arbitrarily. Yield anything you'd like!
}
def stop(self):
"""Stops the code."""
# Not needed here, because e2b.run_code isn't stateful.
pass
def terminate(self):
"""Terminates the entire process."""
# Not needed here, because e2b.run_code isn't stateful.
pass
# (Tip: Do this before adding/removing languages, otherwise OI might retain the state of previous languages:)
interpreter.computer.terminate()
# Give Open Interpreter its languages. This will only let it run PythonE2B:
interpreter.computer.languages = [PythonE2B]
# Try it out!
interpreter.chat("What's 349808*38490739?")
``` |