Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add docs for run code helper method

Vasek Mlejnsky8c59d6d366b2
diff --git a/apps/docs/package.json b/apps/docs/package.json
index 3b30d3da1..c3510d35b 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -59,13 +59,13 @@
"unist-util-filter": "^4.0.1",
"unist-util-visit": "^4.1.1",
"usehooks-ts": "^2.9.1",
- "zustand": "^4.3.2"
+ "zustand": "^4.3.2",
+ "@e2b/sdk": "^0.3.2"
},
"devDependencies": {
- "@e2b/sdk": "^0.3.0",
"@nodelib/fs.walk": "^2.0.0",
"encoding": "^0.1.13",
- "sharp": "^0.32.0",
- "eslint-config-next": "13.4.19"
+ "eslint-config-next": "13.4.19",
+ "sharp": "^0.32.0"
}
-}
+}
\ No newline at end of file
diff --git a/apps/docs/src/app/getting-started/api-key/page.mdx b/apps/docs/src/app/getting-started/api-key/page.mdx
index 7253b26df..f2b189908 100644
--- a/apps/docs/src/app/getting-started/api-key/page.mdx
+++ b/apps/docs/src/app/getting-started/api-key/page.mdx
@@ -4,4 +4,4 @@ import APIKey from './APIKey'
<APIKey/>
-Follow the [Basics](/getting-started/basics) to start using the SDK.
+Follow the [Basics](/playground/overview) to start using the SDK.
diff --git a/apps/docs/src/app/playgrounds/execute/page.mdx b/apps/docs/src/app/playgrounds/execute/page.mdx
new file mode 100644
index 000000000..3647ab385
--- /dev/null
+++ b/apps/docs/src/app/playgrounds/execute/page.mdx
@@ -0,0 +1,27 @@
+export const metadata = {
+ title: 'E2B Documentation - Cloud for AI Agents & Apps',
+ description: 'AI Playgrounds Code Execution',
+}
+
+# Code Execution
+
+You can use E2B SDK to execute code generated by your AI agent and LLM inside the AI playground - a sandboxed cloud environment. {{ className: 'lead' }}
+There are 2 ways to run code with E2B SDK. {{ className: 'lead' }}
+
+### 1. Use the `runCode`/`run_code` methods in JS and Python SDK to run the LLM-generated code {{ className: 'lead' }}
+
+This approach is very stragithforward and you don't need to worry about managing the playground's lifecycle or the process lifecycle.
+
+<CodeGroupAutoload
+ path="code_exec/run_code"
+ isRunnable={true}
+/>
+
+### 2. Run the LLM-generated code using the low-level [session.process](/playgrounds/process) module {{ className: 'lead' }}
+
+This approach gives you more control about the underlying session and the running process. You can for example stream stdout and stderr, or kill the process that's running the code.
+
+<CodeGroupAutoload
+ path="code_exec/process"
+ isRunnable={true}
+/>
diff --git a/apps/docs/src/app/playgrounds/process/page.mdx b/apps/docs/src/app/playgrounds/process/page.mdx
index e4d79a80b..e4dffc335 100644
--- a/apps/docs/src/app/playgrounds/process/page.mdx
+++ b/apps/docs/src/app/playgrounds/process/page.mdx
@@ -1,5 +1,5 @@
export const metadata = {
- title: 'E2B Documentation - Cloud for AI Agents Apps',
+ title: 'E2B Documentation - Cloud for AI Agents & Apps',
description: 'AI Playgrounds Running Processes',
}
@@ -12,7 +12,9 @@ export const sections = [
]
## Process
+
Here are the basic operations you can do with the process inside a cloud playground:
+
- [Start process](/getting-started/basics#start-process)
- [Stop process](/getting-started/basics#stop-process)
- [Stdout](/getting-started/basics#stream-stdout)
@@ -20,16 +22,36 @@ Here are the basic operations you can do with the process inside a cloud playgro
- [Stderr](/getting-started/basics#stream-stderr)
## Start process
-<CodeGroupAutoload path="basics/process_start" isRunnable={true} />
+
+<CodeGroupAutoload
+ path="basics/process_start"
+ isRunnable={true}
+/>
## Stop process
-<CodeGroupAutoload path="basics/process_stop" isRunnable={false} />
+
+<CodeGroupAutoload
+ path="basics/process_stop"
+ isRunnable={false}
+/>
## Stream stdout
-<CodeGroupAutoload path="basics/process_stream_stdout" isRunnable={false} />
+
+<CodeGroupAutoload
+ path="basics/process_stream_stdout"
+ isRunnable={false}
+/>
## Stream stderr
-<CodeGroupAutoload path="basics/process_stream_stderr" isRunnable={false} />
+
+<CodeGroupAutoload
+ path="basics/process_stream_stderr"
+ isRunnable={false}
+/>
## Send stdin
-<CodeGroupAutoload path="basics/process_write_stdin" isRunnable={true} />
\ No newline at end of file
+
+<CodeGroupAutoload
+ path="basics/process_write_stdin"
+ isRunnable={true}
+/>
diff --git a/apps/docs/src/code/js/code_exec/process.js b/apps/docs/src/code/js/code_exec/process.js
new file mode 100644
index 000000000..b409142cd
--- /dev/null
+++ b/apps/docs/src/code/js/code_exec/process.js
@@ -0,0 +1,30 @@
+import { Session } from '@e2b/sdk'
+
+// 1. Start the playground session
+const session = await Session.create({
+ // Select the right runtime
+ // 'Node', 'Bash', 'Python3', 'Java', 'Go', 'Rust', 'PHP', 'Perl', 'DotNET'
+ id: 'Python3',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// 2. Save the LLM-generated code inside the playground
+const llmGeneratedCode = 'print("This code was generated by LLM")'
+await session.filesystem.write('/main.py', llmGeneratedCode)
+
+
+// 3. Start the execution of the JavaScript file we saved
+const proc = await session.process.start({ // $HighlightLine
+ cmd: 'python /main.py', // $HighlightLine
+ // 4. Stream stdout, stderr
+ onStdout: (data) => console.log(data.line), // $HighlightLine
+ onStderr: (data) => console.log(data.line), // $HighlightLine
+}) // $HighlightLine
+
+// 4. Wait for the process to finish
+await proc.finished
+
+// 5. Or you can access output after the process has finished
+const output = proc.output
+
+await session.close()
diff --git a/apps/docs/src/code/js/code_exec/run_code.js b/apps/docs/src/code/js/code_exec/run_code.js
new file mode 100644
index 000000000..82a0309a6
--- /dev/null
+++ b/apps/docs/src/code/js/code_exec/run_code.js
@@ -0,0 +1,7 @@
+import { runCode } from '@e2b/sdk'
+
+const llmGeneratedCode = 'print("This code was generated by LLM")'
+
+const { stdout, stderr } = await runCode('Python3', llmGeneratedCode) // $HighlightLine
+console.log(stdout)
+console.log(stderr)
diff --git a/apps/docs/src/code/python/code_exec/process.py b/apps/docs/src/code/python/code_exec/process.py
new file mode 100644
index 000000000..fe6e996d6
--- /dev/null
+++ b/apps/docs/src/code/python/code_exec/process.py
@@ -0,0 +1,39 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+def print_out(output):
+ print(output.line)
+
+async def main():
+ # 1. Start the playground session
+ session = await Session.create(
+ # Select the right runtime
+ # 'Node', 'Bash', 'Python3', 'Java', 'Go', 'Rust', 'PHP', 'Perl', 'DotNET'
+ id="Python3",
+ api_key=E2B_API_KEY,
+ )
+
+ # 2. Save the LLM-generated code inside the playground
+ llm_generated_code = """print("This code was generated by LLM")"""
+ await session.filesystem.write('/main.py', llm_generated_code)
+
+ # 3. Start the execution of the JavaScript file we saved
+ proc = await session.process.start( # $HighlightLine
+ cmd="python3 /main.py", # $HighlightLine
+ # 4. Stream stdout, stderr
+ on_stdout=print_out, # $HighlightLine
+ on_stderr=print_out, # $HighlightLine
+ ) # $HighlightLine
+
+ # 4. Wait for the process to finish
+ await proc
+
+ # 5. Or you can access output after the process has finished
+ output = proc.output
+
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/code/python/code_exec/run_code.py b/apps/docs/src/code/python/code_exec/run_code.py
new file mode 100644
index 000000000..15f228b29
--- /dev/null
+++ b/apps/docs/src/code/python/code_exec/run_code.py
@@ -0,0 +1,11 @@
+import asyncio
+from e2b import run_code
+
+llm_generated_code = 'print("This code was generated by LLM")'
+
+async def main():
+ stdout, stderr = await run_code('Python3', llm_generated_code) # $HighlightLine
+ print(stdout)
+ print(stderr)
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/components/Navigation.tsx b/apps/docs/src/components/Navigation.tsx
index 6a45a6694..e127008e1 100644
--- a/apps/docs/src/components/Navigation.tsx
+++ b/apps/docs/src/components/Navigation.tsx
@@ -20,6 +20,7 @@ import {
ShieldAlert,
KeyRound,
Settings,
+ Binary,
} from 'lucide-react'
interface NavGroup {
@@ -332,6 +333,16 @@ export const navigation = [
title: 'Running Processes',
href: '/playgrounds/process',
},
+ {
+ icon: (
+ <Binary
+ strokeWidth={1}
+ size={20}
+ />
+ ),
+ title: 'Executing Code',
+ href: '/playgrounds/execute',
+ },
// {
// icon: (
// <FileUp
@@ -377,7 +388,7 @@ export const navigation = [
{
title: 'AI Agents Use Case',
links: [
- { title: 'Execute Code', href: '/agents/exec' },
+ // { title: 'Execute Code', href: '/agents/exec' },
// { title: 'Installing Dependencies', href: '/agents/deps' },
{ title: 'Clone GitHub Repository', href: '/agents/clone-repo' },
{ title: 'Run Shell Commands', href: '/agents/shell-commands' },
diff --git a/package-lock.json b/package-lock.json
index 4bee441f8..f8ca72d22 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -222,6 +222,7 @@
"version": "0.1.0",
"dependencies": {
"@algolia/autocomplete-core": "^1.7.3",
+ "@e2b/sdk": "^0.3.2",
"@headlessui/react": "^1.7.15",
"@headlessui/tailwindcss": "^0.2.0",
"@mdx-js/loader": "^2.1.5",
@@ -272,7 +273,6 @@
"zustand": "^4.3.2"
},
"devDependencies": {
- "@e2b/sdk": "^0.3.0",
"@nodelib/fs.walk": "^2.0.0",
"encoding": "^0.1.13",
"eslint-config-next": "13.4.19",
@@ -1975,20 +1975,6 @@
"license": "MIT",
"peer": true
},
- "apps/docs/node_modules/bufferutil": {
- "version": "4.0.7",
- "dev": true,
- "hasInstallScript": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "dependencies": {
- "node-gyp-build": "^4.3.0"
- },
- "engines": {
- "node": ">=6.14.2"
- }
- },
"apps/docs/node_modules/busboy": {
"version": "1.6.0",
"dependencies": {
@@ -2976,14 +2962,6 @@
"license": "ISC",
"peer": true
},
- "apps/docs/node_modules/isomorphic-ws": {
- "version": "4.0.1",
- "dev": true,
- "license": "MIT",
- "peerDependencies": {
- "ws": "*"
- }
- },
"apps/docs/node_modules/jest-worker": {
"version": "27.5.1",
"license": "MIT",
@@ -4293,26 +4271,6 @@
}
}
},
- "apps/docs/node_modules/node-gyp-build": {
- "version": "4.6.0",
- "dev": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "bin": {
- "node-gyp-build": "bin.js",
- "node-gyp-build-optional": "optional.js",
- "node-gyp-build-test": "build-test.js"
- }
- },
- "apps/docs/node_modules/normalize-path": {
- "version": "3.0.0",
- "dev": true,
- "license": "MIT",
- "engines": {
- "node": ">=0.10.0"
- }
- },
"apps/docs/node_modules/once": {
"version": "1.4.0",
"dev": true,
@@ -4321,15 +4279,6 @@
"wrappy": "1"
}
},
- "apps/docs/node_modules/openapi-typescript-fetch": {
- "version": "1.1.3",
- "dev": true,
- "license": "MIT",
- "engines": {
- "node": ">= 12.0.0",
- "npm": ">= 7.0.0"
- }
- },
"apps/docs/node_modules/optionator": {
"version": "0.9.3",
"dev": true,
@@ -4464,11 +4413,6 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
- "apps/docs/node_modules/platform": {
- "version": "1.3.6",
- "dev": true,
- "license": "MIT"
- },
"apps/docs/node_modules/prelude-ls": {
"version": "1.2.1",
"dev": true,
@@ -4712,19 +4656,6 @@
"url": "https://github.com/sponsors/isaacs"
}
},
- "apps/docs/node_modules/rpc-websocket-client": {
- "version": "1.1.4",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "isomorphic-ws": "^4.0.1",
- "uuid": "^3.3.3",
- "ws": "^7.1.2"
- },
- "engines": {
- "node": ">=6.0.0"
- }
- },
"apps/docs/node_modules/run-parallel": {
"version": "1.2.0",
"funding": [
@@ -5294,28 +5225,6 @@
}
}
},
- "apps/docs/node_modules/utf-8-validate": {
- "version": "5.0.10",
- "dev": true,
- "hasInstallScript": true,
- "license": "MIT",
- "optional": true,
- "peer": true,
- "dependencies": {
- "node-gyp-build": "^4.3.0"
- },
- "engines": {
- "node": ">=6.14.2"
- }
- },
- "apps/docs/node_modules/uuid": {
- "version": "3.4.0",
- "dev": true,
- "license": "MIT",
- "bin": {
- "uuid": "bin/uuid"
- }
- },
"apps/docs/node_modules/uvu": {
"version": "0.5.6",
"license": "MIT",
@@ -5475,26 +5384,6 @@
"dev": true,
"license": "ISC"
},
- "apps/docs/node_modules/ws": {
- "version": "7.5.9",
- "dev": true,
- "license": "MIT",
- "engines": {
- "node": ">=8.3.0"
- },
- "peerDependencies": {
- "bufferutil": "^4.0.1",
- "utf-8-validate": "^5.0.2"
- },
- "peerDependenciesMeta": {
- "bufferutil": {
- "optional": true
- },
- "utf-8-validate": {
- "optional": true
- }
- }
- },
"apps/docs/node_modules/yallist": {
"version": "4.0.0",
"dev": true,
diff --git a/packages/python-sdk/e2b/session/run_code.py b/packages/python-sdk/e2b/session/run_code.py
index 16c2f51b3..b08d8126a 100644
--- a/packages/python-sdk/e2b/session/run_code.py
+++ b/packages/python-sdk/e2b/session/run_code.py
@@ -36,7 +36,7 @@ async def run_code(
binary = "python3"
filepath = "/index.py"
else:
- raise Exception(f'Invalid runtime "{runtime}". Please contactus if you need support for this runtime')
+ raise Exception(f'Invalid runtime "{runtime}". Please contact us ([email protected]) if you need support for this runtime')
session = await Session.create(
id=env_id,