Notebooks
M
Mistral AI
Codestral Code Interpreter

Codestral Code Interpreter

mistral-cookbookthird_partyE2B_Code_Interpretingcodestral-code-interpreter-python

Codestral with code interpreting and analyzing dataset

This AI assistant is powered by the open-source Code Interpreter SDK by E2B. The SDK quickly creates a secure cloud sandbox powered by Firecracker. Inside this sandbox is a running Jupyter server that the LLM can use.

Read more about Mistral's new Codestral model here.

Step 1: Install dependencies

We start with installing the E2B code interpreter SDK and Mistral's Python SDK.

[45]
Requirement already satisfied: mistralai==0.4.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (0.4.2)
Requirement already satisfied: e2b_code_interpreter==0.0.10 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.10)
Requirement already satisfied: httpx<1,>=0.25 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from mistralai==0.4.2->-r requirements.txt (line 1)) (0.27.0)
Requirement already satisfied: orjson<3.11,>=3.9.10 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from mistralai==0.4.2->-r requirements.txt (line 1)) (3.9.15)
Requirement already satisfied: pydantic<3,>=2.5.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from mistralai==0.4.2->-r requirements.txt (line 1)) (2.7.1)
Requirement already satisfied: e2b>=0.17.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (0.17.1)
Requirement already satisfied: websocket-client<2.0.0,>=1.7.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (1.7.0)
Requirement already satisfied: aenum>=3.1.11 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (3.1.15)
Requirement already satisfied: aiohttp>=3.8.4 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (3.9.5)
Requirement already satisfied: jsonrpcclient>=4.0.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (4.0.3)
Requirement already satisfied: python-dateutil>=2.8.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (2.8.2)
Requirement already satisfied: requests>=2.31.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (2.31.0)
Requirement already satisfied: typing-extensions>=4.8.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (4.9.0)
Requirement already satisfied: urllib3>=1.25.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (2.2.1)
Requirement already satisfied: websockets>=11.0.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (11.0.3)
Requirement already satisfied: anyio in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (3.7.1)
Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (2024.2.2)
Requirement already satisfied: httpcore==1.* in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (1.0.2)
Requirement already satisfied: idna in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (3.6)
Requirement already satisfied: sniffio in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (1.3.0)
Requirement already satisfied: h11<0.15,>=0.13 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.25->mistralai==0.4.2->-r requirements.txt (line 1)) (0.14.0)
Requirement already satisfied: annotated-types>=0.4.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pydantic<3,>=2.5.2->mistralai==0.4.2->-r requirements.txt (line 1)) (0.5.0)
Requirement already satisfied: pydantic-core==2.18.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pydantic<3,>=2.5.2->mistralai==0.4.2->-r requirements.txt (line 1)) (2.18.2)
Requirement already satisfied: aiosignal>=1.1.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp>=3.8.4->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp>=3.8.4->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (23.1.0)
Requirement already satisfied: frozenlist>=1.1.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp>=3.8.4->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (1.4.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp>=3.8.4->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (6.0.4)
Requirement already satisfied: yarl<2.0,>=1.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp>=3.8.4->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (1.9.2)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.8.2->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (1.16.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from requests>=2.31.0->e2b>=0.17.1->e2b_code_interpreter==0.0.10->-r requirements.txt (line 2)) (3.3.2)
Note: you may need to restart the kernel to use updated packages.

Step 2: Define API keys and prompt

Let's define our variables with API keys for Mistral and E2B together with the model ID and prompt.

We won't be defining any tools, because this example is made to work universally, including Mistral's models that don't fully support tool usage (function calling) yet. To learn more about function calling with Mistral's LLMs, see this docs page.

[46]

We instruct the model to return messages in Markdown and then parse and extract the Python code block.

[47]

Step 3: Implement the method for code interpreting

Here's the main function that uses the E2B code interpreter SDK. We'll be calling this function a little bit further in the code when we're parsing the Codestral's response with tool calls.

[48]

Step 4: Implement the method for calling Codestral and parsing its response

Now we're going to define and implement chat method. In this method, we'll call the Codestral LLM, parse the output to extract any Python code block, and call our code_interpret method we defined above.

[49]

Step 5: Implement the method for uploading dataset to code interpreter sandbox

The file gets uploaded to the E2B sandbox where our code interpreter is running. We get the file's remote path in the remote_path variable.

[50]

Step 6: Put everything together

In this last step, we put all the pieces together. We instantiate a new code interpreter instance using

with CodeInterpreter(api_key=E2B_API_KEY) as code_interpreter:

and then call the chat method with our user message and the code_interpreter instance.

[51]
Uploading dataset to Code Interpreter sandbox...
Uploaded at /home/user/global_economy_indicators.csv

==================================================
User message: Make a chart showing linear regression of the relationship between GDP per capita and life expectancy from the global_economy_indicators. Filter out any missing values or values in wrong format.
==================================================
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Load the dataset
df = pd.read_csv('/home/user/global_economy_indicators.csv')

# Filter out missing values
df = df.dropna(subset=['GDP per capita (current US$)', 'Life expectancy at birth, total (years)'])

# Convert columns to numeric, errors='coerce' will turn the invalid parsing into NaN
df['GDP per capita (current US$)'] = pd.to_numeric(df['GDP per capita (current US$)'], errors='coerce')
df['Life expectancy at birth, total (years)'] = pd.to_numeric(df['Life expectancy at birth, total (years)'], errors='coerce')

# Drop NaN values after conversion
df = df.dropna(subset=['GDP per capita (current US$)', 'Life expectancy at birth, total (years)'])

# Prepare the data for linear regression
X = df[['GDP per capita (current US$)']]
y = df['Life expectancy at birth, total (years)']

# Fit the linear regression model
model = LinearRegression()
model.fit(X, y)

# Predict life expectancy for all GDP per capita values
y_pred = model.predict(X)

# Plot the data and the regression line
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
plt.title('Relationship between GDP per capita and life expectancy')
plt.xlabel('GDP per capita (current US$)')
plt.ylabel('Life expectancy at birth, total (years)')
plt.show()
Running code interpreter...
Output