04 Semantic Kernel Python Aiagent Bookinghotel
Example Sample Hotel and Flight Booker Agent
This solution will help you book flight tickets and hotel. The scenario is a trip London Heathrow LHR Feb 20th 2024 to New York JFK returning Feb 27th 2025 flying economy with British Airways only. I want a stay in a Hilton hotel in New York please provide costs for the flight and hotel.
Initialize the Azure AI Agent Service and get configuration information from .env
.env
Create a .env file
.env contains the connection string of Azure AI Agent Service, the model used by AOAI, and the corresponding Google API Search service API, ENDPOINT, etc.
- AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME = "Your Azure AI Agent Service Model Deployment Name"
[NOTE] You will need a model with 100,000 Rate Limit (Tokens per minute) Rate Limit of 600 (Request per minute)
You can get model in Azure AI Foundry - Model and Endpoint.
-
AZURE_AI_AGENT_PROJECT_CONNECTION_STRING = "Your Azure AI Agent Service Project Connection String"
You can get the project connection string in your project overview in AI Foundry Portal Screen.
-
SERPAPI_SEARCH_API_KEY = "Your SERPAPI Search API KEY"
-
SERPAPI_SEARCH_ENDPOINT = "Your SERPAPI Search Endpoint"
To get the Model Deployment Name and Project Connection String of Azure AI Agent Service, you need to create Azure AI Agent Service. It is recommended to use this template to create it directly (Note: Azure AI Agent Service is currently set in a limited region. It is recommended that you refer to this link to set the region)
Agent needs to access SERPAPI. It is recommended to register using this link. After registration, you can obtain a unique API KEY and ENDPOINT
Setup
To run this notebook, you will need to make sure you've installed the required libraries by running pip install -r requirements.txt.
Your Semantic Kernel version should be at least 1.27.2.
Load your .env file setting and resources please ensure you have added your keys and setting and created a local .env file.
Log in to Azure
You now need to log in to Azure. Open a terminal and run the following command:
az login
This command will prompt you to enter your Azure credentials, enabling the Azure AI Agent service to function correctly.
Explanation:
This is a variable that stores the API key for accessing a SERP (Search Engine Results Page) API service. An API key is a unique identifier used to authenticate requests associated with your account.
Purpose: The purpose of this line is to store the API key in a variable so that it can be used to authenticate requests to the SERP API service. The API key is required to access the service and perform searches. How to Get a SERP API Key: To get a SERP API key, follow these general steps at https://serpapi.com (the exact steps may vary depending on the specific SERP API service you are using):
Choose a SERP API Service: There are several SERP API services available, such as SerpAPI, Google Custom Search JSON API, and others. Choose the one that best fits your needs.
Sign Up for an Account: Go to the website of the chosen SERP API service and sign up for an account. You may need to provide some basic information and verify your email address.
Create an API Key: After signing up, log in to your account and navigate to the API section or dashboard. Look for an option to create or generate a new API key. Copy the API Key to your .env file.
Explanation:
BASE_URL: This is a variable that stores the base URL for the SERP API endpoint. The variable name BASE_URL is a convention used to indicate that this URL is the starting point for making API requests. 'https://serpapi.com/search':
This is the actual URL string assigned to the BASE_URL variable. It represents the endpoint for performing search queries using the SERP API.
Purpose:
The purpose of this line is to define a constant that holds the base URL for the SERP API. This URL will be used as the starting point for constructing API requests to perform search operations.
Usage:
By defining the base URL in a variable, you can easily reuse it throughout your code whenever you need to make requests to the SERP API. This makes your code more maintainable and reduces the risk of errors from hardcoding the URL in multiple places. The current example is https://serpapi.com/search?engine=bing which is using Bing search API. Different API can be selected at https://Serpapi.com
Explanation:
This is where your plugin code is located.
Class Definition: class BookingPlugin: Defines a class named BookingPlugin that contains methods for booking hotels and flights.
Hotel Booking Method:
@kernel_function(description="booking hotel"): A decorator that describes the function as a kernel function for booking hotels.def booking_hotel(self, query: Annotated[str, "The name of the city"], check_in_date: Annotated[str, "Hotel Check-in Time"], check_out_date: Annotated[str, "Hotel Check-out Time"]) -> Annotated[str, "Return the result of booking hotel information"]:: Defines a method for booking hotels with annotated parameters and return type.
The method constructs a dictionary of parameters for the hotel booking request and sends a GET request to the SERP API. It checks the response status and returns the hotel properties if successful, or None if the request failed.
Flight Booking Method:
@kernel_function(description="booking flight"): A decorator that describes the function as a kernel function for booking flights.def booking_flight(self, origin: Annotated[str, "The name of Departure"], destination: Annotated[str, "The name of Destination"], outbound_date: Annotated[str, "The date of outbound"], return_date: Annotated[str, "The date of Return_date"]) -> Annotated[str, "Return the result of booking flight information"]:: Defines a method for booking flights with annotated parameters and return type.
The method constructs dictionaries of parameters for the outbound and return flight requests and sends GET requests to the SERP API. It checks the response status and appends the flight information to the result string if successful, or prints an error message if the request failed. The method returns the result string containing the flight information.
Explanation:
Import Statements: Import necessary modules for Azure credentials, AI agent, chat message content, author role, and kernel function decorator.
Asynchronous Context Manager: async with (DefaultAzureCredential() as creds, AzureAIAgent.create_client(credential=creds, conn_str="...") as client,): This sets up an asynchronous context manager to handle Azure credentials and create an AI agent client.
Agent Name and Instructions:
AGENT_NAME = "BookingAgent": Defines the name of the agent.AGENT_INSTRUCTIONS = """...""": Provides detailed instructions for the agent on how to handle booking requests.
Create Agent Definition: agent_definition = await client.agents.create_agent(...): Creates an agent definition with the specified model, name, and instructions.
Create AzureAI Agent: agent = AzureAIAgent(...): Creates an AzureAI agent using the client, agent definition, and the defined plugin.
Create Thread: thread: AzureAIAgentThread | None = None: Create a thread for the agent. It isn't required to first create a thread - if the value of None is provided, a new thread will be created during the first invocation and returned as part of the response.
User Inputs: user_inputs = ["..."]: Defines a list of user inputs for the agent to process.
In the finally block, delete the thread and agent to clean up resources.
Authentication
The DefaultAzureCredential class is part of the Azure SDK for Python. It provides a default way to authenticate with Azure services. It attempts to authenticate using multiple methods in a specific order, such as environment variables, managed identity, and Azure CLI credentials.
Asynchronous Operations: The aio module indicates that the DefaultAzureCredential class supports asynchronous operations. This means you can use it with asyncio to perform non-blocking authentication requests.