Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(docs): Consistent order for JS & PY examples in guide

Pavel 'Strajk' Dolecek57401b922150
diff --git a/apps/docs/src/app/guide/simple-gpt4-code-interpreter/page.mdx b/apps/docs/src/app/guide/simple-gpt4-code-interpreter/page.mdx
index 3d221cf4a..c78a73dcf 100644
--- a/apps/docs/src/app/guide/simple-gpt4-code-interpreter/page.mdx
+++ b/apps/docs/src/app/guide/simple-gpt4-code-interpreter/page.mdx
@@ -44,16 +44,15 @@ E2B allows you to execute the GPT-generated code in a sandboxed cloud environmen
First, install the required packages. We need to install the E2B package and OpenAI package.
<CodeGroup isRunnable={false}>
-```bash {{ language: 'python' }}
-pip install e2b
-pip install openai
-```
-
```bash {{ language: 'js' }}
npm i @e2b/sdk
npm i openai
```
+```bash {{ language: 'python' }}
+pip install e2b
+pip install openai
+```
</CodeGroup>
## Get API keys
@@ -68,6 +67,11 @@ Let's get our API keys for E2B and OpenAI. We'll need them to use the APIs.
Now we need to import the packages we installed in the previous step.
<CodeGroup isRunnable={false}>
+```js
+import e2b from '@e2b/sdk'
+import OpenAI from 'openai'
+```
+
```python
import e2b
import openai
@@ -76,12 +80,6 @@ import openai
import asyncio
import json
```
-
-```js
-import e2b from '@e2b/sdk'
-import OpenAI from 'openai'
-```
-
</CodeGroup>
## Make calls to GPT
@@ -89,15 +87,6 @@ import OpenAI from 'openai'
Here we set up the OpenAI chat completion endpoint that allows to call the GPT-4 (or GPT-3.5) models.
<CodeGroup isRunnable={false}>
-```python
-response = openai.ChatCompletion.create(
- model="gpt-4", # Or use gpt-3.5-turbo
- messages=[
- {"role": "system", "content": "You are a senior developer that can code in JavaScript. Always produce valid JSON."},
- ]
-)
-```
-
```js
const openai = new OpenAI()
const chatCompletion = await openai.chat.completions.create({
@@ -106,11 +95,22 @@ const chatCompletion = await openai.chat.completions.create({
{
role: 'system',
content: 'You are a senior developer that can code in JavaScript. Always produce valid JSON.',
- },
+ }
],
})
```
+```python
+response = openai.ChatCompletion.create(
+ model="gpt-4", # Or use gpt-3.5-turbo
+ messages=[
+ {
+ "role": "system",
+ "content": "You are a senior developer that can code in JavaScript. Always produce valid JSON."
+ },
+ ]
+)
+```
</CodeGroup>
If you print the response, you should see something like this:
@@ -144,25 +144,6 @@ If you print the response, you should see something like this:
We're going to take an advantage of the [OpenAI functions](https://openai.com/blog/function-calling-and-other-api-updates). Defining these functions will make it easier instructing the model that it can write JavaScript code to complete request from the user. We'll execute this code later using E2B.
<CodeGroup isRunnable={false}>
-```python
-functions = [
- {
- "name": "exec_code",
- "description": "Executes the passed JavaScript code using Nodejs and returns the stdout and stderr.",
- "parameters": {
- "type": "object",
- "properties": {
- "code": {
- "type": "string",
- "description": "The JavaScript code to execute.",
- },
- },
- "required": ["code"],
- },
- }
-]
-```
-
```js
const functions = [
{
@@ -182,6 +163,24 @@ const functions = [
]
```
+```python
+functions = [
+ {
+ "name": "exec_code",
+ "description": "Executes the passed JavaScript code using Nodejs and returns the stdout and stderr.",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "string",
+ "description": "The JavaScript code to execute.",
+ },
+ },
+ "required": ["code"],
+ },
+ }
+]
+```
</CodeGroup>
We created an OpenAI function `exec_code` that expects a single parameter `code`. The `code` parameter will be the Python code generated by GPT that we'll execute.
@@ -190,19 +189,6 @@ Now we pass the `functions` variable to the GPT call we made earlier and also ad
The new code is marked by the highlighted lines.
<CodeGroup isRunnable={false}>
-```python
-response = openai.ChatCompletion.create(
- model="gpt-4",
- messages=[
- {"role": "system", "content": "You are a senior developer that can code in JavaScript. Always produce valid JSON."},
- {"role": "user", "content": "Write hello world"}, # $HighlightLine
- {"role": "assistant", "content": "print(\"hello world\")", "name":"exec_code"}, # $HighlightLine
- {"role": "user", "content": "Generate first 100 fibonacci numbers"}, # $HighlightLine
- ],
- functions=functions, # $HighlightLine
-)
-```
-
```js
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4',
@@ -229,6 +215,18 @@ const chatCompletion = await openai.chat.completions.create({
})
```
+```python
+response = openai.ChatCompletion.create(
+ model="gpt-4",
+ messages=[
+ {"role": "system", "content": "You are a senior developer that can code in JavaScript. Always produce valid JSON."},
+ {"role": "user", "content": "Write hello world"}, # $HighlightLine
+ {"role": "assistant", "content": "print(\"hello world\")", "name":"exec_code"}, # $HighlightLine
+ {"role": "user", "content": "Generate first 100 fibonacci numbers"}, # $HighlightLine
+ ],
+ functions=functions, # $HighlightLine
+)
+```
</CodeGroup>
If you print the GPT response now, you'll most likely see the model is calling the `exec_code` function we defined earlier and is passing code for generating fibonacci numbers in the first element of the `choices` JSON array.
@@ -259,29 +257,6 @@ If you print the GPT response now, you'll most likely see the model is calling t
We need to parse the GPT's response and detect if it contains a function call. If it does and it's actually the `exec_code` function, we need to parse the `arguments` field, get the `code` argument and actually run the code using E2B (we'll do it in the next step).
<CodeGroup isRunnable={false}>
-```python
-# ... previous code
-
-message = response["choices"][0]["message"]
-if (func := message.get("function_call")):
- func_name = func["name"]
-
- # Get rid of newlines and leading/trailing spaces in the raw function arguments JSON string.
- # This sometimes help to avoid JSON parsing errors.
- args = func["arguments"].strip().replace("\n", "")
- # Parse the cleaned up JSON string.
- func_args = json.loads(args)
-
- # If the model is calling the exec_code function we defined in the `functions` variable, we want to save the `code` argument to a variable.
- if func_name == "exec_code":
- code = func_args["code"]
- # TODO: Execute the code using E2B.
-else:
- # The model didn't call a function, so we just print the message.
- content = message["content"]
- print(content)
-```
-
```js
// ... previous code
@@ -309,7 +284,28 @@ if (func) {
}
```
+```python
+# ... previous code
+message = response["choices"][0]["message"]
+if (func := message.get("function_call")):
+ func_name = func["name"]
+
+ # Get rid of newlines and leading/trailing spaces in the raw function arguments JSON string.
+ # This sometimes help to avoid JSON parsing errors.
+ args = func["arguments"].strip().replace("\n", "")
+ # Parse the cleaned up JSON string.
+ func_args = json.loads(args)
+
+ # If the model is calling the exec_code function we defined in the `functions` variable, we want to save the `code` argument to a variable.
+ if func_name == "exec_code":
+ code = func_args["code"]
+ # TODO: Execute the code using E2B.
+else:
+ # The model didn't call a function, so we just print the message.
+ content = message["content"]
+ print(content)
+```
</CodeGroup>
@@ -322,19 +318,6 @@ All we need to add is just a single line of code. I hgihtlighted it in the code
<CodeGroup isRunnable={false}>
-```python
-# ... previous code
-
-# If the model is calling the exec_code function we defined in the `functions` variable, we want to save the `code` argument to a variable.
-if func_name == "exec_code":
- code = func_args["code"]
- stdout, stderr = await e2b.run_code("Node16", code) # $HighlightLine
- print(stdout) # $HighlightLine
- print(stderr) # $HighlightLine
-
-# ... rest of the code
-```
-
```js
// ... previous code
@@ -350,6 +333,18 @@ if (funcName === "exec_code") {
// ... rest of the code
```
+```python
+# ... previous code
+
+# If the model is calling the exec_code function we defined in the `functions` variable, we want to save the `code` argument to a variable.
+if func_name == "exec_code":
+ code = func_args["code"]
+ stdout, stderr = await e2b.run_code("Node16", code) # $HighlightLine
+ print(stdout) # $HighlightLine
+ print(stderr) # $HighlightLine
+
+# ... rest of the code
+```
</CodeGroup>
And this what the executed code for "Generate first 100 fibonacci numbers" prompt prints:
@@ -400,39 +395,6 @@ We're going to solve this problem by letting GPT install missing libraries by ad
This is how our new `functions` variable looks like. The highlighted lines are the new code.
<CodeGroup isRunnable={false}>
-```python
-functions = [
- {
- "name": "exec_code",
- "description": "Executes the passed JavaScript code using Nodejs and returns the stdout and stderr.",
- "parameters": {
- "type": "object",
- "properties": {
- "code": {
- "type": "string",
- "description": "The JavaScript code to execute.",
- },
- },
- "required": ["code"],
- },
- },
- { # $HighlightLine
- "name": "install_package", # $HighlightLine
- "description": "Installs the passed npm package.", # $HighlightLine
- "parameters": { # $HighlightLine
- "type": "object", # $HighlightLine
- "properties": { # $HighlightLine
- "name": { # $HighlightLine
- "type": "string", # $HighlightLine
- "description": "The name of an npm package to install.", # $HighlightLine
- }, # $HighlightLine
- }, # $HighlightLine
- "required": ["name"], # $HighlightLine
- }, # $HighlightLine
- } # $HighlightLine
-]
-```
-
```js
const functions = [
{
@@ -466,6 +428,38 @@ const functions = [
}, // $HighlightLine
];
```
+```python
+functions = [
+ {
+ "name": "exec_code",
+ "description": "Executes the passed JavaScript code using Nodejs and returns the stdout and stderr.",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "string",
+ "description": "The JavaScript code to execute.",
+ },
+ },
+ "required": ["code"],
+ },
+ },
+ { # $HighlightLine
+ "name": "install_package", # $HighlightLine
+ "description": "Installs the passed npm package.", # $HighlightLine
+ "parameters": { # $HighlightLine
+ "type": "object", # $HighlightLine
+ "properties": { # $HighlightLine
+ "name": { # $HighlightLine
+ "type": "string", # $HighlightLine
+ "description": "The name of an npm package to install.", # $HighlightLine
+ }, # $HighlightLine
+ }, # $HighlightLine
+ "required": ["name"], # $HighlightLine
+ }, # $HighlightLine
+ } # $HighlightLine
+]
+```
</CodeGroup>
Now we need to add a new `if` statement to our code that will detect if the model is calling the `install_package` function and if it does, we need to install the package using E2B.
@@ -482,4 +476,4 @@ TODO
## Final code
-You can find the final code for both Python and JavaScript in [this GitHub repository](https://github.com/e2b-dev/e2b-cookbook/tree/main). {{ className: 'lead' }}
\ No newline at end of file
+You can find the final code for both Python and JavaScript in [this GitHub repository](https://github.com/e2b-dev/e2b-cookbook/tree/main). {{ className: 'lead' }}