From 02f508bdfaca08ae7b75165ba83b2b7551cc7b4b Mon Sep 17 00:00:00 2001 From: Giles Odigwe Date: Fri, 21 Nov 2025 17:07:24 -0800 Subject: [PATCH 1/3] openapi + memory search samples --- .../azure_ai/azure_ai_with_memory_search.py | 92 +++++++++++++++++++ .../agents/azure_ai/azure_ai_with_openapi.py | 54 +++++++++++ 2 files changed, 146 insertions(+) create mode 100644 python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py create mode 100644 python/samples/getting_started/agents/azure_ai/azure_ai_with_openapi.py diff --git a/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py new file mode 100644 index 0000000000..cc4a6c9cef --- /dev/null +++ b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py @@ -0,0 +1,92 @@ +# Copyright (c) Microsoft. All rights reserved. +import asyncio +import os + +from agent_framework.azure import AzureAIClient +from azure.ai.projects.aio import AIProjectClient +from azure.ai.projects.models import MemoryStoreDefaultDefinition, MemoryStoreDefaultOptions +from azure.core.exceptions import ResourceNotFoundError +from azure.identity.aio import AzureCliCredential + +""" +Azure AI Agent with Memory Search Example + +This sample demonstrates usage of AzureAIClient with memory search capabilities +to retrieve relevant past user messages and maintain conversation context across sessions. +It shows explicit memory store creation using Azure AI Projects client and agent creation +using the Agent Framework. + +Prerequisites: +1. Set AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME environment variables. +2. Set AZURE_AI_CHAT_MODEL_DEPLOYMENT_NAME for the memory chat model. +3. Set AZURE_AI_EMBEDDING_MODEL_DEPLOYMENT_NAME for the memory embedding model. +4. Deploy both a chat model (e.g. gpt-4.1) and an embedding model (e.g. text-embedding-3-small). +""" + + +async def main() -> None: + endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"] + memory_store_name = "agent_framework_memory_store" + + async with AzureCliCredential() as credential: + # First, create the memory store using Azure AI Projects client + async with AIProjectClient(endpoint=endpoint, credential=credential) as project_client: + # Delete memory store if it already exists + try: + await project_client.memory_stores.delete(memory_store_name) + print(f"Memory store `{memory_store_name}` deleted") + except ResourceNotFoundError: + pass + + # Create a memory store using proper model classes + memory_store_definition = MemoryStoreDefaultDefinition( + chat_model=os.environ["AZURE_AI_CHAT_MODEL_DEPLOYMENT_NAME"], + embedding_model=os.environ["AZURE_AI_EMBEDDING_MODEL_DEPLOYMENT_NAME"], + options=MemoryStoreDefaultOptions(user_profile_enabled=True, chat_summary_enabled=True), + ) + + memory_store = await project_client.memory_stores.create( + name=memory_store_name, + description="Memory store for Agent Framework conversations", + definition=memory_store_definition, + ) + print(f"Created memory store: {memory_store.name} ({memory_store.id}): {memory_store.description}") + + # Then, create the agent using Agent Framework + async with AzureAIClient(async_credential=credential).create_agent( + name="MyMemoryAgent", + instructions="""You are a helpful assistant that remembers past conversations. + Use the memory search tool to recall relevant information from previous interactions.""", + tools={ + "type": "memory_search", + "memory_store_name": memory_store.name, + "scope": "user_123", + "update_delay": 1, # Wait 1 second before updating memories (use higher value in production) + }, + ) as agent: + # First interaction - establish some preferences + print("=== First conversation ===") + query1 = "I prefer dark roast coffee" + print(f"User: {query1}") + result1 = await agent.run(query1) + print(f"Agent: {result1}\n") + + # Wait for memories to be processed + print("Waiting for memories to be stored...") + await asyncio.sleep(5) # Reduced wait time for demo purposes + + # Second interaction - test memory recall + print("=== Second conversation ===") + query2 = "Please order my usual coffee" + print(f"User: {query2}") + result2 = await agent.run(query2) + print(f"Agent: {result2}\n") + + # Clean up - delete the memory store + async with AIProjectClient(endpoint=endpoint, credential=credential) as project_client: + await project_client.memory_stores.delete(memory_store_name) + print("Memory store deleted") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/python/samples/getting_started/agents/azure_ai/azure_ai_with_openapi.py b/python/samples/getting_started/agents/azure_ai/azure_ai_with_openapi.py new file mode 100644 index 0000000000..8ef165e838 --- /dev/null +++ b/python/samples/getting_started/agents/azure_ai/azure_ai_with_openapi.py @@ -0,0 +1,54 @@ +# Copyright (c) Microsoft. All rights reserved. +import asyncio +import json +from pathlib import Path + +import aiofiles +from agent_framework.azure import AzureAIClient +from azure.identity.aio import AzureCliCredential + +""" +Azure AI Agent with OpenAPI Tool Example + +This sample demonstrates usage of AzureAIClient with OpenAPI tools +to call external APIs defined by OpenAPI specifications. + +Prerequisites: +1. Set AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME environment variables. +2. The countries.json OpenAPI specification is included in the resources folder. +""" + + +async def main() -> None: + # Load the OpenAPI specification + resources_path = Path(__file__).parent.parent / "resources" / "countries.json" + + async with aiofiles.open(resources_path, "r") as f: + content = await f.read() + openapi_countries = json.loads(content) + + async with ( + AzureCliCredential() as credential, + AzureAIClient(async_credential=credential).create_agent( + name="MyOpenAPIAgent", + instructions="""You are a helpful assistant that can use country APIs to provide information. + Use the available OpenAPI tools to answer questions about countries, currencies, and demographics.""", + tools={ + "type": "openapi", + "openapi": { + "name": "get_countries", + "spec": openapi_countries, + "description": "Retrieve information about countries by currency code", + "auth": {"type": "anonymous"}, + }, + }, + ) as agent, + ): + query = "What is the name and population of the country that uses currency with abbreviation THB?" + print(f"User: {query}") + result = await agent.run(query) + print(f"Agent: {result}\n") + + +if __name__ == "__main__": + asyncio.run(main()) From 49d127004cce1d76c9fd6e36b0da1898765ef05e Mon Sep 17 00:00:00 2001 From: Giles Odigwe Date: Mon, 24 Nov 2025 09:30:24 -0800 Subject: [PATCH 2/3] readme update --- python/samples/getting_started/agents/azure_ai/README.md | 2 ++ .../agents/azure_ai/azure_ai_with_memory_search.py | 1 + 2 files changed, 3 insertions(+) diff --git a/python/samples/getting_started/agents/azure_ai/README.md b/python/samples/getting_started/agents/azure_ai/README.md index 7a3b2d4520..e7ab6d1eb7 100644 --- a/python/samples/getting_started/agents/azure_ai/README.md +++ b/python/samples/getting_started/agents/azure_ai/README.md @@ -25,7 +25,9 @@ This folder contains examples demonstrating different ways to create and use age | [`azure_ai_with_sharepoint.py`](azure_ai_with_sharepoint.py) | Shows how to use SharePoint grounding with Azure AI agents to search through SharePoint content and answer user questions with proper citations. Requires a SharePoint connection configured in your Azure AI project. | | [`azure_ai_with_thread.py`](azure_ai_with_thread.py) | Demonstrates thread management with Azure AI agents, including automatic thread creation for stateless conversations and explicit thread management for maintaining conversation context across multiple interactions. | | [`azure_ai_with_image_generation.py`](azure_ai_with_image_generation.py) | Shows how to use the `ImageGenTool` with Azure AI agents to generate images based on text prompts. | +| [`azure_ai_with_memory_search.py`](azure_ai_with_memory_search.py) | Shows how to use memory search functionality with Azure AI agents for conversation persistence. Demonstrates creating memory stores and enabling agents to search through conversation history. | | [`azure_ai_with_microsoft_fabric.py`](azure_ai_with_microsoft_fabric.py) | Shows how to use Microsoft Fabric with Azure AI agents to query Fabric data sources and provide responses based on data analysis. Requires a Microsoft Fabric connection configured in your Azure AI project. | +| [`azure_ai_with_openapi.py`](azure_ai_with_openapi.py) | Shows how to integrate OpenAPI specifications with Azure AI agents using dictionary-based tool configuration. Demonstrates using external REST APIs for dynamic data lookup. | | [`azure_ai_with_web_search.py`](azure_ai_with_web_search.py) | Shows how to use the `HostedWebSearchTool` with Azure AI agents to perform web searches and retrieve up-to-date information from the internet. | ## Environment Variables diff --git a/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py index cc4a6c9cef..95e8ab28f2 100644 --- a/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py +++ b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py @@ -36,6 +36,7 @@ async def main() -> None: await project_client.memory_stores.delete(memory_store_name) print(f"Memory store `{memory_store_name}` deleted") except ResourceNotFoundError: + # Memory store does not exist, so nothing to delete. Safe to ignore. pass # Create a memory store using proper model classes From 4d65bab9010d72f348d986cdce335354de5709f3 Mon Sep 17 00:00:00 2001 From: Giles Odigwe Date: Mon, 24 Nov 2025 16:33:33 -0800 Subject: [PATCH 3/3] memory store name fix --- .../azure_ai/azure_ai_with_memory_search.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py index 95e8ab28f2..481bf2f151 100644 --- a/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py +++ b/python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py @@ -1,11 +1,11 @@ # Copyright (c) Microsoft. All rights reserved. import asyncio import os +import uuid from agent_framework.azure import AzureAIClient from azure.ai.projects.aio import AIProjectClient from azure.ai.projects.models import MemoryStoreDefaultDefinition, MemoryStoreDefaultOptions -from azure.core.exceptions import ResourceNotFoundError from azure.identity.aio import AzureCliCredential """ @@ -26,19 +26,12 @@ async def main() -> None: endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"] - memory_store_name = "agent_framework_memory_store" + # Generate a unique memory store name to avoid conflicts + memory_store_name = f"agent_framework_memory_store_{uuid.uuid4().hex[:8]}" async with AzureCliCredential() as credential: - # First, create the memory store using Azure AI Projects client + # Create the memory store using Azure AI Projects client async with AIProjectClient(endpoint=endpoint, credential=credential) as project_client: - # Delete memory store if it already exists - try: - await project_client.memory_stores.delete(memory_store_name) - print(f"Memory store `{memory_store_name}` deleted") - except ResourceNotFoundError: - # Memory store does not exist, so nothing to delete. Safe to ignore. - pass - # Create a memory store using proper model classes memory_store_definition = MemoryStoreDefaultDefinition( chat_model=os.environ["AZURE_AI_CHAT_MODEL_DEPLOYMENT_NAME"],