In this tutorial, we demonstrate how we activate functional calls in Mistral agents using the Standard JSON shoe format. By defining your function’s input parameters with a clear schedule, you can make your custom tools seamlessly to be associated with the agent – enabling powerful, dynamic interactions.
We use Aviationstack API to retrieve data from real -time flight status and show how external APIs can be integrated as convertible features within a Mistral Agent.
Step 1: Setting up dependencies
Installation of Mistral Library
Loading the Mistral API key
You can get an API key from https://console.mistral.ai/api-Kays
from getpass import getpass
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
Loading the Aviation Stack API key
You can sign up for a free API key from their dashboard to get started.
AVIATIONSTACK_API_KEY = getpass('Enter Aviation Stack API: ')
Step 2: Definition of the Custom feature
Next, we define a python function get_FLIGHT_STATUS (), which calls Aviationstack API to pick up real-time status for a flight. The feature accepts an optional flight_iata parameter and returns key information such as airline name, flight status, departure and arrival Airports and scheduled times. If no matching flight exists, it will return 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: Creating the Mistral Client and Agent
In this step, we create a Mistral agent that uses tool calls to retrieve real-time airline. The agent, called Flight Status Agent, is configured to use the “Mistral Medium-Medium-2505” model and is equipped with a custom feature tool named Get_Flight_Status. This tool is defined using a JSON chart that accepts a single required parameter: Flight’s IATA code (eg “AI101”). Once the agent is implemented, the agent can automatically invoke this feature when it detects a relevant user request, enabling trouble -free integration between natural language inputs and structured API response.
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 and handling function
In this step, we start a conversation with the aircraft status agent by asking a natural language question: “What is the current status of AI101?”. The Mistral model detects that it must invoke the GET_FLIGHT_STATUS function and returns a functional call request. We analyze the arguments, run the feature locally using Aviationstack API and return the result back to the agent using functional needrth. Finally, the model incorporates the API response and generates a natural language response with the current flight status that 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)
Check the laptop on GitHub. All credit for this research goes to the researchers in this project. You are also welcome to follow us on Twitter And don’t forget to join our 95k+ ml subbreddit and subscribe to Our newsletter.
I am a candidate for a civil engineer (2022) from Jamia Millia Islamia, New Delhi, and I have a great interest in data sciences, especially neural networks and their use in different areas.