Integrating Mistral AI with Dynamic Function Calls: A Step-by-Step Guide
We’ll explore how to integrate Mistral AI with dynamic function calls, enabling real-time responses based on user queries.
One thing to remember, the model itself does not execute functions. Instead, it identifies the appropriate functions to call based on the user’s query and provides us with the necessary information like function name and arguments in a structured format, typically JSON.
We’ll use an example involving a restaurant reservation system where users can inquire about and book reservations.
Step 1: Define Tools and Setup Functions
First, we define the functions that the Mistral model can call. These functions interact with a simulated database to check availability and make reservations.
import pandas as pd
import json
# Simulated database for restaurant reservations
data = {
'date': ['2022-05-01', '2022-05-02', '2022-05-03'],
'availability': [5, 2, 0] # Number of tables available
}
df = pd.DataFrame(data)
# Function to check table availability
def check_availability(df: pd.DataFrame, date: str) -> str:
if date in df['date'].values:
available_tables = int(df.loc[df['date'] == date, 'availability'].values[0]) # Convert numpy.int64 to int
if available_tables > 0:
return json.dumps({"date": date, "available": True, "tables": available_tables})
return json.dumps({"date": date, "available": False})
# Function to make a reservation
def book_table(df: pd.DataFrame, date: str) -> str:
availability_info = json.loads(check_availability(df, date))
if availability_info['available']:
df.loc[df['date'] == date, 'availability'] -= 1
return json.dumps({"date": date, "booking_status": "confirmed"})
return json.dumps({"date": date, "booking_status": "full"})
# Test date
# test_date = '2022-05-01'
# Check availability on the test date
# availability_result = check_availability(df, test_date)
# print("Availability Check:", json.loads(availability_result))
# Attempt to book a table on the test date
# booking_result = book_table(df, test_date)
# print("Booking Attempt:", json.loads(booking_result))
# Check availability again to see if the number of tables has decreased
# availability_after_booking = check_availability(df, test_date)
# print("Availability After Booking:", json.loads(availability_after_booking))
Step 2: Configure the Mistral AI Model
Next, we configure the Mistral model to use these functions. We specify how the functions can be called by the model using tools and tool calls.
from mistralai.client import MistralClient
# Initialize the client
client = MistralClient(api_key="your_api_key")
# Define tool specifications in JSON schema
tool_check_availability = {
"type": "function",
"function": {
"name": "check_availability",
"description": "Check table availability on a specific date",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "The date to check availability for."
}
},
"required": ["date"],
},
},
}
tool_book_table = {
"type": "function",
"function": {
"name": "book_table",
"description": "Book a table on a specific date",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "The date to book a table for."
}
},
"required": ["date"],
},
},
}
# Register tools with Mistral
tools = [tool_check_availability, tool_book_table]
Step 3: Handling User Queries via Mistral
We simulate handling user queries where Mistral decides which registered function to call based on the user’s intent extracted from their message.
# Example of a user message asking about table availability
user_messages = [
{"role": "user", "content": "Is there a table available on 2022-05-01?"}
]
# Call Mistral model to handle the chat
response = client.chat(
model="mistral-large-latest",
messages=user_messages,
tools=tools,
tool_choice="auto"
)
# Print Mistral's response
print("Mistral's response:", response.choices[0].message.content)
Step 4: Execute Functions Returned by the Mistral Model
Finally, we execute the function calls as returned by the Mistral model to obtain and process data based on user requests.
import json
# Execute the function calls as specified by the Mistral model
def execute_tool_calls(tool_calls, df):
responses = []
for call in tool_calls:
tool_function = call.function
print("tool function ", tool_function)
function_name = tool_function.name
print("function_name ", function_name)
args = json.loads(tool_function.arguments)
print("args ", args)
if function_name == "check_availability":
response = check_availability(df, **args)
elif function_name == "book_table":
response = book_table(df, **args)
else:
response = json.dumps({"error": f"Function {function_name} not found."})
responses.append(json.loads(response))
return responses
mistral_response = response.choices[0].message
tool_responses = execute_tool_calls(mistral_response.tool_calls, df)
print("Tool Responses:", tool_responses)
Final Response:
tool function name='check_availability' arguments='{"date": "2022-05-01"}'
function_name check_availability
args {'date': '2022-05-01'}
Tool Responses: [{'date': '2022-05-01', 'available': True, 'tables': 4}]
This comprehensive guide showcases how you can leverage the power of Mistral AI to manage dynamic interactions within your applications, enhancing both efficiency and user engagement through real-time AI-driven responses.
References: