Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

hello world JS [wip]

Vasek Mlejnskya972e3b7d15d
diff --git a/apps/docs/src/app/hello-world/js/page.mdx b/apps/docs/src/app/hello-world/js/page.mdx
new file mode 100644
index 000000000..d5d75bdcc
--- /dev/null
+++ b/apps/docs/src/app/hello-world/js/page.mdx
@@ -0,0 +1,135 @@
+import Image from 'next/image'
+import imgChart from '@/images/hello-world-chart.png'
+
+# Hello World.js
+This JavaScript guide will show you basics of E2B:
+- Connect the code interpreter to an LLM
+- Prompt the LLM to generate the Python code
+- Execute the AI-generated Python code in a secure E2B sandbox
+
+### Get full code
+Check out the [full code in our cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/hello-world-js).
+
+### Overview
+1. Install Code Interpreter SDK
+1. Prepare the prompt and tools for LLM
+1. Prepare code interpreting
+1. Call LLM and parse response with tools
+1. Create code interpreter and run everything
+1. Save generated chart
+
+
+## 1. Install Code Interpreter SDK
+
+The Code Interpreter SDK allows you to run AI-generated code in a secure small VM - **E2B sandbox** - made for AI code execution.
+Inside the sandbox is a Jupyter server running that you can control from our SDK through the `notebook.execCell()` method.
+
+Check out the [SDK's repository on GitHub](https://github.com/e2b-dev/code-interpreter).
+
+<CodeGroup isRunnable={false}>
+```bash {{ language: 'javascript' }}
+TODO
+```
+</CodeGroup>
+
+Get your E2B API key [here](/getting-started/api-key) and save it to `.env` in your root directory.
+<CodeGroup isFileName title=".env" isRunnable={false}>
+```bash
+E2B_API_KEY="e2b-api-key"
+```
+</CodeGroup>
+
+## 2. Prepare the prompt and tools for LLM
+We'll be using Anthropic's [Claude 3 Opus](https://www.anthropic.com/news/claude-3-family) model but E2B works with any LLM so feel free to pick any you want!
+
+<Note>
+Usually, all you need from the model is just support for tool use. If the LLM doesn't support tool use, you can ask the LLM to respond with Markdown or XML and parse the LLM's output on your own. Then just pass the parsed code from code blocks to the code interpreter.
+</Note>
+
+Create the `model.py` file and paste the following code.
+<CodeGroup isFileName title="model.py" isRunnable={false}>
+```js
+TODO
+```
+</CodeGroup>
+
+This defines our system prompt and the `tools` dictionary with available tools for the LLM - namely the `"execute_python"` tool.
+A little bit later, we'll connect `"execute_python"` to the E2B's code interpretrer.
+
+
+## 3. Prepare code interpreting
+We'll create a new function called `code_interpret()` in a separate file `code_interpreter.py`.
+
+<CodeGroup isFileName title="code_interpreter.py" isRunnable={false}>
+```python {{ language: 'python' }}
+TODO
+```
+</CodeGroup>
+
+This function takes the `CodeInterpreter` object from our SDK, and `code` as paramaters.
+The `code` parameter is the code generated by the LLM.
+
+Inside the function, we call the `code_interpreter.notebook.exec_cell()` method. The `exec_cell()` takes `code` argument, and executes this `code` inside E2B sandbox.
+
+## 4. Call LLM and parse response with tools
+We're using Claude 3 Opus. Get your [Anthropic API key](https://console.anthropic.com/), save it to `.env` file, and install the [Anthropic SDK](https://docs.anthropic.com/claude/reference/client-sdks).
+
+<CodeGroup isFileName title=".env" isRunnable={false}>
+```bash
+ANTHROPIC_API_KEY="anthropic-api-key"
+```
+</CodeGroup>
+
+<CodeGroup isRunnable={false}>
+```bash {{ language: 'python' }}
+pip install anthropic
+```
+</CodeGroup>
+
+Now we'll put everything together.
+Create the `main.py` file, import dependencies, and create the `chat()` function that will do the LLM calling and tool parsing.
+
+<CodeGroup isFileName title="main.py" isRunnable={false}>
+```python {{ language: 'python' }}
+TODO
+```
+</CodeGroup>
+
+## 5. Create code interpreter and run everything
+Now we put all together, and run our program. In the end of `main.py` add following code prompting the LLM to visualize a distribution of height of men and print the median.
+
+<CodeGroup isFileName title="main.py" isRunnable={false}>
+```python {{ language: 'python' }}
+TODO
+```
+</CodeGroup>
+
+You should see similar results to this:
+
+```sh
+stdout=['The median male height is 175.5 cm\n'] stderr=[]
+<Figure size 800x400 with 1 Axes>
+```
+
+We got our median in the logs (`stdout`, and `stderr`) but we also something intering in `first_result`. It looks like a chart.
+```sh
+<Figure size 800x400 with 1 Axes>
+```
+
+6. Save generated chart
+
+This looks like a plot. Let's save it to a file. Add the following to the end of `main.py`.
+<CodeGroup isFileName title="main.py" isRunnable={false}>
+```python {{ language: 'python' }}
+TODO
+```
+</CodeGroup>
+
+The chart got saved to the `chart.png` file and it should look similar to this:
+
+<Image
+ src={imgChart}
+ className="rounded w-full"
+ alt="Chart visualizing distribution height of men"
+ unoptimized
+/>
\ No newline at end of file
diff --git a/apps/docs/src/app/hello-world/py/page.mdx b/apps/docs/src/app/hello-world/py/page.mdx
index 933be5a90..aead25c41 100644
--- a/apps/docs/src/app/hello-world/py/page.mdx
+++ b/apps/docs/src/app/hello-world/py/page.mdx
@@ -1,8 +1,7 @@
import Image from 'next/image'
import imgChart from '@/images/hello-world-chart.png'
-
-# Hello World
+# Hello World.py
This Python guide will show you basics of E2B:
- Connect the code interpreter to an LLM
- Prompt the LLM to generate the Python code