misc / A-mem /agentic_memory /memory_system.py
NingsenWang's picture
Upload A-mem project snapshot
6239ad9 verified
from typing import Any, Dict, List, Optional, Tuple
import uuid
from datetime import datetime
import json
import logging
from .llm_controller import LLMController
from .retrievers import ChromaRetriever, PersistentChromaRetriever
logger = logging.getLogger(__name__)
def _extract_json_blob(raw_text: str) -> str:
raw_text = raw_text.strip()
if raw_text.startswith("{") and raw_text.endswith("}"):
return raw_text
start = raw_text.find("{")
end = raw_text.rfind("}")
if start != -1 and end != -1 and end > start:
return raw_text[start : end + 1]
return raw_text
class MemoryNote:
"""A memory note that represents a single unit of information in the memory system.
This class encapsulates all metadata associated with a memory, including:
- Core content and identifiers
- Temporal information (creation and access times)
- Semantic metadata (keywords, context, tags)
- Relationship data (links to other memories)
- Usage statistics (retrieval count)
- Evolution tracking (history of changes)
"""
def __init__(
self,
content: str,
id: Optional[str] = None,
keywords: Optional[List[str]] = None,
links: Optional[List[str]] = None,
retrieval_count: Optional[int] = None,
timestamp: Optional[str] = None,
last_accessed: Optional[str] = None,
context: Optional[str] = None,
evolution_history: Optional[List[Any]] = None,
category: Optional[str] = None,
tags: Optional[List[str]] = None,
source_metadata: Optional[Dict[str, Any]] = None,
):
"""Initialize a new memory note with its associated metadata.
Args:
content (str): The main text content of the memory
id (Optional[str]): Unique identifier for the memory. If None, a UUID will be generated
keywords (Optional[List[str]]): Key terms extracted from the content
links (Optional[Dict]): References to related memories
retrieval_count (Optional[int]): Number of times this memory has been accessed
timestamp (Optional[str]): Creation time in format YYYYMMDDHHMM
last_accessed (Optional[str]): Last access time in format YYYYMMDDHHMM
context (Optional[str]): The broader context or domain of the memory
evolution_history (Optional[List]): Record of how the memory has evolved
category (Optional[str]): Classification category
tags (Optional[List[str]]): Additional classification tags
"""
# Core content and ID
self.content = content
self.id = id or str(uuid.uuid4())
# Semantic metadata
self.keywords = keywords or []
self.links = links or []
self.context = context or "General"
self.category = category or "Uncategorized"
self.tags = tags or []
# Temporal information
current_time = datetime.now().strftime("%Y%m%d%H%M")
self.timestamp = timestamp or current_time
self.last_accessed = last_accessed or current_time
# Usage and evolution data
self.retrieval_count = retrieval_count or 0
self.evolution_history = evolution_history or []
self.source_metadata = source_metadata or {}
class AgenticMemorySystem:
"""Core memory system that manages memory notes and their evolution.
This system provides:
- Memory creation, retrieval, update, and deletion
- Content analysis and metadata extraction
- Memory evolution and relationship management
- Hybrid search capabilities
"""
def __init__(
self,
model_name: str = "all-MiniLM-L6-v2",
llm_backend: str = "openai",
llm_model: str = "gpt-4o-mini",
evo_threshold: int = 100,
api_key: Optional[str] = None,
collection_name: str = "memories",
persist_directory: Optional[str] = None,
embedding_backend: str = "sentence_transformer",
embedding_api_key: Optional[str] = None,
llm_base_url: Optional[str] = None,
api_call_logger: Optional[Any] = None,
auto_analyze: bool = False,
raise_on_ingest_error: bool = False,
reset_collection: bool = True,
preserve_upstream_neighbor_indexing: bool = False,
):
"""Initialize the memory system.
Args:
model_name: Name of the sentence transformer model
llm_backend: LLM backend to use (openai/ollama)
llm_model: Name of the LLM model
evo_threshold: Number of memories before triggering evolution
api_key: API key for the LLM service
"""
self.memories: Dict[str, MemoryNote] = {}
self.model_name = model_name
self.collection_name = collection_name
self.persist_directory = persist_directory
self.embedding_backend = embedding_backend
self.embedding_api_key = embedding_api_key or api_key
self.auto_analyze = auto_analyze
self.raise_on_ingest_error = raise_on_ingest_error
self.preserve_upstream_neighbor_indexing = preserve_upstream_neighbor_indexing
self._retriever_kwargs = {
"collection_name": self.collection_name,
"model_name": self.model_name,
"embedding_backend": self.embedding_backend,
"api_key": self.embedding_api_key,
"call_logger": api_call_logger,
}
self.retriever = self._create_retriever()
if reset_collection:
self.retriever.reset_collection()
self.llm_controller = LLMController(
llm_backend,
llm_model,
api_key,
base_url=llm_base_url,
call_logger=api_call_logger,
)
self.evo_cnt = 0
self.evo_threshold = evo_threshold
# Evolution system prompt
self._evolution_system_prompt = '''
You are an AI memory evolution agent responsible for managing and evolving a knowledge base.
Analyze the the new memory note according to keywords and context, also with their several nearest neighbors memory.
Make decisions about its evolution.
The new memory context:
{context}
content: {content}
keywords: {keywords}
The nearest neighbors memories:
{nearest_neighbors_memories}
Based on this information, determine:
1. Should this memory be evolved? Consider its relationships with other memories.
2. What specific actions should be taken (strengthen, update_neighbor)?
2.1 If choose to strengthen the connection, which memory should it be connected to? Can you give the updated tags of this memory?
2.2 If choose to update_neighbor, you can update the context and tags of these memories based on the understanding of these memories. If the context and the tags are not updated, the new context and tags should be the same as the original ones. Generate the new context and tags in the sequential order of the input neighbors.
Tags should be determined by the content of these characteristic of these memories, which can be used to retrieve them later and categorize them.
Note that the length of new_tags_neighborhood must equal the number of input neighbors, and the length of new_context_neighborhood must equal the number of input neighbors.
The number of neighbors is {neighbor_number}.
Return your decision in JSON format with the following structure:
{{
"should_evolve": True or False,
"actions": ["strengthen", "update_neighbor"],
"suggested_connections": ["neighbor_memory_ids"],
"tags_to_update": ["tag_1",..."tag_n"],
"new_context_neighborhood": ["new context",...,"new context"],
"new_tags_neighborhood": [["tag_1",...,"tag_n"],...["tag_1",...,"tag_n"]],
}}
'''
def _create_retriever(self):
if self.persist_directory:
return PersistentChromaRetriever(
directory=self.persist_directory,
extend=False,
**self._retriever_kwargs,
)
return ChromaRetriever(**self._retriever_kwargs)
def _note_to_metadata(self, note: MemoryNote) -> Dict[str, Any]:
metadata = {
"id": note.id,
"content": note.content,
"keywords": note.keywords,
"links": note.links,
"retrieval_count": note.retrieval_count,
"timestamp": note.timestamp,
"last_accessed": note.last_accessed,
"context": note.context,
"evolution_history": note.evolution_history,
"category": note.category,
"tags": note.tags,
"source_metadata": note.source_metadata,
}
if isinstance(note.source_metadata, dict):
for key, value in note.source_metadata.items():
if key not in metadata:
metadata[key] = value
return metadata
def analyze_content(self, content: str) -> Dict:
"""Analyze content using LLM to extract semantic metadata.
Uses a language model to understand the content and extract:
- Keywords: Important terms and concepts
- Context: Overall domain or theme
- Tags: Classification categories
Args:
content (str): The text content to analyze
Returns:
Dict: Contains extracted metadata with keys:
- keywords: List[str]
- context: str
- tags: List[str]
"""
prompt = """Generate a structured analysis of the following content by:
1. Identifying the most salient keywords (focus on nouns, verbs, and key concepts)
2. Extracting core themes and contextual elements
3. Creating relevant categorical tags
Format the response as a JSON object:
{
"keywords": [
// several specific, distinct keywords that capture key concepts and terminology
// Order from most to least important
// Don't include keywords that are the name of the speaker or time
// At least three keywords, but don't be too redundant.
],
"context":
// one sentence summarizing:
// - Main topic/domain
// - Key arguments/points
// - Intended audience/purpose
,
"tags": [
// several broad categories/themes for classification
// Include domain, format, and type tags
// At least three tags, but don't be too redundant.
]
}
Content for analysis:
""" + content
try:
self.llm_controller.set_call_context(
stage="ingest_llm",
operation="analyze_content",
)
response = self.llm_controller.get_completion(
prompt,
response_format={
"type": "json_schema",
"json_schema": {
"name": "response",
"schema": {
"type": "object",
"properties": {
"keywords": {
"type": "array",
"items": {"type": "string"},
},
"context": {"type": "string"},
"tags": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["keywords", "context", "tags"],
"additionalProperties": False,
},
"strict": True,
},
},
)
except Exception as e:
if self.raise_on_ingest_error:
raise
logger.error(f"Error analyzing content request: {e}")
return {"keywords": [], "context": "General", "tags": []}
try:
return json.loads(_extract_json_blob(response))
except Exception as e:
logger.error(f"Error parsing content analysis JSON: {e}")
return {"keywords": [], "context": "General", "tags": []}
def add_note(self, content: str, time: str = None, **kwargs) -> str:
"""Add a new memory note"""
if time is not None:
kwargs['timestamp'] = time
if self.auto_analyze:
analysis = self.analyze_content(content)
kwargs.setdefault("keywords", analysis.get("keywords", []))
kwargs.setdefault("context", analysis.get("context", "General"))
kwargs.setdefault("tags", analysis.get("tags", []))
note = MemoryNote(content=content, **kwargs)
evo_label, note = self.process_memory(note)
self.memories[note.id] = note
metadata = self._note_to_metadata(note)
self.retriever.add_document(note.content, metadata, note.id)
if evo_label == True:
self.evo_cnt += 1
if self.evo_cnt % self.evo_threshold == 0:
self.consolidate_memories()
return note.id
def consolidate_memories(self):
"""Consolidate memories: update retriever with new documents"""
self.retriever.reset_collection()
for memory in self.memories.values():
metadata = self._note_to_metadata(memory)
self.retriever.add_document(memory.content, metadata, memory.id)
def find_related_memories(self, query: str, k: int = 5) -> Tuple[str, List[Any]]:
"""Find related memories using ChromaDB retrieval"""
if not self.memories:
return "", []
try:
# Get results from ChromaDB
results = self.retriever.search(query, k)
# Convert to list of memories
memory_str = ""
neighbor_ids: List[Any] = []
if 'ids' in results and results['ids'] and len(results['ids']) > 0 and len(results['ids'][0]) > 0:
for i, doc_id in enumerate(results['ids'][0]):
# Get metadata from ChromaDB results
if i < len(results['metadatas'][0]):
metadata = results['metadatas'][0][i]
# Format memory string
memory_str += f"memory index:{i}\ttalk start time:{metadata.get('timestamp', '')}\tmemory content: {metadata.get('content', '')}\tmemory context: {metadata.get('context', '')}\tmemory keywords: {str(metadata.get('keywords', []))}\tmemory tags: {str(metadata.get('tags', []))}\n"
if self.preserve_upstream_neighbor_indexing:
neighbor_ids.append(i)
else:
neighbor_ids.append(doc_id)
return memory_str, neighbor_ids
except Exception as e:
logger.error(f"Error in find_related_memories: {str(e)}")
return "", []
def find_related_memories_raw(self, query: str, k: int = 5) -> str:
"""Find related memories using ChromaDB retrieval in raw format"""
if not self.memories:
return ""
# Get results from ChromaDB
results = self.retriever.search(query, k)
# Convert to list of memories
memory_str = ""
if 'ids' in results and results['ids'] and len(results['ids']) > 0:
for i, doc_id in enumerate(results['ids'][0][:k]):
if i < len(results['metadatas'][0]):
# Get metadata from ChromaDB results
metadata = results['metadatas'][0][i]
# Add main memory info
memory_str += f"talk start time:{metadata.get('timestamp', '')}\tmemory content: {metadata.get('content', '')}\tmemory context: {metadata.get('context', '')}\tmemory keywords: {str(metadata.get('keywords', []))}\tmemory tags: {str(metadata.get('tags', []))}\n"
# Add linked memories if available
links = metadata.get('links', [])
j = 0
for link_id in links:
if link_id in self.memories and j < k:
neighbor = self.memories[link_id]
memory_str += f"talk start time:{neighbor.timestamp}\tmemory content: {neighbor.content}\tmemory context: {neighbor.context}\tmemory keywords: {str(neighbor.keywords)}\tmemory tags: {str(neighbor.tags)}\n"
j += 1
return memory_str
def read(self, memory_id: str) -> Optional[MemoryNote]:
"""Retrieve a memory note by its ID.
Args:
memory_id (str): ID of the memory to retrieve
Returns:
MemoryNote if found, None otherwise
"""
return self.memories.get(memory_id)
def update(self, memory_id: str, **kwargs) -> bool:
"""Update a memory note.
Args:
memory_id: ID of memory to update
**kwargs: Fields to update
Returns:
bool: True if update successful
"""
if memory_id not in self.memories:
return False
note = self.memories[memory_id]
# Update fields
for key, value in kwargs.items():
if hasattr(note, key):
setattr(note, key, value)
metadata = self._note_to_metadata(note)
# Delete and re-add to update
self.retriever.delete_document(memory_id)
self.retriever.add_document(document=note.content, metadata=metadata, doc_id=memory_id)
return True
def delete(self, memory_id: str) -> bool:
"""Delete a memory note by its ID.
Args:
memory_id (str): ID of the memory to delete
Returns:
bool: True if memory was deleted, False if not found
"""
if memory_id in self.memories:
# Delete from ChromaDB
self.retriever.delete_document(memory_id)
# Delete from local storage
del self.memories[memory_id]
return True
return False
def _search_raw(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
"""Internal search method that returns raw results from ChromaDB.
This is used internally by the memory evolution system to find
related memories for potential evolution.
Args:
query (str): The search query text
k (int): Maximum number of results to return
Returns:
List[Dict[str, Any]]: Raw search results from ChromaDB
"""
results = self.retriever.search(query, k)
return [{'id': doc_id, 'score': score}
for doc_id, score in zip(results['ids'][0], results['distances'][0])]
def search(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
"""Search for memories using a hybrid retrieval approach."""
# Get results from ChromaDB (only do this once)
search_results = self.retriever.search(query, k)
memories = []
# Process ChromaDB results
for i, doc_id in enumerate(search_results['ids'][0]):
memory = self.memories.get(doc_id)
if memory:
memories.append({
'id': doc_id,
'content': memory.content,
'context': memory.context,
'keywords': memory.keywords,
'score': search_results['distances'][0][i]
})
return memories[:k]
def _search(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
"""Search for memories using a hybrid retrieval approach.
This method combines results from both:
1. ChromaDB vector store (semantic similarity)
2. Embedding-based retrieval (dense vectors)
The results are deduplicated and ranked by relevance.
Args:
query (str): The search query text
k (int): Maximum number of results to return
Returns:
List[Dict[str, Any]]: List of search results, each containing:
- id: Memory ID
- content: Memory content
- score: Similarity score
- metadata: Additional memory metadata
"""
# Get results from ChromaDB
chroma_results = self.retriever.search(query, k)
memories = []
# Process ChromaDB results
for i, doc_id in enumerate(chroma_results['ids'][0]):
memory = self.memories.get(doc_id)
if memory:
memories.append({
'id': doc_id,
'content': memory.content,
'context': memory.context,
'keywords': memory.keywords,
'score': chroma_results['distances'][0][i]
})
# Get results from embedding retriever
embedding_results = self.retriever.search(query, k)
# Combine results with deduplication
seen_ids = set(m['id'] for m in memories)
for result in embedding_results:
memory_id = result.get('id')
if memory_id and memory_id not in seen_ids:
memory = self.memories.get(memory_id)
if memory:
memories.append({
'id': memory_id,
'content': memory.content,
'context': memory.context,
'keywords': memory.keywords,
'score': result.get('score', 0.0)
})
seen_ids.add(memory_id)
return memories[:k]
def search_agentic(self, query: str, k: int = 5) -> List[Dict[str, Any]]:
"""Search for memories using ChromaDB retrieval."""
if not self.memories:
return []
try:
# Get results from ChromaDB
results = self.retriever.search(query, k)
# Process results
memories = []
seen_ids = set()
# Check if we have valid results
if ('ids' not in results or not results['ids'] or
len(results['ids']) == 0 or len(results['ids'][0]) == 0):
return []
# Process ChromaDB results
for i, doc_id in enumerate(results['ids'][0][:k]):
if doc_id in seen_ids:
continue
if i < len(results['metadatas'][0]):
metadata = results['metadatas'][0][i]
# Create result dictionary with all metadata fields
memory_dict = {
'id': doc_id,
'content': metadata.get('content', ''),
'context': metadata.get('context', ''),
'keywords': metadata.get('keywords', []),
'tags': metadata.get('tags', []),
'links': metadata.get('links', []),
'timestamp': metadata.get('timestamp', ''),
'category': metadata.get('category', 'Uncategorized'),
'source_metadata': metadata.get('source_metadata', {}),
'is_neighbor': False
}
# Add score if available
if 'distances' in results and len(results['distances']) > 0 and i < len(results['distances'][0]):
memory_dict['score'] = results['distances'][0][i]
memories.append(memory_dict)
seen_ids.add(doc_id)
# Add linked memories (neighbors)
neighbor_count = 0
for memory in list(memories): # Use a copy to avoid modification during iteration
if neighbor_count >= k:
break
# Get links from metadata
links = memory.get('links', [])
if not links and 'id' in memory:
# Try to get links from memory object
mem_obj = self.memories.get(memory['id'])
if mem_obj:
links = mem_obj.links
for link_id in links:
if link_id not in seen_ids and neighbor_count < k:
neighbor = self.memories.get(link_id)
if neighbor:
memories.append({
'id': link_id,
'content': neighbor.content,
'context': neighbor.context,
'keywords': neighbor.keywords,
'tags': neighbor.tags,
'links': neighbor.links,
'timestamp': neighbor.timestamp,
'category': neighbor.category,
'source_metadata': neighbor.source_metadata,
'is_neighbor': True
})
seen_ids.add(link_id)
neighbor_count += 1
return memories[:k]
except Exception as e:
logger.error(f"Error in search_agentic: {str(e)}")
return []
def process_memory(self, note: MemoryNote) -> Tuple[bool, MemoryNote]:
"""Process a memory note and determine if it should evolve.
Args:
note: The memory note to process
Returns:
Tuple[bool, MemoryNote]: (should_evolve, processed_note)
"""
# For first memory or testing, just return the note without evolution
if not self.memories:
return False, note
try:
# Get nearest neighbors
neighbors_text, neighbor_ids = self.find_related_memories(note.content, k=5)
if not neighbors_text or not neighbor_ids:
return False, note
# Format neighbors for LLM - in this case, neighbors_text is already formatted
# Query LLM for evolution decision
prompt = self._evolution_system_prompt.format(
content=note.content,
context=note.context,
keywords=note.keywords,
nearest_neighbors_memories=neighbors_text,
neighbor_number=len(neighbor_ids)
)
try:
self.llm_controller.set_call_context(
stage="ingest_llm",
operation="process_memory",
note_id=note.id,
)
response = self.llm_controller.get_completion(
prompt,
response_format={"type": "json_schema", "json_schema": {
"name": "response",
"schema": {
"type": "object",
"properties": {
"should_evolve": {
"type": "boolean"
},
"actions": {
"type": "array",
"items": {
"type": "string"
}
},
"suggested_connections": {
"type": "array",
"items": {
"type": "string"
}
},
"new_context_neighborhood": {
"type": "array",
"items": {
"type": "string"
}
},
"tags_to_update": {
"type": "array",
"items": {
"type": "string"
}
},
"new_tags_neighborhood": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"required": ["should_evolve", "actions", "suggested_connections",
"tags_to_update", "new_context_neighborhood", "new_tags_neighborhood"],
"additionalProperties": False
},
"strict": True
}}
)
except Exception as e:
if self.raise_on_ingest_error:
raise
logger.error(f"Error in memory evolution request: {str(e)}")
return False, note
try:
response_json = json.loads(_extract_json_blob(response))
should_evolve = response_json["should_evolve"]
if should_evolve:
actions = response_json["actions"]
for action in actions:
if action == "strengthen":
suggest_connections = response_json["suggested_connections"]
new_tags = response_json["tags_to_update"]
note.links.extend(suggest_connections)
note.tags = new_tags
elif action == "update_neighbor":
new_context_neighborhood = response_json["new_context_neighborhood"]
new_tags_neighborhood = response_json["new_tags_neighborhood"]
for i in range(min(len(neighbor_ids), len(new_tags_neighborhood))):
tag = new_tags_neighborhood[i]
if i < len(new_context_neighborhood):
context = new_context_neighborhood[i]
else:
if self.preserve_upstream_neighbor_indexing:
noteslist = list(self.memories.values())
if i >= len(noteslist):
continue
context = noteslist[i].context
else:
neighbor_note = self.memories.get(neighbor_ids[i])
if neighbor_note is None:
continue
context = neighbor_note.context
if self.preserve_upstream_neighbor_indexing:
noteslist = list(self.memories.values())
notes_id = list(self.memories.keys())
memorytmp_idx = neighbor_ids[i]
if memorytmp_idx < len(noteslist):
notetmp = noteslist[memorytmp_idx]
notetmp.tags = tag
notetmp.context = context
if memorytmp_idx < len(notes_id):
self.memories[notes_id[memorytmp_idx]] = notetmp
else:
neighbor_id = neighbor_ids[i]
neighbor_note = self.memories.get(neighbor_id)
if neighbor_note is not None:
neighbor_note.tags = tag
neighbor_note.context = context
self.memories[neighbor_id] = neighbor_note
return should_evolve, note
except (json.JSONDecodeError, KeyError, Exception) as e:
logger.error(f"Error in memory evolution: {str(e)}")
return False, note
except Exception as e:
if self.raise_on_ingest_error:
raise
logger.error(f"Error in process_memory: {str(e)}")
return False, note