diff --git a/00-course-setup/README.md b/00-course-setup/README.md index 5f810114..7d7e2f86 100644 --- a/00-course-setup/README.md +++ b/00-course-setup/README.md @@ -13,7 +13,7 @@ This lesson will cover how to run the code samples of this course. To begin, please clone or fork the GitHub Repository. This will make your own version of the course material so that you can run, test and tweak the code! -This can be done by clicking link to [fork the repo](https://github.com/microsoft/ai-agents-for-beginners/fork){target="_blank"}. +This can be done by clicking link to fork the repo You should now have your own forked version of this course in the following link: @@ -25,7 +25,7 @@ Currently this course uses the Github Models Marketplace to offer free access to To access this service, you will need to create a GitHub Personal Access Token. -This can be done by going to your [Personal Access Tokens settings](https://github.com/settings/personal-access-tokens){target="_blank"} in your GitHub Account. +This can be done by going to your Personal Access Tokens settings in your GitHub Account. Select the `Fine-grained tokens` options on the left side of your screen. @@ -61,8 +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](https://discord.gg/kzRShWzttr){target="_blank"} or [create an issue](https://github.com/microsoft/ai-agents-for-beginners/issues?WT.mc_id=academic-105485-koreyst){target="_blank"}. - - - - +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 diff --git a/05-agentic-rag/code_samples/05-autogen.ipynb b/05-agentic-rag/code_samples/05-autogen.ipynb new file mode 100644 index 00000000..327fca68 --- /dev/null +++ b/05-agentic-rag/code_samples/05-autogen.ipynb @@ -0,0 +1,491 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Agentic RAG with Autogen\n", + "\n", + "This notebook demonstrates implementing Retrieval-Augmented Generation (RAG) using Autogen agents with enhanced evaluation capabilities." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Make sure to run this cell before running the rest of the notebook\n", + "%pip install chromadb" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install pysqlite3-binary\n", + "# __import__('pysqlite3')\n", + "# import sys\n", + "# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "from typing import List, Dict\n", + "import time\n", + "import os\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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create the Client \n", + "\n", + "First, we initialize the Azure AI Chat Completion Client. This client will be used to interact with the Azure OpenAI service to generate responses to user queries." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "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" + ] + } + ], + "source": [ + "client = AzureAIChatCompletionClient(\n", + " model=\"gpt-4o-mini\",\n", + " endpoint=\"https://models.inference.ai.azure.com\",\n", + " credential=AzureKeyCredential(os.environ[\"GITHUB_TOKEN\"]),\n", + " model_info={\n", + " \"json_output\": True,\n", + " \"function_calling\": True,\n", + " \"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", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Vector Database Initialization\n", + "\n", + "We initialize ChromaDB with persistent storage and add enhanced sample documents. ChromaDB will be used to store and retrieve documents that provide context for generating accurate responses." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "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" + ] + } + ], + "source": [ + "# Initialize ChromaDB with persistent storage\n", + "chroma_client = chromadb.PersistentClient(path=\"./chroma_db\")\n", + "collection = chroma_client.create_collection(\n", + " name=\"travel_documents\",\n", + " metadata={\"description\": \"travel_service\"},\n", + " get_or_create=True\n", + ")\n", + "\n", + "# Enhanced sample documents\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", + "# Add documents with metadata\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": [ + "## Agent Configuration\n", + "\n", + "We configure the retrieval and assistant agents. The retrieval agent is specialized in finding relevant information using semantic search, while the assistant generates detailed responses based on the retrieved information." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "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", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## RAGEvaluator Class\n", + "\n", + "We define the `RAGEvaluator` class to evaluate the response based on various metrics like response length, source citations, response time, and context relevance." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "class RAGEvaluator:\n", + " def __init__(self):\n", + " self.responses = []\n", + " self.metrics = {}\n", + " \n", + " def evaluate_response(self, query: str, response: str, context: List[str]) -> Dict:\n", + " # Calculate response time\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", + " '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", + " def _calculate_relevance(self, query: str, context: List[str]) -> float:\n", + " # Simple relevance scoring\n", + " return sum(1 for c in context if query.lower() in c.lower()) / len(context)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Query Processing with RAG\n", + "\n", + "We define the `ask_rag` function to send the query to the assistant, process the response, and evaluate it. This function handles the interaction with the assistant and uses the evaluator to measure the quality of the response." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "async def ask_rag(query: str, evaluator: RAGEvaluator):\n", + " try:\n", + " # Get response with timing\n", + " start_time = time.time()\n", + " response = await assistant.on_messages(\n", + " [TextMessage(content=query, source=\"user\")],\n", + " cancellation_token=CancellationToken(),\n", + " )\n", + " processing_time = time.time() - start_time\n", + " \n", + " # Evaluate response\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", + " }\n", + " except Exception as e:\n", + " print(f\"Error processing query: {e}\")\n", + " return None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Example usage\n", + "\n", + "We initialize the evaluator and define the queries that we want to process and evaluate." + ] + }, + { + "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, + "metadata": {}, + "outputs": [], + "source": [ + "async def main():\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']}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Run the Script\n", + "\n", + "We check if the script is running in an interactive environment or a standard script, and run the main function accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "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", + "\n", + "3. **Luxury Train Journeys**: Experiences like traveling on the Belmond Orient Express or Rocky Mountaineer, combining scenic vistas with sumptuous dining.\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", + "\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" + ] + } + ], + "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())" + ] + } + ], + "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 +}