|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Annotated, TypedDict |
| 4 | + |
| 5 | +from tool import MemMachineTools, create_add_memory_tool, create_search_memory_tool |
| 6 | + |
| 7 | +# ============================================================================ |
| 8 | +# Configuration |
| 9 | +# ============================================================================ |
| 10 | +# Configuration values can be set via environment variables or use defaults |
| 11 | +MEMORY_BACKEND_URL = os.getenv("MEMORY_BACKEND_URL", "http://localhost:8080") |
| 12 | +LANGGRAPH_GROUP_ID = os.getenv("LANGGRAPH_GROUP_ID", "langgraph_demo") |
| 13 | +LANGGRAPH_AGENT_ID = os.getenv("LANGGRAPH_AGENT_ID", "demo_agent") |
| 14 | +LANGGRAPH_USER_ID = os.getenv("LANGGRAPH_USER_ID", "demo_user") |
| 15 | +LANGGRAPH_SESSION_ID = os.getenv("LANGGRAPH_SESSION_ID", "demo_session_001") |
| 16 | + |
| 17 | + |
| 18 | +# State definition for LangGraph workflow |
| 19 | +class AgentState(TypedDict): |
| 20 | + """State for the agent workflow.""" |
| 21 | + |
| 22 | + messages: Annotated[list, "List of messages in the conversation"] |
| 23 | + user_id: str |
| 24 | + context: str |
| 25 | + memory_tool_results: Annotated[list, "Results from memory tool calls"] |
| 26 | + |
| 27 | + |
| 28 | +def simple_memory_workflow_demo(): |
| 29 | + """ |
| 30 | + Simple demo showing basic memory operations without LangGraph dependency. |
| 31 | +
|
| 32 | + This demonstrates the MemMachine tools functionality that can be integrated |
| 33 | + into LangGraph workflows. |
| 34 | + """ |
| 35 | + print("=" * 60) |
| 36 | + print("MemMachine LangGraph Tools Demo") |
| 37 | + print("=" * 60) |
| 38 | + |
| 39 | + # Initialize tools |
| 40 | + print("\n1. Initializing MemMachine tools...") |
| 41 | + print(" Configuration:") |
| 42 | + print(f" - Backend URL: {MEMORY_BACKEND_URL}") |
| 43 | + print(f" - Group ID: {LANGGRAPH_GROUP_ID}") |
| 44 | + print(f" - Agent ID: {LANGGRAPH_AGENT_ID}") |
| 45 | + print(f" - User ID: {LANGGRAPH_USER_ID}") |
| 46 | + print(f" - Session ID: {LANGGRAPH_SESSION_ID}") |
| 47 | + |
| 48 | + tools = MemMachineTools( |
| 49 | + base_url=MEMORY_BACKEND_URL, |
| 50 | + group_id=LANGGRAPH_GROUP_ID, |
| 51 | + agent_id=LANGGRAPH_AGENT_ID, |
| 52 | + user_id=LANGGRAPH_USER_ID, |
| 53 | + session_id=LANGGRAPH_SESSION_ID, |
| 54 | + ) |
| 55 | + |
| 56 | + # Check if server is available |
| 57 | + try: |
| 58 | + health = tools.client.health_check() |
| 59 | + print(f"✅ MemMachine server is healthy: {health.get('status', 'ok')}") |
| 60 | + except Exception as e: |
| 61 | + print(f"❌ MemMachine server not available: {e}") |
| 62 | + print(f" Please start MemMachine server on {MEMORY_BACKEND_URL}") |
| 63 | + return |
| 64 | + |
| 65 | + # Create tool functions |
| 66 | + add_memory = create_add_memory_tool(tools) |
| 67 | + search_memory = create_search_memory_tool(tools) |
| 68 | + |
| 69 | + print("\n2. Adding memories...") |
| 70 | + # Add some memories |
| 71 | + memories_to_add = [ |
| 72 | + { |
| 73 | + "content": "User prefers working with Python for backend development", |
| 74 | + "metadata": {"category": "preference", "technology": "Python"}, |
| 75 | + }, |
| 76 | + { |
| 77 | + "content": "User mentioned they are working on a project deadline this Friday", |
| 78 | + "metadata": {"category": "task", "urgency": "high"}, |
| 79 | + }, |
| 80 | + { |
| 81 | + "content": "User enjoys hiking on weekends and lives in San Francisco", |
| 82 | + "metadata": {"category": "personal", "hobby": "hiking"}, |
| 83 | + }, |
| 84 | + { |
| 85 | + "content": "User is interested in machine learning and AI agents", |
| 86 | + "metadata": {"category": "interest", "field": "AI"}, |
| 87 | + }, |
| 88 | + ] |
| 89 | + |
| 90 | + for mem in memories_to_add: |
| 91 | + result = add_memory( |
| 92 | + content=mem["content"], |
| 93 | + metadata=mem["metadata"], |
| 94 | + ) |
| 95 | + if result["status"] == "success": |
| 96 | + print(f" ✅ Added: {mem['content'][:50]}...") |
| 97 | + else: |
| 98 | + print(f" ❌ Failed: {result.get('message', 'Unknown error')}") |
| 99 | + |
| 100 | + print("\n3. Searching memories...") |
| 101 | + # Search for memories |
| 102 | + search_queries = [ |
| 103 | + "What does the user prefer for development?", |
| 104 | + "What are the user's upcoming deadlines?", |
| 105 | + "What are the user's hobbies?", |
| 106 | + "What is the user interested in?", |
| 107 | + ] |
| 108 | + |
| 109 | + for query in search_queries: |
| 110 | + print(f"\n 🔍 Query: {query}") |
| 111 | + result = search_memory(query=query, limit=3) |
| 112 | + |
| 113 | + if result["status"] == "success": |
| 114 | + results = result["results"] |
| 115 | + episodic = results.get("episodic_memory", []) |
| 116 | + |
| 117 | + if episodic: |
| 118 | + print(f" Found {len(episodic)} relevant memories:") |
| 119 | + for i, mem in enumerate(episodic[:3], 1): |
| 120 | + content = ( |
| 121 | + mem.get("content", "") if isinstance(mem, dict) else str(mem) |
| 122 | + ) |
| 123 | + print(f" {i}. {content[:80]}...") |
| 124 | + else: |
| 125 | + print(" No memories found") |
| 126 | + else: |
| 127 | + print(f" ❌ Search failed: {result.get('message', 'Unknown error')}") |
| 128 | + |
| 129 | + print("\n4. Getting context...") |
| 130 | + context = tools.get_context() |
| 131 | + print(f" Context: {json.dumps(context, indent=2)}") |
| 132 | + |
| 133 | + # Cleanup |
| 134 | + tools.close() |
| 135 | + |
| 136 | + |
| 137 | +def main(): |
| 138 | + """Main demo function.""" |
| 139 | + try: |
| 140 | + # Run simple demo |
| 141 | + simple_memory_workflow_demo() |
| 142 | + except KeyboardInterrupt: |
| 143 | + print("\n\nDemo interrupted by user.") |
| 144 | + except Exception as e: |
| 145 | + print(f"\n❌ Demo failed with error: {e}") |
| 146 | + import traceback |
| 147 | + |
| 148 | + traceback.print_exc() |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments