翻篇收录说明
翻篇网页快照

Connect LLMs to E2B - E2B Docs

https://docs.e2b.dev/quickstart/connect-llms

最近抓取:2026-12-04 18:16:54(北京时间)

最近内容变化:2026-12-02 15:45:39(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Connect any LLM to E2B sandboxes with tool use or function calling, with examples for OpenAI, Anthropic, Mistral, Groq, Vercel AI SDK, LangChain, and more.

正文摘录

E2B can work with any LLM and AI framework. The easiest way to connect an LLM to E2B is to use the tool use capabilities of the LLM (sometimes known as function calling). If the LLM doesn’t support tool use, you can, for example, prompt the LLM to output code snippets and then manually extract the code snippets with RegEx. Contents OpenAI Anthropic Mistral Groq Fireworks AI Vercel AI SDK CrewAI LangChain LlamaIndex Ollama Hugging Face OpenAI Simple Python# pip install openai e2b-code-interpreter from openai import OpenAI 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.create() as sandbox: execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) print(result) Function calling 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", "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"] } } }] # Generate text with OpenAI response = client.chat.completions.create( 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.create() as sandbox: code = json.loads(tool_call.function.arguments)['code'] execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) # 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(final_response.choices[0].message.content) Anthropic Simple Python# pip install anthropic e2b-code-interpreter from anthropic import Anthropic from e2b_code_interpreter import Sandbox # Create Anthropic client anthropic = Anthropic() 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'" # Send messages to Anthropic API response = anthropic.messages.create( model="claude-3-5-sonnet-20240620", max_tokens=1024, messages=[ {"role": "assistant", "content": system_prompt}, {"role": "user", "content": prompt} ] ) # Extract code from response code = response.content[0].text # Execute code in E2B Sandbox with Sandbox.create() as sandbox: execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) print(result) Function calling 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.create() as sandbox: code = tool_input['code'] execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) # 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) Mistral Simple Python# pip install mistralai e2b-code-interpreter import os from mistralai import Mistral from e2b_code_interpreter import Sandbox # Create Mistral client 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'" # Send the prompt to the model response = client.chat.complete( model="codestral-latest", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": prompt} ] ) # Extract the code from the response code = response.choices[0].message.content # Execute code in E2B Sandbox with Sandbox.create() as sandbox: execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) print(result) Function calling 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.create() as sandbox: code = json.loads(tool_call.function.arguments)['code'] execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) # 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) Groq Python # pip install groq e2b-code-interpreter import os from groq import Groq from e2b_code_interpreter import Sandbox api_key = os.environ["GROQ_API_KEY"] # Create Groq client client = Groq(api_key=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.'" # Send the prompt to the model response = client.chat.completions.create( model="llama3-70b-8192", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}, ] ) # Extract the code from the response code = response.choices[0].message.content # Execute code in E2B Sandbox with Sandbox.create() as sandbox: execution = sandbox.run_code(code) result = execution.text or "".join(execution.logs.stdout) print(result) Fireworks AI Fireworks AI serves open-source models through a fast inference API. The examples use the Fireworks Python SDK, which reads your key from the FIREWORKS_API_KEY environment variable. Any serverless model with function calling support works. Simple Python# pip install fireworks-ai e2b-code-interpreter from fireworks import Fireworks from e2b_code_interpreter import Sandbox # Create Fireworks client client = Fireworks() 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'" # Send the prompt to the model response = client.chat.completions.create( model="accounts/fireworks/models/gpt-oss-120b", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": prompt} ] ) # Extract the code from the response code = response.choices[0].message.content # Execute code in E2B Sandbox with