Skip to content

Commit 57a14db

Browse files
committed
updates
1 parent ecb7521 commit 57a14db

File tree

4 files changed

+99
-91
lines changed

4 files changed

+99
-91
lines changed

Quickstart-Agentic-Retrieval/quickstart.ipynb

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,15 @@
235235
},
236236
{
237237
"cell_type": "code",
238-
"execution_count": 6,
238+
"execution_count": 7,
239239
"id": "918ded26",
240240
"metadata": {},
241241
"outputs": [],
242242
"source": [
243243
"from azure.search.documents.agent import KnowledgeAgentRetrievalClient\n",
244244
"from azure.search.documents.agent.models import KnowledgeAgentRetrievalRequest, KnowledgeAgentMessage, KnowledgeAgentMessageTextContent, KnowledgeAgentIndexParams\n",
245-
"import azure.search.documents._utils\n",
246245
"\n",
247-
"agent_client = KnowledgeAgentRetrievalClient(endpoint=endpoint, agent_name=agent_name, authentication_policy=azure.search.documents._utils.get_authentication_policy(credential))\n",
246+
"agent_client = KnowledgeAgentRetrievalClient(endpoint=endpoint, agent_name=agent_name, credential=credential)\n",
248247
"\n",
249248
"messages.append({\n",
250249
" \"role\": \"user\",\n",
@@ -276,7 +275,7 @@
276275
},
277276
{
278277
"cell_type": "code",
279-
"execution_count": 7,
278+
"execution_count": 8,
280279
"id": "d4d78fbe",
281280
"metadata": {},
282281
"outputs": [
@@ -334,7 +333,7 @@
334333
},
335334
{
336335
"cell_type": "code",
337-
"execution_count": 8,
336+
"execution_count": 9,
338337
"id": "7fccf4b0",
339338
"metadata": {},
340339
"outputs": [
@@ -348,7 +347,7 @@
348347
" \"id\": 0,\n",
349348
" \"type\": \"ModelQueryPlanning\",\n",
350349
" \"input_tokens\": 1407,\n",
351-
" \"output_tokens\": 318\n",
350+
" \"output_tokens\": 311\n",
352351
" },\n",
353352
" {\n",
354353
" \"id\": 1,\n",
@@ -357,8 +356,8 @@
357356
" \"query\": {\n",
358357
" \"search\": \"suburban belts December brightening urban cores light levels\"\n",
359358
" },\n",
360-
" \"query_time\": \"2025-05-02T04:50:56.846Z\",\n",
361-
" \"elapsed_ms\": 611\n",
359+
" \"query_time\": \"2025-05-06T15:43:41.162Z\",\n",
360+
" \"elapsed_ms\": 714\n",
362361
" },\n",
363362
" {\n",
364363
" \"id\": 2,\n",
@@ -367,9 +366,9 @@
367366
" \"query\": {\n",
368367
" \"search\": \"Phoenix nighttime street grid visibility from space\"\n",
369368
" },\n",
370-
" \"query_time\": \"2025-05-02T04:50:57.331Z\",\n",
369+
" \"query_time\": \"2025-05-06T15:43:41.683Z\",\n",
371370
" \"count\": 2,\n",
372-
" \"elapsed_ms\": 485\n",
371+
" \"elapsed_ms\": 520\n",
373372
" }\n",
374373
"]\n",
375374
"Results\n",
@@ -408,7 +407,7 @@
408407
},
409408
{
410409
"cell_type": "code",
411-
"execution_count": 9,
410+
"execution_count": 10,
412411
"id": "da260539",
413412
"metadata": {},
414413
"outputs": [],
@@ -434,25 +433,24 @@
434433
},
435434
{
436435
"cell_type": "code",
437-
"execution_count": 10,
436+
"execution_count": 11,
438437
"id": "b2dea5c2",
439438
"metadata": {},
440439
"outputs": [
441440
{
442441
"name": "stdout",
443442
"output_type": "stream",
444443
"text": [
445-
"Suburban belts display larger December brightening than urban cores because seasonal lighting, such\n",
446-
"as holiday decorations, tends to be more prevalent in residential areas where individual homes often\n",
447-
"contribute significantly to increased brightness. In contrast, urban cores, while having higher\n",
448-
"absolute light levels, don't experience as much variability due to the consistent presence of\n",
449-
"streetlights, commercial lighting, and less individual contribution to seasonal displays [ref_id:0].\n",
450-
"The Phoenix nighttime street grid is sharply visible from space because it is laid out in a regular\n",
451-
"grid of city blocks and streets, typical of western U.S. cities. This lighting pattern is evident\n",
452-
"from the low-Earth-orbit vantage point and highlights the extensive spread of urbanization. In\n",
453-
"contrast, large stretches of interstate between midwestern cities remain comparatively dim as these\n",
454-
"areas primarily consist of long, undeveloped stretches without urban lighting patterns, typical of\n",
455-
"agricultural or undeveloped land [ref_id:1].\n"
444+
"Suburban belts tend to display larger December brightening compared to urban cores even though the\n",
445+
"absolute light levels are higher downtown because suburban areas often have more residential\n",
446+
"Christmas lighting during the holiday season. In contrast, urban cores have more constant commercial\n",
447+
"lighting throughout the year, which changes less dramatically in December [Source 0]. The Phoenix\n",
448+
"nighttime street grid is sharply visible from space because the city's urban layout follows a\n",
449+
"regular grid pattern typical of many Western U.S. cities. This grid pattern, along with the street\n",
450+
"lighting, becomes distinctly visible from space. In contrast, large stretches of interstate between\n",
451+
"Midwestern cities are dimmer due to lower population density and reduced road lighting, as they pass\n",
452+
"through more rural areas without the extensive grid-like street lighting found in metropolitan areas\n",
453+
"like Phoenix [Source 1].\n"
456454
]
457455
}
458456
],
@@ -476,25 +474,24 @@
476474
},
477475
{
478476
"cell_type": "code",
479-
"execution_count": 11,
477+
"execution_count": 12,
480478
"id": "9126b4ce",
481479
"metadata": {},
482480
"outputs": [
483481
{
484482
"name": "stdout",
485483
"output_type": "stream",
486484
"text": [
487-
"Suburban belts display larger December brightening compared to urban cores even though absolute\n",
488-
"light levels are higher downtown due to the widespread use of decorative lighting during the holiday\n",
489-
"season. Suburban areas tend to have more residential properties where festive lighting is commonly\n",
490-
"used, contributing to this phenomenon. Additionally, suburban development often spreads outward,\n",
491-
"leading to a more extensive area where lighting may be present compared to the concentrated urban\n",
492-
"cores {ref_id: 0}. Regarding Phoenix's sharply visible nighttime street grid from space, Phoenix\n",
493-
"and many cities in the western United States feature a regular grid layout of city blocks and\n",
494-
"streets. This grid is particularly evident at night because street lighting creates a clear and\n",
495-
"sharp pattern visible from space. In contrast, the interstate between midwestern cities may appear\n",
496-
"dimmer because these areas generally have less consistent lighting and lower population density,\n",
497-
"causing stretches of roadway to be less illuminated {ref_id: 1}.\n"
485+
"Suburban areas often display larger December brightening compared to urban cores because of\n",
486+
"widespread decorative lighting practices during the holiday season in residential neighborhoods,\n",
487+
"whereas urban cores, despite having higher absolute light levels, may not experience as pronounced\n",
488+
"increases due to the density and type of lighting already present there [ref_id: 1]. The reason the\n",
489+
"Phoenix nighttime street grid is so sharply visible from space is due to its regular grid layout of\n",
490+
"city blocks and streets, which highlight street lighting patterns. This structure, common to many\n",
491+
"central and western U.S. cities, facilitates easy visibility from space, making individual lights\n",
492+
"and intersections conspicuous. This contrasts with large stretches of interstate between midwestern\n",
493+
"cities, which often remain dim due to fewer intersections or less dense lighting along rural or\n",
494+
"undeveloped areas [ref_id: 0].\n"
498495
]
499496
}
500497
],
@@ -518,7 +515,7 @@
518515
},
519516
{
520517
"cell_type": "code",
521-
"execution_count": 12,
518+
"execution_count": 13,
522519
"id": "7034e480",
523520
"metadata": {},
524521
"outputs": [],
@@ -550,7 +547,7 @@
550547
},
551548
{
552549
"cell_type": "code",
553-
"execution_count": 13,
550+
"execution_count": 14,
554551
"id": "35a1bfcb",
555552
"metadata": {},
556553
"outputs": [
@@ -693,7 +690,7 @@
693690
},
694691
{
695692
"cell_type": "code",
696-
"execution_count": 14,
693+
"execution_count": 15,
697694
"id": "6f74c2c3",
698695
"metadata": {},
699696
"outputs": [
@@ -707,7 +704,7 @@
707704
" \"id\": 0,\n",
708705
" \"type\": \"ModelQueryPlanning\",\n",
709706
" \"input_tokens\": 2283,\n",
710-
" \"output_tokens\": 110\n",
707+
" \"output_tokens\": 207\n",
711708
" },\n",
712709
" {\n",
713710
" \"id\": 1,\n",
@@ -716,9 +713,19 @@
716713
" \"query\": {\n",
717714
" \"search\": \"how to locate lava flows at night\"\n",
718715
" },\n",
719-
" \"query_time\": \"2025-05-02T04:51:05.929Z\",\n",
716+
" \"query_time\": \"2025-05-06T15:44:00.218Z\",\n",
720717
" \"count\": 6,\n",
721-
" \"elapsed_ms\": 346\n",
718+
" \"elapsed_ms\": 497\n",
719+
" },\n",
720+
" {\n",
721+
" \"id\": 2,\n",
722+
" \"type\": \"AzureSearchQuery\",\n",
723+
" \"target_index\": \"earth_at_night\",\n",
724+
" \"query\": {\n",
725+
" \"search\": \"best practices for observing lava at night\"\n",
726+
" },\n",
727+
" \"query_time\": \"2025-05-06T15:44:00.571Z\",\n",
728+
" \"elapsed_ms\": 352\n",
722729
" }\n",
723730
"]\n",
724731
"Results\n",
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
azure-search-documents==11.6.0b11
21
azure-identity
32
openai
43
aiohttp
54
ipykernel
65
dotenv
7-
requests
6+
requests
7+
8+
--extra-index-url https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/
9+
azure-search-documents==11.6.0a20250505003

agent-example/agent-example.ipynb

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -159,35 +159,37 @@
159159
},
160160
{
161161
"cell_type": "code",
162-
"execution_count": 4,
162+
"execution_count": null,
163163
"id": "fbe31e32",
164164
"metadata": {},
165165
"outputs": [],
166166
"source": [
167-
"import requests\n",
168167
"\n",
169-
"create_agent_request = {\n",
170-
" \"name\": agent_name,\n",
171-
" \"targetIndexes\": [ { \"indexName\": index_name } ],\n",
172-
" \"models\": [\n",
173-
" {\n",
174-
" \"kind\": \"azureOpenAI\",\n",
175-
" \"azureOpenAIParameters\": {\n",
176-
" \"resourceUri\": azure_openai_endpoint,\n",
177-
" \"apiKey\": None,\n",
178-
" \"deploymentId\": azure_openai_gpt_model,\n",
179-
" \"modelName\": azure_openai_gpt_model\n",
180-
" }\n",
181-
" }\n",
182-
" ]\n",
183-
"}\n",
168+
"from azure.search.documents.indexes.models import KnowledgeAgent, KnowledgeAgentAzureOpenAIModel, KnowledgeAgentTargetIndex, KnowledgeAgentRequestLimits, AzureOpenAIVectorizerParameters\n",
169+
"from azure.search.documents.indexes import SearchIndexClient\n",
184170
"\n",
185-
"response = requests.put(\n",
186-
" url=f\"{endpoint}/agents/{agent_name}?api-version={api_version}\",\n",
187-
" headers={ \"Authorization\": f\"Bearer {token_provider()}\" },\n",
188-
" json=create_agent_request\n",
171+
"agent = KnowledgeAgent(\n",
172+
" name=agent_name,\n",
173+
" models=[\n",
174+
" KnowledgeAgentAzureOpenAIModel(\n",
175+
" azure_open_ai_parameters=AzureOpenAIVectorizerParameters(\n",
176+
" resource_url=azure_openai_endpoint,\n",
177+
" deployment_name=azure_openai_gpt_deployment,\n",
178+
" model_name=azure_openai_gpt_model\n",
179+
" )\n",
180+
" )\n",
181+
" ],\n",
182+
" target_indexes=[\n",
183+
" KnowledgeAgentTargetIndex(\n",
184+
" index_name=index_name,\n",
185+
" default_reranker_threshold=2.5\n",
186+
" )\n",
187+
" ],\n",
189188
")\n",
190-
"response.raise_for_status()\n"
189+
"\n",
190+
"index_client = SearchIndexClient(endpoint=endpoint, credential=credential)\n",
191+
"index_client.create_or_update_agent(agent)\n",
192+
"print(f\"Knowledge agent '{agent_name}' created or updated successfully\")"
191193
]
192194
},
193195
{
@@ -254,13 +256,18 @@
254256
},
255257
{
256258
"cell_type": "code",
257-
"execution_count": 11,
259+
"execution_count": null,
258260
"id": "de2ee775",
259261
"metadata": {},
260262
"outputs": [],
261263
"source": [
262264
"from azure.ai.projects.models import FunctionTool, ToolSet, ListSortOrder\n",
263265
"\n",
266+
"from azure.search.documents.agent import KnowledgeAgentRetrievalClient\n",
267+
"from azure.search.documents.agent.models import KnowledgeAgentRetrievalRequest, KnowledgeAgentMessage, KnowledgeAgentMessageTextContent, KnowledgeAgentIndexParams\n",
268+
"\n",
269+
"agent_client = KnowledgeAgentRetrievalClient(endpoint=endpoint, agent_name=agent_name, credential=credential)\n",
270+
"\n",
264271
"thread = project_client.agents.create_thread()\n",
265272
"retrieval_results = {}\n",
266273
"\n",
@@ -274,36 +281,26 @@
274281
" messages = project_client.agents.list_messages(thread.id, limit=5, order=ListSortOrder.DESCENDING)\n",
275282
" # Reverse the order so the most recent message is last\n",
276283
" messages.data.reverse()\n",
277-
" retrieval_request = {\n",
278-
" \"messages\" : [ { \"role\": msg.role, \"content\": [ { \"text\": msg_content.text.value, \"type\": msg_content.type } for msg_content in msg.content ] } for msg in messages.data ],\n",
279-
" \"targetIndexParams\" : [\n",
280-
" { \n",
281-
" \"indexName\" : index_name,\n",
282-
" \"rerankerThreshold\": 2.5\n",
283-
" } \n",
284-
" ]\n",
285-
" }\n",
286-
" response = requests.post(\n",
287-
" url=f\"{endpoint}/agents/{agent_name}/retrieve?api-version={api_version}\",\n",
288-
" headers={ \"Authorization\": f\"Bearer {token_provider()}\" },\n",
289-
" json=retrieval_request\n",
284+
" retrieval_result = agent_client.knowledge_retrieval.retrieve(\n",
285+
" retrieval_request=KnowledgeAgentRetrievalRequest(\n",
286+
" messages=[KnowledgeAgentMessage(role=msg[\"role\"], content=[KnowledgeAgentMessageTextContent(text=msg.content[0].text)]) for msg in messages.data],\n",
287+
" target_index_params=[KnowledgeAgentIndexParams(index_name=index_name, reranker_threshold=2.5)]\n",
288+
" )\n",
290289
" )\n",
291-
" response.raise_for_status()\n",
292-
" result = response.json()\n",
293290
"\n",
294291
" # Associate the retrieval results with the last message in the conversation\n",
295292
" last_message = messages.data[-1]\n",
296-
" retrieval_results[last_message.id] = result\n",
293+
" retrieval_results[last_message.id] = retrieval_result\n",
297294
"\n",
298295
" # Return the grounding response to the agent\n",
299-
" return result[\"response\"][0][\"content\"][0][\"text\"]\n",
296+
" return retrieval_result.response[0].content[0].text\n",
300297
"\n",
301298
"# Link to AI Agent Service function calling tutorial\n",
302299
"user_functions = { agentic_retrieval }\n",
303300
"functions = FunctionTool(user_functions)\n",
304301
"toolset = ToolSet()\n",
305302
"toolset.add(functions)\n",
306-
"project_client.agents.enable_auto_function_calls(toolset=toolset)"
303+
"project_client.agents.enable_auto_function_calls(toolset=toolset)\n"
307304
]
308305
},
309306
{
@@ -384,7 +381,7 @@
384381
},
385382
{
386383
"cell_type": "code",
387-
"execution_count": 13,
384+
"execution_count": null,
388385
"id": "7b90fefa",
389386
"metadata": {},
390387
"outputs": [
@@ -456,14 +453,15 @@
456453
}
457454
],
458455
"source": [
459-
"retrieval_results = retrieval_results.get(message.id)\n",
460-
"if retrieval_results is None:\n",
456+
"\n",
457+
"retrieval_result = retrieval_results.get(message.id)\n",
458+
"if retrieval_result is None:\n",
461459
" raise RuntimeError(f\"No retrieval results found for message {message.id}\")\n",
462460
"\n",
463461
"print(\"Retrieval activity\")\n",
464-
"print(json.dumps(retrieval_results[\"activity\"], indent=2))\n",
462+
"print(json.dumps([activity.as_dict() for activity in retrieval_result.activity], indent=2))\n",
465463
"print(\"Retrieval results\")\n",
466-
"print(json.dumps(retrieval_results[\"references\"], indent=2))"
464+
"print(json.dumps([reference.as_dict() for reference in retrieval_result.references], indent=2))\n"
467465
]
468466
},
469467
{

agent-example/requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
azure-search-documents==11.6.0b11
21
azure-ai-projects
32
azure-identity
43
aiohttp
54
ipykernel
65
dotenv
7-
requests
6+
7+
--extra-index-url https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/
8+
azure-search-documents==11.6.0a20250505003

0 commit comments

Comments
 (0)