-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Mem0 Support for filters in V2 Search API
#2725
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
Closed
Closed
Changes from all commits
Commits
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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
python/samples/getting_started/context_providers/mem0/mem0_filters.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,75 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| from datetime import datetime, timedelta | ||
|
|
||
| from agent_framework.azure import AzureAIAgentClient | ||
| from agent_framework.mem0 import Mem0Provider | ||
| from azure.identity.aio import AzureCliCredential | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Example demonstrating advanced filtering with Mem0 context provider.""" | ||
| print("=== Mem0 Advanced Filtering Example ===\n") | ||
|
|
||
| # For Azure authentication, run `az login` command in terminal or replace AzureCliCredential with preferred | ||
| # authentication option. | ||
| # For Mem0 authentication, set Mem0 API key via "api_key" parameter or MEM0_API_KEY environment variable. | ||
| async with ( | ||
| AzureCliCredential() as credential, | ||
| AzureAIAgentClient(credential=credential).create_agent( | ||
| name="FilterAssistant", | ||
| instructions="You are a helpful assistant that retrieves and summarizes memories.", | ||
| context_providers=Mem0Provider(user_id="demo_user"), | ||
| ) as agent, | ||
| ): | ||
| # Store some memories with different timestamps | ||
| print("Storing memories...") | ||
| await agent.run("I love Python programming.") | ||
| await agent.run("My favorite color is blue.") | ||
| await agent.run("I work as a software engineer.") | ||
|
|
||
| # Wait for memories to be indexed | ||
| print("Waiting for memories to be processed...") | ||
| await asyncio.sleep(12) # Empirically determined delay for Mem0 indexing | ||
|
|
||
| # Calculate a date from a week ago for filtering | ||
| week_ago = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d") | ||
|
|
||
| print("\n=== Using OR Filter ===") | ||
| # Use OR logic to find memories matching either condition | ||
| query = "What do you know about me?" | ||
| print(f"User: {query}") | ||
| print(f"Filter: Memories from another user OR created after {week_ago}") | ||
|
|
||
| result = await agent.run( | ||
| query, | ||
| filters={ | ||
| "OR": [ | ||
| {"user_id": "another_user"}, # This won't match | ||
| {"created_at": {"gte": week_ago}}, # This will match our recent memories | ||
| ] | ||
| }, | ||
| ) | ||
| print(f"Agent: {result}\n") | ||
|
|
||
| print("\n=== Using Complex Filter ===") | ||
| # Demonstrate combining multiple filter conditions | ||
| query = "Tell me about my preferences" | ||
| print(f"User: {query}") | ||
| print("Filter: Recent memories with specific keywords") | ||
|
|
||
| result = await agent.run( | ||
| query, | ||
| filters={ | ||
| "AND": [ | ||
| {"user_id": "demo_user"}, | ||
| {"created_at": {"gte": week_ago}}, | ||
| ] | ||
| }, | ||
| ) | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add an example how it looks like from user perspective?