The Mistral Agents API allows developers to create intelligent and modular agents equipped with a wide range of capacities. Key characteristics include:
- Support for a variety of multimodal models, covering both textual and image -based interactions.
- Conversation memory, allowing agents to keep the context on several user messages.
- Flexibility to engage with individual models, autonomous agents or coordinates between several agents in a single flow.
- Access integrated into essential tools such as code execution, web navigation, image generation and a document library.
- A powerful agent transfer mechanism, allowing agents to collaborate by passing tasks between others if necessary.
In this guide, we will show how to create a basic mathematics resolution agent using the Mistral Agents API. Our agent will use the code tool to manage and solve mathematical problems per program.
Step 1: Control of dependencies
Installation of the Mistral Library
Loading the API Mistral key
You can get an API key from https://console.mistral.ai/api- Keys
from getpass import getpass
apiKey = getpass('Enter Mistral API Key: ')
Step 2: Creation of the Mistral Customer and Agent
The following code creates a personalized mathematical agent using the API Mistral Agents. The agent, named Mathematics assistanceis configured to solve mathematical problems, assess expressions and explain concepts. He uses the Mistral-Medium-2505 Model with integrated mistral code_ interpret Tool, allowing it to execute Python code if necessary. The agent is initialized with clear instructions and set with specific completion parameters to ensure precise and targeted responses.
from mistralai import Mistral
client = Mistral(apiKey)
math_agent = client.beta.agents.create(
model="mistral-medium-2505",
description="An agent that solves math problems and evaluates expressions.",
name="Math Helper",
instructions="You are a helpful math assistant. You can explain concepts, solve equations, and evaluate math expressions using the code interpreter.",
tools=({"type": "code_interpreter"}),
completion_args={
"temperature": 0.2,
"top_p": 0.9
}
)
Step 3: Execute the agent
Initialization of conversation
The following code initiates a new conversation with math_agent, asking it to resolve the quadratic equation 2x² + 3x – 2 = 0. The Start () method sends the entry request to the agent, which uses the model and the specified tools (such as the code interpreter) to generate an answer. The result, including the explanation of the assistant and the execution of the code, is stored in the response variable.
response = client.beta.conversations.start(
agent_id=math_agent.id, inputs="Solve the quadratic equation 2x² + 3x - 2 = 0", #store=False
)
print(response)
You can use the following code to obtain the final output and the code executed:
response.outputs(2).content
print(response.outputs(1).info('code'))
Draw the results of the code executed
response = client.beta.conversations.append(
conversation_id=response.conversation_id, inputs="Plot the function f(x) = 2x² + 3x - 2"
)
Continue the conversation using Conversations. Ensures that the agent retains the context and is based on previous interactions, allowing a more natural and coherent dialogue.
file_id = response.outputs(2).content(0).file_id
file_bytes = client.files.download(file_id=file_id).read()
with open(f"image_generated.png", "wb") as file:
file.write(file_bytes)
This code will download the image generated as image_generated.png in the current repertoire. We can display the same using the following code
from IPython.display import Image, display
image_path = "image_generated.png"
display(Image(filename=image_path))
Discover the Notebook here. All the merit of this research goes to researchers in this project. Also, don't hesitate to follow us Twitter And don't forget to join our 95K + ML Subdreddit and subscribe to Our newsletter.
