Download agent/utils/memory.py from Anirudh1950/TITANS-Research-Agent: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/Anirudh1950/TITANS-Research-Agent/resolve/main/agent/utils/memory.py
- Command line
-
hf download hf://Anirudh1950/TITANS-Research-Agent/agent/utils/memory.py
-
curl -L -o memory.py https://huggingface.co/Anirudh1950/TITANS-Research-Agent/resolve/main/agent/utils/memory.py
3.38 kB
| import chromadb | |
| import uuid | |
| from typing import List, Dict, Any | |
| class MemoryManager: | |
| def __init__(self, db_path: str = "./chroma_db", collection_name: str = "scraper_memory"): | |
| print(f"Initializing Long-Term Memory (ChromaDB) at '{db_path}'...") | |
| try: | |
| self.client = chromadb.PersistentClient(path=db_path) | |
| self.collection = self.client.get_or_create_collection(name=collection_name) | |
| except Exception as e: | |
| print(f"Error initializing memory: {e}") | |
| self.client = None | |
| self.collection = None | |
| def store(self, items: List[Dict[str, Any]], topic: str): | |
| """ | |
| Stores scraped items into Long-Term Memory. | |
| """ | |
| if not self.collection or not items: | |
| return | |
| documents = [] | |
| metadatas = [] | |
| ids = [] | |
| print(f"Memorizing {len(items)} items for topic '{topic}'...") | |
| for item in items: | |
| # Create a rich text representation for embedding | |
| content = f"{item.get('title', '')}\n{item.get('text', '') or item.get('snippet', '')}\n{item.get('content', '')}" | |
| if not content.strip(): | |
| continue | |
| documents.append(content) | |
| # Metadata must be simple types | |
| meta = { | |
| "source": item.get('source', 'unknown'), | |
| "url": item.get('url') or item.get('link') or "none", | |
| "topic": topic, | |
| "timestamp": str(item.get("timestamp", "")) | |
| } | |
| metadatas.append(meta) | |
| # Unique ID | |
| ids.append(f"{topic}_{str(uuid.uuid4())[:8]}") | |
| try: | |
| self.collection.add( | |
| documents=documents, | |
| metadatas=metadatas, | |
| ids=ids | |
| ) | |
| print(" [Memory] Successfully stored items.") | |
| except Exception as e: | |
| print(f" [Memory] Error storing items: {e}") | |
| def recall(self, query: str, n_results: int = 3) -> List[Dict[str, Any]]: | |
| """ | |
| Recalls relevant information from memory. | |
| """ | |
| if not self.collection: | |
| return [] | |
| print(f"Recalling memories for: '{query}'...") | |
| try: | |
| results = self.collection.query( | |
| query_texts=[query], | |
| n_results=n_results | |
| ) | |
| # Format results back to list of dicts | |
| recalled_items = [] | |
| if results['documents']: | |
| for i, doc in enumerate(results['documents'][0]): | |
| meta = results['metadatas'][0][i] | |
| recalled_items.append({ | |
| "content": doc, | |
| "source": meta.get("source"), | |
| "url": meta.get("url"), | |
| "memory_type": "long_term_recall" | |
| }) | |
| if recalled_items: | |
| print(f" [Memory] Recalled {len(recalled_items)} past facts.") | |
| else: | |
| print(" [Memory] No relevant memories found.") | |
| return recalled_items | |
| except Exception as e: | |
| print(f" [Memory] Error recalling: {e}") | |
| return [] | |