Skip to content

Add support for additional language runtimes #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from

Conversation

jakubno
Copy link
Member

@jakubno jakubno commented Jun 8, 2024

Support for multiple languages

Description

This feature adds following kernels (= language runtimes) to the Code Interpreter SDK (besides the default Python):

  • R
  • JavaScript
  • Java
  • Bash

Installation

You need to install a pre-release version of the SDK

Python

pip install e2b-code-interpreter==0.0.10a0

JavaScript/TypeScript

npm i @e2b/code-interpreter@0.0.9-multikernel-code-interpreterer.0

Usage

You can use multiple kernels in a single code interpreter sandbox.

Python

with CodeInterpreter() as sandbox:
    # 1. Set JS kernel - available kernel names: 'r', 'javascript', 'java', 'bash'
    js_id = sandbox.notebook.create_kernel(kernel_name="javascript")
    # 2. Run JS code inside code interpreter!
    execution = sandbox.notebook.exec_cell("console.log('Hello World!')", kernel_id=js_id)
    print(execution)
    # 3. Use Python again
    sandbox.notebook.exec_cell("print('Hello World!')")

JavaScript/TypeScript

const sandbox = await CodeInterpreter.create()

// 1. Set JS kernel - available kernel names: 'r', 'javascript', 'java', 'bash'
const jsID = await sandbox.notebook.createKernel({ kernelName: 'javascript' })
// 2. Run JS code inside code interpreter!
const execution = await sandbox.notebook.execCell("console.log('Hello World!')", { kernelID: jsID })
console.log(execution)
// 3. Use Python again
await sandbox.notebook.execCell('print("Hello World!")')

await sandbox.close()

Customization

If you would like to preinstall some packages or add your own languages, you can do it by copying all files except e2b.toml from the template folder on this branch to the directory where is your e2b.Dockerfile. You can freely edit the files and built your own template by running

e2b template build -c "/home/user/.jupyter/start-up.sh"

You can find more about custom templates in here.
In the production release, this process will be significantly simplified.

@jakubno jakubno added python-rc Python SDK - Release candidate js-rc JS SDK - Release candidate labels Jun 8, 2024
@jakubno jakubno force-pushed the multikernel-code-interpreterer branch from cdb71e9 to bc44bbf Compare June 13, 2024 09:29
@jakubno jakubno force-pushed the multikernel-code-interpreterer branch from bc44bbf to 9cfc9bb Compare June 13, 2024 09:58
@mlejva mlejva changed the title Multikernel code interpreterer Add support for additional language runtimes Jun 13, 2024
@mlejva
Copy link
Member

mlejva commented Jun 19, 2024

The JavaScript runtime doesn't support import. Ideally, we should fix it and import should just work. Less ideally (and the "less" is big here), we need to put a disclaimer here for users.

@mlejva
Copy link
Member

mlejva commented Jun 19, 2024

Similarly with await. The await doesn't work in the top most scope. Eg running this code

const fs = require('node:fs');
const fetch = require('node-fetch');

console.log('Hello');

const url = 'https://jsonplaceholder.typicode.com/posts/1';

// Fetch data from the API
const response = await fetch(url);
const data = await response.text();
console.log(data);

will produce the following error

ExecutionError {
  name: 'SyntaxError',
  value: 'await is only valid in async functions and the top level bodies of modules',
  tracebackRaw: [
    'evalmachine.<anonymous>:10',
    'const response = await fetch(url);',
    '                 ^^^^^',
    '',
    'SyntaxError: await is only valid in async functions and the top level bodies of modules',
    '    at new Script (node:vm:94:7)',
    '    at createScript (node:vm:250:10)',
    '    at Object.runInThisContext (node:vm:298:10)',
    '    at run ([eval]:1020:15)',
    '    at onRunRequest ([eval]:864:18)',
    '    at onMessage ([eval]:828:13)',
    '    at process.emit (node:events:517:28)',
    '    at emit (node:internal/child_process:944:14)',
    '    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)'
  ]
}
@mlejva
Copy link
Member

mlejva commented Jun 19, 2024

Deno's Jupyter kernel would tick both boxes (import and top-level await):
https://blog.jupyter.org/bringing-modern-javascript-to-the-jupyter-notebook-fc998095081e

We'd need to check if NPM dependencies work out of the box for users

@mlejva
Copy link
Member

mlejva commented Jun 22, 2024

Imagine a use case where you're using Code Interpreter in a serverless function. You are also using multiple kernels because your AI app can run both Python and R. It's currently hard to know which kernel my sandbox should use because the listKernels() method isn't much useful without also specifying the name of the kernel. Currently all you see is an array of kernel IDs. Following code

const kernels = await sbx.notebook.listKernels()
console.log('Kernels', kernels)

prints this

Kernels [ '044ca206-09a3-4d03-a01f-39c9d9bad7e4' ]

I also can't solve this with adding some metadata to a sandbox because metadata are only able to be set during the creation of the sandbox. This means that I need to save the kernel IDs somewhere in my database which implies a need for a database.

@ValentaTomas ValentaTomas changed the base branch from main to beta July 18, 2024 10:50
@ValentaTomas
Copy link
Member

The additional language runtimes in template are merged into the beta branch.

@ValentaTomas ValentaTomas deleted the multikernel-code-interpreterer branch July 18, 2024 11:15
@timrbula
Copy link

Deno's Jupyter kernel would tick both boxes (import and top-level await): https://blog.jupyter.org/bringing-modern-javascript-to-the-jupyter-notebook-fc998095081e

We'd need to check if NPM dependencies work out of the box for users

Did y'all consider tslab? It supports both JavaScript and TypeScript, imports and top-level `await. Yes, a similar feature set to Deno's kernel, but it's a Node.js runtime which more people are familiar with.

@mlejva
Copy link
Member

mlejva commented Jul 19, 2024

The additional language runtimes in template are merged into the beta branch.

@ValentaTomas if this is closed now, we need to document how to use different language runtimes somewhere.

EDIT: I also think we should open a new issue discussing the JS kernel problems that are mentioned here

@mlejva
Copy link
Member

mlejva commented Jul 19, 2024

Deno's Jupyter kernel would tick both boxes (import and top-level await): https://blog.jupyter.org/bringing-modern-javascript-to-the-jupyter-notebook-fc998095081e
We'd need to check if NPM dependencies work out of the box for users

Did y'all consider tslab? It supports both JavaScript and TypeScript, imports and top-level `await. Yes, a similar feature set to Deno's kernel, but it's a Node.js runtime which more people are familiar with.

Hey @timrbula thank you for the tip. I think we briefly looked at it. One of the big deciding factors for us would be if the project is active, will it keep being supported, and how much "complete" it is. We need to look at tslab in more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js-rc JS SDK - Release candidate python-rc Python SDK - Release candidate
5 participants