Created
June 1, 2025 01:33
-
-
Save deshraj/3b63ecedbef5561ad07cd49865ccf198 to your computer and use it in GitHub Desktop.
Mem0 Strands Agent
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
| import logging | |
| from dotenv import load_dotenv | |
| from strands import Agent | |
| from strands_tools import mem0_memory, use_llm | |
| logger = logging.getLogger(__name__) | |
| # Load environment variables from .env file if it exists | |
| load_dotenv() | |
| USER_ID = "mem0_user" | |
| # System prompt for the memory agent | |
| MEMORY_SYSTEM_PROMPT = f"""You are a personal assistant that maintains context by remembering user details. | |
| Capabilities: | |
| - Store new information using mem0_memory tool (action="store") | |
| - Retrieve relevant memories (action="retrieve") | |
| - List all memories (action="list") | |
| - Provide personalized responses | |
| Key Rules: | |
| - Always include user_id={USER_ID} in tool calls | |
| - Be conversational and natural in responses | |
| - Format output clearly | |
| - Acknowledge stored information | |
| - Only share relevant information | |
| - Politely indicate when information is unavailable | |
| """ | |
| logging.basicConfig(level=logging.DEBUG) | |
| # Create an agent with memory capabilities | |
| memory_agent = Agent( | |
| system_prompt=MEMORY_SYSTEM_PROMPT, | |
| tools=[mem0_memory, use_llm], | |
| ) | |
| # Initialize some demo memories | |
| def initialize_demo_memories(): | |
| """Initialize some demo memories to showcase functionality.""" | |
| content = """My name is Alex. I like to travel and stay in Airbnbs rather than hotels. I am planning a trip to Japan next spring. I enjoy hiking and outdoor photography as hobbies. I have a dog named Max. My favorite cuisine is Italian food.""" # noqa | |
| memory_agent.tool.mem0_memory(action="store", content=content, user_id=USER_ID) | |
| # Example usage | |
| if __name__ == "__main__": | |
| print("\n🧠 Memory Agent 🧠\n") | |
| print("This example demonstrates using Strands Agents' memory capabilities") | |
| print("to store and retrieve information.") | |
| print("\nOptions:") | |
| print(" 'demo' - Initialize demo memories") | |
| print(" 'exit' - Exit the program") | |
| print("\nOr try these examples:") | |
| print(" - Remember that I prefer window seats on flights") | |
| print(" - What do you know about me?") | |
| print(" - What are my travel preferences?") | |
| print(" - Do I have any pets?") | |
| # Interactive loop | |
| while True: | |
| try: | |
| user_input = input("\n> ") | |
| if user_input.lower() == "exit": | |
| print("\nGoodbye! 👋") | |
| break | |
| elif user_input.lower() == "demo": | |
| initialize_demo_memories() | |
| print("\nDemo memories initialized!") | |
| continue | |
| # Call the memory agent | |
| memory_agent(user_input) | |
| except KeyboardInterrupt: | |
| print("\n\nExecution interrupted. Exiting...") | |
| break | |
| except Exception as e: | |
| print(f"\nAn error occurred: {str(e)}") | |
| print("Please try a different request.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment