Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add section about code interpreter
Jakub Novake7518d5b3cbb
diff --git a/apps/docs/src/app/code-interpreter/examples/page.mdx b/apps/docs/src/app/code-interpreter/examples/page.mdxnew file mode 100644index 000000000..c9c36bb84--- /dev/null+++ b/apps/docs/src/app/code-interpreter/examples/page.mdx@@ -0,0 +1,136 @@+# Examples++++## Minimal example with the sharing context++The following example demonstrates how to create a shared context between multiple code executions. This is useful when you want to share variables between different code cells.++<CodeGroup isRunnable={false}>+```js+import { CodeInterpreter } from '@e2b/code-interpreter'++const sandbox = await CodeInterpreter.create()+await sandbox.execPython('x = 1')++const result = await sandbox.execPython('x += 1; x')+console.log(result.output) // outputs 2++await sandbox.close()+```+```python+from e2b_code_interpreter import CodeInterpreter++with CodeInterpreter() as sandbox:+ sandbox.exec_cell("x = 1")++ result = sandbox.exec_cell("x+=1; x")+ print(result.result) # outputs 2++```+</CodeGroup>++## Get charts and any display-able data++<CodeGroup isRunnable={false}>+```js+import { CodeInterpreter } from '@e2b/code-interpreter';++const sandbox = await CodeInterpreter.create();++const code = `+import matplotlib.pyplot as plt+import numpy as np++x = np.linspace(0, 20, 100)+y = np.sin(x)++plt.plot(x, y)+plt.show()+`;++// you can install dependencies in "jupyter notebook style"+await sandbox.execPython("!pip install matplotlib");++const result = await sandbox.execPython(code);++// this contains the image data, you can e.g. save it to file or send to frontend+result.display_data[0]["image/png"];++await sandbox.close();+```+```python+import base64+import io++from matplotlib import image as mpimg, pyplot as plt++from e2b_code_interpreter import CodeInterpreter++code = """+import matplotlib.pyplot as plt+import numpy as np++x = np.linspace(0, 20, 100)+y = np.sin(x)++plt.plot(x, y)+plt.show()+"""++with CodeInterpreter() as sandbox:+ # you can install dependencies in "jupyter notebook style"+ sandbox.exec_cell("!pip install matplotlib")++ # plot random graph+ result = sandbox.exec_cell(code)++# there's your image+image = result.display_data[0]["image/png"]++# example how to show the image / prove it works+i = base64.b64decode(image)+i = io.BytesIO(i)+i = mpimg.imread(i, format='PNG')++plt.imshow(i, interpolation='nearest')+plt.show()+```+</CodeGroup>++## Streaming code output++<CodeGroup isRunnable={false}>+```js+import { CodeInterpreter } from "@e2b/code-interpreter";++code = `+import time++print("hello")+time.sleep(5)+print("world")+`;++const sandbox = await CodeInterpreter.create();++await sandbox.execPython(+ code,+ (out) => console.log(out),+ (outErr) => console.error(outErr),+);+````+```python+from e2b_code_interpreter import CodeInterpreter++code = """+import time++print("hello")+time.sleep(5)+print("world")+"""+with CodeInterpreter() as sandbox:+ sandbox.exec_cell(code, on_stdout=print, on_stderr=print)+```+</ CodeGroup>diff --git a/apps/docs/src/app/code-interpreter/installation/page.mdx b/apps/docs/src/app/code-interpreter/installation/page.mdxnew file mode 100644index 000000000..1fe04fd7e--- /dev/null+++ b/apps/docs/src/app/code-interpreter/installation/page.mdx@@ -0,0 +1,19 @@+import { Libraries } from '@/components/Libraries'++# Installation++You can install the code interpreter using the following commands:++<CodeGroup isRunnable={false}>+```bash {{ language: 'js' }}+npm install @e2b/code-interpreter+```+```bash {{ language: 'python' }}+pip install e2b-code-interpreter+```+</CodeGroup>+++## Next steps++1. [Follow the examples](/code-interpreter/examples/)diff --git a/apps/docs/src/app/code-interpreter/overview/page.mdx b/apps/docs/src/app/code-interpreter/overview/page.mdxnew file mode 100644index 000000000..5a8818040--- /dev/null+++ b/apps/docs/src/app/code-interpreter/overview/page.mdx@@ -0,0 +1,84 @@+import Link from 'next/link'+import {+ FileCode,+} from 'lucide-react'+++# Code Interpreter++<Link+ className="w-fit flex items-center justify-start space-x-2"+ href="https://github.com/e2b-dev/code-interpreter/blob/main/template"+ rel="noopener noreferrer"+ target="_blank"+>+ <FileCode+ size={18}+ className="text-brand-600"+ />+ Template source code+</Link>+++## Motivation++The code generated by LLMs is often split into code blocks, where each subsequent block references the previous one. This is a common pattern in Jupyter notebooks, where each cell can reference the variables and definitions from the previous cells. In the classical sandbox each code execution is independent and does not share the context with the previous executions.++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](https://jupyter-client.readthedocs.io/en/latest/messaging.html). 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](https://github.com/e2b-dev/E2B/blob/main/sandboxes/code-interpreter/e2b_matplotlib_backend.py) of our code interpreter.++The current code interpreter allows to run Python code but each run share the context. That means that subsequent runs can reference to variables, definitions, etc from past code execution runs.++## Features++- **Stateful Execution**: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.+- **Displaying Graph & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.++## Typical Use Cases++- **Interactive Code Interpreter**: Run code snippets and share context between them.+- **Data Analysis**: Analyze data and visualize it using charts.++## Pre-installed packages++The full and always up-to-date list can be found in the [`requirements.txt`](https://github.com/e2b-dev/E2B/blob/stateful-code-interpreter/sandboxes/code-interpreter-stateful/requirements.txt) file.++```text+# Jupyter server requirements+jupyter-server==2.13.0+ipykernel==6.29.3+ipython==8.22.2++# Other packages+aiohttp==3.9.3+beautifulsoup4==4.12.3+bokeh==3.3.4+gensim==4.3.2+imageio==2.34.0+joblib==1.3.2+librosa==0.10.1+matplotlib==3.8.3+nltk==3.8.1+numpy==1.26.4+opencv-python==4.9.0.80+openpyxl==3.1.2+pandas==1.5.3+plotly==5.19.0+pytest==8.1.0+python-docx==1.1.0+pytz==2024.1+requests==2.26.0+scikit-image==0.22.0+scikit-learn==1.4.1.post1+scipy==1.12.0+seaborn==0.13.2+soundfile==0.12.1+spacy==3.7.4+textblob==0.18.0+tornado==6.4+urllib3==1.26.7+xarray==2024.2.0+xlrd==2.0.1+```diff --git a/apps/docs/src/app/code-interpreter/results/page.mdx b/apps/docs/src/app/code-interpreter/results/page.mdxnew file mode 100644index 000000000..ce4df376e--- /dev/null+++ b/apps/docs/src/app/code-interpreter/results/page.mdx@@ -0,0 +1,10 @@+# Results explained++The object returned by the `execCell` method contains the following fields:++- `result`: contains the result of the cell execution+ - if there's an variable on the last line, it will evaluated (there's an detailed explanation in the [Jupyter documentation](https://ipython.readthedocs.io/en/stable/development/execution.html#running-user-code)+- `success`: a boolean indicating whether the cell execution was successful+- `error`: the error message, if any+- `onStdout`: the cell standard output, as a string+- `onStderr`: the cell standard error, as a string\ No newline at end of filediff --git a/apps/docs/src/app/code-interpreter/template/page.mdx b/apps/docs/src/app/code-interpreter/template/page.mdxnew file mode 100644index 000000000..841b25980--- /dev/null+++ b/apps/docs/src/app/code-interpreter/template/page.mdx@@ -0,0 +1,17 @@+# Custom template using Code Interpreter++##+The template requires custom setup. If you want to build your own custom template and use Code Interpreter, you need to do:++Copy jupyter_server_config.py and start-up.sh from this PR+Add following commands in your Dockerfile+# Installs jupyter server and kernel+RUN pip install jupyter-server ipykernel ipython+RUN ipython kernel install --name "python3" --user+# Copes jupyter server config+COPY ./jupyter_server_config.py /home/user/.jupyter/+# Setups jupyter server+COPY ./start-up.sh /home/user/.jupyter/+RUN chmod +x /home/user/.jupyter/start-up.sh+Add the following option -c "/home/user/.jupyter/start-up.sh" to e2b template build command or add this line to your e2b.toml.+start_cmd = "/home/user/.jupyter/start-up.sh"diff --git a/apps/docs/src/app/guide/connect-bucket/page.mdx b/apps/docs/src/app/guide/connect-bucket/page.mdxindex a0c845187..7626cf01e 100644--- a/apps/docs/src/app/guide/connect-bucket/page.mdx+++ b/apps/docs/src/app/guide/connect-bucket/page.mdx@@ -1,5 +1,3 @@-import Image from 'next/image'-# Connecting bucket to the sandboxTo enable persistence of the data in the sandbox we can use a bucket to store the data. We are leveraging the fuse file system to mount the bucket to the sandbox.diff --git a/apps/docs/src/app/sandbox/templates/premade/TemplateName.tsx b/apps/docs/src/app/sandbox/templates/premade/TemplateName.tsxdeleted file mode 100644index 1da0dcb58..000000000--- a/apps/docs/src/app/sandbox/templates/premade/TemplateName.tsx+++ /dev/null@@ -1,14 +0,0 @@-export interface Props {- templateName: string-}--export function TemplateName({- templateName,-}: Props) {- return (- <div className="pr-1 w-[320px] flex items-center justify-start space-x-2 border border-gray-600 rounded">- <span className="font-mono text-sm px-2 h-8 flex items-center justify-center border-r border-gray-600">template name</span>- <span className="font-mono text-sm text-gray-200">{templateName}</span>- </div>- )-}\ No newline at end of filediff --git a/apps/docs/src/app/sandbox/templates/premade/page.mdx b/apps/docs/src/app/sandbox/templates/premade/page.mdxdeleted file mode 100644index 4f3b507d1..000000000--- a/apps/docs/src/app/sandbox/templates/premade/page.mdx+++ /dev/null@@ -1,202 +0,0 @@-import Link from 'next/link'-import {- FileCode,-} from 'lucide-react'--import { TemplateName } from './TemplateName'--# Premade Sandboxes--To get your started quickly, we offer two premade sandboxes.--## 1. Default Sandbox-<Link- className="w-fit flex items-center space-x-2 justify-start"- href="https://github.com/e2b-dev/e2b/blob/main/sandboxes/base/e2b.Dockerfile"- rel="noopener noreferrer"- target="_blank"->- <FileCode- size={18}- className="text-brand-600"- />- Template File-</Link>--<TemplateName templateName="base"/>--### Features-Ubuntu-based sandbox with Nodejs, Python 3, and essentials libraries like `curl`.-- Ideal for getting started with the SDK.--### Usage-The base sandbox has all the features of the [Sandbox API](/sandbox/api/envs) available.-------## 2. Code Interpreter Sandbox-<Link- className="w-fit flex items-center justify-start space-x-2"- href="https://github.com/e2b-dev/e2b/blob/main/sandboxes/code-interpreter/e2b.Dockerfile"- rel="noopener noreferrer"- target="_blank"->- <FileCode- size={18}- className="text-brand-600"- />- Template File-</Link>--<TemplateName templateName="code-interpreter"/>--### Features-Ubuntu-based sandbox with Python 3 and data science Python libraries like `pandas`, `numpy`, or `matplotlib`.-- See the complete list of pre-installed Python packages [here](https://github.com/e2b-dev/E2B/blob/main/sandboxes/code-interpreter/requirements.txt).-- Ideal for the Data Analysis and Code Interpreter use cases.---### Usage-You can use all the regular [Sandbox API](/sandbox/api/envs) inside the Code Interpreter / Data Analysis sandbox. On top of that, you can use a few extra methods specifcially made for the use case of a code interpreter powered by an LLM.--- **Create sandbox**-<CodeGroup isRunnable={false}>-```js {{ language: 'js' }}-import { CodeInterpreter } from 'e2b'-const sandbox = await CodeInterpreter.create()--// Close sandbox once you're done-await sandbox.close()-```--```python {{ language: 'python' }}-from e2b import CodeInterpreter-sandbox = CodeInterpreter()--# Close sandbox once you're done-sandbox.close()-```-</CodeGroup>--- **Run Python code**-<CodeGroup isRunnable={false}>-```js {{ language: 'js' }}-import { CodeInterpreter } from 'e2b'--// 1. Create sandbox-const sandbox = await CodeInterpreter.create()--// 2. Get code that was generated by LLM and run it-const llmGeneratedPythonCode = 'print("Hello World")'-// `artifacts` are chart files created by matplotlib-const { stdout, stderr, artifacts } = await sandbox.runPython(llmGeneratedPythonCode)--// 3. Retrieve chart files:-for (const artifact of artifacts) {- // Now you can save this file, send it to frontend, or anything else- const downloadedChart = await artifact.download()-}--// 4. Close sandbox-await sandbox.close-```--```python {{ language: 'python' }}-from e2b import CodeInterpreter--# 1. Create sandbox-sandbox = CodeInterpreter()--# 2. Get code that was generated by LLM and run it-llm_generated_python_code = 'print("Hello World")'-# `artifacts` are chart files created by matplotlib-stdout, stderr, artifacts = sandbox.run_python(llm_generated_python_code)--# 3. Retrieve chart files:-for artifact in artifacts:- # Now you can save this file, send it to frontend, or anything else- downloaded_chart = artifact.download()--# 4. Close sandbox-sandbox.close()-```-</CodeGroup>--- **Install Python packages**--Installs Python packages using `pip`.-<CodeGroup isRunnable={false}>-```js {{ language: 'js' }}-import { CodeInterpreter } from 'e2b'--// 1. Create sandbox-const sandbox = await CodeInterpreter.create()--// 2. Install Python package-await CodeInterpreter.installPythonPackages('ffmpeg')--// 3. Close sandbox-await sandbox.close()-```-```python {{ language: 'python' }}-from e2b import CodeInterpreter--# 1. Create sandbox-sandbox = CodeInterpreter()--# 2. Install Python package-sandbox.install_python_packages('ffmpeg')--# 3. Close sandbox-sandbox.close()-```-</CodeGroup>--- **Install system packages**--Installs system packages using `apt-get`.-<CodeGroup isRunnable={false}>-```js {{ language: 'js' }}-import { CodeInterpreter } from 'e2b'--// 1. Create sandbox-const sandbox = await CodeInterpreter.create()--// 2. Install system package-await CodeInterpreter.installSystemPackages('ffmpeg')--// 3. Close sandbox-await sandbox.close()-```-```python {{ language: 'python' }}-from e2b import CodeInterpreter--# 1. Create sandbox-sandbox = CodeInterpreter()--# 2. Install system package-sandbox.install_system_packages('ffmpeg')--# 3. Close sandbox-sandbox.close()-```-</CodeGroup>--{/* - Listen to new files generated by executed code (charts, images, etc)-<CodeGroup isRunnable={false}>-```js {{ language: 'js' }}-await CodeInterpreter.installSystemPackages('ffmpeg')-await sandbox.close()-```-```python {{ language: 'python' }}-sandbox = CodeInterpreter.install_system_packages('ffmpeg')-sandbox.close()-```-</CodeGroup> */}--{/* - Stream code output-- Stream code errors-- Upload file-- Download file */}--diff --git a/apps/docs/src/components/Navigation/routes.tsx b/apps/docs/src/components/Navigation/routes.tsxindex be44f56ee..09fa36cda 100644--- a/apps/docs/src/components/Navigation/routes.tsx+++ b/apps/docs/src/components/Navigation/routes.tsx@@ -1,7 +1,9 @@import {- Boxes,+ BarChart,+ Book,Braces,ChevronRightSquare,+ Cog,Cpu,DollarSign,FileDown,@@ -10,6 +12,7 @@ import {Folder,FolderTree,Hammer,+ HardDrive,HeartHandshake,KeyRound,Link,@@ -21,53 +24,31 @@ import {TerminalSquare,Timer,Variable,- HardDrive,- BarChart,+ Wrench, FileCode, Lightbulb,} from 'lucide-react'-export const routes = [{title: 'Overview',links: [{ title: 'Introduction', href: '/' },{- icon: (- <DollarSign- strokeWidth={1}- size={20}- />- ),+ icon: <DollarSign strokeWidth={1} size={20} />,title: 'Pricing',href: '/pricing',},{- icon: (- <BarChart- strokeWidth={1}- size={20}- />- ),+ icon: <BarChart strokeWidth={1} size={20} />,title: 'Track usage',href: '/usage',},{- icon: (- <HeartHandshake- strokeWidth={1}- size={20}- />- ),+ icon: <HeartHandshake strokeWidth={1} size={20} />,title: 'Open source',href: '/open-source',},{- icon: (- <ShieldQuestion- strokeWidth={1}- size={20}- />- ),+ icon: <ShieldQuestion strokeWidth={1} size={20} />,title: 'Getting help',href: '/getting-help',},@@ -77,32 +58,17 @@ export const routes = [title: 'Getting Started',links: [{- icon: (- <Settings- strokeWidth={1}- size={20}- />- ),+ icon: <Settings strokeWidth={1} size={20} />,title: 'Installation',href: '/getting-started/installation',},{- icon: (- <KeyRound- strokeWidth={1}- size={20}- />- ),+ icon: <KeyRound strokeWidth={1} size={20} />,title: 'API Key',href: '/getting-started/api-key',},{- icon: (- <Hammer- strokeWidth={1}- size={20}- />- ),+ icon: <Hammer strokeWidth={1} size={20} />,title: 'Guide: Custom Code Interpreter',href: '/guide/simple-gpt4-code-interpreter',//@@ -132,22 +98,12 @@ export const routes = [href: '/sandbox/overview',},{- icon: (- <Cpu- strokeWidth={1}- size={20}- />- ),+ icon: <Cpu strokeWidth={1} size={20} />,title: 'Compute',href: '/sandbox/compute',},{- icon: (- <PencilRuler- strokeWidth={1}- size={20}- />- ),+ icon: <PencilRuler strokeWidth={1} size={20} />,title: 'Customization',href: '/sandbox/custom',},@@ -161,182 +117,122 @@ export const routes = [href: '/sandbox/templates/overview',},{- icon: (- <File- strokeWidth={1}- size={20}- />- ),+ icon: <File strokeWidth={1} size={20} />,title: 'Template File',href: '/sandbox/templates/template-file',},{- icon: (- <PlaySquare- strokeWidth={1}- size={20}- />- ),+ icon: <PlaySquare strokeWidth={1} size={20} />,title: 'Start Command',href: '/sandbox/templates/start-cmd',},{- icon: (- <Boxes- strokeWidth={1}- size={20}- />- ),- title: 'Premade Sandboxes',- href: '/sandbox/templates/premade',- },- {- icon: (- <Hammer- strokeWidth={1}- size={20}- />- ),+ icon: <Hammer strokeWidth={1} size={20} />,title: 'Guide: Creating Custom Sandbox',href: '/guide/custom-sandbox',},{- icon: (- <HardDrive- strokeWidth={1}- size={20}- />- ),+ icon: <HardDrive strokeWidth={1} size={20} />,title: 'Connecting buckets to persist data',href: '/guide/connect-bucket',},- ]+ ],},{title: 'Sandbox API',links: [{- icon: (- <Variable- strokeWidth={1}- size={20}- />- ),+ icon: <Variable strokeWidth={1} size={20} />,title: 'Environment Variables',href: '/sandbox/api/envs',},{- icon: (- <FolderTree- strokeWidth={1}- size={20}- />- ),+ icon: <FolderTree strokeWidth={1} size={20} />,title: 'Filesystem',href: '/sandbox/api/filesystem',},{- icon: (- <ChevronRightSquare- strokeWidth={1}- size={20}- />- ),+ icon: <ChevronRightSquare strokeWidth={1} size={20} />,title: 'Process',href: '/sandbox/api/process',},{- icon: (- <Folder- strokeWidth={1}- size={20}- />- ),+ icon: <Folder strokeWidth={1} size={20} />,title: 'Working Directory',href: '/sandbox/api/cwd',},{- icon: (- <Link- strokeWidth={1}- size={20}- />- ),+ icon: <Link strokeWidth={1} size={20} />,title: 'Sandbox URL',href: '/sandbox/api/url',},{- icon: (- <FileUp- strokeWidth={1}- size={20}- />- ),+ icon: <FileUp strokeWidth={1} size={20} />,title: 'Upload Files',href: '/sandbox/api/upload',},{- icon: (- <FileDown- strokeWidth={1}- size={20}- />- ),+ icon: <FileDown strokeWidth={1} size={20} />,title: 'Download Files',href: '/sandbox/api/download',},{- icon: (- <Timer- strokeWidth={1}- size={20}- />- ),+ icon: <Timer strokeWidth={1} size={20} />,title: 'Timeouts',href: '/sandbox/api/timeouts',},{- icon: (- <RefreshCw- strokeWidth={1}- size={20}- />- ),+ icon: <RefreshCw strokeWidth={1} size={20} />,title: 'Connect to running sandbox',href: '/sandbox/api/reconnect',},{- icon: (- <Braces- strokeWidth={1}- size={20}- />- ),+ icon: <Braces strokeWidth={1} size={20} />,title: 'Sandbox metadata',href: '/sandbox/api/metadata',},],},+ {+ title: 'Code Interpreter',+ links: [+ {+ icon: <FileCode strokeWidth={1} size={20} />,+ title: 'Overview',+ href: '/code-interpreter/overview',+ },+ {+ icon: <Cog strokeWidth={1} size={20} />,+ title: 'Installation',+ href: '/code-interpreter/installation',+ },+ {+ icon: <Lightbulb strokeWidth={1} size={20} />,+ title: 'Results explained',+ href: '/code-interpreter/results',+ },+ {+ icon: <Book strokeWidth={1} size={20} />,+ title: 'Examples',+ href: '/code-interpreter/examples',+ },+ {+ icon: <Wrench strokeWidth={1} size={20} />,+ title: 'Extending Code Interpreter',+ href: '/code-interpreter/template',+ },+ ],+ },{title: 'CLI API',links: [{- icon: (- <Settings- strokeWidth={1}- size={20}- />- ),+ icon: <Settings strokeWidth={1} size={20} />,title: 'Installation',href: '/cli/installation',},{- icon: (- <TerminalSquare- strokeWidth={1}- size={20}- />- ),+ icon: <TerminalSquare strokeWidth={1} size={20} />,title: 'Commands',href: '/cli/commands',},