Closed jakubno closed 7 months ago
Latest commit: 1491f5a12b8f036f5e0cf21d6e1eebc523ccb2be
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
User feedback we've received so far:
exec_python
to exec_jupyter
and support other languages like Bash, Julia, or RI did a basic benchmarking of the new Code Interpreter 2.0 SDK and compared it to our production SDK. All in JS.
It's not completely fair comparison because there's no execPython
in the production SDK but it should give us a sense of direction.
Code
import { CodeInterpreterV2 } from 'e2b'
const iterations = 100;
let createSandboxTime = 0;
let execPythonXEquals1Time = 0;
let execPythonXPlusEquals1Time = 0;
let sandboxCloseTime = 0;
for (let i = 0; i < iterations; i++) {
console.log('Iteration:', i + 1)
let startTime = performance.now();
const sandbox = await CodeInterpreterV2.create();
createSandboxTime += performance.now() - startTime;
startTime = performance.now();
await sandbox.execPython('x = 1');
execPythonXEquals1Time += performance.now() - startTime;
startTime = performance.now();
const result = await sandbox.execPython('x+=1; x');
execPythonXPlusEquals1Time += performance.now() - startTime;
startTime = performance.now();
await sandbox.close();
sandboxCloseTime += performance.now() - startTime;
}
console.log(`Average Create Sandbox Time: ${createSandboxTime / iterations}ms`);
console.log(`Average Execute Python x = 1 Time: ${execPythonXEquals1Time / iterations}ms`);
console.log(`Average Execute Python x+=1; x Time: ${execPythonXPlusEquals1Time / iterations}ms`);
console.log(`Average Sandbox Close Time: ${sandboxCloseTime / iterations}ms`);
Results
Average Create Sandbox Time: 2830.4465337133406ms
Average Execute Python x = 1 Time: 583.8239275050163ms
Average Execute Python x+=1; x Time: 277.89283413648604ms
Average Sandbox Close Time: 0.19184373140335084ms
Code
import * as e2b from 'e2b'
const iterations = 100;
let createSandboxTime = 0;
let execPythonXEquals1Time = 0;
let execPythonXPlusEquals1Time = 0;
let sandboxCloseTime = 0;
for (let i = 0; i < iterations; i++) {
console.log('Iteration:', i + 1)
let startTime = performance.now();
const sandbox = await e2b.Sandbox.create();
createSandboxTime += performance.now() - startTime;
startTime = performance.now();
await sandbox.process.startAndWait('python3 -c "x = 1"');
execPythonXEquals1Time += performance.now() - startTime;
startTime = performance.now();
await sandbox.process.startAndWait('python3 -c "x+=1; print(x)"');
execPythonXPlusEquals1Time += performance.now() - startTime;
startTime = performance.now();
await sandbox.close();
sandboxCloseTime += performance.now() - startTime;
}
console.log(`Average Sandbox Creation Time: ${createSandboxTime / iterations}ms`);
console.log(`Average Process Start and Wait (x=1) Time: ${execPythonXEquals1Time / iterations}ms`);
console.log(`Average Process Start and Wait (x+=1; print(x)) Time: ${execPythonXPlusEquals1Time / iterations}ms`);
console.log(`Average Sandbox Close Time: ${sandboxCloseTime / iterations}ms`);
Results
Average Sandbox Creation Time: 1773.701356651783ms
Average Process Start and Wait (x=1) Time: 171.03909373521805ms
Average Process Start and Wait (x+=1; print(x)) Time: 134.90086081504822ms
Average Sandbox Close Time: 0.16264041662216186ms
Sandbox creation time: ~1.1s worse
Run a process: ~300ms worse
Sandbox close time: virtually same
Thanks for the great work, it seems to be working for the most part.
I have a quick bug that might come up in the future. I'm using JS SDK.
When I create the sandbox with no initial cwd
, it should default to /home/user
source. But when I try and access sandboxv2.cwd
it returns undefined. Is this intended behaviour?
Hey @im-calvin thank you for catching this.
We're moving the code interpreter SDK to a separate repository. We'll publish it as a separate package. The rest of the work and all the fixes will be there.
@lawrencecchen you can start using the new SDK https://github.com/e2b-dev/code-interpreter
JavaScript
npm install @e2b/code-interpreter
Python
pip install e2b-code-interpreter
Stateful code interpreter
The current version of the code interpreter SDK allows to run Python code but each run has its own separate context. That means that subsequent runs can't reference to variables, definitions, etc from past code execution runs.
This is suboptimal for a lot of Python use cases with LLMs. Especially GPT-3.5 and 4 expects it runs in a Jupyter Notebook environment. Even when ones tries to convince it otherwise. In practice, LLMs will generate code blocks which have references to previous code blocks. This becomes an issue if a user wants to execute each code block separately which often is the use case.
This new code interpreter template runs a Jupyter server inside the sandbox, which allows for sharing context between code executions. Additionally, this new template also partly implements the Jupyter Kernel messaging protocol. This means that, for example, support for plotting charts is now improved and we don't need to do hack-ish solutions like in the current production version of our code interpreter.
Current state
Known limited in features such as:
We'll be updating this PR with new releases as we gather more user feedback.
Installation
Install the experimental release candidate
Python
JavaScript
Examples
Minimal example with the sharing context:
Python
JavaScript
Get charts and any display-able data
Python
JavaScript
Streaming code output
Python
JavaScript
Pre-installed Python packages inside the sandbox
The full and always up-to-date list can be found in the
requirements.txt
file.Custom template using Code Interpreter
If you're using a custom sandbox template, you currently need to go through a bit of a manual setup:
jupyter_server_config.py
andstart-up.sh
from this PRcurl
andjq
in your Dockerfile or just add-c "/home/user/.jupyter/start-up.sh"
toe2b template build
command or add this line to youre2b.toml
.