Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add hello world for JS
Vasek Mlejnsky425481e07ce7
diff --git a/apps/docs/src/app/hello-world/js/page.mdx b/apps/docs/src/app/hello-world/js/page.mdxindex d5d75bdcc..5ee149151 100644--- a/apps/docs/src/app/hello-world/js/page.mdx+++ b/apps/docs/src/app/hello-world/js/page.mdx@@ -1,8 +1,8 @@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:+# Hello World.ts+This JavaScript guide will show you the 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@@ -12,7 +12,7 @@ Check out the [full code in our cookbook](https://github.com/e2b-dev/e2b-cookboo### Overview1. Install Code Interpreter SDK-1. Prepare the prompt and tools for LLM+1. Prepare prompt and tools for LLM1. Prepare code interpreting1. Call LLM and parse response with tools1. Create code interpreter and run everything@@ -28,7 +28,7 @@ Check out the [SDK's repository on GitHub](https://github.com/e2b-dev/code-inter<CodeGroup isRunnable={false}>```bash {{ language: 'javascript' }}-TODO+npm i @e2b/code-interpreter```</CodeGroup>@@ -39,17 +39,49 @@ E2B_API_KEY="e2b-api-key"```</CodeGroup>-## 2. Prepare the prompt and tools for LLM+## 2. Prepare prompt and tools for LLMWe'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+Create the `model.ts` file and paste the following code.+<CodeGroup isFileName title="model.ts" isRunnable={false}>+```ts+import { Tool } from '@anthropic-ai/sdk/src/resources/beta/tools'++export const MODEL_NAME = 'claude-3-opus-20240229'++export const SYSTEM_PROMPT = `+## your job & context+you are a python data scientist. you are given tasks to complete and you run python code to solve them.+- the python code runs in jupyter notebook.+- every time you call \`execute_python\` tool, the python code is executed in a separate cell. it's okay to multiple calls to \`execute_python\`.+- display visualizations using matplotlib or any other visualization library directly in the notebook. don't worry about saving the visualizations to a file.+- you have access to the internet and can make api requests.+- you also have access to the filesystem and can read/write files.+- you can install any pip package (if it exists) if you need to but the usual packages for data analysis are already preinstalled.+- you can run any python code you want, everything is running in a secure sandbox environment.+`++export const tools: Tool[] = [+ {+ name: 'execute_python',+ description: 'Execute python code in a Jupyter notebook cell and returns any result, stdout, stderr, display_data, and error.',+ input_schema: {+ type: 'object',+ properties: {+ code: {+ type: 'string',+ description: 'The python code to execute in a single cell.'+ }+ },+ required: ['code']+ }+ }+]+```</CodeGroup>@@ -58,18 +90,41 @@ A little bit later, we'll connect `"execute_python"` to the E2B's code interpret## 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+We'll create a new function called `codeInterpret()` in a separate file `codeInterpreter.ts`.++<CodeGroup isFileName title="codeInterpreter.ts" isRunnable={false}>+```ts {{ language: 'javascript' }}+import { CodeInterpreter } from '@e2b/code-interpreter'++export async function codeInterpret(codeInterpreter: CodeInterpreter, code: string) {+ console.log(`\n${'='.repeat(50)}\n> Running following AI-generated code:\n${code}\n${'='.repeat(50)}`);++ const exec = await codeInterpreter.notebook.execCell(+ code,+ {+ // You can stream logs from the code interpreter+ // onStderr: (stderr: string) => console.log("\n[Code Interpreter stdout]", stderr),+ // onStdout: (stdout: string) => console.log("\n[Code Interpreter stderr]", stdout),+ //+ // You can also stream additional results like charts, images, etc.+ // onResult: ...+ }+ )++ if (exec.error) {+ console.log('[Code Interpreter error]', exec.error) // Runtime error+ return undefined+ }++ return exec+}```</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.+Inside the function, we call the `codeInterpreter.notebook.execCell()` method. The `execCell()` takes `code` argument, and executes this `code` inside E2B sandbox.## 4. Call LLM and parse response with toolsWe'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).@@ -82,25 +137,93 @@ ANTHROPIC_API_KEY="anthropic-api-key"<CodeGroup isRunnable={false}>```bash {{ language: 'python' }}-pip install anthropic+npm i @anthropic-ai/sdk```</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+Create the `index.ts` file, import dependencies, and create the `chat()` function that will do the LLM calling and tool parsing.++<CodeGroup isFileName title="index.ts" isRunnable={false}>+```ts {{ language: 'javascript' }}+import * as fs from 'fs'++import 'dotenv/config'+import { CodeInterpreter, Execution } from '@e2b/code-interpreter'+import Anthropic from '@anthropic-ai/sdk'++import {+ MODEL_NAME,+ SYSTEM_PROMPT,+ tools,+} from './model'+import { codeInterpret } from './codeInterpreter'++const anthropic = new Anthropic()++async function chat(codeInterpreter: CodeInterpreter, userMessage: string): Promise<Execution | undefined> {+ console.log('Waiting for Claude...')++ const msg = await anthropic.beta.tools.messages.create({+ model: MODEL_NAME,+ system: SYSTEM_PROMPT,+ max_tokens: 4096,+ messages: [{role: 'user', content: userMessage}],+ tools,+ })++ console.log(`\n${'='.repeat(50)}\nModel response: ${msg.content}\n${'='.repeat(50)}`)+ console.log(msg)++ if (msg.stop_reason === 'tool_use') {+ const toolBlock = msg.content.find((block) => block.type === 'tool_use');+ const toolName = toolBlock.name+ const toolInput = toolBlock.input++ console.log(`\n${'='.repeat(50)}\nUsing tool: ${toolName}\n${'='.repeat(50)}`);++ if (toolName === 'execute_python') {+ const code = toolInput.code+ return codeInterpret(codeInterpreter, code)+ }+ return undefined+ }+}```</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.+Now we put all together, and run our program. In the end of `index.ts` add following code prompting the LLM to visualize a distribution of height of men and print the median.++<CodeGroup isFileName title="index.ts" isRunnable={false}>+```ts {{ language: 'javascript' }}+async function run() {+ const userMessage = 'Visualize a distribution of height of men based on the latest data you know. Also print the median value.'++ const codeInterpreter = await CodeInterpreter.create()++ const codeOutput = await chat(codeInterpreter, userMessage)+ if (!codeOutput) {+ console.log('No code output')+ return+ }-<CodeGroup isFileName title="main.py" isRunnable={false}>-```python {{ language: 'python' }}-TODO+ const logs = codeOutput.logs+ console.log(logs)++ if (codeOutput.results.length == 0) {+ console.log('No results')+ return+ }++ const firstResult = codeOutput.results[0]+ // Print description of the first rich result+ console.log(firstResult.text)++ await codeInterpreter.close()+}++run()```</CodeGroup>@@ -111,17 +234,57 @@ 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.+We got our median in the logs (`stdout`, and `stderr`) but we also something intering in `firstResult`.```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+This looks like a plot. Let's save it to a file. Update the `run()` function like this.+<CodeGroup isFileName title="index.ts" isRunnable={false}>+```ts {{ language: 'javscript' }}+async function run() {+ const userMessage = 'Visualize a distribution of height of men based on the latest data you know. Also print the median value.'++ const codeInterpreter = await CodeInterpreter.create()++ const codeOutput = await chat(codeInterpreter, userMessage)+ if (!codeOutput) {+ console.log('No code output')+ return+ }++ const logs = codeOutput.logs+ console.log(logs)++ if (codeOutput.results.length == 0) {+ console.log('No results')+ return+ }++ const firstResult = codeOutput.results[0]+ // Print description of the first rich result+ console.log(firstResult.text)++ // If we received a chart in PNG form, we can visualize it+ if (firstResult.png) {+ // Decode the base64 encoded PNG data+ const pngData = Buffer.from(firstResult.png, 'base64');++ // Generate a unique filename for the PNG+ const filename = 'chart.png';++ // Save the decoded PNG data to a file+ fs.writeFileSync(filename, pngData);++ console.log(`Saved chart to ${filename}`);+ }++ await codeInterpreter.close()+}++run()```</CodeGroup>diff --git a/apps/docs/src/app/hello-world/py/page.mdx b/apps/docs/src/app/hello-world/py/page.mdxindex dac8e5b53..8b4508ced 100644--- a/apps/docs/src/app/hello-world/py/page.mdx+++ b/apps/docs/src/app/hello-world/py/page.mdx@@ -2,7 +2,7 @@ import Image from 'next/image'import imgChart from '@/images/hello-world-chart.png'-# Hello World+# Hello World.pyThis Python guide will show you the basics of E2B:- Connect the code interpreter to an LLM- Prompt the LLM to generate the Python code@@ -210,7 +210,7 @@ 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.+We got our median in the logs (`stdout`, and `stderr`) but we also something intering in `first_result`.```sh<Figure size 800x400 with 1 Axes>```diff --git a/apps/docs/src/components/Navigation/routes.tsx b/apps/docs/src/components/Navigation/routes.tsxindex 69c8cbe48..dc84b7cee 100644--- a/apps/docs/src/components/Navigation/routes.tsx+++ b/apps/docs/src/components/Navigation/routes.tsx@@ -41,7 +41,7 @@ export const routes = [href: '/',},{- title: 'helloWorld.[js|ts]',+ title: 'helloWorld.[ts|js]',href: '/hello-world/js',icon: <Image src={logoNode} width={20} height={20} alt="Python logo" />,isFontMono: true,