On this tutorial, we’ll show easy methods to allow perform calling in Mistral Brokers utilizing the usual JSON schema format. By defining your perform’s enter parameters with a transparent schema, you may make your customized instruments seamlessly callable by the agent—enabling highly effective, dynamic interactions.
We shall be utilizing the AviationStack API to retrieve real-time flight standing information, showcasing how exterior APIs could be built-in as callable features inside a Mistral Agent.
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
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
Loading the Aviation Stack API Key
You may join a free API key from their dashboard to get began.
AVIATIONSTACK_API_KEY = getpass('Enter Aviation Stack API: ')
Step 2: Defining the Customized Perform
Subsequent, we outline a Python perform get_flight_status() that calls the AviationStack API to retrieve the real-time standing of a flight. The perform accepts an non-compulsory flight_iata parameter and returns key particulars corresponding to airline identify, flight standing, departure and arrival airports, and scheduled occasions. If no matching flight is discovered, it gracefully returns an error message.
import requests
from typing import Dict
def get_flight_status(flight_iata=None):
"""
Retrieve flight standing utilizing non-compulsory filters: dep_iata, arr_iata, flight_iata.
"""
params = {
"access_key": AVIATIONSTACK_API_KEY,
"flight_iata": flight_iata
}
response = requests.get("http://api.aviationstack.com/v1/flights", params=params)
information = response.json()
if "information" in information and information["data"]:
flight = information["data"][0]
return {
"airline": flight["airline"]["name"],
"flight_iata": flight["flight"]["iata"],
"standing": flight["flight_status"],
"departure_airport": flight["departure"]["airport"],
"arrival_airport": flight["arrival"]["airport"],
"scheduled_departure": flight["departure"]["scheduled"],
"scheduled_arrival": flight["arrival"]["scheduled"],
}
else:
return {"error": "No flight discovered for the supplied parameters."}
Step 3: Creating the Mistral shopper and Agent
On this step, we create a Mistral Agent that makes use of tool-calling to fetch real-time flight info. The agent, named Flight Standing Agent, is configured to make use of the “mistral-medium-2505” mannequin and is provided with a customized perform device named get_flight_status. This device is outlined utilizing a JSON schema that accepts a single required parameter: the flight’s IATA code (e.g., “AI101”). As soon as deployed, the agent can robotically invoke this perform at any time when it detects a related person question, enabling seamless integration between pure language inputs and structured API responses.
from mistralai import Mistral
shopper = Mistral(MISTRAL_API_KEY)
flight_status_agent = shopper.beta.brokers.create(
mannequin="mistral-medium-2505",
description="Gives real-time flight standing utilizing aviationstack API.",
identify="Flight Standing Agent",
instruments=[
{
"type": "function",
"function": {
"name": "get_flight_status",
"description": "Retrieve the current status of a flight by its IATA code (e.g. AI101).",
"parameters": {
"type": "object",
"properties": {
"flight_iata": {
"type": "string",
"description": "IATA code of the flight (e.g. AI101)"
},
},
"required": ["flight_iata"]
}
}
}
]
)
Step 4: Beginning the Dialog and dealing with Perform Calling
On this step, we provoke a dialog with the Flight Standing Agent by asking a pure language query: “What’s the present standing of AI101?”. The Mistral mannequin detects that it ought to invoke the get_flight_status perform and returns a perform name request. We parse the arguments, run the perform domestically utilizing the AviationStack API, and return the outcome again to the agent utilizing FunctionResultEntry. Lastly, the mannequin incorporates the API response and generates a pure language reply with the present flight standing, which we print to the console.
from mistralai import FunctionResultEntry
import json
# Consumer begins a dialog
response = shopper.beta.conversations.begin(
agent_id=flight_status_agent.id,
inputs=[{"role": "user", "content": "What's the current status of AI101?"}]
)
# Examine if mannequin requested a perform name
if response.outputs[-1].sort == "perform.name" and response.outputs[-1].identify == "get_flight_status":
args = json.hundreds(response.outputs[-1].arguments)
# Run the perform
function_result = json.dumps(get_flight_status(**args))
# Create outcome entry
result_entry = FunctionResultEntry(
tool_call_id=response.outputs[-1].tool_call_id,
outcome=function_result
)
# Return outcome to agent
response = shopper.beta.conversations.append(
conversation_id=response.conversation_id,
inputs=[result_entry]
)
print(response.outputs[-1].content material)
else:
print(response.outputs[-1].content material)
Try the Notebook on GitHub. All credit score for this analysis goes to the researchers of this challenge. Additionally, be happy to observe us on Twitter and don’t neglect to affix 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 Knowledge Science, particularly Neural Networks and their utility in varied areas.