Basic memory unit with metadata
| 259 | raise ValueError("Backend must be 'openai', 'ollama', or 'sglang'") |
| 260 | |
| 261 | class MemoryNote: |
| 262 | """Basic memory unit with metadata""" |
| 263 | def __init__(self, |
| 264 | content: str, |
| 265 | id: Optional[str] = None, |
| 266 | keywords: Optional[List[str]] = None, |
| 267 | links: Optional[Dict] = None, |
| 268 | importance_score: Optional[float] = None, |
| 269 | retrieval_count: Optional[int] = None, |
| 270 | timestamp: Optional[str] = None, |
| 271 | last_accessed: Optional[str] = None, |
| 272 | context: Optional[str] = None, |
| 273 | evolution_history: Optional[List] = None, |
| 274 | category: Optional[str] = None, |
| 275 | tags: Optional[List[str]] = None, |
| 276 | llm_controller: Optional[LLMController] = None): |
| 277 | |
| 278 | self.content = content |
| 279 | |
| 280 | # Generate metadata using LLM if not provided and controller is available |
| 281 | if llm_controller and any(param is None for param in [keywords, context, category, tags]): |
| 282 | analysis = self.analyze_content(content, llm_controller) |
| 283 | print("analysis", analysis) |
| 284 | keywords = keywords or analysis["keywords"] |
| 285 | context = context or analysis["context"] |
| 286 | tags = tags or analysis["tags"] |
| 287 | |
| 288 | # Set default values for optional parameters |
| 289 | self.id = id or str(uuid.uuid4()) |
| 290 | self.keywords = keywords or [] |
| 291 | self.links = links or [] |
| 292 | self.importance_score = importance_score or 1.0 |
| 293 | self.retrieval_count = retrieval_count or 0 |
| 294 | current_time = datetime.now().strftime("%Y%m%d%H%M") |
| 295 | self.timestamp = timestamp or current_time |
| 296 | self.last_accessed = last_accessed or current_time |
| 297 | |
| 298 | # Handle context that can be either string or list |
| 299 | self.context = context or "General" |
| 300 | if isinstance(self.context, list): |
| 301 | self.context = " ".join(self.context) # Convert list to string by joining |
| 302 | |
| 303 | self.evolution_history = evolution_history or [] |
| 304 | self.category = category or "Uncategorized" |
| 305 | self.tags = tags or [] |
| 306 | |
| 307 | @staticmethod |
| 308 | def analyze_content(content: str, llm_controller: LLMController) -> Dict: |
| 309 | """Analyze content to extract keywords, context, and other metadata""" |
| 310 | prompt = """Generate a structured analysis of the following content by: |
| 311 | 1. Identifying the most salient keywords (focus on nouns, verbs, and key concepts) |
| 312 | 2. Extracting core themes and contextual elements |
| 313 | 3. Creating relevant categorical tags |
| 314 | |
| 315 | Format the response as a JSON object: |
| 316 | { |
| 317 | "keywords": [ |
| 318 | // several specific, distinct keywords that capture key concepts and terminology |