Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix
Vasek Mlejnskydb3c827f7728
diff --git a/apps/docs/src/app/llm-platforms/openai/js.mdx b/apps/docs/src/app/llm-platforms/openai/js.mdxindex 45a7783d3..e69de29bb 100644--- a/apps/docs/src/app/llm-platforms/openai/js.mdx+++ b/apps/docs/src/app/llm-platforms/openai/js.mdx@@ -1,196 +0,0 @@--## JavaScript-<Note title="Source Code">-See the full JavaScript code in our [cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main/guides/openai-assistant-js).-</Note>--### `sandbox.addAction()`-In JavaScript, we will use the `sandbox.addAction()` method to register a JavaScript function as a runnable action for the AI assistant and the LLM.--The JavaScript function you register will always receive the `sandbox` instance as the first argument.-This allows you to define your actions in separate files.--<Note title="Source Code">-[TODO: Fix link] See the full Python code in our [cookbook]().-</Note>--### 1. Import E2B and create a sandbox--<CodeGroup isFileName title="index.ts" isRunnable={false}>-```js-import OpenAI from 'openai'-import { Sandbox } from '@e2b/sdk' // $HighlightLine--const openai = new OpenAI()-const sandbox = await Sandbox.create({ // $HighlightLine- onStdout: console.log, // $HighlightLine- onStderr: console.error, // $HighlightLine-}) // $HighlightLine-```-</CodeGroup>--### 2. Define the assistant's actions-An assistant action is a regular JavaScript function that takes the sandbox object as the first argument and an object with arguments as the second. The assistant will be able to call this function to complete its task when it runs.-The action can do pretty much anything you want - running code, downloading files, analyzing data, for example running a terminal command.--We define three actions:-- `saveCodeToFile`- - Takes `code` and `filename` as arguments and saves the code to a file inside the sandbox.-- `listFiles`- - Returns a list of files on a given `path`.-- `readFile`- - Reads a file on the `path` and returns the content of this file.--<CodeGroup isFileName title="actions.ts" isRunnable={false}>-```js-import { Sandbox } from '@e2b/sdk'-import path from 'path'--export async function saveCodeToFile(- sandbox: Sandbox,- { code, filename }: { code: string; filename: string },-): Promise<string> {- try {- const dir = path.dirname(filename)-- await sandbox.filesystem.makeDir(dir)- await sandbox.filesystem.write(filename, code)-- return 'success'- } catch (e) {- return `Error: ${e.message}}`- }-}--export async function listFiles(sandbox: Sandbox, { path }: { path: string }): Promise<string> {- try {- const files = await sandbox.filesystem.list(path)- const response = files.map(file => (file.isDir ? `dir: ${file.name}` : file.name)).join('\n')- return response- } catch (e) {- return `Error: ${e.message}}`- }-}--export async function readFile(sandbox: Sandbox, { path }: { path: string }): Promise<string> {- try {- return await sandbox.filesystem.read(path)- } catch (e) {- return `Error: ${e.message}}`- }-}-```-</CodeGroup>--### 3. Add action to the sandbox-Now we need to make sure the sandbox knows about the actions we defined above. We'll use the `addAction` method.--<CodeGroup isFileName title="index.ts" isRunnable={false}>-```js-// Import our action-import { listFiles, readFile, saveCodeToFile } from './actions'--// Add action to the sandbox we created in the previous steps-sandbox- .addAction(readFile)- .addAction(saveCodeToFile)- .addAction(listFiles)-```-</CodeGroup>--### 4. Create AI Assistant-We'll use the OpenAI API to create a new AI Assistant. You can read the full code [here](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/assistant.ts).--Keep a close attention to the `functions` definition inside the [`assistant.ts`](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/assistant.ts#L7-L67) file. We're using the [Function calling](https://platform.openai.com/docs/assistants/tools/function-calling) feature to give our assistant an ability to call the sandbox actions we defined.--<CodeGroup isFileName title="assistant.ts" isRunnable={false}>-```js-import { AssistantCreateParams } from 'openai/src/resources/beta/assistants/assistants'--export const functions: Array<- | AssistantCreateParams.AssistantToolsCode- | AssistantCreateParams.AssistantToolsRetrieval- | AssistantCreateParams.AssistantToolsFunction-> = [- // Save code to file- {- type: 'function',- function: {- name: 'saveCodeToFile', // $HighlightLine- description: 'Save code to file',- parameters: {- type: 'object',- properties: {- code: { // $HighlightLine- type: 'string',- description: 'The code to save',- },- filename: { // $HighlightLine- type: 'string',- description: 'The filename including the path and extension',- },- },- },- },- },- // ... rest of the functions- ]--// ... rest of the file-```-</CodeGroup>--- **The `name` field is the same name as the name of the sandbox action we created in the previous step.**-- **The parameters `code` and `filename` are the parameters passed to the sandbox action as a part of the `args` dictionary.**---### 5. Let the assistant run our actions-<Note title="How Assistants Work">-Read the official [OpenAI's assistants documentation](https://platform.openai.com/docs/assistants/how-it-works).-</Note>--Once the assistant is created, we use the OpenAI API to retrieve it, create a thread, send messages to the thread, and finally run the thread.--When the thread is running, we get a `run` object. The `run.status` field tells us if our assistant requires to run any action.--If the `run.status` indicates that the assistant wants to call actions, we just pass the `run` object to the `sandbox.openai.actions.run()` function.-The E2B sandbox will automatically call the correct actions and return correctly formatted output object (called `outputs` in the code below) that you can submit back to the assistant.--<CodeGroup isFileName title="index.ts" isRunnable={false}>-```js-// ... rest of the logic--const assistant = await openai.beta.assistants.retrieve(AI_ASSISTANT_ID)-let run = await openai.beta.threads.runs.create(thread.id, { assistant_id: assistant.id })--// Start polling the run object-assistantLoop: while (true) {- await sleep(1000)-- switch (run.status) {- case 'requires_action': { // $HighlightLine- const outputs = await sandbox.openai.actions.run(run) // $HighlightLine-- if (outputs.length > 0) { // $HighlightLine- await openai.beta.threads.runs.submitToolOutputs(thread.id, run.id, { // $HighlightLine- tool_outputs: outputs, // $HighlightLine- }) // $HighlightLine- } // $HighlightLine-- break- }- // ... handle rest of the `run` states- default:- console.error(`Unknown status: ${run.status}`)- break assistantLoop- }-- run = await openai.beta.threads.runs.retrieve(thread.id, run.id)-}--// Close the sandbox once everything is done-await sandbox.close()-```-</CodeGroup>--See the full code [here](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/index.ts).\ No newline at end of filediff --git a/apps/docs/src/app/llm-platforms/openai/page.mdx b/apps/docs/src/app/llm-platforms/openai/page.mdxindex 67702334d..7bd411ecc 100644--- a/apps/docs/src/app/llm-platforms/openai/page.mdx+++ b/apps/docs/src/app/llm-platforms/openai/page.mdx@@ -3,7 +3,8 @@E2B offers a native support for OpenAI's [Assistants API](https://platform.openai.com/docs/assistants/overview). You can use the E2B's Sandbox as an environment for your assistants's tool execution.<Note title="What's OpenAI's Assistants API?">-The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries.+The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries. The Assistants API currently supports three types of tools: Code Interpreter, Retrieval, and Function calling.+</Note>The specific feature of the OpenAI's Assistants API that we'll take advantage of is the [Function calling](https://platform.openai.com/docs/assistants/tools/function-calling).@@ -23,7 +24,7 @@ The Python function you register will always receive the `sandbox` instance as tThis allows you to define your actions in separate files.<Note title="Source Code">-[TODO: Fix link] See the full Python code in our [cookbook]().+See the full Python code in our [cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main/guides/openai-assistant-py).</Note>### 1. Import E2B and create a sandbox@@ -185,4 +186,200 @@ sandbox.close()See the full code [here](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-py/openai_assistant/main.py).----\ No newline at end of file+---++## JavaScript+<Note title="Source Code">+See the full JavaScript code in our [cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main/guides/openai-assistant-js).+</Note>++### `sandbox.addAction()`+In JavaScript, we will use the `sandbox.addAction()` method to register a JavaScript function as a runnable action for the AI assistant and the LLM.++The JavaScript function you register will always receive the `sandbox` instance as the first argument.+This allows you to define your actions in separate files.++<Note title="Source Code">+[TODO: Fix link] See the full Python code in our [cookbook]().+</Note>++### 1. Import E2B and create a sandbox++<CodeGroup isFileName title="index.ts" isRunnable={false}>+```js+import OpenAI from 'openai'+import { Sandbox } from '@e2b/sdk' // $HighlightLine++const openai = new OpenAI()+const sandbox = await Sandbox.create({ // $HighlightLine+ onStdout: console.log, // $HighlightLine+ onStderr: console.error, // $HighlightLine+}) // $HighlightLine+```+</CodeGroup>++### 2. Define the assistant's actions+An assistant action is a regular JavaScript function that takes the sandbox object as the first argument and an object with arguments as the second. The assistant will be able to call this function to complete its task when it runs.+The action can do pretty much anything you want - running code, downloading files, analyzing data, for example running a terminal command.++We define three actions:+- `saveCodeToFile`+ - Takes `code` and `filename` as arguments and saves the code to a file inside the sandbox.+- `listFiles`+ - Returns a list of files on a given `path`.+- `readFile`+ - Reads a file on the `path` and returns the content of this file.++<CodeGroup isFileName title="actions.ts" isRunnable={false}>+```js+import { Sandbox } from '@e2b/sdk'+import path from 'path'++export async function saveCodeToFile(+ sandbox: Sandbox,+ { code, filename }: { code: string; filename: string },+): Promise<string> {+ try {+ const dir = path.dirname(filename)++ await sandbox.filesystem.makeDir(dir)+ await sandbox.filesystem.write(filename, code)++ return 'success'+ } catch (e) {+ return `Error: ${e.message}}`+ }+}++export async function listFiles(sandbox: Sandbox, { path }: { path: string }): Promise<string> {+ try {+ const files = await sandbox.filesystem.list(path)+ const response = files.map(file => (file.isDir ? `dir: ${file.name}` : file.name)).join('\n')+ return response+ } catch (e) {+ return `Error: ${e.message}}`+ }+}++export async function readFile(sandbox: Sandbox, { path }: { path: string }): Promise<string> {+ try {+ return await sandbox.filesystem.read(path)+ } catch (e) {+ return `Error: ${e.message}}`+ }+}+```+</CodeGroup>++### 3. Add action to the sandbox+Now we need to make sure the sandbox knows about the actions we defined above. We'll use the `addAction` method.++<CodeGroup isFileName title="index.ts" isRunnable={false}>+```js+// Import our action+import { listFiles, readFile, saveCodeToFile } from './actions'++// Add action to the sandbox we created in the previous steps+sandbox+ .addAction(readFile)+ .addAction(saveCodeToFile)+ .addAction(listFiles)+```+</CodeGroup>++### 4. Create AI Assistant+We'll use the OpenAI API to create a new AI Assistant. You can read the full code [here](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/assistant.ts).++Keep a close attention to the `functions` definition inside the [`assistant.ts`](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/assistant.ts#L7-L67) file. We're using the [Function calling](https://platform.openai.com/docs/assistants/tools/function-calling) feature to give our assistant an ability to call the sandbox actions we defined.++<CodeGroup isFileName title="assistant.ts" isRunnable={false}>+```js+import { AssistantCreateParams } from 'openai/src/resources/beta/assistants/assistants'++export const functions: Array<+ | AssistantCreateParams.AssistantToolsCode+ | AssistantCreateParams.AssistantToolsRetrieval+ | AssistantCreateParams.AssistantToolsFunction+> = [+ // Save code to file+ {+ type: 'function',+ function: {+ name: 'saveCodeToFile', // $HighlightLine+ description: 'Save code to file',+ parameters: {+ type: 'object',+ properties: {+ code: { // $HighlightLine+ type: 'string',+ description: 'The code to save',+ },+ filename: { // $HighlightLine+ type: 'string',+ description: 'The filename including the path and extension',+ },+ },+ },+ },+ },+ // ... rest of the functions+ ]++// ... rest of the file+```+</CodeGroup>++- **The `name` field is the same name as the name of the sandbox action we created in the previous step.**+- **The parameters `code` and `filename` are the parameters passed to the sandbox action as a part of the `args` dictionary.**+++### 5. Let the assistant run our actions+<Note title="How Assistants Work">+Read the official [OpenAI's assistants documentation](https://platform.openai.com/docs/assistants/how-it-works).+</Note>++Once the assistant is created, we use the OpenAI API to retrieve it, create a thread, send messages to the thread, and finally run the thread.++When the thread is running, we get a `run` object. The `run.status` field tells us if our assistant requires to run any action.++If the `run.status` indicates that the assistant wants to call actions, we just pass the `run` object to the `sandbox.openai.actions.run()` function.+The E2B sandbox will automatically call the correct actions and return correctly formatted output object (called `outputs` in the code below) that you can submit back to the assistant.++<CodeGroup isFileName title="index.ts" isRunnable={false}>+```js+// ... rest of the logic++const assistant = await openai.beta.assistants.retrieve(AI_ASSISTANT_ID)+let run = await openai.beta.threads.runs.create(thread.id, { assistant_id: assistant.id })++// Start polling the run object+assistantLoop: while (true) {+ await sleep(1000)++ switch (run.status) {+ case 'requires_action': { // $HighlightLine+ const outputs = await sandbox.openai.actions.run(run) // $HighlightLine++ if (outputs.length > 0) { // $HighlightLine+ await openai.beta.threads.runs.submitToolOutputs(thread.id, run.id, { // $HighlightLine+ tool_outputs: outputs, // $HighlightLine+ }) // $HighlightLine+ } // $HighlightLine++ break+ }+ // ... handle rest of the `run` states+ default:+ console.error(`Unknown status: ${run.status}`)+ break assistantLoop+ }++ run = await openai.beta.threads.runs.retrieve(thread.id, run.id)+}++// Close the sandbox once everything is done+await sandbox.close()+```+</CodeGroup>++See the full code [here](https://github.com/e2b-dev/e2b-cookbook/blob/main/guides/openai-assistant-js/index.ts).\ No newline at end of file