@@ -243,7 +243,9 @@ def _ensure_index(self) -> None:
243243 indices = self .redis .execute_command ("FT._LIST" )
244244 # Convert both the index name and list items to strings for comparison
245245 index_name_str = str (self .index_name )
246- indices_str = [str (idx ) if isinstance (idx , bytes ) else idx for idx in indices ]
246+ indices_str = [
247+ str (idx ) if isinstance (idx , bytes ) else idx for idx in indices
248+ ]
247249 if index_name_str in indices_str :
248250 self ._index_exists = True
249251 return
@@ -534,11 +536,14 @@ def __init__(
534536 "Redis" if redis_client else "in-memory" ,
535537 )
536538
537- def store_memory_vectors (self , memory_entry : Dict [str , Any ]) -> bool :
539+ def store_memory_vectors (
540+ self , memory_entry : Dict [str , Any ], tier : str = "stm"
541+ ) -> bool :
538542 """Store vectors for a memory entry in appropriate indices.
539543
540544 Args:
541545 memory_entry: Memory entry with embeddings
546+ tier: Tier to store the vectors in ("stm", "im", or "ltm")
542547
543548 Returns:
544549 True if storage was successful
@@ -553,22 +558,27 @@ def store_memory_vectors(self, memory_entry: Dict[str, Any]) -> bool:
553558
554559 success = True
555560
556- # Store in STM index if full vector is available
557- if "full_vector" in embeddings :
561+ # Store in STM index
562+ if tier == "stm" :
563+ logger .debug ("Storing STM vector for memory %s" , memory_id )
558564 success = success and self .stm_index .add (
559565 memory_id , embeddings ["full_vector" ], metadata
560566 )
561567
562- # Store in IM index if compressed vector is available
563- if "compressed_vector" in embeddings :
568+ # Store in IM index
569+ if tier == "im" :
570+ #! TODO: Use compressed vector
571+ logger .debug ("Storing IM vector for memory %s" , memory_id )
564572 success = success and self .im_index .add (
565- memory_id , embeddings ["compressed_vector " ], metadata
573+ memory_id , embeddings ["full_vector " ], metadata
566574 )
567575
568- # Store in LTM index if abstract vector is available
569- if "abstract_vector" in embeddings :
576+ # Store in LTM index
577+ if tier == "ltm" :
578+ #! TODO: Use abstract vector
579+ logger .debug ("Storing LTM vector for memory %s" , memory_id )
570580 success = success and self .ltm_index .add (
571- memory_id , embeddings ["abstract_vector " ], metadata
581+ memory_id , embeddings ["full_vector " ], metadata
572582 )
573583
574584 return success
@@ -594,36 +604,55 @@ def find_similar_memories(
594604 # Create filter function if metadata filter is provided
595605 filter_fn = None
596606 if metadata_filter :
597- logger .debug ("Creating filter function for metadata filter: %s" , metadata_filter )
607+ logger .debug (
608+ "Creating filter function for metadata filter: %s" , metadata_filter
609+ )
598610
599611 def filter_fn (metadata ):
600612 logger .debug ("Checking metadata: %s" , metadata )
613+ unmatched_keys = [] # Initialize the list before using it
601614 for key , value in metadata_filter .items ():
602615 # Try direct match in top-level metadata
603616 if key in metadata and metadata [key ] == value :
604617 logger .debug ("Found direct match for %s: %s" , key , value )
605618 continue
606-
619+
607620 # Special handling for 'type' field - also check 'memory_type'
608- if key == 'type' and 'memory_type' in metadata and metadata ['memory_type' ] == value :
621+ if (
622+ key == "type"
623+ and "memory_type" in metadata
624+ and metadata ["memory_type" ] == value
625+ ):
609626 logger .debug ("Found match for type in memory_type: %s" , value )
610627 continue
611-
628+
612629 # Try match in nested content.metadata
613- if 'content' in metadata and isinstance (metadata ['content' ], dict ):
614- content = metadata ['content' ]
615- if 'metadata' in content and isinstance (content ['metadata' ], dict ):
616- content_metadata = content ['metadata' ]
617- if key in content_metadata and content_metadata [key ] == value :
618- logger .debug ("Found nested match for %s: %s in content.metadata" , key , value )
630+ if "content" in metadata and isinstance (metadata ["content" ], dict ):
631+ content = metadata ["content" ]
632+ if "metadata" in content and isinstance (
633+ content ["metadata" ], dict
634+ ):
635+ content_metadata = content ["metadata" ]
636+ if (
637+ key in content_metadata
638+ and content_metadata [key ] == value
639+ ):
640+ logger .debug (
641+ "Found nested match for %s: %s in content.metadata" ,
642+ key ,
643+ value ,
644+ )
619645 continue
620-
646+
621647 # No match found for this key
622648 unmatched_keys .append ((key , value ))
623649 return False
624-
650+
625651 if unmatched_keys :
626- logger .debug ("No matches found for the following keys and values: %s" , unmatched_keys )
652+ logger .debug (
653+ "No matches found for the following keys and values: %s" ,
654+ unmatched_keys ,
655+ )
627656 else :
628657 logger .debug ("All filter criteria matched" )
629658 # All keys matched
0 commit comments