mem0 Memory System

  • examples
#!/usr/bin/env python3 """ ✨ mem0 MCP Server Autonomous Memory Example ✨ Made with ❤️ by Pink Pixel This script demonstrates how the mem0 MCP server automatically extracts and stores information from conversations without explicit memory commands. """ import os import sys import argparse import time from client import Mem0Client from dotenv import load_dotenv # Add parent directory to path to import modules sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) # Load environment variables from .env file if present load_dotenv() def main(): """Example application using mem0 MCP server with autonomous memory""" parser = argparse.ArgumentParser(description="mem0 Autonomous Memory Example") parser.add_argument( "--server-url", type=str, default="http://localhost:8000", help="URL of the mem0 MCP server" ) parser.add_argument( "--provider", type=str, default="openai", choices=["openai", "anthropic", "google", "deepseek", "openrouter", "ollama"], help="LLM provider to use" ) parser.add_argument( "--user-id", type=str, default=None, help="User ID for memory operations (defaults to a random ID if not provided)" ) args = parser.parse_args() # Generate a random user ID if not provided if not args.user_id: import uuid args.user_id = f"user_{uuid.uuid4().hex[:8]}" print(f"✨ mem0 Autonomous Memory Example ✨") print(f"Made with ❤️ by Pink Pixel") print(f"\nConnecting to server: {args.server_url}") print(f"Using provider: {args.provider}") print(f"User ID: {args.user_id}") # Initialize the client client = Mem0Client(base_url=args.server_url) # Check if the server is running if not client.health_check(): print("Error: Could not connect to the mem0 MCP server") print("Make sure the server is running with: python server.py") return # Configure the memory provider print("\nConfiguring memory provider...") client.configure( provider=args.provider, data_dir=f"./memory_data_{args.provider}", embedding_provider="openai" if args.provider != "ollama" else "ollama" ) # Define a system prompt that encourages the AI to use the user's information naturally system_prompt = """ You are a helpful assistant that remembers details about the user you're talking to. Use what you know about the user to provide personalized responses. Never explicitly mention that you're using stored memories - just incorporate the information naturally. If you don't know something about the user, don't make assumptions - just respond based on what you do know. """ # Start the conversation print("\n--- Conversation with Autonomous Memory ---") print("(Type 'exit' to quit)") print("(The system will automatically remember information about you)") # Example conversation flow to demonstrate memory capabilities examples = [ "Let me introduce myself. My name is Alex and I'm a software engineer from Seattle.", "I love hiking in the mountains and my favorite food is Thai curry.", "I have a golden retriever named Max who is 3 years old.", "What can you tell me about myself?", "I'm planning a trip to Japan next month. Any recommendations?", "I also enjoy photography and playing the guitar in my free time.", "What are my hobbies again?", "I'm thinking about learning a new programming language. What would you suggest based on what you know about me?" ] print("\nExample messages you can try (or type your own):") for i, example in enumerate(examples, 1): print(f"{i}. {example}") while True: try: # Get user input user_input = input("\nYou: ") # Check if the user wants to exit if user_input.lower() in ["exit", "quit", "bye"]: print("Goodbye!") break # Check if the user entered a number to use an example if user_input.isdigit() and 1 <= int(user_input) <= len(examples): example_idx = int(user_input) - 1 user_input = examples[example_idx] print(f"Using example: {user_input}") # Send the message to the server print("Thinking...") start_time = time.time() response = client.chat( message=user_input, user_id=args.user_id, system_prompt=system_prompt ) end_time = time.time() print(f"\nAI ({(end_time - start_time):.2f}s): {response}") except KeyboardInterrupt: print("\nExiting...") break except Exception as e: print(f"Error: {str(e)}") if __name__ == "__main__": main()