The Mistral Brokers API allows builders to create good, modular brokers geared up with a variety of capabilities. Key options embrace:
- Assist for a wide range of multimodal fashions, masking each textual content and image-based interactions.
- Dialog reminiscence, permitting brokers to retain context throughout a number of consumer messages.
- The flexibleness to interact with particular person fashions, standalone brokers, or coordinate between a number of brokers in a single circulate.
- Constructed-in entry to important instruments like code execution, internet shopping, picture era, and a doc library.
- A robust agent handoff mechanism, enabling brokers to collaborate by passing duties between one another as wanted.
On this information, we’ll reveal how you can construct a primary math-solving agent utilizing the Mistral Brokers API. Our agent will use the code interpreter instrument to deal with and clear up math issues programmatically.
Step 1: Organising dependencies
Putting in the Mistral library
Loading the Mistral API Key
You may get an API key from https://console.mistral.ai/api-keys
from getpass import getpass
apiKey = getpass('Enter Mistral API Key: ')
Step 2: Creating the Mistral consumer and Agent
The next code creates a customized math agent utilizing the Mistral Brokers API. The agent, named Math Helper, is configured to unravel mathematical issues, consider expressions, and clarify ideas. It makes use of the mistral-medium-2505 mannequin together with Mistral’s built-in code_interpreter instrument, permitting it to run Python code when wanted. The agent is initialized with clear directions and tuned with particular completion parameters to make sure correct and centered responses.
from mistralai import Mistral
consumer = Mistral(apiKey)
math_agent = consumer.beta.brokers.create(
mannequin="mistral-medium-2505",
description="An agent that solves math issues and evaluates expressions.",
identify="Math Helper",
directions="You're a useful math assistant. You'll be able to clarify ideas, clear up equations, and consider math expressions utilizing the code interpreter.",
instruments=[{"type": "code_interpreter"}],
completion_args={
"temperature": 0.2,
"top_p": 0.9
}
)
Step 3: Working the Agent
Initializing the dialog
The next code initiates a brand new dialog with the math_agent, asking it to unravel the quadratic equation 2x² + 3x – 2 = 0. The beginning() methodology sends the enter question to the agent, which makes use of the required mannequin and instruments (just like the code interpreter) to generate a response. The outcome, together with the assistant’s rationalization and code execution, is saved within the response variable.
response = consumer.beta.conversations.begin(
agent_id=math_agent.id, inputs="Resolve the quadratic equation 2x² + 3x - 2 = 0", #retailer=False
)
print(response)
You should utilize the next code to get the ultimate output and the executed code:
response.outputs[2].content material
print(response.outputs[1].information['code'])
Plotting the outcomes of the executed code
response = consumer.beta.conversations.append(
conversation_id=response.conversation_id, inputs="Plot the perform f(x) = 2x² + 3x - 2"
)
Persevering with the dialog utilizing conversations.append ensures that the agent retains the context and builds upon the earlier interactions, permitting for a extra pure and coherent dialogue.
file_id = response.outputs[2].content material[0].file_id
file_bytes = consumer.recordsdata.obtain(file_id=file_id).learn()
with open(f"image_generated.png", "wb") as file:
file.write(file_bytes)
This code will obtain the generated picture as image_generated.png within the present listing. We will show the the identical utilizing the next code
from IPython.show import Picture, show
image_path = "image_generated.png"
show(Picture(filename=image_path))
Take a look at the Notebook here. All credit score for this analysis goes to the researchers of this undertaking. Additionally, be happy to observe us on Twitter and don’t overlook to hitch our 95k+ ML SubReddit and Subscribe to our Newsletter.

I’m a Civil Engineering Graduate (2022) from Jamia Millia Islamia, New Delhi, and I’ve a eager curiosity in Information Science, particularly Neural Networks and their utility in numerous areas.