-
Notifications
You must be signed in to change notification settings - Fork 812
Python: AzureAI OpenAPI + Memory Search Samples #2390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+142
−0
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
python/samples/getting_started/agents/azure_ai/azure_ai_with_memory_search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # 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: | ||
giles17 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # 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"], | ||
| 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()) | ||
54 changes: 54 additions & 0 deletions
54
python/samples/getting_started/agents/azure_ai/azure_ai_with_openapi.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.