import Image from 'next/image' import imgChart from '@/images/analyze-data-chart.png'
You can use E2B Sandbox to run AI-generated code to analyze data. Here's how the AI data analysis workflow usually looks like:
- Your user has a dataset in CSV format or other formats.
- You prompt the LLM to generate code (usually Python) based on the user's data.
- The sandbox runs the AI-generated code and returns the results.
- You display the results to the user.
This short example will show you how to use E2B Sandbox to run AI-generated code to analyze CSV data.
- Install dependencies
- Set your API keys
- Download example CSV file
- Initialize the sandbox and upload the dataset to the sandbox
- Prepare the method for running AI-generated code
- Prepare the prompt and initialize Anthropic client
- Connect the sandbox to the LLM with tool calling
- Parse the LLM response and run the AI-generated code in the sandbox
- Save the generated chart
- Run the code
- Full final code
Install the E2B SDK and Claude SDK to your project by running the following command in your terminal.
```bash {{ language: 'js' }} npm i @e2b/code-interpreter @anthropic-ai/sdk dotenv ``` ```bash {{ language: 'python' }} pip install e2b-code-interpreter anthropic python-dotenv ```- Get your E2B API key from E2B Dashboard.
- Get your Claude API key from Claude API Dashboard.
- Paste the keys into your
.envfile.
{/* We'll be using the publicly available AirBnB NYC dataset. */}
We'll be using the publicly available dataset of about 10,000 movies.
- Click the "Download" button at the top of the page.
- Select "Download as zip (2 MB)".
- Unzip the file and you should see
dataset.csvfile. Move it to the root of your project.
We'll upload the dataset from the third step to the sandbox and save it as dataset.csv file.
// Create sandbox const sbx = await Sandbox.create()
// Upload the dataset to the sandbox const content = fs.readFileSync('dataset.csv') const datasetPathInSandbox = await sbx.files.write('dataset.csv', content)
```python {{ language: 'python', description: 'main.py' }}
from dotenv import load_dotenv
load_dotenv()
from e2b_code_interpreter import Sandbox
# Create sandbox
sbx = Sandbox.create()
# Upload the dataset to the sandbox
dataset_path_in_sandbox = ""
with open("dataset.csv", "rb") as f:
dataset_path_in_sandbox = sbx.files.write("dataset.csv", f)
Add the following code to the file. Here we're adding the method for code execution.
```js {{ language: 'js', description: 'index.ts' }} // ... code from the previous stepasync function runAIGeneratedCode(aiGeneratedCode: string) { console.log('Running the code in the sandbox....') const execution = await sbx.runCode(aiGeneratedCode) console.log('Code execution finished!') console.log(execution) }
```python {{ language: 'python', description: 'main.py' }}
# ... code from the previous step
def run_ai_generated_code(ai_generated_code: str):
print('Running the code in the sandbox....')
execution = sbx.run_code(ai_generated_code)
print('Code execution finished!')
print(execution)
The prompt we'll be using describes the dataset and the analysis we want to perform like this: 1. Describe the columns in the CSV dataset. 2. Ask the LLM what we want to analyze - here we want to analyze the vote average over time. We're asking for a chart as the output. 3. Instruct the LLM to generate Python code for the data analysis.
```js {{ language: 'js', description: 'index.ts' }} import Anthropic from '@anthropic-ai/sdk'const prompt = ` I have a CSV file about movies. It has about 10k rows. It's saved in the sandbox at ${dataset_path_in_sandbox.path}. These are the columns:
- 'id': number, id of the movie
- 'original_language': string like "eng", "es", "ko", etc
- 'original_title': string that's name of the movie in the original language
- 'overview': string about the movie
- 'popularity': float, from 0 to 9137.939. It's not normalized at all and there are outliers
- 'release_date': date in the format yyyy-mm-dd
- 'title': string that's the name of the movie in english
- 'vote_average': float number between 0 and 10 that's representing viewers voting average
- 'vote_count': int for how many viewers voted
I want to better understand how the vote average has changed over the years. Write Python code that analyzes the dataset based on my request and produces right chart accordingly`
const anthropic = new Anthropic() console.log('Waiting for the model response...') const msg = await anthropic.messages.create({ model: 'claude-3-5-sonnet-20240620', max_tokens: 1024, messages: [{ role: 'user', content: prompt }], })
```python {{ language: 'python', description: 'main.py' }}
from anthropic import Anthropic
prompt = '''
I have a CSV file about movies. It has about 10k rows. It's saved in the sandbox at {datasetPathInSandbox.path}.
These are the columns:
- 'id': number, id of the movie
- 'original_language': string like "eng", "es", "ko", etc
- 'original_title': string that's name of the movie in the original language
- 'overview': string about the movie
- 'popularity': float, from 0 to 9137.939. It's not normalized at all and there are outliers
- 'release_date': date in the format yyyy-mm-dd
- 'title': string that's the name of the movie in english
- 'vote_average': float number between 0 and 10 that's representing viewers voting average
- 'vote_count': int for how many viewers voted
I want to better understand how the vote average has changed over the years. Write Python code that analyzes the dataset based on my request and produces right chart accordingly'''
anthropic = Anthropic()
msg = anthropic.messages.create(
model='claude-3-5-sonnet-20240620',
max_tokens=1024,
messages=[
{"role": "user", "content": prompt}
]
)
We'll use Claude's ability to use tools (function calling) to run the code in the sandbox.
The way we'll do it is by connecting the method for running AI-generated code we created in the previous step to the Claude model.
Update the initialization of the Anthropic client to include the tool use like this:
const msg = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20240620',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
tools: [ // $HighlightLine
{ // $HighlightLine
name: 'run_python_code', // $HighlightLine
description: 'Run Python code', // $HighlightLine
input_schema: { // $HighlightLine
type: 'object', // $HighlightLine
properties: { // $HighlightLine
code: { // $HighlightLine
type: 'string', // $HighlightLine
description: 'The Python code to run', // $HighlightLine
}, // $HighlightLine
}, // $HighlightLine
required: ['code'], // $HighlightLine
}, // $HighlightLine
}, // $HighlightLine
], // $HighlightLine
})msg = anthropic.messages.create(
model='claude-3-5-sonnet-20240620',
max_tokens=1024,
messages=[
{"role": "user", "content": prompt}
],
tools=[ # $HighlightLine
{ # $HighlightLine
"name": "run_python_code", # $HighlightLine
"description": "Run Python code", # $HighlightLine
"input_schema": { # $HighlightLine
"type": "object", # $HighlightLine
"properties": { # $HighlightLine
"code": { "type": "string", "description": "The Python code to run" }, # $HighlightLine
}, # $HighlightLine
"required": ["code"], # $HighlightLine
}, # $HighlightLine
}, # $HighlightLine
], # $HighlightLine
)Now we'll parse the msg object to get the code from the LLM's response based on the tool we created in the previous step.
Once we have the code, we'll pass it to the runAIGeneratedCode method in JavaScript or run_ai_generated_code method in Python we created in the previous step to run the code in the sandbox.
interface CodeRunToolInput { code: string }
for (const contentBlock of msg.content) { if (contentBlock.type === 'tool_use') { if (contentBlock.name === 'run_python_code') { const code = (contentBlock.input as CodeRunToolInput).code console.log('Will run following code in the sandbox', code) // Execute the code in the sandbox await runAIGeneratedCode(code) } } }
```python {{ language: 'python', description: 'main.py' }}
for content_block in msg.content:
if content_block.type == 'tool_use':
if content_block.name == 'run_python_code':
code = content_block.input['code']
print('Will run following code in the sandbox', code)
# Execute the code in the sandbox
run_ai_generated_code(code)
When running code in the sandbox for data analysis, you can get different types of results. Including stdout, stderr, charts, tables, text, runtime errors, and more.
In this example we're specifically asking for a chart so we'll be looking for the chart in the results.
Let's update the runAIGeneratedCode method in JavaScript and run_ai_generated_code method in Python to check for the chart in the results and save it to the file.
async function runAIGeneratedCode(aiGeneratedCode: string) {
console.log('Running the code in the sandbox....')
const execution = await sbx.runCode(aiGeneratedCode)
console.log('Code execution finished!')
// First let's check if the code ran successfully.
if (execution.error) { // $HighlightLine
console.error('AI-generated code had an error.') // $HighlightLine
console.log(execution.error.name) // $HighlightLine
console.log(execution.error.value) // $HighlightLine
console.log(execution.error.traceback) // $HighlightLine
process.exit(1) // $HighlightLine
} // $HighlightLine
// Iterate over all the results and specifically check for png files that will represent the chart.
let resultIdx = 0 // $HighlightLine
for (const result of execution.results) { // $HighlightLine
if (result.png) { // $HighlightLine
// Save the png to a file
// The png is in base64 format.
fs.writeFileSync(`chart-${resultIdx}.png`, result.png, { encoding: 'base64' }) // $HighlightLine
console.log(`Chart saved to chart-${resultIdx}.png`) // $HighlightLine
resultIdx++ // $HighlightLine
} // $HighlightLine
} // $HighlightLine
}def run_ai_generated_code(ai_generated_code: str):
print('Running the code in the sandbox....')
execution = sbx.run_code(ai_generated_code)
print('Code execution finished!')
# First let's check if the code ran successfully.
if execution.error: # $HighlightLine
print('AI-generated code had an error.') # $HighlightLine
print(execution.error.name) # $HighlightLine
print(execution.error.value) # $HighlightLine
print(execution.error.traceback) # $HighlightLine
sys.exit(1) # $HighlightLine
# Iterate over all the results and specifically check for png files that will represent the chart.
result_idx = 0 # $HighlightLine
for result in execution.results: # $HighlightLine
if result.png: # $HighlightLine
# Save the png to a file
# The png is in base64 format.
with open(f'chart-{result_idx}.png', 'wb') as f: # $HighlightLine
f.write(base64.b64decode(result.png)) # $HighlightLine
print(f'Chart saved to chart-{result_idx}.png') # $HighlightLine
result_idx += 1 # $HighlightLineNow you can run the whole code to see the results.
```bash {{ language: 'js' }} npx tsx index.ts ``` ```bash {{ language: 'python' }} python main.py ```You should see the chart in the root of your project that will look similar to this:
Check the full code in JavaScript and Python below:
import 'dotenv/config'
import fs from 'fs'
import Anthropic from '@anthropic-ai/sdk'
import { Sandbox } from '@e2b/code-interpreter'
// Create sandbox
const sbx = await Sandbox.create()
// Upload the dataset to the sandbox
const content = fs.readFileSync('dataset.csv')
const datasetPathInSandbox = await sbx.files.write('/home/user/dataset.csv', content)
async function runAIGeneratedCode(aiGeneratedCode: string) {
const execution = await sbx.runCode(aiGeneratedCode)
if (execution.error) {
console.error('AI-generated code had an error.')
console.log(execution.error.name)
console.log(execution.error.value)
console.log(execution.error.traceback)
process.exit(1)
}
// Iterate over all the results and specifically check for png files that will represent the chart.
let resultIdx = 0
for (const result of execution.results) {
if (result.png) {
// Save the png to a file
// The png is in base64 format.
fs.writeFileSync(`chart-${resultIdx}.png`, result.png, { encoding: 'base64' })
console.log('Chart saved to chart-${resultIdx}.png')
resultIdx++
}
}
}
const prompt = `
I have a CSV file about movies. It has about 10k rows. It's saved in the sandbox at ${datasetPathInSandbox.path}.
These are the columns:
- 'id': number, id of the movie
- 'original_language': string like "eng", "es", "ko", etc
- 'original_title': string that's name of the movie in the original language
- 'overview': string about the movie
- 'popularity': float, from 0 to 9137.939. It's not normalized at all and there are outliers
- 'release_date': date in the format yyyy-mm-dd
- 'title': string that's the name of the movie in english
- 'vote_average': float number between 0 and 10 that's representing viewers voting average
- 'vote_count': int for how many viewers voted
I want to better understand how the vote average has changed over the years. Write Python code that analyzes the dataset based on my request and produces right chart accordingly`
const anthropic = new Anthropic()
console.log('Waiting for the model response...')
const msg = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20240620',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
tools: [
{
name: 'run_python_code',
description: 'Run Python code',
input_schema: {
type: 'object',
properties: {
code: {
type: 'string',
description: 'The Python code to run',
},
},
required: ['code'],
},
},
],
})
interface CodeRunToolInput {
code: string
}
for (const contentBlock of msg.content) {
if (contentBlock.type === 'tool_use') {
if (contentBlock.name === 'run_python_code') {
const code = (contentBlock.input as CodeRunToolInput).code
console.log('Will run following code in the sandbox', code)
// Execute the code in the sandbox
await runAIGeneratedCode(code)
}
}
}import sys
import base64
from dotenv import load_dotenv
load_dotenv()
from e2b_code_interpreter import Sandbox
from anthropic import Anthropic
# Create sandbox
sbx = Sandbox.create()
# Upload the dataset to the sandbox
with open("../dataset.csv", "rb") as f:
dataset_path_in_sandbox = sbx.files.write("dataset.csv", f)
def run_ai_generated_code(ai_generated_code: str):
print('Running the code in the sandbox....')
execution = sbx.run_code(ai_generated_code)
print('Code execution finished!')
# First let's check if the code ran successfully.
if execution.error:
print('AI-generated code had an error.')
print(execution.error.name)
print(execution.error.value)
print(execution.error.traceback)
sys.exit(1)
# Iterate over all the results and specifically check for png files that will represent the chart.
result_idx = 0
for result in execution.results:
if result.png:
# Save the png to a file
# The png is in base64 format.
with open(f'chart-{result_idx}.png', 'wb') as f:
f.write(base64.b64decode(result.png))
print(f'Chart saved to chart-{result_idx}.png')
result_idx += 1
prompt = f"""
I have a CSV file about movies. It has about 10k rows. It's saved in the sandbox at {dataset_path_in_sandbox.path}.
These are the columns:
- 'id': number, id of the movie
- 'original_language': string like "eng", "es", "ko", etc
- 'original_title': string that's name of the movie in the original language
- 'overview': string about the movie
- 'popularity': float, from 0 to 9137.939. It's not normalized at all and there are outliers
- 'release_date': date in the format yyyy-mm-dd
- 'title': string that's the name of the movie in english
- 'vote_average': float number between 0 and 10 that's representing viewers voting average
- 'vote_count': int for how many viewers voted
I want to better understand how the vote average has changed over the years.
Write Python code that analyzes the dataset based on my request and produces right chart accordingly"""
anthropic = Anthropic()
print("Waiting for model response...")
msg = anthropic.messages.create(
model='claude-3-5-sonnet-20240620',
max_tokens=1024,
messages=[
{"role": "user", "content": prompt}
],
tools=[
{
"name": "run_python_code",
"description": "Run Python code",
"input_schema": {
"type": "object",
"properties": {
"code": { "type": "string", "description": "The Python code to run" },
},
"required": ["code"]
}
}
]
)
for content_block in msg.content:
if content_block.type == "tool_use":
if content_block.name == "run_python_code":
code = content_block.input["code"]
print("Will run following code in the sandbox", code)
# Execute the code in the sandbox
run_ai_generated_code(code)