In this tutorial, we will show how to allow call functions in Mistral agents using the standard JSON diagram format. By defining the input settings of your function with a clear diagram, you can make your personalized tools designed transparently by the agent – allowing powerful and dynamic interactions.
We will use the API Aviationsstack to recover the state of the flight in real time, presenting the way in which external APIs can be integrated as calls called a Mistral Agent.
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
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
Loading the API button on aviation battery
You can register for a free API key to their dashboard To start.
AVIATIONSTACK_API_KEY = getpass('Enter Aviation Stack API: ')
Step 2: Definition of the personalized function
Then, we define a Python Get_Flight_Status () function which calls the API Aviationstack to recover the state in real time of a flight. The function accepts an optional Flight_iata parameter and returns key details such as the airline name, flight status, departure and arrival airports, and scheduled hours. If no corresponding flight is found, it graciously returns an error message.
import requests
from typing import Dict
def get_flight_status(flight_iata=None):
"""
Retrieve flight status using optional 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)
data = response.json()
if "data" in data and data("data"):
flight = data("data")(0)
return {
"airline": flight("airline")("name"),
"flight_iata": flight("flight")("iata"),
"status": 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 found for the provided parameters."}
Step 3: Creation of the Mistral Customer and Agent
In this stage, we create a Mistral agent who uses the tool call to recover real -time flight information. The agent, named the named flight agent, is configured to use the “Mistral-Medium-25505” model and is equipped with a personalized function tool called Get_Flight_Status. This tool is defined using a JSON diagram which accepts a single required parameter: the IATA code of the flight (for example, “AI101”). Once deployed, the agent can automatically invoke this function each time he detects a relevant user query, allowing transparent integration between natural language inputs and structured API responses.
from mistralai import Mistral
client = Mistral(MISTRAL_API_KEY)
flight_status_agent = client.beta.agents.create(
model="mistral-medium-2505",
description="Provides real-time flight status using aviationstack API.",
name="Flight Status Agent",
tools=(
{
"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: Start of the conversation function and the call function
In this stage, we start a conversation with the state of the flight agent by asking a question of natural language: “What is the current status of AI101?”. The Mistral model detects that it should invoke the Get_Flight_Status function and returns a request for a function. We analyze the arguments, execute the function locally using the APIATIATIONSTACK APIATION and refer the result to the agent using the function function. Finally, the model incorporates the API response and generates an answer in natural language with the current flight, which we print to the console.
from mistralai import FunctionResultEntry
import json
# User starts a conversation
response = client.beta.conversations.start(
agent_id=flight_status_agent.id,
inputs=({"role": "user", "content": "What's the current status of AI101?"})
)
# Check if model requested a function call
if response.outputs(-1).type == "function.call" and response.outputs(-1).name == "get_flight_status":
args = json.loads(response.outputs(-1).arguments)
# Run the function
function_result = json.dumps(get_flight_status(**args))
# Create result entry
result_entry = FunctionResultEntry(
tool_call_id=response.outputs(-1).tool_call_id,
result=function_result
)
# Return result to agent
response = client.beta.conversations.append(
conversation_id=response.conversation_id,
inputs=(result_entry)
)
print(response.outputs(-1).content)
else:
print(response.outputs(-1).content)
Discover the GitHub notebook. 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.
