@@ -305,6 +305,109 @@ Performance Examples
305305
306306 return tree
307307
308+ LangMem Integration for AI Agent Memory
309+ ----------------------------------------
310+
311+ .. code-block :: python
312+
313+ from prollytree import VersionedKvStore
314+ from langgraph.store.base import BaseStore, Item
315+ from langmem import create_manage_memory_tool, create_search_memory_tool
316+ import json
317+ import time
318+
319+ class ProllyTreeLangMemStore (BaseStore ):
320+ """ LangMem-compatible BaseStore using ProllyTree backend"""
321+
322+ def __init__ (self , repo_path : str ):
323+ self .store = VersionedKvStore(f " { repo_path} /data " )
324+
325+ def put (self , namespace , key , value ):
326+ """ Store memory with namespace and key"""
327+ prolly_key = f " { ' /' .join(namespace)} # { key} "
328+ self .store.insert(prolly_key.encode(), json.dumps(value).encode())
329+ self .store.commit(f " Store memory: { key} " )
330+
331+ def get (self , namespace , key ):
332+ """ Retrieve memory by namespace and key"""
333+ prolly_key = f " { ' /' .join(namespace)} # { key} "
334+ value = self .store.get(prolly_key.encode())
335+ if value:
336+ return Item(
337+ value = json.loads(value.decode()),
338+ key = key,
339+ namespace = namespace,
340+ created_at = time.time(),
341+ updated_at = time.time()
342+ )
343+ return None
344+
345+ def example_langmem_integration ():
346+ """ Example using ProllyTree as backend for LangMem AI agent memory"""
347+
348+ # Create ProllyTree store with branching support
349+ store = ProllyTreeLangMemStore(" ./langmem_store" )
350+
351+ # Create LangMem memory tools for AI agents
352+ manage_tool = create_manage_memory_tool(
353+ namespace = (" memories" , " user_001" ),
354+ store = store,
355+ instructions = " Store important user preferences and context"
356+ )
357+
358+ search_tool = create_search_memory_tool(
359+ namespace = (" memories" , " user_001" ),
360+ store = store
361+ )
362+
363+ print (" === LangMem + ProllyTree Integration ===" )
364+
365+ # Simulate agent storing memories
366+ memories = [
367+ {
368+ " content" : " User prefers dark mode interfaces" ,
369+ " memory_type" : " preference"
370+ },
371+ {
372+ " content" : " User is learning machine learning with Python" ,
373+ " memory_type" : " context"
374+ },
375+ {
376+ " content" : " User works best in the morning hours" ,
377+ " memory_type" : " behavioral"
378+ }
379+ ]
380+
381+ for memory in memories:
382+ result = manage_tool.invoke(memory)
383+ print (f " Stored: { memory[' content' ][:40 ]} ... " )
384+
385+ # Search for relevant memories
386+ search_result = search_tool.invoke({" query" : " user preferences" })
387+ print (f " \\ nFound { len (search_result)} relevant memories " )
388+
389+ # Create experimental branch for testing
390+ store.store.create_branch(" experiment" )
391+
392+ # Store experimental memory in branch
393+ store.store.checkout(" experiment" )
394+ experimental_memory = {
395+ " content" : " Testing new AI assistant features" ,
396+ " memory_type" : " experimental"
397+ }
398+ manage_tool.invoke(experimental_memory)
399+
400+ # Switch back to main - experimental memory isolated
401+ store.store.checkout(" main" )
402+
403+ print (" \\ nFeatures demonstrated:" )
404+ print (" ✅ LangMem tool integration" )
405+ print (" ✅ Git-like versioning for memories" )
406+ print (" ✅ Branch-based memory isolation" )
407+ print (" ✅ Persistent storage across sessions" )
408+
409+ return store
410+
308411 Branch Merging and Conflict Resolution
309412---------------------------------------
310413
@@ -435,6 +538,9 @@ Running Examples
435538 print (" \\ n=== AI Agent Memory ===" )
436539 example_ai_agent_memory()
437540
541+ print (" \\ n=== LangMem Integration ===" )
542+ example_langmem_integration()
543+
438544 print (" \\ n=== Branch Merging ===" )
439545 example_merge_operations()
440546
0 commit comments