Notebooks
M
Microsoft
05 Autogen Chromadb

05 Autogen Chromadb

agentic-ai05-agentic-ragai-agents-frameworkcode_samplesagentic-RAGagentic-frameworksemantic-kernelmicrosoft-ai-agents-for-beginnersgenerative-aiai-agentsautogen

Agentic RAG with Autogen

This notebook demonstrates implementing Retrieval-Augmented Generation (RAG) using Autogen agents with enhanced evaluation capabilities.

SQLite Version Fix If you encounter the error:

RuntimeError: Your system has an unsupported version of sqlite3. Chroma requires sqlite3 >= 3.35.0

Uncomment this code block at the start of your notebook:

[2]
[ ]
[3]
True

Create the Client

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.

[4]

Vector Database Initialization

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.

[5]

Context Provider Implementation

The ContextProvider class handles context retrieval and integration from multiple sources:

  1. Vector Database Retrieval: Uses ChromaDB to perform semantic search on travel documents
  2. Weather Information: Maintains a simulated weather database for major cities
  3. Unified Context: Combines both document and weather data into comprehensive context

Key methods:

  • get_retrieval_context(): Retrieves relevant documents based on query
  • get_weather_data(): Provides weather information for specified locations
  • get_unified_context(): Combines both document and weather contexts for enhanced responses
[6]

Agent Configuration

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.

[7]

Query Processing with RAG

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.

[8]

Example usage

We initialize the evaluator and define the queries that we want to process and evaluate.

[ ]

Run the Script

We check if the script is running in an interactive environment or a standard script, and run the main function accordingly.

[12]
=== Autogen RAG Demo ===


Query: What does Contoso's travel insurance cover?

--- Context Used ---
Retrieved Context:
Document: Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.
Metadata: {'source': 'training', 'type': 'explanation'}

Document: Contoso Travel offers luxury vacation packages to exotic destinations worldwide.
Metadata: {'source': 'training', 'type': 'explanation'}


-------------------

Response: Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.

==================================================


Query: What's the weather like in London?
Location: london

--- Context Used ---
Retrieved Context:
Document: Popular destinations include the Maldives, Swiss Alps, and African safaris.
Metadata: {'source': 'training', 'type': 'explanation'}

Document: Contoso Travel offers luxury vacation packages to exotic destinations worldwide.
Metadata: {'source': 'training', 'type': 'explanation'}


Weather Information for london:
Weather for London:
Temperature: 60°F
Condition: Rainy
Humidity: 80%
Wind: 15 mph
-------------------

Response: The weather in London is rainy with a temperature of 60°F, 80% humidity, and winds at 15 mph.

==================================================


Query: What luxury destinations does Contoso offer and what's the weather in Paris?
Location: paris

--- Context Used ---
Retrieved Context:
Document: Contoso Travel offers luxury vacation packages to exotic destinations worldwide.
Metadata: {'source': 'training', 'type': 'explanation'}

Document: Contoso Travel provides exclusive access to boutique hotels and private guided tours.
Metadata: {'source': 'training', 'type': 'explanation'}


Weather Information for paris:
Weather for Paris:
Temperature: 68°F
Condition: Cloudy
Humidity: 70%
Wind: 8 mph
-------------------

Response: Contoso Travel offers luxury vacation packages to exotic destinations worldwide and provides exclusive access to boutique hotels and private guided tours. The weather in Paris is cloudy with a temperature of 68°F, 70% humidity, and winds at 8 mph.

==================================================