Skip to content

Commit 86479bc

Browse files
committed
update
1 parent 2c14740 commit 86479bc

File tree

2 files changed

+47
-50
lines changed

2 files changed

+47
-50
lines changed

agentic-retrieval-pipeline-example/agent-example.ipynb

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"load_dotenv(override=True) # take environment variables from .env.\n",
9494
"\n",
9595
"# The following variables from your .env file are used in this notebook\n",
96-
"project_conn_str = os.environ[\"PROJECT_CONNECTION_STRING\"]\n",
96+
"project_endpoint = os.environ[\"PROJECT_ENDPOINT\"]\n",
9797
"agent_model = os.getenv(\"AGENT_MODEL\", \"gpt-4o-mini\")\n",
9898
"endpoint = os.environ[\"AZURE_SEARCH_ENDPOINT\"]\n",
9999
"credential = DefaultAzureCredential()\n",
@@ -104,8 +104,7 @@
104104
"azure_openai_gpt_model = os.getenv(\"AZURE_OPENAI_GPT_MODEL\", \"gpt-4o-mini\")\n",
105105
"azure_openai_embedding_deployment = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\", \"text-embedding-3-large\")\n",
106106
"azure_openai_embedding_model = os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\", \"text-embedding-3-large\")\n",
107-
"agent_name = os.getenv(\"AZURE_SEARCH_AGENT_NAME\", \"earth-search-agent\")\n",
108-
"api_version = \"2025-05-01-Preview\""
107+
"agent_name = os.getenv(\"AZURE_SEARCH_AGENT_NAME\", \"earth-search-agent\")"
109108
]
110109
},
111110
{
@@ -190,7 +189,7 @@
190189
},
191190
{
192191
"cell_type": "code",
193-
"execution_count": 3,
192+
"execution_count": 4,
194193
"id": "f98f31e7",
195194
"metadata": {},
196195
"outputs": [
@@ -220,22 +219,22 @@
220219
"id": "e3d0081e",
221220
"metadata": {},
222221
"source": [
223-
"## Create a search agent on Azure AI Search\n",
222+
"## Create a knowledge agent on Azure AI Search\n",
224223
"\n",
225-
"This steps creates a search agent on Azure AI Search. This agent is a wrapper to a large language model, used for sending queries to an agentic retrieval pipeline. The maximum output size refers to the query response. Setting this value helps you control token usage and how many tokens are sent to the LLM."
224+
"This steps creates a knowledge agent on Azure AI Search. This agent is a wrapper to a large language model, used for sending queries to an agentic retrieval pipeline. The maximum output size refers to the query response. Setting this value helps you control token usage and how many tokens are sent to the LLM."
226225
]
227226
},
228227
{
229228
"cell_type": "code",
230-
"execution_count": 4,
229+
"execution_count": 3,
231230
"id": "fbe31e32",
232231
"metadata": {},
233232
"outputs": [
234233
{
235234
"name": "stdout",
236235
"output_type": "stream",
237236
"text": [
238-
"Search agent 'earth-search-agent' created or updated successfully\n"
237+
"Knowledge agent 'earth-search-agent' created or updated successfully\n"
239238
]
240239
}
241240
],
@@ -268,7 +267,7 @@
268267
"\n",
269268
"index_client = SearchIndexClient(endpoint=endpoint, credential=credential)\n",
270269
"index_client.create_or_update_agent(agent)\n",
271-
"print(f\"Search agent '{agent_name}' created or updated successfully\")"
270+
"print(f\"Knowledge agent '{agent_name}' created or updated successfully\")"
272271
]
273272
},
274273
{
@@ -283,32 +282,37 @@
283282
},
284283
{
285284
"cell_type": "code",
286-
"execution_count": null,
285+
"execution_count": 4,
287286
"id": "6eb0ebd3",
288287
"metadata": {},
289288
"outputs": [
290289
{
291290
"data": {
292291
"text/plain": [
293-
"{'object': 'list', 'data': [{'id': 'asst_sXngVG4fzu0bQJNFmTxzX6h4', 'object': 'assistant', 'created_at': 1746940725, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o-mini', 'instructions': '\\nAn Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer.\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'}, {'id': 'asst_lzafrxILzTmhdFaMMEVjuhE8', 'object': 'assistant', 'created_at': 1746940688, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o-mini', 'instructions': '\\nAn Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer.\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'}], 'first_id': 'asst_sXngVG4fzu0bQJNFmTxzX6h4', 'last_id': 'asst_lzafrxILzTmhdFaMMEVjuhE8', 'has_more': False}"
292+
"[{'id': 'asst_X7JMxX1SW6EYuovQn6WZobeN', 'object': 'assistant', 'created_at': 1747705233, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o', 'instructions': '\\nA Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer using the format [ref_id].\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'},\n",
293+
" {'id': 'asst_bFc7GdLyN96HS1BII3mTfc1O', 'object': 'assistant', 'created_at': 1747705029, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o', 'instructions': '\\nA Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer using the format [ref_id].\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'},\n",
294+
" {'id': 'asst_DAcq0xRvJroakPMxCQ30qW5n', 'object': 'assistant', 'created_at': 1747704511, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o', 'instructions': '\\nA Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer using the format [ref_id].\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'},\n",
295+
" {'id': 'asst_L6Qdwc5dvghAZ1Z7xeM1RsDb', 'object': 'assistant', 'created_at': 1747703597, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o', 'instructions': '\\nA Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer.\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'},\n",
296+
" {'id': 'asst_8l6Md1sf5Hh4A3FuF5p9NsBx', 'object': 'assistant', 'created_at': 1747703585, 'name': 'earth-search-agent', 'description': None, 'model': 'gpt-4o', 'instructions': '\\nA Q&A agent that can answer questions about the Earth at night.\\nSources have a JSON format with a ref_id that must be cited in the answer.\\nIf you do not have the answer, respond with \"I don\\'t know\".\\n', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'},\n",
297+
" {'id': 'asst_IVF9BRKXJYK4Z6fSPrZByQKD', 'object': 'assistant', 'created_at': 1747701537, 'name': 'Agent638', 'description': None, 'model': 'gpt-4o', 'instructions': '', 'tools': [], 'top_p': 1.0, 'temperature': 1.0, 'tool_resources': {}, 'metadata': {}, 'response_format': 'auto'}]"
294298
]
295299
},
296-
"execution_count": 5,
300+
"execution_count": 4,
297301
"metadata": {},
298302
"output_type": "execute_result"
299303
}
300304
],
301305
"source": [
302306
"from azure.ai.projects import AIProjectClient\n",
303307
"\n",
304-
"project_client = AIProjectClient.from_connection_string(project_conn_str, credential=credential)\n",
308+
"project_client = AIProjectClient(endpoint=project_endpoint, credential=credential)\n",
305309
"\n",
306-
"project_client.agents.list_agents()"
310+
"list(project_client.agents.list_agents())"
307311
]
308312
},
309313
{
310314
"cell_type": "code",
311-
"execution_count": null,
315+
"execution_count": 5,
312316
"id": "aa363122",
313317
"metadata": {},
314318
"outputs": [
@@ -321,14 +325,9 @@
321325
}
322326
],
323327
"source": [
324-
"from azure.ai.projects import AIProjectClient\n",
325-
"import json\n",
326-
"\n",
327-
"project_client = AIProjectClient.from_connection_string(project_conn_str, credential=credential)\n",
328-
"\n",
329328
"instructions = \"\"\"\n",
330329
"A Q&A agent that can answer questions about the Earth at night.\n",
331-
"Sources have a JSON format with a ref_id that must be cited in the answer.\n",
330+
"Sources have a JSON format with a ref_id that must be cited in the answer using the format [ref_id].\n",
332331
"If you do not have the answer, respond with \"I don't know\".\n",
333332
"\"\"\"\n",
334333
"agent = project_client.agents.create_agent(\n",
@@ -352,51 +351,52 @@
352351
},
353352
{
354353
"cell_type": "code",
355-
"execution_count": 7,
354+
"execution_count": 15,
356355
"id": "de2ee775",
357356
"metadata": {},
358357
"outputs": [],
359358
"source": [
360-
"from azure.ai.projects.models import FunctionTool, ToolSet, ListSortOrder\n",
359+
"from azure.ai.agents.models import FunctionTool, ToolSet, ListSortOrder\n",
361360
"\n",
362361
"from azure.search.documents.agent import KnowledgeAgentRetrievalClient\n",
363362
"from azure.search.documents.agent.models import KnowledgeAgentRetrievalRequest, KnowledgeAgentMessage, KnowledgeAgentMessageTextContent, KnowledgeAgentIndexParams\n",
364363
"\n",
365364
"agent_client = KnowledgeAgentRetrievalClient(endpoint=endpoint, agent_name=agent_name, credential=credential)\n",
366365
"\n",
367-
"thread = project_client.agents.create_thread()\n",
366+
"thread = project_client.agents.threads.create()\n",
368367
"retrieval_results = {}\n",
369368
"\n",
370369
"def agentic_retrieval() -> str:\n",
371370
" \"\"\"\n",
372371
" Searches a NASA e-book about images of Earth at night and other science related facts.\n",
373372
" The returned string is in a JSON format that contains the reference id.\n",
374373
" Be sure to use the same format in your agent's response\n",
374+
" You must refer to references by id number\n",
375375
" \"\"\"\n",
376376
" # Take the last 5 messages in the conversation\n",
377-
" messages = project_client.agents.list_messages(thread.id, limit=5, order=ListSortOrder.DESCENDING)\n",
377+
" messages = project_client.agents.messages.list(thread.id, limit=5, order=ListSortOrder.DESCENDING)\n",
378378
" # Reverse the order so the most recent message is last\n",
379-
" messages.data.reverse()\n",
379+
" messages = list(messages)\n",
380+
" messages.reverse()\n",
380381
" retrieval_result = agent_client.knowledge_retrieval.retrieve(\n",
381382
" retrieval_request=KnowledgeAgentRetrievalRequest(\n",
382-
" messages=[KnowledgeAgentMessage(role=msg[\"role\"], content=[KnowledgeAgentMessageTextContent(text=msg.content[0].text)]) for msg in messages.data if msg[\"role\"] != \"system\"],\n",
383+
" messages=[KnowledgeAgentMessage(role=msg[\"role\"], content=[KnowledgeAgentMessageTextContent(text=msg.content[0].text)]) for msg in messages if msg[\"role\"] != \"system\"],\n",
383384
" target_index_params=[KnowledgeAgentIndexParams(index_name=index_name, reranker_threshold=2.5)]\n",
384385
" )\n",
385386
" )\n",
386387
"\n",
387388
" # Associate the retrieval results with the last message in the conversation\n",
388-
" last_message = messages.data[-1]\n",
389+
" last_message = messages[-1]\n",
389390
" retrieval_results[last_message.id] = retrieval_result\n",
390391
"\n",
391392
" # Return the grounding response to the agent\n",
392393
" return retrieval_result.response[0].content[0].text\n",
393394
"\n",
394-
"# Link to AI Agent Service function calling tutorial\n",
395-
"user_functions = { agentic_retrieval }\n",
396-
"functions = FunctionTool(user_functions)\n",
395+
"# https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/function-calling\n",
396+
"functions = FunctionTool({ agentic_retrieval })\n",
397397
"toolset = ToolSet()\n",
398398
"toolset.add(functions)\n",
399-
"project_client.agents.enable_auto_function_calls(toolset=toolset)\n"
399+
"project_client.agents.enable_auto_function_calls(tools=toolset)"
400400
]
401401
},
402402
{
@@ -413,35 +413,31 @@
413413
},
414414
{
415415
"cell_type": "code",
416-
"execution_count": 8,
416+
"execution_count": 17,
417417
"id": "f1fc04fc",
418418
"metadata": {},
419419
"outputs": [
420420
{
421421
"name": "stdout",
422422
"output_type": "stream",
423423
"text": [
424-
"Agent response: The differences in nighttime brightness between suburban belts and urban cores can be attributed to several factors:\n",
425-
"\n",
426-
"1\n",
427-
" **December Brightening**: Suburban areas tend to have a greater relative increase in brightness during December due to seasonal decorations, festive lighting, and residential activity\n",
428-
" In contrast, urban cores may have higher absolute light levels due to commercial lighting, but the types of light and their arrangement can lead to a less pronounced increase compared to the festive displays in suburban settings\n",
429-
" Urban centers often focus on commercial light sources that may not display the same wide variability in illumination as residential areas do during holidays (Source: ref_id 0)\n",
424+
"Agent response: 1\n",
425+
" **Why do suburban belts display larger December brightening than urban cores even though absolute light levels are higher downtown?**\n",
426+
" This phenomenon likely reflects seasonal increases in localized residential lighting in suburban areas due to holiday decorations, such as string lights on houses and trees, which are less common in dense urban cores\n",
427+
" Downtown areas tend to have more consistent light levels year-round due to commercial and industrial lighting that does not vary significantly seasonally [ref_id:0]\n",
430428
"\n",
431429
"\n",
432430
"2\n",
433-
" **Visibility of Phoenix's Nighttime Grid**: The sharp visibility of the Phoenix nighttime street grid from space can be attributed to its planned urban layout, which features a grid pattern of streets and blocks that are well-illuminated\n",
434-
" This grid is a result of urban design that promotes easy navigation and access, leading to concentrated bright spots along major streets and intersections\n",
435-
" In contrast, the interstate stretches in the Midwest tend to connect larger rural areas with fewer lighted nodes due to the sparser population and fewer urban developments, making those areas appear relatively dim from space (Source: ref_id 1)\n",
436-
"\n",
437-
"\n",
438-
"The differences in light patterns between urban and suburban areas, alongside the planned layouts of cities like Phoenix, significantly influence how they are perceived in nighttime satellite imagery\n",
431+
" **Why is the Phoenix nighttime street grid so sharply visible from space, while large stretches of the interstate between midwestern cities remain comparatively dim?**\n",
432+
" The Phoenix metropolitan area is characterized by a highly organized grid-like structure of blocks and streets, which are illuminated by streetlights throughout the urban and suburban zones\n",
433+
" Additionally, areas along the Grand Avenue and at intersections often have shopping centers and gas stations that contribute to brighter lighting\n",
434+
" Midwestern interstates, in contrast, traverse less densely populated regions between cities where illumination is limited to occasional spots such as rest areas or exits, leading to a less pronounced visibility of the roadway lighting from space [ref_id:1][ref_id:2]\n",
439435
"\n"
440436
]
441437
}
442438
],
443439
"source": [
444-
"message = project_client.agents.create_message(\n",
440+
"message = project_client.agents.messages.create(\n",
445441
" thread_id=thread.id,\n",
446442
" role=\"user\",\n",
447443
" content=\"\"\"\n",
@@ -450,10 +446,10 @@
450446
" \"\"\"\n",
451447
")\n",
452448
"\n",
453-
"run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id, toolset=toolset)\n",
449+
"run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id, toolset=toolset)\n",
454450
"if run.status == \"failed\":\n",
455451
" raise RuntimeError(f\"Run failed: {run.last_error}\")\n",
456-
"output = project_client.agents.list_messages(thread_id=thread.id).get_last_text_message_by_role(\"assistant\").text.value\n",
452+
"output = project_client.agents.messages.get_last_message_text_by_role(thread_id=thread.id, role=\"assistant\").text.value\n",
457453
"\n",
458454
"print(\"Agent response:\", output.replace(\".\", \"\\n\"))\n"
459455
]
@@ -831,7 +827,7 @@
831827
"name": "python",
832828
"nbconvert_exporter": "python",
833829
"pygments_lexer": "ipython3",
834-
"version": "3.12.10"
830+
"version": "3.11.9"
835831
}
836832
},
837833
"nbformat": 4,

agentic-retrieval-pipeline-example/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
azure-ai-projects
1+
azure-ai-projects==1.0.0b11
2+
azure-ai-agents==1.0.0
23
azure-identity
34
aiohttp
45
ipykernel

0 commit comments

Comments
 (0)