Get 1 Free Month of Skillshare Shop Here

Practical guide: Starting with API Mistral Agents

by Brenden Burgess

When you buy through links on our site, we may earn a commission at no extra cost to you. However, this does not influence our evaluations.

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
AD 4nXfqzp3f0e1fH0zpqKwt L1Pr k51dbiiUkZ70ze6r5OULIjCEt 9iMWYSCNanx7SrJqK3eC7sD6rw1vX5mMzIGP8dU0fzTu bRff2zITw JJr1AMybqBVCUqhORTekSb6fcx0P g?key=rrS0r QBCGbplhp8IDcDA
print(response.outputs(1).info('code'))
AD 4nXeP942f1McSBqIY11xDFnC9i2aZX 0eQ1BiD9uLnsC sBHS9uyK5bysJIHHdTh9B2iWjcQ7PCw54e4wFLKh3e7FiGBtqpB0eyHymC6gqZzEJ7elWJy uz21xZZAUjRjvF6URR3TUg?key=rrS0r QBCGbplhp8IDcDA

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))
AD 4nXeTNYhxKmvcJd6lMAgwLCYfRa6Yq19x2UPStsGA6Sucf w1dbpG2vKnAKnlxE7od5SYidPogRtm59E76bT83wo3k8Ae2sDWGdgSge4hEi7V11M5Ka iAbq1BN0ytPPDqrS bF0FSg?key=rrS0r QBCGbplhp8IDcDA

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.


PASSPORT SIZE PHOTO

I graduated in Civil Engineering (2022) by Jamia Millia Islamia, New Delhi, and I have a great interest in data science, in particular neural networks and their application in various fields.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

AI Engine Chatbot
AI Avatar
Hi! I'm Learnopoly’s AI Course Advisor. What would you like to learn today?