Skip to content

armgit / E2B

Public Imported from GitHub
0

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

updated "Connect LLMs to E2B" guide

Mish Ushakov1c2c7ee53a83
diff --git a/apps/web/src/app/(docs)/docs/quickstart/connect-llms/page.mdx b/apps/web/src/app/(docs)/docs/quickstart/connect-llms/page.mdx
index 65dc60696..097911547 100644
--- a/apps/web/src/app/(docs)/docs/quickstart/connect-llms/page.mdx
+++ b/apps/web/src/app/(docs)/docs/quickstart/connect-llms/page.mdx
@@ -11,16 +11,14 @@ If the LLM doesn't support tool use, you can, for example, prompt the LLM to out
- [CrewAI](#crewai)
- [LangChain](#langchain)
- [LlamaIndex](#llamaindex)
+- [Ollama](#ollama)
---
## OpenAI
-{/* You can use E2B with all the OpenAI's langauge models. */}
-{/* ### o1 family
-TODO */}
+### Simple
-{/* ### GPT-4 family & GPT-3.5 family */}
<CodeGroup>
```python
# pip install openai e2b-code-interpreter
@@ -29,8 +27,52 @@ from e2b_code_interpreter import Sandbox
# Create OpenAI client
client = OpenAI()
+system = "You are a helpful assistant that can execute python code in a Jupyter notebook. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
prompt = "Calculate how many r's are in the word 'strawberry'"
+# Send messages to OpenAI API
+response = client.chat.completions.create(
+ model="gpt-4o",
+ messages=[
+ {"role": "system", "content": system},
+ {"role": "user", "content": prompt}
+ ]
+)
+
+# Extract the code from the response
+code = response.choices[0].message.content
+
+# Execute code in E2B Sandbox
+if code:
+ with Sandbox() as sandbox:
+ execution = sandbox.run_code(code)
+ result = execution.text
+
+ print(result)
+```
+</CodeGroup>
+
+### Function calling
+
+<CodeGroup>
+```python
+# pip install openai e2b-code-interpreter
+import json
+from openai import OpenAI
+from e2b_code_interpreter import Sandbox
+
+# Create OpenAI client
+client = OpenAI()
+model = "gpt-4o"
+
+# Define the messages
+messages = [
+ {
+ "role": "user",
+ "content": "Calculate how many r's are in the word 'strawberry'"
+ }
+]
+
# Define the tools
tools = [{
"type": "function",
@@ -52,37 +94,49 @@ tools = [{
# Generate text with OpenAI
response = client.chat.completions.create(
- model="gpt-4o",
- messages=[{"role": "user", "content": prompt}],
+ model=model,
+ messages=messages,
tools=tools,
)
-# Execute the tool if it's called by the model
-if response.choices[0].message.tool_calls:
- tool_call = response.choices[0].message.tool_calls[0]
- if tool_call.function.name == "execute_python":
- # Create a sandbox and execute the code
- with Sandbox() as sandbox:
- code = tool_call.function.arguments
- execution = sandbox.run_code(code)
- result = execution.text
+# Append the response message to the messages list
+response_message = response.choices[0].message
+messages.append(response_message)
- # Send the result back to the model
- response = client.chat.completions.create(
- model="gpt-4o",
- messages=[
- {"role": "user", "content": prompt},
- {"role": "function", "name": "execute_python", "content": result}
- ]
- )
+# Execute the tool if it's called by the model
+if response_message.tool_calls:
+ for tool_call in response_message.tool_calls:
+ if tool_call.function.name == "execute_python":
+ # Create a sandbox and execute the code
+ with Sandbox() as sandbox:
+ code = json.loads(tool_call.function.arguments)['code']
+ execution = sandbox.run_code(code)
+ result = execution.text
+
+ # Send the result back to the model
+ messages.append({
+ "role": "tool",
+ "name": "execute_python",
+ "content": result,
+ "tool_call_id": tool_call.id,
+ })
+
+# Generate the final response
+final_response = client.chat.completions.create(
+ model=model,
+ messages=messages
+)
-print(response.choices[0].message.content)
+print(final_response.choices[0].message.content)
```
</CodeGroup>
---
## Anthropic
+
+### Simple
+
<CodeGroup>
```python
# pip install anthropic e2b-code-interpreter
@@ -116,9 +170,98 @@ print(result)
```
</CodeGroup>
+### Function calling
+
+<CodeGroup>
+```python
+# pip install anthropic e2b-code-interpreter
+from anthropic import Anthropic
+from e2b_code_interpreter import Sandbox
+
+# Create Anthropic client
+client = Anthropic()
+model = "claude-3-5-sonnet-20240620"
+
+# Define the messages
+messages = [
+ {
+ "role": "user",
+ "content": "Calculate how many r's are in the word 'strawberry'"
+ }
+]
+
+# Define the tools
+tools = [{
+ "name": "execute_python",
+ "description": "Execute python code in a Jupyter notebook cell and return (not print) the result",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "string",
+ "description": "The python code to execute in a single cell"
+ }
+ },
+ "required": ["code"]
+ }
+}]
+
+# Generate text with Anthropic
+message = client.messages.create(
+ model=model,
+ max_tokens=1024,
+ messages=messages,
+ tools=tools
+)
+
+# Append the response message to the messages list
+messages.append({
+ "role": "assistant",
+ "content": message.content
+})
+
+# Execute the tool if it's called by the model
+if message.stop_reason == "tool_use":
+ tool_use = next(block for block in message.content if block.type == "tool_use")
+ tool_name = tool_use.name
+ tool_input = tool_use.input
+
+ if tool_name == "execute_python":
+ with Sandbox() as sandbox:
+ code = tool_input['code']
+ execution = sandbox.run_code(code)
+ result = execution.text
+
+ # Append the tool result to the messages list
+ messages.append({
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": tool_use.id,
+ "content": result,
+ }
+ ],
+ })
+
+# Generate the final response
+final_response = client.messages.create(
+ model=model,
+ max_tokens=1024,
+ messages=messages,
+ tools=tools
+)
+
+print(final_response.content[0].text)
+```
+</CodeGroup>
+
---
## Mistral
+
+### Simple
+
<CodeGroup>
```python
# pip install mistralai e2b-code-interpreter
@@ -126,10 +269,8 @@ import os
from mistralai import Mistral
from e2b_code_interpreter import Sandbox
-api_key = os.environ["MISTRAL_API_KEY"]
-
# Create Mistral client
-client = Mistral(api_key=api_key)
+client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
system_prompt = "You are a helpful assistant that can execute python code in a Jupyter notebook. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
prompt = "Calculate how many r's are in the word 'strawberry'"
@@ -154,11 +295,120 @@ print(result)
```
</CodeGroup>
+### Function calling
+
+<CodeGroup>
+```python
+# pip install mistralai e2b-code-interpreter
+import os
+import json
+from mistralai import Mistral
+from e2b_code_interpreter import Sandbox
+
+# Create Mistral client
+client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
+model = "mistral-large-latest"
+messages = [
+ {
+ "role": "user",
+ "content": "Calculate how many r's are in the word 'strawberry'"
+ }
+]
+
+# Define the tools
+tools = [{
+ "type": "function",
+ "function": {
+ "name": "execute_python",
+ "description": "Execute python code in a Jupyter notebook cell and return result",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "string",
+ "description": "The python code to execute in a single cell"
+ }
+ },
+ "required": ["code"]
+ }
+ }
+}]
+
+# Send the prompt to the model
+response = client.chat.complete(
+ model=model,
+ messages=messages,
+ tools=tools
+)
+
+# Append the response message to the messages list
+response_message = response.choices[0].message
+messages.append(response_message)
+
+# Execute the tool if it's called by the model
+if response_message.tool_calls:
+ for tool_call in response_message.tool_calls:
+ if tool_call.function.name == "execute_python":
+ # Create a sandbox and execute the code
+ with Sandbox() as sandbox:
+ code = json.loads(tool_call.function.arguments)['code']
+ execution = sandbox.run_code(code)
+ result = execution.text
+
+ # Send the result back to the model
+ messages.append({
+ "role": "tool",
+ "name": "execute_python",
+ "content": result,
+ "tool_call_id": tool_call.id,
+ })
+
+# Generate the final response
+final_response = client.chat.complete(
+ model=model,
+ messages=messages,
+)
+
+print(final_response.choices[0].message.content)
+```
+</CodeGroup>
+
---
## Vercel AI SDK
Vercel's [AI SDK](https://sdk.vercel.ai) offers support for multiple different LLM providers through a unified JavaScript interface that's easy to use.
+### Simple
+
+<CodeGroup>
+```js
+// npm install ai @ai-sdk/openai @e2b/code-interpreter
+import { openai } from '@ai-sdk/openai'
+import { generateText } from 'ai'
+import { Sandbox } from '@e2b/code-interpreter'
+
+// Create OpenAI client
+const model = openai('gpt-4o')
+const system = "You are a helpful assistant that can execute python code in a Jupyter notebook. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
+const prompt = "Calculate how many r's are in the word 'strawberry'"
+
+// Generate code with OpenAI
+const { text: code } = await generateText({
+ model,
+ system,
+ prompt
+})
+
+// Run the code in E2B Sandbox
+const sandbox = await Sandbox.create()
+const { text, results, logs, error } = await sandbox.runCode(code)
+
+console.log(text)
+```
+</CodeGroup>
+
+### Function calling
+
<CodeGroup>
```js
// npm install ai @ai-sdk/openai zod @e2b/code-interpreter
@@ -178,7 +428,7 @@ const { text } = await generateText({
prompt,
tools: {
// Define a tool that runs code in a sandbox
- codeInterpreter: {
+ execute_python: {
description: 'Execute python code in a Jupyter notebook cell and return result',
parameters: z.object({
code: z.string().describe('The python code to execute in a single cell'),
@@ -253,6 +503,9 @@ print(result)
## LangChain
[LangChain](https://langchain.com/) offers support multiple different LLM providers.
+
+### Simple
+
<CodeGroup>
```python
# pip install langchain langchain-openai e2b-code-interpreter
@@ -288,12 +541,106 @@ print(result)
```
</CodeGroup>
+### Agent
+
+<CodeGroup>
+```python
+# pip install langchain langchain-openai e2b-code-interpreter
+from langchain_core.prompts import ChatPromptTemplate
+from langchain_core.tools import tool
+from langchain.agents import create_tool_calling_agent, AgentExecutor
+from langchain_openai import ChatOpenAI
+from e2b_code_interpreter import Sandbox
+
+system_prompt = "You are a helpful assistant that can execute python code in a Jupyter notebook. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
+prompt = "Calculate how many r's are in the word 'strawberry'"
+
+# Define the tool
+@tool
+def execute_python(code: str):
+ """
+ Execute python code in a Jupyter notebook.
+ """
+ with Sandbox() as sandbox:
+ execution = sandbox.run_code(code)
+ return execution.text
+
+# Define LangChain components
+prompt_template = ChatPromptTemplate.from_messages([
+ ("system", system_prompt),
+ ("human", "{input}"),
+ ("placeholder", "{agent_scratchpad}"),
+])
+
+tools = [execute_python]
+llm = ChatOpenAI(model="gpt-4o", temperature=0)
+
+agent = create_tool_calling_agent(llm, tools, prompt_template)
+agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
+
+# Run the agent
+agent_executor.invoke({"input": prompt})
+```
+</CodeGroup>
+
+### Function calling
+
+<CodeGroup>
+```python
+# pip install langchain langchain-openai e2b-code-interpreter
+from langchain_openai import ChatOpenAI
+from langchain.tools import Tool
+from langchain.schema import HumanMessage, AIMessage, FunctionMessage
+from e2b_code_interpreter import Sandbox
+
+def execute_python(code: str):
+ with Sandbox() as sandbox:
+ execution = sandbox.run_code(code)
+ return execution.text
+
+# Define a tool that uses the E2B Sandbox
+e2b_sandbox_tool = Tool(
+ name="execute_python",
+ func=execute_python,
+ description="Execute python code in a Jupyter notebook cell and return result"
+)
+
+# Initialize the language model and bind the tool
+llm = ChatOpenAI(model="gpt-4o").bind_tools([e2b_sandbox_tool])
+
+# Define the messages
+messages = [
+ HumanMessage(content="Calculate how many 'r's are in the word 'strawberry'.")
+]
+
+# Run the model with a prompt
+result = llm.invoke(messages)
+messages.append(AIMessage(content=result.content))
+
+# Check if the model called the tool
+if result.additional_kwargs.get('tool_calls'):
+ tool_call = result.additional_kwargs['tool_calls'][0]
+ if tool_call['function']['name'] == "execute_python":
+ code = tool_call['function']['arguments']
+ execution_result = execute_python(code)
+
+ # Send the result back to the model
+ messages.append(
+ FunctionMessage(name="execute_python", content=execution_result)
+ )
+
+final_result = llm.invoke(messages)
+print(final_result.content)
+```
+</CodeGroup>
+
---
## LlamaIndex
[LlamaIndex](https://www.llamaindex.ai/) offers support multiple different LLM providers.
<CodeGroup>
```python
+# pip install llama-index e2b-code-interpreter
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import ReActAgent
@@ -305,7 +652,7 @@ def execute_python(code: str):
execution = sandbox.run_code(code)
return execution.text
-e2b_interpreter_tool = FunctionTool.from_defaults(
+e2b_sandbox_tool = FunctionTool.from_defaults(
name="execute_python",
description="Execute python code in a Jupyter notebook cell and return result",
fn=execute_python
@@ -315,8 +662,40 @@ e2b_interpreter_tool = FunctionTool.from_defaults(
llm = OpenAI(model="gpt-4o")
# Initialize ReAct agent
-agent = ReActAgent.from_tools([e2b_interpreter_tool], llm=llm, verbose=True)
+agent = ReActAgent.from_tools([e2b_sandbox_tool], llm=llm, verbose=True)
agent.chat("Calculate how many r's are in the word 'strawberry'")
```
</CodeGroup>
+## Ollama
+
+<CodeGroup>
+```python
+# pip install ollama
+import ollama
+from e2b_code_interpreter import Sandbox
+
+# Send the prompt to the model
+response = ollama.chat(
+ model="llama3.2",
+ messages=[{
+ "role": "system",
+ "content": "You are a helpful assistant that can execute python code in a Jupyter notebook. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
+ },
+ {
+ "role": "user",
+ "content": "Calculate how many r's are in the word 'strawberry'"
+ }
+])
+
+# Extract the code from the response
+code = response['message']['content']
+
+# Execute code in E2B Sandbox
+with Sandbox() as sandbox:
+ execution = sandbox.run_code(code)
+ result = execution.logs.stdout
+
+print(result)
+```
+</CodeGroup>