0

A hands-on guide: Getting started with the Mistral Agents API

The Mistral Adents API enables developers to create intelligent modular agents equipped with various features. Key features include:

  • Supports a variety of multi-model models covering text-based and image-based interactions.
  • Dialogue memory, allowing the proxy to retain context in multiple user messages.
  • Flexibility to interact with multiple agents in a single model, independent agent or a single stream.
  • Built-in access basic tools such as code execution, web browsing, image generation and document libraries.
  • A powerful proxy handover mechanism that allows proxy collaboration with each other as needed tasks.

In this guide, we will demonstrate how to build basic mathematical solvers using the Mistral Agents API. Our agents will use code interpreter tools to process and solve mathematical problems programmatically.

Step 1: Set up the dependency

Install the Mistral library

Loading the Mistral API key

You can get an API key from it

from getpass import getpass
apiKey = getpass('Enter Mistral API Key: ')

Step 2: Create a Mistral Client and Agent

The following code uses the Mistral proxy API to create a custom math proxy. Agent, naming Mathematics Assistantconfigured to solve mathematical problems, evaluate expressions and explain concepts. It uses Mistral-Medium-2505 Models and Mistral’s built-in Code_interPreter Tools that allow it to run Python code when needed. Initialize the proxy with clear instructions and adjust it with specific completion parameters to ensure accurate and centralized response.

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: Run the Agent

Initialize the conversation

The following code initiates a new conversation with Math_agent, requiring it to solve the quadratic equation 2x² + 3x – 2 = 0. The start() method sends the input query to the proxy, which uses the specified model and tool (such as a code interpreter) to generate the response. The result, including the interpretation 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 get the final output and executed code:

response.outputs[2].content
print(response.outputs[1].info['code'])

Draw the result of executing the code

response = client.beta.conversations.append(
    conversation_id=response.conversation_id, inputs="Plot the function f(x) = 2x² + 3x - 2"
)

Continue the conversation dialogue Ensure that the agent retains context and builds on previous interactions for a more natural and coherent conversation.

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 generated image as image_generated.png In the current directory. We can use the following code to display the same

from IPython.display import Image, display
image_path = "image_generated.png"

display(Image(filename=image_path))

Check out the notebook here. All credits for this study are to the researchers on the project. Also, please stay tuned for us twitter And don’t forget to join us 95k+ ml reddit And subscribe Our newsletter.


I am a civil engineering graduate in Islamic Islam in Jamia Milia New Delhi (2022) and I am very interested in data science, especially neural networks and their applications in various fields.