Skip to content

Commit dd87ab5

Browse files
committed
Added Azure AI Search Option
For Agentic RAG using Azure AI Search Capability
1 parent 041df09 commit dd87ab5

File tree

4 files changed

+735
-2
lines changed

4 files changed

+735
-2
lines changed

.env.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ AZURE_OPENAI_ENDPOINT="https://..."
44
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME="..."
55
AZURE_OPENAI_API_VERSION="..."
66
PROJECT_CONNECTION_STRING="..."
7-
GITHUB_TOKEN="..."
7+
GITHUB_TOKEN="..."
8+
AZURE_SEARCH_SERVICE_ENDPOINT = "https://..."
9+
AZURE_SEARCH_API_KEY = "..."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Agentic RAG with Autogen using Azure AI Services\n",
8+
"\n",
9+
"This notebook demonstrates implementing Retrieval-Augmented Generation (RAG) using Autogen agents with enhanced evaluation capabilities."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import os\n",
19+
"import time\n",
20+
"import asyncio\n",
21+
"from typing import List, Dict\n",
22+
"\n",
23+
"from autogen_agentchat.agents import AssistantAgent\n",
24+
"from autogen_core import CancellationToken\n",
25+
"from autogen_agentchat.messages import TextMessage\n",
26+
"from azure.core.credentials import AzureKeyCredential\n",
27+
"from autogen_ext.models.azure import AzureAIChatCompletionClient\n",
28+
"\n",
29+
"from azure.search.documents import SearchClient\n",
30+
"from azure.search.documents.indexes import SearchIndexClient\n",
31+
"from azure.search.documents.indexes.models import SearchIndex, SimpleField, SearchFieldDataType, SearchableField\n"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"## Create the Client \n",
39+
"\n",
40+
"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."
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 2,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"client = AzureAIChatCompletionClient(\n",
50+
" model=\"gpt-4o-mini\",\n",
51+
" endpoint=\"https://models.inference.ai.azure.com\",\n",
52+
" credential=AzureKeyCredential(os.environ[\"GITHUB_TOKEN\"]),\n",
53+
" model_info={\n",
54+
" \"json_output\": True,\n",
55+
" \"function_calling\": True,\n",
56+
" \"vision\": True,\n",
57+
" \"family\": \"unknown\",\n",
58+
" },\n",
59+
")"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"## Vector Database Initialization\n",
67+
"\n",
68+
"We initialize Azure AI Search with persistent storage and add enhanced sample documents. Azure AI Search will be used to store and retrieve documents that provide context for generating accurate responses."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 3,
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"data": {
78+
"text/plain": [
79+
"[<azure.search.documents._generated.models._models_py3.IndexingResult at 0x1c479d5b6b0>,\n",
80+
" <azure.search.documents._generated.models._models_py3.IndexingResult at 0x1c479d5b710>,\n",
81+
" <azure.search.documents._generated.models._models_py3.IndexingResult at 0x1c479d5b6e0>,\n",
82+
" <azure.search.documents._generated.models._models_py3.IndexingResult at 0x1c479d5b740>,\n",
83+
" <azure.search.documents._generated.models._models_py3.IndexingResult at 0x1c479d5b770>]"
84+
]
85+
},
86+
"execution_count": 3,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"# Initialize Azure AI Search with persistent storage\n",
93+
"search_service_endpoint = os.getenv(\"AZURE_SEARCH_SERVICE_ENDPOINT\")\n",
94+
"search_api_key = os.getenv(\"AZURE_SEARCH_API_KEY\")\n",
95+
"index_name = \"travel-documents\"\n",
96+
"\n",
97+
"search_client = SearchClient(\n",
98+
" endpoint=search_service_endpoint,\n",
99+
" index_name=index_name,\n",
100+
" credential=AzureKeyCredential(search_api_key)\n",
101+
")\n",
102+
"\n",
103+
"index_client = SearchIndexClient(\n",
104+
" endpoint=search_service_endpoint,\n",
105+
" credential=AzureKeyCredential(search_api_key)\n",
106+
")\n",
107+
"\n",
108+
"# Define the index schema\n",
109+
"fields = [\n",
110+
" SimpleField(name=\"id\", type=SearchFieldDataType.String, key=True),\n",
111+
" SearchableField(name=\"content\", type=SearchFieldDataType.String)\n",
112+
"]\n",
113+
"\n",
114+
"index = SearchIndex(name=index_name, fields=fields)\n",
115+
"\n",
116+
"# Create the index\n",
117+
"index_client.create_index(index)\n",
118+
"\n",
119+
"# Enhanced sample documents\n",
120+
"documents = [\n",
121+
" {\"id\": \"1\", \"content\": \"Contoso Travel offers luxury vacation packages to exotic destinations worldwide.\"},\n",
122+
" {\"id\": \"2\", \"content\": \"Our premium travel services include personalized itinerary planning and 24/7 concierge support.\"},\n",
123+
" {\"id\": \"3\", \"content\": \"Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.\"},\n",
124+
" {\"id\": \"4\", \"content\": \"Popular destinations include the Maldives, Swiss Alps, and African safaris.\"},\n",
125+
" {\"id\": \"5\", \"content\": \"Contoso Travel provides exclusive access to boutique hotels and private guided tours.\"}\n",
126+
"]\n",
127+
"\n",
128+
"# Add documents to the index\n",
129+
"search_client.upload_documents(documents)\n"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"def get_retrieval_context(query: str) -> str:\n",
139+
" results = search_client.search(query)\n",
140+
" context_strings = []\n",
141+
" for result in results:\n",
142+
" context_strings.append(f\"Document: {result['content']}\")\n",
143+
" return \"\\n\\n\".join(context_strings) if context_strings else \"No results found\""
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"## Agent Configuration\n",
151+
"\n",
152+
"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."
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 5,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"# Create agents with enhanced capabilities\n",
162+
"assistant = AssistantAgent(\n",
163+
" name=\"assistant\",\n",
164+
" model_client=client,\n",
165+
" system_message=(\n",
166+
" \"You are a helpful AI assistant that provides answers using ONLY the provided context. \"\n",
167+
" \"Do NOT include any external information. Base your answer entirely on the context given below.\"\n",
168+
" ),\n",
169+
")"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"## RAGEvaluator Class\n",
177+
"\n",
178+
"We define the `RAGEvaluator` class to evaluate the response based on various metrics like response length, source citations, response time, and context relevance."
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 14,
184+
"metadata": {},
185+
"outputs": [],
186+
"source": [
187+
"class RAGEvaluator:\n",
188+
" def __init__(self):\n",
189+
" self.responses: List[Dict] = []\n",
190+
"\n",
191+
" def evaluate_response(self, query: str, response: str, context: List[Dict]) -> Dict:\n",
192+
" # Basic metrics: response length, citation count, and a simple relevance score.\n",
193+
" start_time = time.time()\n",
194+
" metrics = {\n",
195+
" 'response_length': len(response),\n",
196+
" 'source_citations': sum(1 for doc in context if doc[\"content\"] in response),\n",
197+
" 'evaluation_time': time.time() - start_time,\n",
198+
" 'context_relevance': self._calculate_relevance(query, context)\n",
199+
" }\n",
200+
" self.responses.append({\n",
201+
" 'query': query,\n",
202+
" 'response': response,\n",
203+
" 'metrics': metrics\n",
204+
" })\n",
205+
" return metrics\n",
206+
"\n",
207+
" def _calculate_relevance(self, query: str, context: List[Dict]) -> float:\n",
208+
" # Simple relevance score: fraction of the documents where the query appears.\n",
209+
" return sum(1 for c in context if query.lower() in c[\"content\"].lower()) / len(context)"
210+
]
211+
},
212+
{
213+
"cell_type": "markdown",
214+
"metadata": {},
215+
"source": [
216+
"## Query Processing with RAG\n",
217+
"\n",
218+
"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."
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 15,
224+
"metadata": {},
225+
"outputs": [],
226+
"source": [
227+
"async def ask_rag(query: str, evaluator: RAGEvaluator):\n",
228+
" try:\n",
229+
" retrieval_context = get_retrieval_context(query)\n",
230+
" # Augment the query with the retrieval context.\n",
231+
" augmented_query = (\n",
232+
" f\"Retrieved Context:\\n{retrieval_context}\\n\\n\"\n",
233+
" f\"User Query: {query}\\n\\n\"\n",
234+
" \"Based ONLY on the above context, please provide the answer.\"\n",
235+
" )\n",
236+
"\n",
237+
" # Send the augmented query as a user message.\n",
238+
" start_time = time.time()\n",
239+
" response = await assistant.on_messages(\n",
240+
" [TextMessage(content=augmented_query, source=\"user\")],\n",
241+
" cancellation_token=CancellationToken(),\n",
242+
" )\n",
243+
" processing_time = time.time() - start_time\n",
244+
"\n",
245+
" # Evaluate the response against our vector-store documents.\n",
246+
" metrics = evaluator.evaluate_response(\n",
247+
" query=query,\n",
248+
" response=response.chat_message.content,\n",
249+
" context=documents\n",
250+
" )\n",
251+
" return {\n",
252+
" 'response': response.chat_message.content,\n",
253+
" 'processing_time': processing_time,\n",
254+
" 'metrics': metrics,\n",
255+
" }\n",
256+
" except Exception as e:\n",
257+
" print(f\"Error processing query: {e}\")\n",
258+
" return None"
259+
]
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"metadata": {},
264+
"source": [
265+
"# Example usage\n",
266+
"\n",
267+
"We initialize the evaluator and define the queries that we want to process and evaluate."
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": 16,
273+
"metadata": {},
274+
"outputs": [],
275+
"source": [
276+
"async def main():\n",
277+
" evaluator = RAGEvaluator()\n",
278+
" queries = [\n",
279+
" \"Can you explain Contoso's travel insurance coverage?\", # Relevant document\n",
280+
" \"What is Neural Network?\" # No relevant document\n",
281+
" ]\n",
282+
" for query in queries:\n",
283+
" print(f\"\\nProcessing Query: {query}\")\n",
284+
" result = await ask_rag(query, evaluator)\n",
285+
" if result:\n",
286+
" print(\"Response:\", result['response'])\n",
287+
" print(\"\\n\" + \"=\"*60 + \"\\n\")"
288+
]
289+
},
290+
{
291+
"cell_type": "markdown",
292+
"metadata": {},
293+
"source": [
294+
"## Run the Script\n",
295+
"\n",
296+
"We check if the script is running in an interactive environment or a standard script, and run the main function accordingly."
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": 17,
302+
"metadata": {},
303+
"outputs": [
304+
{
305+
"name": "stdout",
306+
"output_type": "stream",
307+
"text": [
308+
"\n",
309+
"Processing Query: Can you explain Contoso's travel insurance coverage?\n",
310+
"Response: Contoso's travel insurance covers medical emergencies, trip cancellations, and lost baggage.\n",
311+
"\n",
312+
"============================================================\n",
313+
"\n",
314+
"\n",
315+
"Processing Query: What is Neural Network?\n",
316+
"Response: No information is available on Neural Network based on the provided context.\n",
317+
"\n",
318+
"============================================================\n",
319+
"\n"
320+
]
321+
}
322+
],
323+
"source": [
324+
"if __name__ == \"__main__\":\n",
325+
" if asyncio.get_event_loop().is_running():\n",
326+
" await main()\n",
327+
" else:\n",
328+
" asyncio.run(main())"
329+
]
330+
},
331+
{
332+
"cell_type": "code",
333+
"execution_count": null,
334+
"metadata": {},
335+
"outputs": [],
336+
"source": []
337+
}
338+
],
339+
"metadata": {
340+
"kernelspec": {
341+
"display_name": "venv",
342+
"language": "python",
343+
"name": "python3"
344+
},
345+
"language_info": {
346+
"codemirror_mode": {
347+
"name": "ipython",
348+
"version": 3
349+
},
350+
"file_extension": ".py",
351+
"mimetype": "text/x-python",
352+
"name": "python",
353+
"nbconvert_exporter": "python",
354+
"pygments_lexer": "ipython3",
355+
"version": "3.12.4"
356+
}
357+
},
358+
"nbformat": 4,
359+
"nbformat_minor": 2
360+
}

0 commit comments

Comments
 (0)