diff --git a/00-course-setup/README.md b/00-course-setup/README.md
index 7d7e2f86..40436551 100644
--- a/00-course-setup/README.md
+++ b/00-course-setup/README.md
@@ -49,7 +49,7 @@ Open that file and paste the token you created into the `GITHUB_TOKEN=` field of
## Install Required Packages
-To ensure you have all the required Python packages for to run the code, run the following command into your terminal.
+To ensure you have all the required Python packages to run the code, run the following command into your terminal.
We recommend creating a Python virtual environment to avoid any conflicts and issues.
@@ -61,4 +61,4 @@ This should install the required Python packages.
You are now ready to run the code of this code, happy learning more about the world of AI Agents!
-If you have any issue running this setup, hop into our Azure AI Community Discord or create an issue.
\ No newline at end of file
+If you have any issues running this setup, hop into our Azure AI Community Discord or create an issue.
diff --git a/01-intro-to-ai-agents/README.md b/01-intro-to-ai-agents/README.md
index 420bee52..6efce8c5 100644
--- a/01-intro-to-ai-agents/README.md
+++ b/01-intro-to-ai-agents/README.md
@@ -11,7 +11,7 @@ To start this course, we begin by getting a better understanding of what AI Agen
This lesson covers:
- What are AI Agents and what are the different types of agents?
-- What use cases are best for AI Agents and how they can help us?
+- What use cases are best for AI Agents and how can they help us?
- What are some of the basic building blocks when designing Agentic Solutions?
## Learning Goals
@@ -34,11 +34,11 @@ Let's break this definition into smaller parts:
- **Sensors** - Environments have information and provide feedback. AI Agents use sensors to gather and interpret this information about the current state of the environment. In the Travel Booking Agent example, the travel booking system can provide information such as hotel availability or flight prices.
- **Actuators** - Once the AI Agent receives the current state of the environment, for the current task the agent determines what action to perform to change the environment. For the travel booking agent, it might be to book an available room for the user.
-
+
**Large Language Models** - The concept of agents existed before the creation of LLMs. The advantage of building AI Agents with LLMs is their ability to interpret human language and data. This ability enables LLMs to interpret environmental information and define a plan to change the environment.
-**Perform Actions** - Outside of AI Agent systems, LLMs are limited to situations where the action is generating content or information based on a user's prompt. Inside AI Agent systems, LLMs can accomplish tasks by interpreting the user's request and using tools that are available in its environment.
+**Perform Actions** - Outside of AI Agent systems, LLMs are limited to situations where the action is generating content or information based on a user's prompt. Inside AI Agent systems, LLMs can accomplish tasks by interpreting the user's request and using tools that are available in their environment.
**Access To Tools** - What tools the LLM has access to is defined by 1) the environment it's operating in and 2) the developer of the AI Agent. For our travel agent example, the agent's tools are limited by the operations available in the booking system, and/or the developer can limit the agent's tool access to flights.
@@ -52,7 +52,7 @@ Now that we have a general definition of AI Agents, let us look at some specific
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Simple Reflex Agents** | Perform immediate actions based on predefined rules. | Travel agent interprets the context of the email and forwards travel complaints to customer service. |
| **Model-Based Reflex Agents** | Perform actions based on a model of the world and changes to that model. | Travel agent prioritizes routes with significant price changes based on access to historical pricing data. |
-| **Goal-Based Agents** | Create plans to achieve specific goals by interpreting the goal and determining actions to reach it. | Travel agent books a journey by determining necessary travel arrangements (car, public transit, flights) from current location to destination. |
+| **Goal-Based Agents** | Create plans to achieve specific goals by interpreting the goal and determining actions to reach it. | Travel agent books a journey by determining necessary travel arrangements (car, public transit, flights) from the current location to the destination. |
| **Utility-Based Agents** | Consider preferences and weigh tradeoffs numerically to determine how to achieve goals. | Travel agent maximizes utility by weighing convenience vs. cost when booking travel. |
| **Learning Agents** | Improve over time by responding to feedback and adjusting actions accordingly. | Travel agent improves by using customer feedback from post-trip surveys to make adjustments to future bookings. |
| **Hierarchical Agents** | Feature multiple agents in a tiered system, with higher-level agents breaking tasks into subtasks for lower-level agents to complete. | Travel agent cancels a trip by dividing the task into subtasks (for example, canceling specific bookings) and having lower-level agents complete them, reporting back to the higher-level agent. |
@@ -64,7 +64,7 @@ In the earlier section, we used the Travel Agent use-case to explain how the dif
Let's look at the types of use cases that AI Agents are best used for:
-
+
- **Open-Ended Problems** - allowing the LLM to determine needed steps to complete a task because it can't always be hardcoded into a workflow.
diff --git a/03-agentic-design-patterns/README.md b/03-agentic-design-patterns/README.md
index 7375e42e..3014e696 100644
--- a/03-agentic-design-patterns/README.md
+++ b/03-agentic-design-patterns/README.md
@@ -27,7 +27,7 @@ After completing this lesson, you will be able to:
## The Agentic Design Principles
-
+
### Agent (Space)
diff --git a/04-tool-use/README.md b/04-tool-use/README.md
index 33b2c42c..6acbcdf9 100644
--- a/04-tool-use/README.md
+++ b/04-tool-use/README.md
@@ -267,7 +267,7 @@ Imagine you are a sales agent at a company called Contoso. You want to develop a
The following image illustrates how you could use Azure AI Agent Service to analyze your sales data:
-
+
To use any of these tools with the service we can create a client and define a tool or toolset. To implement this practically we can use the following Python code. The LLM will be able to look at the toolset and decide whether to use the user created function, `fetch_sales_data_using_sqlite_query`, or the pre-built Code Interpreter depending on the user request.
diff --git a/04-tool-use/code_samples/04-semantic-kernel-tool.ipynb b/04-tool-use/code_samples/04-semantic-kernel-tool.ipynb
index 1c318d9c..2751cb5d 100644
--- a/04-tool-use/code_samples/04-semantic-kernel-tool.ipynb
+++ b/04-tool-use/code_samples/04-semantic-kernel-tool.ipynb
@@ -16,11 +16,10 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
- "import asyncio\n",
"import os\n",
"import asyncio\n",
"\n",
@@ -228,7 +227,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": ".venv",
+ "display_name": "venv",
"language": "python",
"name": "python3"
},
@@ -242,7 +241,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.12.8"
+ "version": "3.12.4"
}
},
"nbformat": 4,
diff --git a/04-tool-use/code_samples/04-sematic-kernel-python-aiagent-bookinghotel.ipynb b/04-tool-use/code_samples/04-sematic-kernel-python-aiagent-bookinghotel.ipynb
new file mode 100644
index 00000000..853fb28d
--- /dev/null
+++ b/04-tool-use/code_samples/04-sematic-kernel-python-aiagent-bookinghotel.ipynb
@@ -0,0 +1,593 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Example Sample Hotel and Flight Booker Agent \n",
+ "\n",
+ "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'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Initialize the Azure AI Agent Service and get configuration information from **.env**\n",
+ "\n",
+ "### **.env** \n",
+ "\n",
+ "Create a .env file \n",
+ "\n",
+ "**.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.\n",
+ "\n",
+ "- **AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME** = \"Your Azure AI Agent Service Model Deployment Name\"\n",
+ "\n",
+ "[**NOTE**] You will need a model with 100,000 Rate Limit (Tokens per minute) Rate Limit of 600 (Request per minute)\n",
+ "\n",
+ " You can get model in Azure AI Foundry - Model and Endpoint. \n",
+ "\n",
+ "\n",
+ "- **AZURE_AI_AGENT_PROJECT_CONNECTION_STRING** = \"Your Azure AI Agent Service Project Connection String\"\n",
+ "\n",
+ " You can get the project connection string in your project overview in AI Foundry Portal Screen.\n",
+ "\n",
+ "- **SERPAPI_SEARCH_API_KEY** = \"Your SERPAPI Search API KEY\"\n",
+ "- **SERPAPI_SEARCH_ENDPOINT** = \"Your SERPAPI Search Endpoint\"\n",
+ "\n",
+ "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](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Ffosteramanda%2Fazure-agent-quickstart-templates%2Frefs%2Fheads%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fstandard-agent%2Fazuredeploy.json) 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](https://learn.microsoft.com/en-us/azure/ai-services/agents/concepts/model-region-support) to set the region)\n",
+ "\n",
+ "Agent needs to access SERPAPI. It is recommended to register using [this link](https://serpapi.com/searches). After registration, you can obtain a unique API KEY and ENDPOINT"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Setup \n",
+ "\n",
+ "To run this notebook, you will need to install the following libraries. Here is a list of the required libraries and the corresponding pip install commands:\n",
+ "\n",
+ "azure-identity: For Azure authentication.\n",
+ "requests: For making HTTP requests.\n",
+ "semantic-kernel: For the semantic kernel framework (assuming this is a custom or specific library, you might need to install it from a specific source or repository)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "!pip install azure-identity\n",
+ "!pip install requests\n",
+ "!pip install semantic-kernel\n",
+ "!pip install --upgrade semantic_kernel\n",
+ "!pip install azure-cli\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation: \n",
+ "import asyncio: This imports the asyncio module, which provides support for asynchronous programming in Python. It allows you to write concurrent code using the async and await syntax.\n",
+ "from typing import Annotated: This imports the Annotated type from the typing module. Annotated is used to add metadata to type hints, which can be useful for various purposes such as validation, documentation, or tooling."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "import asyncio\n",
+ "from typing import Annotated"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "Load your .env file setting and resources please ensure you have added your keys and setting and created a local .env file."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "from dotenv import load_dotenv\n",
+ "\n",
+ "# Load environment variables from .env file\n",
+ "load_dotenv()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Login to Azure \n",
+ "\n",
+ "You Now need to login into Azure Open a terminal and run the `az login` command"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "Import Statement: from azure.identity.aio import DefaultAzureCredential: This imports the DefaultAzureCredential class from the azure.identity.aio module. The aio part of the module name indicates that it is designed for asynchronous operations.\n",
+ "\n",
+ "Purpose of DefaultAzureCredential: 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.\n",
+ "\n",
+ "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."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from azure.identity.aio import DefaultAzureCredential"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "from semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings: This imports the AzureAIAgent and AzureAIAgentSettings classes from the semantic_kernel.agents.azure_ai module. These classes are likely used to configure and interact with an AI agent provided by Azure.\n",
+ "\n",
+ "from semantic_kernel.contents.chat_message_content import ChatMessageContent: This imports the ChatMessageContent class from the semantic_kernel.contents.chat_message_content module. This class is likely used to handle the content of chat messages.\n",
+ "\n",
+ "from semantic_kernel.contents.utils.author_role import AuthorRole: This imports the AuthorRole class from the semantic_kernel.contents.utils.author_role module. This class is likely used to define the role of the author in a chat message (e.g., user, assistant).\n",
+ "\n",
+ "from semantic_kernel.functions.kernel_function_decorator import kernel_function: This imports the kernel_function decorator from the semantic_kernel.functions.kernel_function_decorator module. This decorator is likely used to mark functions as kernel functions, which can be used within the semantic kernel framework.\n",
+ "\n",
+ "# Purpose of the Imports:\n",
+ "These imports set up the necessary components for working with an AI agent and handling chat messages within the semantic kernel framework. Here's a brief overview of each component:\n",
+ "\n",
+ "AzureAIAgent: This class is likely used to create and manage an AI agent that interacts with Azure AI services.\n",
+ "\n",
+ "AzureAIAgentSettings: This class is likely used to configure settings for the AzureAIAgent, such as authentication credentials and service endpoints.\n",
+ "\n",
+ "ChatMessageContent: This class is likely used to represent the content of chat messages, including text, metadata, and other relevant information.\n",
+ "\n",
+ "AuthorRole: This class is likely used to define the role of the author in a chat message, such as whether the message was sent by the user or the AI assistant.\n",
+ "\n",
+ "kernel_function: This decorator is likely used to mark functions as kernel functions, which can be used within the semantic kernel framework. Kernel functions are typically used to perform specific tasks or operations within the framework."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings\n",
+ "from semantic_kernel.contents.chat_message_content import ChatMessageContent\n",
+ "from semantic_kernel.contents.utils.author_role import AuthorRole\n",
+ "from semantic_kernel.functions.kernel_function_decorator import kernel_function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "This is a class imported from the semantic_kernel.agents.azure_ai module. It is used to configure settings for an AI agent that interacts with Azure AI services.\n",
+ "\n",
+ "create() Method: The create() method is a class method of AzureAIAgentSettings. It is used to create and initialize an instance of AzureAIAgentSettings with default or predefined settings.\n",
+ "\n",
+ "ai_agent_settings Variable: The variable ai_agent_settings is assigned the instance of AzureAIAgentSettings created by the create() method. This instance contains the configuration settings needed to set up and use an Azure AI agent.\n",
+ "\n",
+ "# Purpose:\n",
+ "The purpose of this line is to create a configuration object (ai_agent_settings) that holds the necessary settings for an Azure AI agent. These settings might include authentication credentials, service endpoints, and other configuration parameters required to interact with Azure AI services."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ai_agent_settings = AzureAIAgentSettings.create()\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "import requests: This statement imports the requests library, which is a popular third-party library in Python used for making HTTP requests.\n",
+ "\n",
+ "Purpose: The requests library simplifies the process of sending HTTP requests and handling responses. It provides a user-friendly interface for interacting with web services and APIs.\n",
+ "\n",
+ "Common Use Cases: Sending HTTP Requests: You can use the requests library to send various types of HTTP requests, such as GET, POST, PUT, DELETE, etc.\n",
+ "\n",
+ "Handling Responses:The library makes it easy to handle responses, including reading response content, headers, status codes, and more.\n",
+ "Working with APIs:\n",
+ "\n",
+ "The requests library is commonly used to interact with RESTful APIs, allowing you to send data to and retrieve data from web services."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import requests"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "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.\n",
+ "\n",
+ "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.\n",
+ "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):\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "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.\n",
+ "Copy the API Key to your .env file."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "SERP_API_KEY='SERPAPI_SEARCH_API_KEY'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "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.\n",
+ "'https://serpapi.com/search':\n",
+ "\n",
+ "This is the actual URL string assigned to the BASE_URL variable. It represents the endpoint for performing search queries using the SERP API.\n",
+ "\n",
+ "# Purpose:\n",
+ "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.\n",
+ "\n",
+ "# Usage:\n",
+ "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"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "BASE_URL = 'https://serpapi.com/search?engine=bing'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "This is where you agent code is located.\n",
+ "\n",
+ "Class Definition: class BookingPlugin:: Defines a class named BookingPlugin that contains methods for booking hotels and flights.\n",
+ "Docstring: \"\"\"Booking Plugin for customers\"\"\": A docstring that describes the purpose of the BookingPlugin class.\n",
+ "Hotel Booking Method:\n",
+ "\n",
+ "@kernel_function(description=\"booking hotel\"): A decorator that describes the function as a kernel function for booking hotels.\n",
+ "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.\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "Flight Booking Method: @kernel_function(description=\"booking flight\"): A decorator that describes the function as a kernel function for booking flights.\n",
+ "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.\n",
+ "\n",
+ "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.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Define Booking Plugin\n",
+ "class BookingPlugin:\n",
+ " \"\"\"Booking Plugin for customers\"\"\"\n",
+ "\n",
+ " @kernel_function(description=\"booking hotel\")\n",
+ " 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\"]:\n",
+ " \"\"\"\n",
+ " Function to book a hotel.\n",
+ " Parameters:\n",
+ " - query: The name of the city\n",
+ " - check_in_date: Hotel Check-in Time\n",
+ " - check_out_date: Hotel Check-out Time\n",
+ " Returns:\n",
+ " - The result of booking hotel information\n",
+ " \"\"\"\n",
+ "\n",
+ " # Define the parameters for the hotel booking request\n",
+ " params = {\n",
+ " \"engine\": \"google_hotels\",\n",
+ " \"q\": query,\n",
+ " \"check_in_date\": check_in_date,\n",
+ " \"check_out_date\": check_out_date,\n",
+ " \"adults\": \"1\",\n",
+ " \"currency\": \"GBP\",\n",
+ " \"gl\": \"uk\",\n",
+ " \"hl\": \"en\",\n",
+ " \"api_key\": SERP_API_KEY\n",
+ " }\n",
+ "\n",
+ " # Send the GET request to the SERP API\n",
+ " response = requests.get(BASE_URL, params=params)\n",
+ "\n",
+ " # Check if the request was successful\n",
+ " if response.status_code == 200:\n",
+ " # Parse the response content as JSON\n",
+ " response = response.json()\n",
+ " # Return the properties from the response\n",
+ " return response[\"properties\"]\n",
+ " else:\n",
+ " # Return None if the request failed\n",
+ " return None\n",
+ "\n",
+ " @kernel_function(description=\"booking flight\")\n",
+ " 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\"]:\n",
+ " \"\"\"\n",
+ " Function to book a flight.\n",
+ " Parameters:\n",
+ " - origin: The name of Departure\n",
+ " - destination: The name of Destination\n",
+ " - outbound_date: The date of outbound\n",
+ " - return_date: The date of Return_date\n",
+ " - airline: The preferred airline carrier\n",
+ " - hotel_brand: The preferred hotel brand\n",
+ " Returns:\n",
+ " - The result of booking flight information\n",
+ " \"\"\"\n",
+ " \n",
+ " # Define the parameters for the outbound flight request\n",
+ " go_params = {\n",
+ " \"engine\": \"google_flights\",\n",
+ " \"departure_id\": \"destination\",\n",
+ " \"arrival_id\": \"origin\",\n",
+ " \"outbound_date\": \"outbound_date\",\n",
+ " \"return_date\": \"return_date\",\n",
+ " \"currency\": \"GBP\",\n",
+ " \"hl\": \"en\",\n",
+ " \"airline\": \"airline\",\n",
+ " \"hotel_brand\": \"hotel_brand\",\n",
+ " \"api_key\": \"SERP_API_KEY\"\n",
+ " }\n",
+ " \n",
+ " \n",
+ " \n",
+ " print(go_params)\n",
+ "\n",
+ " # Send the GET request for the outbound flight\n",
+ " go_response = requests.get(BASE_URL, params=go_params)\n",
+ "\n",
+ " # Initialize the result string\n",
+ " result = ''\n",
+ "\n",
+ " # Check if the outbound flight request was successful\n",
+ " if go_response.status_code == 200:\n",
+ " # Parse the response content as JSON\n",
+ " response = go_response.json()\n",
+ " # Append the outbound flight information to the result\n",
+ " result += \"# outbound \\n \" + str(response)\n",
+ " else:\n",
+ " # Print an error message if the request failed\n",
+ " print('error!!!')\n",
+ "\n",
+ " # Define the parameters for the return flight request\n",
+ " back_params = {\n",
+ " #\"engine\": \"google_flights\",\n",
+ " \"departure_id\": destination,\n",
+ " \"arrival_id\": origin,\n",
+ " \"outbound_date\": outbound_date,\n",
+ " \"return_date\": return_date,\n",
+ " \"currency\": \"GBP\",\n",
+ " \"hl\": \"en\",\n",
+ " \"api_key\": SERP_API_KEY\n",
+ " }\n",
+ "\n",
+ " # Send the GET request for the return flight\n",
+ " back_response = requests.get(BASE_URL, params=back_params)\n",
+ "\n",
+ " # Check if the return flight request was successful\n",
+ " if back_response.status_code == 200:\n",
+ " # Parse the response content as JSON\n",
+ " response = back_response.json()\n",
+ " # Append the return flight information to the result\n",
+ " result += \"\\n # return \\n\" + str(response)\n",
+ " else:\n",
+ " # Print an error message if the request failed\n",
+ " print('error!!!')\n",
+ "\n",
+ " # Print the result\n",
+ " print(result)\n",
+ "\n",
+ " # Return the result\n",
+ " return result\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "Import Statements: Import necessary modules for Azure credentials, AI agent, chat message content, author role, and kernel function decorator.\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "Agent Name and Instructions: AGENT_NAME = \"BookingAgent\": Defines the name of the agent.\n",
+ "AGENT_INSTRUCTIONS = \"\"\"...\"\"\": Provides detailed instructions for the agent on how to handle booking requests.\n",
+ "\n",
+ "Create Agent Definition: agent_definition = await client.agents.create_agent(...): Creates an agent definition with the specified model, name, and instructions.\n",
+ "\n",
+ "Create AzureAI Agent: agent = AzureAIAgent(...): Creates an AzureAI agent using the client and agent definition.\n",
+ "\n",
+ "Add Booking Plugin: agent.kernel.add_plugin(BookingPlugin(), plugin_name=\"booking\"): Adds the BookingPlugin to the agent's kernel.\n",
+ "\n",
+ "Create Thread: thread = await client.agents.create_thread(): Creates a new thread for the agent.\n",
+ "\n",
+ "User Inputs: user_inputs = [\"...\"]: Defines a list of user inputs for the agent to process.\n",
+ "\n",
+ "Process User Inputs:For each user input, add it as a chat message, invoke the agent, and print the agent's response.\n",
+ "Clean Up:\n",
+ "\n",
+ "In the finally block, delete the thread and agent to clean up resources."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Import necessary modules\n",
+ "from azure.identity.aio import DefaultAzureCredential\n",
+ "from semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings\n",
+ "from semantic_kernel.contents.chat_message_content import ChatMessageContent\n",
+ "from semantic_kernel.contents.utils.author_role import AuthorRole\n",
+ "from semantic_kernel.functions.kernel_function_decorator import kernel_function\n",
+ "\n",
+ "# Azure AI Setting\n",
+ "async with (\n",
+ " DefaultAzureCredential() as creds,\n",
+ " AzureAIAgent.create_client(\n",
+ " credential=creds,\n",
+ " conn_str=ai_agent_settings.project_connection_string.get_secret_value(),\n",
+ " ) as client,\n",
+ "): \n",
+ " \n",
+ " # Define the agent's name and instructions\n",
+ " AGENT_NAME = \"BookingAgent\"\n",
+ " AGENT_INSTRUCTIONS = \"\"\"\n",
+ " You are a booking agent, help me to book flights or hotels.\n",
+ "\n",
+ " Thought: Understand the user's intention and confirm whether to use the reservation system to complete the task.\n",
+ "\n",
+ " Action:\n",
+ " - If booking a flight, convert the departure name and destination name into airport codes.\n",
+ " - If booking a hotel or flight, use the corresponding API to call. Ensure that the necessary parameters are available. If any parameters are missing, use default values or assumptions to proceed.\n",
+ " - If it is not a hotel or flight booking, respond with the final answer only.\n",
+ " - Output the results using a markdown table:\n",
+ " - For flight bookings, separate the outbound and return contents and list them in the order of Departure_airport Name | Airline | Flight Number | Departure Time | Arrival_airport Name | Arrival Time | Duration | Airplane | Travel Class | Price (USD) | Legroom | Extensions | Carbon Emissions (kg).\n",
+ " - For hotel bookings, list them in the order of Properties Name | Properties description | check_in_time | check_out_time | prices | nearby_places | hotel_class | gps_coordinates.\n",
+ "\"\"\"\n",
+ "\n",
+ " # Create agent definition with the specified model, name, and instructions\n",
+ " agent_definition = await client.agents.create_agent(\n",
+ " model=ai_agent_settings.model_deployment_name,\n",
+ " name=AGENT_NAME,\n",
+ " instructions=AGENT_INSTRUCTIONS,\n",
+ " )\n",
+ "\n",
+ " # Create the AzureAI Agent using the client and agent definition\n",
+ " agent = AzureAIAgent(\n",
+ " client=client,\n",
+ " definition=agent_definition,\n",
+ " # Optionally configure polling options\n",
+ " # polling_options=RunPollingOptions(run_polling_interval=timedelta(seconds=1)),\n",
+ " )\n",
+ "\n",
+ " # Add the BookingPlugin to the agent's kernel\n",
+ " agent.kernel.add_plugin(BookingPlugin(), plugin_name=\"booking\")\n",
+ "\n",
+ " # Create a new thread for the agent\n",
+ " thread = await client.agents.create_thread()\n",
+ "\n",
+ " # This is your prompt for the activity or task you want to complete \n",
+ " # Define user inputs for the agent to process we have provided some example prompts to test and validate \n",
+ " user_inputs = [\n",
+ " # \"Can you tell me the round-trip air ticket from London to New York JFK aiport, the departure time is February 17, 2025, and the return time is February 23, 2025\"\n",
+ " # \"Book a hotel in New York from Feb 20,2025 to Feb 24,2025\"\n",
+ " \"Help me book flight tickets and hotel for the following trip London Heathrow LHR Feb 20th 2025 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\"\n",
+ " # \"I have a business trip from London LHR to New York JFK on Feb 20th 2025 to Feb 27th 2025, can you help me to book a hotel and flight tickets\"\n",
+ " ]\n",
+ "\n",
+ " try:\n",
+ " # Process each user input\n",
+ " for user_input in user_inputs:\n",
+ " # Add the user input as a chat message\n",
+ " await agent.add_chat_message(\n",
+ " thread_id=thread.id, message=ChatMessageContent(role=AuthorRole.USER, content=user_input)\n",
+ " )\n",
+ " print(f\"# User: '{user_input}'\")\n",
+ " # Invoke the agent for the specified thread\n",
+ " async for content in agent.invoke(\n",
+ " thread_id=thread.id,\n",
+ " temperature=0.2, # Override the agent-level temperature setting with a run-time value\n",
+ " ):\n",
+ " if content.role != AuthorRole.TOOL:\n",
+ " print(f\"# Agent: {content.content}\")\n",
+ " finally:\n",
+ " # Clean up by deleting the thread and agent\n",
+ " await client.agents.delete_thread(thread.id)\n",
+ " await client.agents.delete_agent(agent.id)"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "3.12.1",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.1"
+ },
+ "polyglot_notebook": {
+ "kernelInfo": {
+ "defaultKernelName": "csharp",
+ "items": [
+ {
+ "aliases": [],
+ "name": "csharp"
+ }
+ ]
+ }
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/05-agentic-rag/code_samples/05-autogen.ipynb b/05-agentic-rag/code_samples/05-autogen.ipynb
index 327fca68..3af98912 100644
--- a/05-agentic-rag/code_samples/05-autogen.ipynb
+++ b/05-agentic-rag/code_samples/05-autogen.ipynb
@@ -11,9 +11,114 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 16,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: chromadb in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (0.6.3)\n",
+ "Requirement already satisfied: build>=1.0.3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.2.2.post1)\n",
+ "Requirement already satisfied: pydantic>=1.9 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (2.10.6)\n",
+ "Requirement already satisfied: chroma-hnswlib==0.7.6 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.7.6)\n",
+ "Requirement already satisfied: fastapi>=0.95.2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.115.8)\n",
+ "Requirement already satisfied: uvicorn>=0.18.3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.34.0)\n",
+ "Requirement already satisfied: numpy>=1.22.5 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (2.2.3)\n",
+ "Requirement already satisfied: posthog>=2.4.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (3.14.1)\n",
+ "Requirement already satisfied: typing_extensions>=4.5.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (4.12.2)\n",
+ "Requirement already satisfied: onnxruntime>=1.14.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.20.1)\n",
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.30.0)\n",
+ "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc>=1.2.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.30.0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.51b0)\n",
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.30.0)\n",
+ "Requirement already satisfied: tokenizers>=0.13.2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.21.0)\n",
+ "Requirement already satisfied: pypika>=0.48.9 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.48.9)\n",
+ "Requirement already satisfied: tqdm>=4.65.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (4.67.1)\n",
+ "Requirement already satisfied: overrides>=7.3.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (7.7.0)\n",
+ "Requirement already satisfied: importlib-resources in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (6.5.2)\n",
+ "Requirement already satisfied: grpcio>=1.58.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (1.70.0)\n",
+ "Requirement already satisfied: bcrypt>=4.0.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (4.2.1)\n",
+ "Requirement already satisfied: typer>=0.9.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.15.1)\n",
+ "Requirement already satisfied: kubernetes>=28.1.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (32.0.0)\n",
+ "Requirement already satisfied: tenacity>=8.2.3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (9.0.0)\n",
+ "Requirement already satisfied: PyYAML>=6.0.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (6.0.2)\n",
+ "Requirement already satisfied: mmh3>=4.0.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (5.1.0)\n",
+ "Requirement already satisfied: orjson>=3.9.12 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (3.10.15)\n",
+ "Requirement already satisfied: httpx>=0.27.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (0.28.1)\n",
+ "Requirement already satisfied: rich>=10.11.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from chromadb) (13.9.4)\n",
+ "Requirement already satisfied: packaging>=19.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from build>=1.0.3->chromadb) (24.2)\n",
+ "Requirement already satisfied: pyproject_hooks in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from build>=1.0.3->chromadb) (1.2.0)\n",
+ "Requirement already satisfied: colorama in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from build>=1.0.3->chromadb) (0.4.6)\n",
+ "Requirement already satisfied: starlette<0.46.0,>=0.40.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from fastapi>=0.95.2->chromadb) (0.45.3)\n",
+ "Requirement already satisfied: anyio in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from httpx>=0.27.0->chromadb) (4.8.0)\n",
+ "Requirement already satisfied: certifi in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from httpx>=0.27.0->chromadb) (2025.1.31)\n",
+ "Requirement already satisfied: httpcore==1.* in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from httpx>=0.27.0->chromadb) (1.0.7)\n",
+ "Requirement already satisfied: idna in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from httpx>=0.27.0->chromadb) (3.10)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from httpcore==1.*->httpx>=0.27.0->chromadb) (0.14.0)\n",
+ "Requirement already satisfied: six>=1.9.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (1.17.0)\n",
+ "Requirement already satisfied: python-dateutil>=2.5.3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (2.9.0.post0)\n",
+ "Requirement already satisfied: google-auth>=1.0.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (2.38.0)\n",
+ "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (1.8.0)\n",
+ "Requirement already satisfied: requests in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (2.32.3)\n",
+ "Requirement already satisfied: requests-oauthlib in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (2.0.0)\n",
+ "Requirement already satisfied: oauthlib>=3.2.2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (3.2.2)\n",
+ "Requirement already satisfied: urllib3>=1.24.2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (2.3.0)\n",
+ "Requirement already satisfied: durationpy>=0.7 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb) (0.9)\n",
+ "Requirement already satisfied: coloredlogs in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
+ "Requirement already satisfied: flatbuffers in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb) (25.2.10)\n",
+ "Requirement already satisfied: protobuf in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb) (5.29.3)\n",
+ "Requirement already satisfied: sympy in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb) (1.13.3)\n",
+ "Requirement already satisfied: deprecated>=1.2.6 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-api>=1.2.0->chromadb) (1.2.18)\n",
+ "Requirement already satisfied: importlib-metadata<=8.5.0,>=6.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-api>=1.2.0->chromadb) (8.5.0)\n",
+ "Requirement already satisfied: googleapis-common-protos~=1.52 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.67.0)\n",
+ "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.30.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.30.0)\n",
+ "Requirement already satisfied: opentelemetry-proto==1.30.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.30.0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation-asgi==0.51b0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.51b0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation==0.51b0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.51b0)\n",
+ "Requirement already satisfied: opentelemetry-semantic-conventions==0.51b0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.51b0)\n",
+ "Requirement already satisfied: opentelemetry-util-http==0.51b0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.51b0)\n",
+ "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation==0.51b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (1.17.2)\n",
+ "Requirement already satisfied: asgiref~=3.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from opentelemetry-instrumentation-asgi==0.51b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (3.8.1)\n",
+ "Requirement already satisfied: monotonic>=1.5 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
+ "Requirement already satisfied: backoff>=1.10.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from posthog>=2.4.0->chromadb) (2.2.1)\n",
+ "Requirement already satisfied: annotated-types>=0.6.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from pydantic>=1.9->chromadb) (0.7.0)\n",
+ "Requirement already satisfied: pydantic-core==2.27.2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from pydantic>=1.9->chromadb) (2.27.2)\n",
+ "Requirement already satisfied: markdown-it-py>=2.2.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from rich>=10.11.0->chromadb) (3.0.0)\n",
+ "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from rich>=10.11.0->chromadb) (2.19.1)\n",
+ "Requirement already satisfied: huggingface-hub<1.0,>=0.16.4 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from tokenizers>=0.13.2->chromadb) (0.28.1)\n",
+ "Requirement already satisfied: click>=8.0.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from typer>=0.9.0->chromadb) (8.1.8)\n",
+ "Requirement already satisfied: shellingham>=1.3.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from typer>=0.9.0->chromadb) (1.5.4)\n",
+ "Requirement already satisfied: httptools>=0.6.3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.6.4)\n",
+ "Requirement already satisfied: python-dotenv>=0.13 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.1)\n",
+ "Requirement already satisfied: watchfiles>=0.13 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.4)\n",
+ "Requirement already satisfied: websockets>=10.4 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb) (15.0)\n",
+ "Requirement already satisfied: cachetools<6.0,>=2.0.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (5.5.1)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.4.1)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (4.9)\n",
+ "Requirement already satisfied: filelock in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (3.17.0)\n",
+ "Requirement already satisfied: fsspec>=2023.5.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (2025.2.0)\n",
+ "Requirement already satisfied: zipp>=3.20 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from importlib-metadata<=8.5.0,>=6.0->opentelemetry-api>=1.2.0->chromadb) (3.21.0)\n",
+ "Requirement already satisfied: mdurl~=0.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->chromadb) (0.1.2)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from requests->kubernetes>=28.1.0->chromadb) (3.4.1)\n",
+ "Requirement already satisfied: sniffio>=1.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from anyio->httpx>=0.27.0->chromadb) (1.3.1)\n",
+ "Requirement already satisfied: humanfriendly>=9.1 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
+ "Requirement already satisfied: mpmath<1.4,>=1.1.0 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from sympy->onnxruntime>=1.14.1->chromadb) (1.3.0)\n",
+ "Requirement already satisfied: pyreadline3 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from humanfriendly>=9.1->coloredlogs->onnxruntime>=1.14.1->chromadb) (3.5.4)\n",
+ "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in e:\\coding material\\python pr\\mlsa blog\\github\\ai-agents-for-beginners\\venv\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.6.1)\n",
+ "Note: you may need to restart the kernel to use updated packages.\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "[notice] A new release of pip is available: 24.0 -> 25.0.1\n",
+ "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
+ ]
+ }
+ ],
"source": [
"# Make sure to run this cell before running the rest of the notebook\n",
"%pip install chromadb"
@@ -34,7 +139,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@@ -46,21 +151,22 @@
},
{
"cell_type": "code",
- "execution_count": 18,
+ "execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
- "from typing import List, Dict\n",
- "import time\n",
"import os\n",
+ "import time\n",
+ "import asyncio\n",
+ "from typing import List, Dict\n",
+ "\n",
"from autogen_agentchat.agents import AssistantAgent\n",
- "from autogen_core.models import UserMessage\n",
"from autogen_core import CancellationToken\n",
"from autogen_agentchat.messages import TextMessage\n",
"from azure.core.credentials import AzureKeyCredential\n",
"from autogen_ext.models.azure import AzureAIChatCompletionClient\n",
- "import chromadb\n",
- "import asyncio"
+ "\n",
+ "import chromadb"
]
},
{
@@ -74,17 +180,9 @@
},
{
"cell_type": "code",
- "execution_count": 19,
+ "execution_count": 3,
"metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "finish_reason='stop' content='The capital of France is Paris.' usage=RequestUsage(prompt_tokens=14, completion_tokens=7) cached=False logprobs=None thought=None\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"client = AzureAIChatCompletionClient(\n",
" model=\"gpt-4o-mini\",\n",
@@ -96,30 +194,6 @@
" \"vision\": True,\n",
" \"family\": \"unknown\",\n",
" },\n",
- ")\n",
- "result = await client.create([UserMessage(content=\"What is the capital of France?\", source=\"user\")])\n",
- "print(result)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Initialize Assistant Agent\n",
- "\n",
- "Next, we create an instance of the `AssistantAgent`. This agent will use the Azure AI Chat Completion Client to generate responses to user queries."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [],
- "source": [
- "assistant = AssistantAgent(\n",
- " name=\"assistant\",\n",
- " model_client=client,\n",
- " system_message=\"You are a helpful assistant.\",\n",
")"
]
},
@@ -134,26 +208,9 @@
},
{
"cell_type": "code",
- "execution_count": 21,
+ "execution_count": 4,
"metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Insert of existing embedding ID: doc_0\n",
- "Insert of existing embedding ID: doc_1\n",
- "Insert of existing embedding ID: doc_2\n",
- "Insert of existing embedding ID: doc_3\n",
- "Insert of existing embedding ID: doc_4\n",
- "Add of existing embedding ID: doc_0\n",
- "Add of existing embedding ID: doc_1\n",
- "Add of existing embedding ID: doc_2\n",
- "Add of existing embedding ID: doc_3\n",
- "Add of existing embedding ID: doc_4\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"# Initialize ChromaDB with persistent storage\n",
"chroma_client = chromadb.PersistentClient(path=\"./chroma_db\")\n",
@@ -180,6 +237,25 @@
")"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def get_retrieval_context(query: str) -> str:\n",
+ " results = collection.query(\n",
+ " query_texts=[query],\n",
+ " include=[\"documents\", \"metadatas\"],\n",
+ " n_results=2 # retrieve top 2 results for extra context\n",
+ " )\n",
+ " context_strings = []\n",
+ " if results and results.get(\"documents\") and len(results[\"documents\"][0]) > 0:\n",
+ " for doc, meta in zip(results[\"documents\"][0], results[\"metadatas\"][0]):\n",
+ " context_strings.append(f\"Document: {doc}\\nMetadata: {meta}\")\n",
+ " return \"\\n\\n\".join(context_strings) if context_strings else \"No results found\""
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -191,23 +267,18 @@
},
{
"cell_type": "code",
- "execution_count": 22,
+ "execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Create agents with enhanced capabilities\n",
- "retrieval_agent = AssistantAgent(\n",
- " name=\"retrieval_agent\",\n",
- " model_client=client,\n",
- " system_message=\"\"\"I am a retrieval agent specialized in finding relevant information.\n",
- " I use semantic search to find the most pertinent context for queries.\"\"\",\n",
- ")\n",
- "\n",
"assistant = AssistantAgent(\n",
" name=\"assistant\",\n",
- " system_message=\"\"\"I am an AI assistant that generates detailed responses based on retrieved information.\n",
- " I cite sources and explain my reasoning process.\"\"\",\n",
" model_client=client,\n",
+ " system_message=(\n",
+ " \"You are a helpful AI assistant that provides answers using ONLY the provided context. \"\n",
+ " \"Do NOT include any external information. Base your answer entirely on the context given below.\"\n",
+ " ),\n",
")"
]
},
@@ -222,36 +293,32 @@
},
{
"cell_type": "code",
- "execution_count": 23,
+ "execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class RAGEvaluator:\n",
" def __init__(self):\n",
- " self.responses = []\n",
- " self.metrics = {}\n",
- " \n",
+ " self.responses: List[Dict] = []\n",
+ "\n",
" def evaluate_response(self, query: str, response: str, context: List[str]) -> Dict:\n",
- " # Calculate response time\n",
+ " # Basic metrics: response length, citation count, and a simple relevance score.\n",
" start_time = time.time()\n",
- " \n",
" metrics = {\n",
" 'response_length': len(response),\n",
" 'source_citations': sum(1 for doc in context if doc in response),\n",
- " 'response_time': time.time() - start_time,\n",
+ " 'evaluation_time': time.time() - start_time,\n",
" 'context_relevance': self._calculate_relevance(query, context)\n",
" }\n",
- " \n",
" self.responses.append({\n",
" 'query': query,\n",
" 'response': response,\n",
" 'metrics': metrics\n",
" })\n",
- " \n",
" return metrics\n",
- " \n",
+ "\n",
" def _calculate_relevance(self, query: str, context: List[str]) -> float:\n",
- " # Simple relevance scoring\n",
+ " # Simple relevance score: fraction of the documents where the query appears.\n",
" return sum(1 for c in context if query.lower() in c.lower()) / len(context)"
]
},
@@ -266,29 +333,38 @@
},
{
"cell_type": "code",
- "execution_count": 24,
+ "execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"async def ask_rag(query: str, evaluator: RAGEvaluator):\n",
" try:\n",
- " # Get response with timing\n",
+ " retrieval_context = get_retrieval_context(query)\n",
+ " # Augment the query with the retrieval context.\n",
+ " augmented_query = (\n",
+ " f\"Retrieved Context:\\n{retrieval_context}\\n\\n\"\n",
+ " f\"User Query: {query}\\n\\n\"\n",
+ " \"Based ONLY on the above context, please provide the answer.\"\n",
+ " )\n",
+ "\n",
+ " # Send the augmented query as a user message.\n",
" start_time = time.time()\n",
" response = await assistant.on_messages(\n",
- " [TextMessage(content=query, source=\"user\")],\n",
+ " [TextMessage(content=augmented_query, source=\"user\")],\n",
" cancellation_token=CancellationToken(),\n",
" )\n",
" processing_time = time.time() - start_time\n",
- " \n",
- " # Evaluate response\n",
+ "\n",
+ " # Evaluate the response against our vector-store documents.\n",
" metrics = evaluator.evaluate_response(\n",
" query=query,\n",
" response=response.chat_message.content,\n",
" context=documents\n",
" )\n",
- " \n",
" return {\n",
" 'response': response.chat_message.content,\n",
+ " 'processing_time': processing_time,\n",
+ " 'metrics': metrics,\n",
" }\n",
" except Exception as e:\n",
" print(f\"Error processing query: {e}\")\n",
@@ -306,30 +382,22 @@
},
{
"cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [],
- "source": [
- "evaluator = RAGEvaluator()\n",
- "queries = [\n",
- " \"What luxury vacation packages does Contoso Travel offer?\",\n",
- " \"Can you explain Contoso's travel insurance coverage?\",\n",
- " \"What destinations and experiences are available through Contoso Travel?\"\n",
- "]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
+ "execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"async def main():\n",
+ " evaluator = RAGEvaluator()\n",
+ " queries = [\n",
+ " \"Can you explain Contoso's travel insurance coverage?\", # Relevant document\n",
+ " \"What is Neural Network?\" # No relevant document\n",
+ " ]\n",
" for query in queries:\n",
" print(f\"\\nProcessing Query: {query}\")\n",
" result = await ask_rag(query, evaluator)\n",
" if result:\n",
- " print(f\"Response: {result['response']}\")"
+ " print(\"Response:\", result['response'])\n",
+ " print(\"\\n\" + \"=\"*60 + \"\\n\")"
]
},
{
@@ -343,126 +411,33 @@
},
{
"cell_type": "code",
- "execution_count": 27,
+ "execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "\n",
- "Processing Query: What luxury vacation packages does Contoso Travel offer?\n",
- "Response: Contoso Travel, which typically represents a fictional or illustrative example in training materials, often provides a range of luxury vacation packages that cater to various tastes and preferences. While I don't have access to specific current offerings from a real company named Contoso Travel, I can outline some common features and types of luxury vacation packages that similar travel agencies might provide.\n",
- "\n",
- "1. **All-Inclusive Resorts**: Packages that include accommodations, meals, drinks, and activities at high-end resorts. Popular destinations might include the Caribbean, Maldives, and Tahiti.\n",
- "\n",
- "2. **Private Yacht Charters**: Luxury vacation packages that offer private yacht rentals, including crewed charters that can explore exotic locales while providing personalized services.\n",
- "\n",
- "3. **Luxury Safari Packages**: These often include stays in exclusive lodges or private villas in picturesque locations like Africa, complete with guided wildlife tours and cultural experiences.\n",
- "\n",
- "4. **Culinary or Wine Tours**: Gourmet experiences that might include private chef dinners, wine tastings in renowned vineyards, or cooking classes with local chefs in destinations famous for their cuisine.\n",
- "\n",
- "5. **Wellness Retreats**: Packages centered around relaxation and rejuvenation, including luxury spa treatments, wellness workshops, and yoga sessions in tranquil settings.\n",
- "\n",
- "6. **Adventure Trips**: Tailored trips that offer high-end experiences combining luxury accommodations with adventurous activities, such as guided trekking in the Himalayas or heli-skiing in Alaska.\n",
- "\n",
- "7. **Cultural Escapades**: Exclusive tours tailored to explore the rich history and culture of regions, often including private tours of museums and historical sites, along with accommodations in luxury hotels.\n",
- "\n",
- "8. **Honeymoon Packages**: Romantic getaways specifically designed for newlyweds, featuring beachfront bungalows, candlelit dinners, and couples' spa treatments.\n",
- "\n",
- "When exploring luxury vacation packages with a specific agency, it's advisable to directly visit their website or contact their customer service to examine current offerings, pricing, and customization options according to individual preferences.\n",
"\n",
"Processing Query: Can you explain Contoso's travel insurance coverage?\n",
- "Response: Since Contoso is often used as a fictional example in training materials, specific details about Contoso's travel insurance coverage are not available. However, I can provide an overview of what you might typically expect from travel insurance coverage offered by a company like Contoso Travel.\n",
- "\n",
- "### Typical Travel Insurance Coverage Components\n",
- "\n",
- "1. **Trip Cancellation/Interruption Insurance**: \n",
- " - This coverage provides reimbursement for non-refundable travel expenses if a trip must be canceled or interrupted due to covered reasons, such as illness, injury, a family emergency, or natural disasters.\n",
- "\n",
- "2. **Medical Coverage**:\n",
- " - This includes expenses for medical treatment if you become ill or injured while traveling. It may also cover emergency medical transportation and evacuation, especially important for international trips.\n",
- "\n",
- "3. **Baggage Coverage**: \n",
- " - This component covers lost, stolen, or damaged luggage and personal belongings. It may reimburse you for the replacement value of lost items or for the costs incurred to purchase essentials if your luggage is delayed.\n",
- "\n",
- "4. **Flight Delay/Cancellation Coverage**:\n",
- " - This reimbursement is for expenses incurred due to delays or cancellations of flights, such as accommodations, meals, and transportation.\n",
- "\n",
- "5. **24/7 Assistance Services**:\n",
- " - Many travel insurance policies offer emergency assistance hotlines that can help with medical appointments, legal referrals, and other urgent needs while traveling.\n",
- "\n",
- "6. **Travel Delay Coverage**:\n",
- " - Covers additional expenses incurred if your trip is delayed for reasons such as severe weather, strikes, or other unexpected events.\n",
- "\n",
- "7. **Accidental Death and Dismemberment Insurance**:\n",
- " - This provides a benefit in case of accidental death or severe injury while traveling.\n",
- "\n",
- "### Important Considerations\n",
- "\n",
- "- **Exclusions**: It’s crucial to read the policy to understand what's not covered. Common exclusions include pre-existing medical conditions, risky activities (like extreme sports), or cancellations due to personal reasons not categorized as emergencies.\n",
- " \n",
- "- **Deductibles**: Some policies may require you to pay a certain amount out of pocket before coverage kicks in.\n",
- "\n",
- "- **Limits on Coverage**: There may be caps on how much you can claim for different types of expenses (like medical costs or baggage loss).\n",
- "\n",
- "To get accurate and specific information regarding travel insurance options from Contoso Travel or any actual travel agency, I recommend checking their official website or contacting their customer service team directly. They can provide detailed policy documents and assist with any specific queries you may have.\n",
- "\n",
- "Processing Query: What destinations and experiences are available through Contoso Travel?\n",
- "Response: While Contoso Travel is a fictional example commonly used in training materials, I can provide an illustrative overview of typical destinations and experiences that a travel agency like Contoso might offer. Here are some common categories of destinations and experiences you could expect:\n",
- "\n",
- "### Popular Destinations\n",
- "\n",
- "1. **Tropical Islands**:\n",
- " - **Maldives**: Known for overwater bungalows, crystal-clear waters, and incredible snorkeling and diving opportunities.\n",
- " - **Bora Bora**: Famous for its picturesque lagoons and luxury resorts, perfect for honeymooners or couples.\n",
- "\n",
- "2. **European Cities**:\n",
- " - **Paris, France**: Tours that include visits to iconic landmarks like the Eiffel Tower, Louvre, and gourmet experiences in local cafes.\n",
- " - **Rome, Italy**: Historical tours exploring ancient ruins such as the Colosseum and Vatican City, along with culinary classes.\n",
- "\n",
- "3. **Adventure Destinations**:\n",
- " - **Costa Rica**: Packages that feature zip-lining through rainforests, surfing, and wildlife tours in national parks.\n",
- " - **New Zealand**: Experiences may include bungee jumping, hiking the Tongariro Alpine Crossing, and exploring the picturesque landscapes featured in \"The Lord of the Rings.\"\n",
- "\n",
- "4. **Cultural Experiences**:\n",
- " - **Kyoto, Japan**: Packages that allow travelers to visit traditional tea houses, the Arashiyama Bamboo Grove, and ancient temples.\n",
- " - **Marrakech, Morocco**: Immersive experiences exploring souks, trying traditional Moroccan cooking, and staying in luxury riads.\n",
- "\n",
- "5. **Safari Adventures**:\n",
- " - **Kenya and Tanzania**: Luxury lodges and guided safari tours to see the Big Five (lion, leopard, rhinoceros, elephant, and Cape buffalo) in their natural habitat.\n",
- "\n",
- "6. **Cruises**:\n",
- " - **Mediterranean Cruises**: Itineraries that include stops in multiple countries, such as Italy, Greece, and Spain, with opportunities for excursions in each port.\n",
- " - **Alaskan Cruises**: Experiences focused on wildlife viewing, glacier touring, and exploring quaint coastal towns.\n",
- "\n",
- "7. **Wellness Retreats**:\n",
- " - **Bali, Indonesia**: Packages that include yoga retreats, spa treatments, and holistic wellness programs in tranquil settings.\n",
- " - **Sedona, Arizona**: Experiences centered around healing, hiking, and spiritual wellness in a stunning natural environment.\n",
- "\n",
- "### Unique Experiences\n",
- "\n",
- "1. **Culinary Tours**: A focus on local food experiences, including cooking classes, wine tastings, and market visits to explore regional cuisines.\n",
- "\n",
- "2. **Wildlife Conservation Programs**: Opportunities to participate in volunteer programs that support wildlife conservation efforts, such as working with elephants in Thailand or rescuing sea turtles.\n",
+ "Response: Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.\n",
"\n",
- "3. **Luxury Train Journeys**: Experiences like traveling on the Belmond Orient Express or Rocky Mountaineer, combining scenic vistas with sumptuous dining.\n",
+ "============================================================\n",
"\n",
- "4. **Personalized Itineraries**: Customizable travel packages allowing clients to select their preferred activities, accommodations, and transportation options.\n",
"\n",
- "5. **Digital Nomad Packages**: Tailored itineraries for remote workers combining travel with work facilities, including co-working spaces in popular locations.\n",
+ "Processing Query: What is Neural Network?\n",
+ "Response: The provided context does not contain any information regarding Neural Networks.\n",
"\n",
- "For specific offerings, experiences, and destinations from Contoso Travel or any other real-world travel agency, it’s best to check their official website or contact them directly to explore current packages and promotional deals.\n"
+ "============================================================\n",
+ "\n"
]
}
],
"source": [
"if __name__ == \"__main__\":\n",
" if asyncio.get_event_loop().is_running():\n",
- " # Running in an interactive environment, use await main()\n",
" await main()\n",
" else:\n",
- " # Running in a standard script, use asyncio.run()\n",
" asyncio.run(main())"
]
}
diff --git a/05-agentic-rag/code_samples/05-semantic-kernel.ipynb b/05-agentic-rag/code_samples/05-semantic-kernel.ipynb
new file mode 100644
index 00000000..2a600f0a
--- /dev/null
+++ b/05-agentic-rag/code_samples/05-semantic-kernel.ipynb
@@ -0,0 +1,363 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Semantic Kernel Tool Use Example\n",
+ "\n",
+ "This document provides an overview and explanation of the code used to create a Semantic Kernel-based tool that integrates with ChromaDB for Retrieval-Augmented Generation (RAG). The example demonstrates how to build an AI agent that retrieves travel documents from a ChromaDB collection, augments user queries with semantic search results, and streams detailed travel recommendations."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Initializing the Environment"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "SQLite Version Fix\n",
+ "If you encounter the error:\n",
+ "```\n",
+ "RuntimeError: Your system has an unsupported version of sqlite3. Chroma requires sqlite3 >= 3.35.0\n",
+ "```\n",
+ "\n",
+ "Uncomment this code block at the start of your notebook:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# %pip install pysqlite3-binary\n",
+ "# __import__('pysqlite3')\n",
+ "# import sys\n",
+ "# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Importing Packages\n",
+ "The following code imports the necessary packages:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "import chromadb\n",
+ "\n",
+ "from openai import AsyncOpenAI\n",
+ "\n",
+ "from semantic_kernel.kernel import Kernel\n",
+ "from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion\n",
+ "from semantic_kernel.contents.chat_history import ChatHistory\n",
+ "from semantic_kernel.functions import kernel_function\n",
+ "from semantic_kernel.functions.kernel_arguments import KernelArguments\n",
+ "from semantic_kernel.connectors.ai import FunctionChoiceBehavior\n",
+ "from semantic_kernel.contents.function_call_content import FunctionCallContent\n",
+ "from semantic_kernel.contents.function_result_content import FunctionResultContent\n",
+ "from semantic_kernel.agents import ChatCompletionAgent"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Creating the Semantic Kernel and AI Service\n",
+ "\n",
+ "A Semantic Kernel instance is created and configured with an asynchronous OpenAI chat completion service. The service is added to the kernel for use in generating responses."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Initialize the asynchronous OpenAI client\n",
+ "client = AsyncOpenAI(\n",
+ " api_key=os.environ[\"GITHUB_TOKEN\"],\n",
+ " base_url=\"https://models.inference.ai.azure.com/\"\n",
+ ")\n",
+ "\n",
+ "# Create a Semantic Kernel instance and add an OpenAI chat completion service.\n",
+ "kernel = Kernel()\n",
+ "chat_completion_service = OpenAIChatCompletion(\n",
+ " ai_model_id=\"gpt-4o-mini\",\n",
+ " async_client=client,\n",
+ " service_id=\"agent\",\n",
+ ")\n",
+ "kernel.add_service(chat_completion_service)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Defining the Prompt Plugin\n",
+ "\n",
+ "The PromptPlugin is a native plugin that defines a function to build an augmented prompt using retrieval context"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "KernelPlugin(name='promptPlugin', description=None, functions={})"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "class PromptPlugin:\n",
+ " @kernel_function(\n",
+ " name=\"build_augmented_prompt\",\n",
+ " description=\"Build an augmented prompt using retrieval context.\"\n",
+ " )\n",
+ " @staticmethod\n",
+ " def build_augmented_prompt(query: str, retrieval_context: str) -> str:\n",
+ " return (\n",
+ " f\"Retrieved Context:\\n{retrieval_context}\\n\\n\"\n",
+ " f\"User Query: {query}\\n\\n\"\n",
+ " \"Based ONLY on the above context, please provide your answer.\"\n",
+ " )\n",
+ "\n",
+ "# Register the plugin with the kernel.\n",
+ "kernel.add_plugin(PromptPlugin(), plugin_name=\"promptPlugin\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Setting Up ChromaDB\n",
+ "\n",
+ "To facilitate Retrieval-Augmented Generation, a persistent ChromaDB client is instantiated and a collection named `\"travel_documents\"` is created (or retrieved if it exists). This collection is then populated with sample travel documents and metadata."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "collection = chromadb.PersistentClient(path=\"./chroma_db\").create_collection(\n",
+ " name=\"travel_documents\",\n",
+ " metadata={\"description\": \"travel_service\"},\n",
+ " get_or_create=True,\n",
+ ")\n",
+ "\n",
+ "documents = [\n",
+ " \"Contoso Travel offers luxury vacation packages to exotic destinations worldwide.\",\n",
+ " \"Our premium travel services include personalized itinerary planning and 24/7 concierge support.\",\n",
+ " \"Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.\",\n",
+ " \"Popular destinations include the Maldives, Swiss Alps, and African safaris.\",\n",
+ " \"Contoso Travel provides exclusive access to boutique hotels and private guided tours.\",\n",
+ "]\n",
+ "\n",
+ "collection.add(\n",
+ " documents=documents,\n",
+ " ids=[f\"doc_{i}\" for i in range(len(documents))],\n",
+ " metadatas=[{\"source\": \"training\", \"type\": \"explanation\"} for _ in documents]\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "A helper function `get_retrieval_context` is defined to query the collection and return the top two relevant documents (with metadata) based on the user query:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def get_retrieval_context(query: str) -> str:\n",
+ " results = collection.query(\n",
+ " query_texts=[query],\n",
+ " include=[\"documents\", \"metadatas\"],\n",
+ " n_results=2\n",
+ " )\n",
+ " context_entries = []\n",
+ " if results and results.get(\"documents\") and results[\"documents\"][0]:\n",
+ " for doc, meta in zip(results[\"documents\"][0], results[\"metadatas\"][0]):\n",
+ " context_entries.append(f\"Document: {doc}\\nMetadata: {meta}\")\n",
+ " return \"\\n\\n\".join(context_entries) if context_entries else \"No retrieval context found.\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Setting the Function Choice Behavior \n",
+ "\n",
+ "In Semantic Kernel, we have the ability to have some control of the agent choice of functions. This is done by using the `FunctionChoiceBehavior` class. \n",
+ "\n",
+ "The code below sets it to `Auto` which allows the agent to choose among the available functions or not choose any. \n",
+ "\n",
+ "This can also be set to:\n",
+ "`FunctionChoiceBehavior.Required` - to require the agent to choose at least one function \n",
+ "`FunctionChoiceBehavior.NoneInvoke` - instructs the agent to not choose any function. (good for testing)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "settings = kernel.get_prompt_execution_settings_from_service_id(\"agent\")\n",
+ "settings.function_choice_behavior = FunctionChoiceBehavior.Auto()\n",
+ "arguments = KernelArguments(settings=settings)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "AGENT_NAME = \"TravelAgent\"\n",
+ "AGENT_INSTRUCTIONS = (\n",
+ " \"Answer travel queries using the provided context. If context is provided, do not say 'I have no context for that.'\"\n",
+ ")\n",
+ "agent = ChatCompletionAgent(\n",
+ " service_id=\"agent\",\n",
+ " kernel=kernel,\n",
+ " name=AGENT_NAME,\n",
+ " instructions=AGENT_INSTRUCTIONS,\n",
+ " arguments=arguments,\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "A helper function `get_augmented_prompt` forces a call to the plugin to build the augmented prompt. It directly calls the static plugin method:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "async def get_augmented_prompt(query: str) -> str:\n",
+ " retrieval_context = get_retrieval_context(query)\n",
+ " return PromptPlugin.build_augmented_prompt(query, retrieval_context)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Running the Agent with Streaming Chat History\n",
+ "The main asynchronous loop creates a chat history for the conversation and, for each user input, first adds the augmented prompt (as a system message) to the chat history so that the agent sees the retrieval context. The user message is also added, and then the agent is invoked using streaming. The output is printed as it streams in."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "# User: 'Can you explain Contoso's travel insurance coverage?'\n",
+ "# Assistant - TravelAgent: 'Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage. This means that if you face any medical issues while traveling, need to cancel your trip, or have your baggage lost during your journey, you are covered under their insurance policy.\n",
+ "============================================================\n",
+ "\n",
+ "# User: 'What is Neural Network?'\n",
+ "# Assistant - TravelAgent: 'I have no context for that.\n",
+ "============================================================\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "async def main():\n",
+ " # Create a chat history.\n",
+ " chat_history = ChatHistory()\n",
+ " \n",
+ " user_inputs = [\n",
+ " \"Can you explain Contoso's travel insurance coverage?\", # Retrieval context available.\n",
+ " \"What is Neural Network?\" # No retrieval context available.\n",
+ " ]\n",
+ " \n",
+ " for user_input in user_inputs:\n",
+ " # Obtain the augmented prompt.\n",
+ " augmented_prompt = await get_augmented_prompt(user_input)\n",
+ " # Add the augmented prompt as a system message so the agent sees the retrieval context.\n",
+ " chat_history.add_system_message(augmented_prompt)\n",
+ " # Also add the user message.\n",
+ " chat_history.add_user_message(user_input)\n",
+ " \n",
+ " print(f\"# User: '{user_input}'\")\n",
+ " \n",
+ " agent_name: str | None = None\n",
+ " print(\"# Assistant - \", end=\"\")\n",
+ " async for content in agent.invoke_stream(chat_history):\n",
+ " if not agent_name:\n",
+ " agent_name = content.name\n",
+ " print(f\"{agent_name}: '\", end=\"\")\n",
+ " if (\n",
+ " not any(isinstance(item, (FunctionCallContent, FunctionResultContent))\n",
+ " for item in content.items)\n",
+ " and content.content.strip()\n",
+ " ):\n",
+ " print(f\"{content.content}\", end=\"\", flush=True)\n",
+ " print(\"\\n\" + \"=\"*60 + \"\\n\")\n",
+ " \n",
+ " # Clear chat history for the next query.\n",
+ " chat_history = ChatHistory()\n",
+ "\n",
+ "await main()"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "venv",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/08-multi-agent/README.md b/08-multi-agent/README.md
index f6739c82..7290ed87 100644
--- a/08-multi-agent/README.md
+++ b/08-multi-agent/README.md
@@ -29,7 +29,7 @@ This pattern is widely used in various fields, including robotics, autonomous sy
So what scenarios are a good use case for using multi-agents? The answer is that there are many scenarios where employing multiple agents is beneficial especially in the following cases:
-- **Large workloads**: Large workloads can be divided into smaller tasks and assigned to different agents, allowing for parallel processing and faster completion. Example of this is in the case of a large data processing task.
+- **Large workloads**: Large workloads can be divided into smaller tasks and assigned to different agents, allowing for parallel processing and faster completion. An example of this is in the case of a large data processing task.
- **Complex tasks**: Complex tasks, like large workloads, can be broken down into smaller subtasks and assigned to different agents, each specializing in a specific aspect of the task. A good example of this is in the case of autonomous vehicles where different agents manage navigation, obstacle detection, and communication with other vehicles.
- **Diverse expertise**: Different agents can have diverse expertise, allowing them to handle different aspects of a task more effectively than a single agent. For this case, a good example is in the case of healthcare where agents can manage diagnostics, treatment plans, and patient monitoring.
@@ -49,11 +49,11 @@ Compare this to a travel bureau run as a mom and pop store versus a travel burea
Before you can implement the multi-agent design pattern, you need to understand the building blocks that make up the pattern.
-Lets make this more concrete by again looking at the example of booking a trip for a user. In this case, the building blocks would include:
+Let's make this more concrete by again looking at the example of booking a trip for a user. In this case, the building blocks would include:
- **Agent Communication**: Agents for finding flights, booking hotels, and rental cars need to communicate and share information about the user's preferences and constraints. You need to decide on the protocols and methods for this communication. What this means concretely is that the agent for finding flights needs to communicate with the agent for booking hotels to ensure that the hotel is booked for the same dates as the flight. That means that the agents need to share information about the user's travel dates, meaning that you need to decide *which agents are sharing info and how they are sharing info*.
- **Coordination Mechanisms**: Agents need to coordinate their actions to ensure that the user's preferences and constraints are met. A user preference could be that they want a hotel close to the airport whereas a constraint could be that rental cars are only available at the airport. This means that the agent for booking hotels needs to coordinate with the agent for booking rental cars to ensure that the user's preferences and constraints are met. This means that you need to decide *how the agents are coordinating their actions*.
-- **Agent Architecture**: Agents need to have the internal structure to make decisions and learn from their interactions with the user. This means that the agent for finding flights needs to have the internal structure to make decisions about which flights to recommend to the user. This means that you need to decide *how the agents are making decisions and learning from their interactions with the user*. Examples on how an agent learn and improve could be that the agent for finding flights could use a machine learning model to recommend flights to the user based on their past preferences.
+- **Agent Architecture**: Agents need to have the internal structure to make decisions and learn from their interactions with the user. This means that the agent for finding flights needs to have the internal structure to make decisions about which flights to recommend to the user. This means that you need to decide *how the agents are making decisions and learning from their interactions with the user*. Examples of how an agent learns and improves could be that the agent for finding flights could use a machine learning model to recommend flights to the user based on their past preferences.
- **Visibility into Multi-Agent Interactions**: You need to have visibility into how the multiple agents are interacting with each other. This means that you need to have tools and techniques for tracking agent activities and interactions. This could be in the form of logging and monitoring tools, visualization tools, and performance metrics.
- **Multi-Agent Patterns**: There are different patterns for implementing multi-agent systems, such as centralized, decentralized, and hybrid architectures. You need to decide on the pattern that best fits your use case.
- **Human in the loop**: In most cases, you will have a human in the loop and you need to instruct the agents when to ask for human intervention. This could be in the form of a user asking for a specific hotel or flight that the agents have not recommended or asking for confirmation before booking a flight or hotel.
@@ -66,7 +66,7 @@ For example, in the case of booking a trip for a user, you could have a dashboar
Let's look at each of these aspects more in detail.
-- **Logging and Monitoring Tools**: You want to have logging done per each action taken by an agent. A log entry could store information on the agent that took the action, the action taken, the time the action was taken, and the outcome of the action. This information can then be used for debugging, optimizing and more.
+- **Logging and Monitoring Tools**: You want to have logging done for each action taken by an agent. A log entry could store information on the agent that took the action, the action taken, the time the action was taken, and the outcome of the action. This information can then be used for debugging, optimizing and more.
- **Visualization Tools**: Visualization tools can help you see the interactions between agents in a more intuitive way. For example, you could have a graph that shows the flow of information between agents. This could help you identify bottlenecks, inefficiencies, and other issues in the system.
@@ -112,11 +112,11 @@ Let's take an example where a user wants a recommendation on the best stock to b
## Scenario: Refund process
-Consider a scenario where a customer is trying to get refund for a product, there can be quite a few agents involved in this process but lets divide it up between agents specific for this process and general agents that can be used in other processes.
+Consider a scenario where a customer is trying to get a refund for a product, there can be quite a few agents involved in this process but let's divide it up between agents specific for this process and general agents that can be used in other processes.
**Agents specific for the refund process**:
-Following there are some agents that could be involved in the refund process:
+Following are some agents that could be involved in the refund process:
- **Customer agent**: This agent represents the customer and is responsible for initiating the refund process.
- **Seller agent**: This agent represents the seller and is responsible for processing the refund.
diff --git a/08-multi-agent/code_samples/08-python_multi_aiagent_bookinghotel.ipynb b/08-multi-agent/code_samples/08-python_multi_aiagent_bookinghotel.ipynb
new file mode 100644
index 00000000..a65b5400
--- /dev/null
+++ b/08-multi-agent/code_samples/08-python_multi_aiagent_bookinghotel.ipynb
@@ -0,0 +1,731 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## **Samples: Multi-AI Agents for Booking hotel**\n",
+ "\n",
+ "In today's fast-paced world, planning a business trip involves more than just booking a flight and a hotel room. It requires a level of coordination and efficiency that can be challenging to achieve. This is where Multi-AI Agents come into play, revolutionizing the way we manage our travel needs.\n",
+ "\n",
+ "Imagine having a team of intelligent agents at your disposal, working together to handle every aspect of your trip with precision and ease. With our advanced AI technology, we have created specialized agents for booking services and itinerary arrangement, ensuring a seamless and stress-free travel experience. \n",
+ "\n",
+ "This is a basic scenario. When planning a business trip, we need to consult with a business travel agent to obtain air ticket information, hotel information, etc. Through AI Agents, we can build agents for booking services and agents for itinerary arrangement to collaborate and improve the level of intelligence. \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Initialize the Azure AI Agent Service and get configuration information from **.env**\n",
+ "\n",
+ "### **.env** \n",
+ "\n",
+ "Create a .env file \n",
+ "\n",
+ "**.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.\n",
+ "\n",
+ "- **AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME** = \"Your Azure AI Agent Service Model Deployment Name\"\n",
+ "\n",
+ "[**NOTE**] You will need a model with 100,000 Rate Limit (Tokens per minute) Rate Limit of 600 (Request per minute)\n",
+ "\n",
+ " You can get model in Azure AI Foundry - Model and Endpoint. \n",
+ "\n",
+ "\n",
+ "- **AZURE_AI_AGENT_PROJECT_CONNECTION_STRING** = \"Your Azure AI Agent Service Project Connection String\"\n",
+ "\n",
+ " You can get the project connection string in your project overview in AI Foundry Portal Screen.\n",
+ "\n",
+ "- **SERPAPI_SEARCH_API_KEY** = \"Your SERPAPI Search API KEY\"\n",
+ "- **SERPAPI_SEARCH_ENDPOINT** = \"Your SERPAPI Search Endpoint\"\n",
+ "\n",
+ "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](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Ffosteramanda%2Fazure-agent-quickstart-templates%2Frefs%2Fheads%2Fmaster%2Fquickstarts%2Fmicrosoft.azure-ai-agent-service%2Fstandard-agent%2Fazuredeploy.json) 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](https://learn.microsoft.com/en-us/azure/ai-services/agents/concepts/model-region-support) to set the region)\n",
+ "\n",
+ "Agent needs to access SERPAPI. It is recommended to register using [this link](https://serpapi.com/searches). After registration, you can obtain a unique API KEY and ENDPOINT"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Login to Azure \n",
+ "\n",
+ "You Now need to login into Azure Open a terminal in VScode and run the `az login` command"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Setup \n",
+ "\n",
+ "To run this notebook, you will need to install the following libraries. Here is a list of the required libraries and the corresponding pip install commands:\n",
+ "\n",
+ "azure-identity: For Azure authentication.\n",
+ "requests: For making HTTP requests.\n",
+ "semantic-kernel: For the semantic kernel framework (assuming this is a custom or specific library, you might need to install it from a specific source or repository)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "!pip install azure-identity\n",
+ "!pip install requests\n",
+ "!pip install semantic-kernel\n",
+ "!pip install --upgrade semantic_kernel\n",
+ "!pip install azure-cli"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation: \n",
+ "import asyncio: This imports the asyncio module, which provides support for asynchronous programming in Python. It allows you to write concurrent code using the async and await syntax.\n",
+ "from typing import Annotated: This imports the Annotated type from the typing module. Annotated is used to add metadata to type hints, which can be useful for various purposes such as validation, documentation, or tooling"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "import asyncio,os\n",
+ "from typing import Annotated"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "By using from dotenv import load_dotenv and load_dotenv(), you can easily manage configuration settings and sensitive information (like API keys and database URLs) in a .env file, keeping them separate from your source code and making your application more secure and easier to configure."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "from dotenv import load_dotenv\n",
+ "\n",
+ "# Load environment variables from .env file\n",
+ "load_dotenv()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "Import Statement: from azure.identity.aio import DefaultAzureCredential: This imports the DefaultAzureCredential class from the azure.identity.aio module. The aio part of the module name indicates that it is designed for asynchronous operations.\n",
+ "\n",
+ "Purpose of DefaultAzureCredential: 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.\n",
+ "\n",
+ "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."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "from azure.identity.aio import DefaultAzureCredential"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "Imports various modules and classes from the semantic_kernel package. Here's a breakdown of each import:\n",
+ "\n",
+ "AgentGroupChat from semantic_kernel.agents: This class handles functionalities related to group chat for AI agents. AzureAIAgent and AzureAIAgentSettings from semantic_kernel.agents.azure_ai\n",
+ "\n",
+ "AzureAIAgent: This class is used to create and manage AI agents that utilize Azure AI services.\n",
+ "\n",
+ "AzureAIAgentSettings: This class is used to configure settings for the AzureAIAgent. TerminationStrategy from semantic_kernel.agents.strategies.termination.termination_strategy:\n",
+ "\n",
+ "This class defines strategies for terminating the execution of AI agents under certain conditions. ChatMessageContent from semantic_kernel.contents.chat_message_content:\n",
+ "\n",
+ "This class is used to handle the content of chat messages.\n",
+ "AuthorRole from semantic_kernel.contents.utils.author_role:\n",
+ "\n",
+ "This class defines different roles for authors in the context of chat messages. \n",
+ "\n",
+ "kernel_function from semantic_kernel.functions.kernel_function_decorator: This decorator is used to define kernel functions, which are functions that can be executed within the semantic kernel framework.\n",
+ "These imports set up the necessary components for creating and managing AI agents that can interact in a group chat environment, possibly for tasks such as booking hotels or similar activities."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "from semantic_kernel.agents import AgentGroupChat\n",
+ "from semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings\n",
+ "from semantic_kernel.agents.strategies.termination.termination_strategy import TerminationStrategy\n",
+ "from semantic_kernel.contents.chat_message_content import ChatMessageContent\n",
+ "from semantic_kernel.contents.utils.author_role import AuthorRole\n",
+ "from semantic_kernel.functions.kernel_function_decorator import kernel_function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "Next we import the CodeInterpreterTool class from the azure.ai.projects.models module. \n",
+ "\n",
+ "CodeInterpreterTool: This class is part of the Azure AI SDK and is used to interpret and execute code within the context of AI projects. It provides functionalities for running code snippets, analyzing code, or integrating code execution within AI workflows.\n",
+ "This import sets up the necessary component for utilizing the CodeInterpreterTool in your project, which could be useful for tasks that involve interpreting and executing code dynamically."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "from azure.ai.projects.models import CodeInterpreterTool"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation: \n",
+ "The ApprovalTerminationStrategy class provides a specific strategy for terminating an AI agent's operation. The agent will terminate if the last message in its interaction history contains the word \"saved\". This could be useful in scenarios where the agent's task is considered complete once it receives confirmation that something has been \"saved\".Define the interaction method. After the reservation plan is saved, it can be stopped when receiving the saved signal"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "class ApprovalTerminationStrategy(TerminationStrategy):\n",
+ " \"\"\"A strategy for determining when an agent should terminate.\"\"\"\n",
+ "\n",
+ " async def should_agent_terminate(self, agent, history):\n",
+ " \"\"\"Check if the agent should terminate.\"\"\"\n",
+ " return \"saved\" in history[-1].content.lower()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "\n",
+ "The line of code initializes an AzureAIAgentSettings object with default or predefined settings by calling the create() method. This settings object (ai_agent_settings) can then be used to configure and manage an AzureAIAgent instance."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "ai_agent_settings = AzureAIAgentSettings.create()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "By importing the requests library, you can easily make HTTP requests and interact with web services in your Python code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "import requests"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "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.\n",
+ "\n",
+ "'GOOGLE_SEARCH_API_KEY': This is a placeholder string. You need to replace ''GOOGLE_SEARCH_API_KEY' with your actual SERP API key.\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "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):\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "Sign Up for an Account:\n",
+ "\n",
+ "Go to the website of the chosen SERP API service https://www.serpapi.com and sign up for an account. You may need to provide some basic information and verify your email address.\n",
+ "\n",
+ "Create an API Key:\n",
+ "\n",
+ "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.\n",
+ "Copy the API Key:\n",
+ "\n",
+ "Once the API key is generated, copy it. This key will be used to authenticate your requests to the SERP API service.\n",
+ "Replace the Placeholder:\n",
+ "\n",
+ "Replace the placeholder in your .env file"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "SERPAPI_SEARCH_API_KEY=os.getenv('SERPAPI_SEARCH_API_KEY')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "SERPAPI_SEARCH_ENDPOINT = os.getenv('SERPAPI_SEARCH_ENDPOINT')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "The BookingPlugin class provides methods for booking hotels and flights using the Serpapi.com Google Search API. It constructs the necessary parameters, sends API requests, and processes the responses to return relevant booking information. The API key (SERPAPI_SEARCH_API_KEY) and endpoint (SERPAPI_SEARCH_ENDPOINT) are used to authenticate and send requests to the Google Search API."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# Define Booking Plugin\n",
+ "class BookingPlugin:\n",
+ " \"\"\"Booking Plugin for customers\"\"\"\n",
+ " @kernel_function(description=\"booking hotel\")\n",
+ " 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-in Time\"])-> Annotated[str, \"Return the result of booking hotel infomation\"]:\n",
+ "\n",
+ " params = {\n",
+ " \"engine\": \"google_hotels\",\n",
+ " \"q\": query,\n",
+ " \"check_in_date\": check_in_date,\n",
+ " \"check_out_date\": check_out_date,\n",
+ " \"adults\": \"2\",\n",
+ " \"currency\": \"USD\",\n",
+ " \"gl\": \"us\",\n",
+ " \"hl\": \"en\",\n",
+ " \"api_key\": SERPAPI_SEARCH_API_KEY\n",
+ " }\n",
+ "\n",
+ " response = requests.get(SERPAPI_SEARCH_ENDPOINT, params=params)\n",
+ " if response.status_code == 200:\n",
+ " response = response.json()\n",
+ " return response[\"properties\"]\n",
+ " else:\n",
+ " return None\n",
+ "\n",
+ " \n",
+ " @kernel_function(description=\"booking fight\")\n",
+ " def booking_fight(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 fight infomation\"]:\n",
+ " \n",
+ " go_params = {\n",
+ " \"engine\": \"google_flights\", \n",
+ " \"departure_id\": origin,\n",
+ " \"arrival_id\": destination,\n",
+ " \"outbound_date\": outbound_date,\n",
+ " \"return_date\": return_date, \n",
+ " \"currency\": \"USD\",\n",
+ " \"hl\": \"en\",\n",
+ " \"api_key\": SERPAPI_SEARCH_API_KEY \n",
+ " }\n",
+ "\n",
+ " print(go_params)\n",
+ "\n",
+ " go_response = requests.get(SERPAPI_SEARCH_ENDPOINT, params=go_params)\n",
+ "\n",
+ "\n",
+ " result = ''\n",
+ "\n",
+ " if go_response.status_code == 200:\n",
+ " response = go_response.json()\n",
+ "\n",
+ " result += \"# outbound \\n \" + str(response)\n",
+ " else:\n",
+ " print('error!!!')\n",
+ " # return None\n",
+ "\n",
+ " \n",
+ " back_params = {\n",
+ " \"engine\": \"google_flights\", \n",
+ " \"departure_id\": destination,\n",
+ " \"arrival_id\": origin,\n",
+ " \"outbound_date\": return_date,\n",
+ " \"return_date\": return_date, \n",
+ " \"currency\": \"USD\",\n",
+ " \"hl\": \"en\",\n",
+ " \"api_key\": SERPAPI_SEARCH_API_KEY \n",
+ " }\n",
+ "\n",
+ "\n",
+ " print(back_params)\n",
+ "\n",
+ "\n",
+ " back_response = requests.get(SERPAPI_SEARCH_ENDPOINT, params=back_params)\n",
+ "\n",
+ "\n",
+ "\n",
+ " if back_response.status_code == 200:\n",
+ " response = back_response.json()\n",
+ "\n",
+ " result += \"\\n # return \\n\" + str(response)\n",
+ "\n",
+ " else:\n",
+ " print('error!!!')\n",
+ " # return None\n",
+ " \n",
+ " print(result)\n",
+ "\n",
+ " return result\n",
+ "\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "The SavePlugin class provides a method saving_plan to save trip plans using Azure AI services. It sets up Azure credentials, creates an AI agent, processes user inputs to generate and save the trip plan content, and handles file saving and cleanup operations. The method returns \"Saved\" upon successful completion."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "class SavePlugin:\n",
+ " \"\"\"Save Plugin for customers\"\"\"\n",
+ " @kernel_function(description=\"saving plan\")\n",
+ " async def saving_plan(self,tripplan: Annotated[str, \"The content of trip plan\"])-> Annotated[str, \"Return status of save content\"]:\n",
+ "\n",
+ " async with (\n",
+ " DefaultAzureCredential() as creds,\n",
+ " AzureAIAgent.create_client(\n",
+ " credential=creds,\n",
+ " conn_str=ai_agent_settings.project_connection_string.get_secret_value(),\n",
+ " ) as client,\n",
+ " ):\n",
+ "\n",
+ " code_interpreter = CodeInterpreterTool()\n",
+ " \n",
+ " agent_definition = await client.agents.create_agent(\n",
+ " model=ai_agent_settings.model_deployment_name,\n",
+ " tools=code_interpreter.definitions,\n",
+ " tool_resources=code_interpreter.resources,\n",
+ " )\n",
+ "\n",
+ "\n",
+ " agent = AzureAIAgent(\n",
+ " client=client,\n",
+ " definition=agent_definition,\n",
+ " )\n",
+ "\n",
+ " thread = await client.agents.create_thread()\n",
+ "\n",
+ "\n",
+ " user_inputs = [\n",
+ " \"\"\"\n",
+ " \n",
+ " You are my Python programming assistant. Generate code,save \"\"\"+ tripplan +\n",
+ " \n",
+ " \"\"\" \n",
+ " and execute it according to the following requirements\n",
+ "\n",
+ " 1. Save blog content to trip-{YYMMDDHHMMSS}.md\n",
+ "\n",
+ " 2. give me the download this file link\n",
+ " \"\"\"\n",
+ " ]\n",
+ "\n",
+ "\n",
+ "\n",
+ " try:\n",
+ " for user_input in user_inputs:\n",
+ " # Add the user input as a chat message\n",
+ " await agent.add_chat_message(\n",
+ " thread_id=thread.id, message=ChatMessageContent(role=AuthorRole.USER, content=user_input)\n",
+ " )\n",
+ " print(f\"# User: '{user_input}'\")\n",
+ " # Invoke the agent for the specified thread\n",
+ " async for content in agent.invoke(thread_id=thread.id):\n",
+ " if content.role != AuthorRole.TOOL:\n",
+ " print(f\"# Agent: {content.content}\")\n",
+ "\n",
+ " \n",
+ " messages = await client.agents.list_messages(thread_id=thread.id)\n",
+ "\n",
+ " # OpenAIPageableListOfThreadMessage\n",
+ " # OpenAIPageableListOfThreadMessage\n",
+ "\n",
+ "\n",
+ " for file_path_annotation in messages.file_path_annotations:\n",
+ "\n",
+ " file_name = os.path.basename(file_path_annotation.text)\n",
+ "\n",
+ " await client.agents.save_file(file_id=file_path_annotation.file_path.file_id, file_name=file_name,target_dir=\"./trip\")\n",
+ "\n",
+ " \n",
+ " finally:\n",
+ " await client.agents.delete_thread(thread.id)\n",
+ " await client.agents.delete_agent(agent.id)\n",
+ "\n",
+ "\n",
+ " return \"Saved\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Explanation:\n",
+ "This code sets up Azure AI agents to handle booking flights and hotels, and saving trip plans based on user inputs. It uses Azure credentials to create and configure the agents, processes user inputs through a group chat, and ensures proper cleanup after the tasks are completed. The agents use specific plugins (BookingPlugin and SavePlugin) to perform their respective tasks.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "dotnet_interactive": {
+ "language": "csharp"
+ },
+ "polyglot_notebook": {
+ "kernelName": "csharp"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "async with (\n",
+ " DefaultAzureCredential() as creds,\n",
+ " AzureAIAgent.create_client(\n",
+ " credential=creds,\n",
+ " conn_str=ai_agent_settings.project_connection_string.get_secret_value(),\n",
+ " ) as client,\n",
+ "):\n",
+ " BOOKING_AGENT_NAME = \"BookingAgent\"\n",
+ " BOOKING_AGENT_INSTRUCTIONS = \"\"\"\n",
+ " You are a booking agent. Help me book flights or hotels.\n",
+ "\n",
+ " Thought: Please understand the user's intention and confirm whether to use the reservation system to complete the task.\n",
+ "\n",
+ " Actions:\n",
+ " - For flight bookings, convert the departure and destination names into airport codes.\n",
+ " - Use the appropriate API for hotel or flight bookings. Verify that all necessary parameters are available. If any parameters are missing, ask the user to provide them. If all parameters are complete, call the corresponding function.\n",
+ " - If the task is not related to hotel or flight booking, respond with the final answer only.\n",
+ " - Output the results using a markdown table:\n",
+ " - For flight bookings, output separate outbound and return contents in the order of:\n",
+ " Departure Airport | Airline | Flight Number | Departure Time | Arrival Airport | Arrival Time | Duration | Airplane | Travel Class | Price (USD) | Legroom | Extensions | Carbon Emissions (kg).\n",
+ " - For hotel bookings, output in the order of:\n",
+ " Property Name | Property Description | Check-in Time | Check-out Time | Prices | Nearby Places | Hotel Class | GPS Coordinates.\n",
+ " \"\"\"\n",
+ "\n",
+ " SAVE_AGENT_NAME = \"SaveAgent\"\n",
+ " SAVE_AGENT_INSTRUCTIONS = \"\"\"\n",
+ " You are a save tool agent. Help me to save the trip plan.\n",
+ " \"\"\"\n",
+ "\n",
+ " # Create agent definition\n",
+ " booking_agent_definition = await client.agents.create_agent(\n",
+ " model=ai_agent_settings.model_deployment_name,\n",
+ " name=BOOKING_AGENT_NAME,\n",
+ " instructions=BOOKING_AGENT_INSTRUCTIONS,\n",
+ " )\n",
+ "\n",
+ " # Create the AzureAI Agent\n",
+ " booking_agent = AzureAIAgent(\n",
+ " client=client,\n",
+ " definition=booking_agent_definition,\n",
+ " # Optionally configure polling options\n",
+ " # polling_options=RunPollingOptions(run_polling_interval=timedelta(seconds=1)),\n",
+ " )\n",
+ "\n",
+ " # Add the sample plugin to the kernel\n",
+ " booking_agent.kernel.add_plugin(BookingPlugin(), plugin_name=\"booking\")\n",
+ "\n",
+ " # Create agent definition\n",
+ " save_agent_definition = await client.agents.create_agent(\n",
+ " model=ai_agent_settings.model_deployment_name,\n",
+ " name=SAVE_AGENT_NAME,\n",
+ " instructions=SAVE_AGENT_INSTRUCTIONS\n",
+ " )\n",
+ "\n",
+ " # Create the AzureAI Agent\n",
+ " save_agent = AzureAIAgent(\n",
+ " client=client,\n",
+ " definition=save_agent_definition,\n",
+ " )\n",
+ "\n",
+ " save_agent.kernel.add_plugin(SavePlugin(), plugin_name=\"saving\")\n",
+ "\n",
+ " user_inputs = [\n",
+ " \"I have a business trip from London to New York in Feb 20 2025 to Feb 27 2025 ,help me to book a hotel and fight tickets and save it\"\n",
+ " ]\n",
+ "\n",
+ " chat = AgentGroupChat(\n",
+ " agents=[booking_agent, save_agent],\n",
+ " termination_strategy=ApprovalTerminationStrategy(agents=[save_agent], maximum_iterations=10),\n",
+ " )\n",
+ "\n",
+ " try:\n",
+ " for user_input in user_inputs:\n",
+ " # Add the user input as a chat message\n",
+ " await chat.add_chat_message(\n",
+ " ChatMessageContent(role=AuthorRole.USER, content=user_input)\n",
+ " )\n",
+ " print(f\"# User: '{user_input}'\")\n",
+ "\n",
+ " async for content in chat.invoke():\n",
+ " print(f\"# {content.role} - {content.name or '*'}: '{content.content}'\")\n",
+ "\n",
+ " print(f\"# IS COMPLETE: {chat.is_complete}\")\n",
+ "\n",
+ " print(\"*\" * 60)\n",
+ " print(\"Chat History (In Descending Order):\\n\")\n",
+ " async for message in chat.get_chat_messages(agent=save_agent):\n",
+ " print(f\"# {message.role} - {message.name or '*'}: '{message.content}'\")\n",
+ " finally:\n",
+ " await chat.reset()\n",
+ " await client.agents.delete_agent(save_agent.id)\n",
+ " await client.agents.delete_agent(booking_agent.id)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "3.12.1",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.1"
+ },
+ "polyglot_notebook": {
+ "kernelInfo": {
+ "defaultKernelName": "csharp",
+ "items": [
+ {
+ "aliases": [],
+ "name": "csharp"
+ }
+ ]
+ }
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/09-metacognition/README.md b/09-metacognition/README.md
index bbf72bd9..6079566e 100644
--- a/09-metacognition/README.md
+++ b/09-metacognition/README.md
@@ -14,7 +14,7 @@ After completing this lesson, you'll be able to:
## Introduction to Metacognition
-Metacognition refers to the higher-order cognitive processes that involve thinking about one’s own thinking. For AI agents, this means being able to evaluate and adjust their actions based on self-awareness and past experiences. Metacognition, or "thinking about thinking," is an important concept in the development of agentic AI systems. It involves AI systems being aware of their own internal processes and being able to monitor, regulate, and adapt their behavior accordingly. Much like we do when we read the room or look a problem. This self-awareness can help AI systems make better decisions, identify errors, and improve their performance over time- again linking back to turing test and the debate over is AI going to take over.
+Metacognition refers to the higher-order cognitive processes that involve thinking about one’s own thinking. For AI agents, this means being able to evaluate and adjust their actions based on self-awareness and past experiences. Metacognition, or "thinking about thinking," is an important concept in the development of agentic AI systems. It involves AI systems being aware of their own internal processes and being able to monitor, regulate, and adapt their behavior accordingly. Much like we do when we read the room or look at a problem. This self-awareness can help AI systems make better decisions, identify errors, and improve their performance over time- again linking back to the Turing test and the debate over whether AI is going to take over.
In the context of agentic AI systems, metacognition can help address several challenges, such as:
- Transparency: Ensuring that AI systems can explain their reasoning and decisions.
@@ -26,7 +26,7 @@ In the context of agentic AI systems, metacognition can help address several cha
Metacognition, or "thinking about thinking," is a higher-order cognitive process that involves self-awareness and self-regulation of one's cognitive processes. In the realm of AI, metacognition empowers agents to evaluate and adapt their strategies and actions, leading to improved problem-solving and decision-making capabilities. By understanding metacognition, you can design AI agents that are not only more intelligent but also more adaptable and efficient. In true metacognition, you’d see the AI explicitly reasoning about its own reasoning.
-Eample: “I prioritized cheaper flights because… I might be missing out on direct flights, so let me re-check.”.
+Example: “I prioritized cheaper flights because… I might be missing out on direct flights, so let me re-check.”.
Keeping track of how or why it chose a certain route.
- Noting that it made mistakes because it over-relied on user preferences from last time, so it modifies its decision-making strategy not just the final recommendation.
- Diagnosing patterns like, “Whenever I see the user mention ‘too crowded,’ I should not only remove certain attractions but also reflect that my method of picking ‘top attractions’ is flawed if I always rank by popularity.”
@@ -53,7 +53,7 @@ Before diving into metacognitive processes, it's essential to understand the bas
These components work together to create an "expertise unit" that can perform specific tasks.
**Example**:
-Consider a travel agent, agent services that not only plans the your holiday but also adjusts its path based on real-time data and past customer journey experiences.
+Consider a travel agent, agent services that not only plans your holiday but also adjusts its path based on real-time data and past customer journey experiences.
### Example: Metacognition in a Travel Agent Service
@@ -237,7 +237,7 @@ travel_agent.adjust_based_on_feedback(feedback)
## 3. Corrective RAG System
-Firstly lets start by understanding the difference between RAG Tool and Pre-emptive Context Load
+Firstly let's start by understanding the difference between RAG Tool and Pre-emptive Context Load

@@ -1109,9 +1109,9 @@ In this example, we'll design a code generating agent, Travel Agent, to assist u
print("Updated Itinerary:", updated_itinerary)
```
-### Leveraging environment awareness and reasoning
+### Leveraging environmental awareness and reasoning
-Based on the schema of the table can indeed enhance the query generation process by leveraging environment awareness and reasoning.
+Based on the schema of the table can indeed enhance the query generation process by leveraging environmental awareness and reasoning.
Here's an example of how this can be done:
diff --git a/10-ai-agents-production/README.md b/10-ai-agents-production/README.md
index 21b9fd8f..80f54397 100644
--- a/10-ai-agents-production/README.md
+++ b/10-ai-agents-production/README.md
@@ -20,9 +20,9 @@ It is important to deploy AI Agents that are trustworthy. Check out the "Buildin
## Evaluating AI Agents
-Before, during, and after deploying AI Agents, having a proper system to evaluate your AI Agents is critical. This will ensure that your system is aligned with your and your users' goals.
+Before, during, and after deploying AI Agents, having a proper system to evaluate your AI Agents is critical. This will ensure that your system is aligned with you and your users' goals.
-To evaluate an AI Agent, it is important to have the ability to evaluate not only the agent's output but the entire system that your AI Agent is operating in. This includes but is not limited to:
+To evaluate an AI Agent, it is important to have the ability to evaluate not only the agent's output but also the entire system that your AI Agent is operating in. This includes but is not limited to:
- The initial model request.
- The agent's ability to identify the intent of the user.
@@ -48,9 +48,9 @@ Here are some strategies to manage the costs of deploying AI Agents to productio
- **Caching Responses** - Identifying common requests and tasks and providing the responses before they go through your agentic system is a good way to reduce the volume of similar requests. You can even implement a flow to identify how similar a request is to your cached requests using more basic AI models.
-- **Using Smaller Models** - Small Language Models (SLMs) can perform well on certain agentic use-cases and will reduce costs significantly. As mentioned earlier, building an evaluation system to determine and compare performance vs larger models is the best way to understand of well an SLM will perform on your use case.
+- **Using Smaller Models** - Small Language Models (SLMs) can perform well on certain agentic use-cases and will reduce costs significantly. As mentioned earlier, building an evaluation system to determine and compare performance vs larger models is the best way to understand how well an SLM will perform on your use case.
-- **Using a Router Model** - A similar strategy is to use a diversity of models and sizes. You can user a LLM/SLM or serverless function to route requests based on complexity to the best fit models. This will also help reduce costs while also ensuring performance on the right tasks.
+- **Using a Router Model** - A similar strategy is to use a diversity of models and sizes. You can use an LLM/SLM or serverless function to route requests based on complexity to the best fit models. This will also help reduce costs while also ensuring performance on the right tasks.
## Congratulations
diff --git a/README.md b/README.md
index 1181a75b..c3c0d32c 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# AI Agents for Beginners - A Course
-
+
## 10 Lessons teaching everything you need to know to start building AI Agents
diff --git a/requirements.txt b/requirements.txt
index 976b74cb..873be035 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,4 +11,4 @@ autogen-agentchat
azure-ai-projects
autogen-ext==0.4.5
pydantic
-
+chromadb
\ No newline at end of file