Replies: 1 comment
-
|
Metadata for extracted memories is essential for production use. Here's a comprehensive approach: Metadata Schema Designfrom dataclasses import dataclass, field
from datetime import datetime
from typing import List, Dict, Optional
from enum import Enum
class MemorySource(Enum):
USER_EXPLICIT = "user_explicit" # User directly stated
USER_IMPLICIT = "user_implicit" # Inferred from behavior
AGENT_DERIVED = "agent_derived" # Agent concluded
EXTERNAL_API = "external_api" # From external source
class ConfidenceLevel(Enum):
HIGH = "high" # Directly stated
MEDIUM = "medium" # Reasonably inferred
LOW = "low" # Speculative
@dataclass
class MemoryMetadata:
# Provenance
source: MemorySource
source_message_id: Optional[str] = None
extraction_model: str = "gpt-4"
# Confidence
confidence: ConfidenceLevel = ConfidenceLevel.MEDIUM
confidence_score: float = 0.8
# Temporal
created_at: datetime = field(default_factory=datetime.utcnow)
valid_from: Optional[datetime] = None
valid_until: Optional[datetime] = None # For time-bound facts
last_accessed: Optional[datetime] = None
access_count: int = 0
# Categorization
category: str = "general" # preference, fact, event, relationship
tags: List[str] = field(default_factory=list)
entities: List[str] = field(default_factory=list)
# Versioning
version: int = 1
supersedes: Optional[str] = None # ID of memory this updates
superseded_by: Optional[str] = NoneUsage in Retrievaldef smart_retrieve(query: str, user_id: str) -> List[Memory]:
memories = mem0.search(query, user_id=user_id)
# Filter and rank by metadata
scored = []
for mem in memories:
score = mem.score # Base vector similarity
# Boost recent memories
age_days = (datetime.utcnow() - mem.metadata.created_at).days
recency_boost = 1.0 / (1 + age_days * 0.1)
# Boost high confidence
confidence_boost = {"high": 1.2, "medium": 1.0, "low": 0.8}
# Penalize expired memories
if mem.metadata.valid_until and mem.metadata.valid_until < datetime.utcnow():
score *= 0.3
final_score = score * recency_boost * confidence_boost[mem.metadata.confidence.value]
scored.append((mem, final_score))
return sorted(scored, key=lambda x: x[1], reverse=True)Key Metadata to Track
More on memory patterns: https://github.com/KeepALifeUS/autonomous-agents |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi mem0 team
I’ve been using
custom_fact_extraction_promptand really like how much control it gives over what gets stored as memory. I wanted to ask about a possible extension to allow custom metadata to be attached to extracted facts.In applications, like personalized tutoring, memories represent a student’s learning state, not just a static fact. Along with the extracted text, it’s useful to capture:
For example:
Examples:
Would it be feasible for
custom_fact_extraction_promptto optionally return metadata along with each fact?Something like:
{ "facts": [ { "text": "Finished 50% of calculus chapter", "metadata": { "category": "progress", "level": "strong" } } ] }Alternatively, is there an existing or recommended pattern for attaching progress levels or custom categories to extracted memories?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions