|
| 1 | +""" |
| 2 | +memory/entity.py — Entity Memory backend for MemoryLens. |
| 3 | +
|
| 4 | +Implements structured named-entity extraction: instead of storing raw message |
| 5 | +history, EntityMemory maintains a key-value store of extracted facts and |
| 6 | +returns those facts as context. When a fact is updated, the store is patched |
| 7 | +in-place, so retrieval always surfaces the current value. |
| 8 | +
|
| 9 | +Extraction is fully local (regex-based, no LLM call) to keep the backend fast |
| 10 | +and reproducible for the benchmark harness. |
| 11 | +
|
| 12 | +Closes #12. |
| 13 | +""" |
| 14 | + |
| 15 | +import re |
| 16 | +from typing import Dict, List, Optional, Tuple |
| 17 | +from .base import BaseMemory |
| 18 | + |
| 19 | + |
| 20 | +# Patterns recognised by the extractor |
| 21 | +# Each tuple: (compiled_pattern, group_name_for_key, group_name_for_value) |
| 22 | +_INJECTION_RE = re.compile( |
| 23 | + r"my\s+(?P<key>[a-z][a-z ]{0,30}?)\s+is\s+(?P<value>.+?)[\.\!]?\s*$", |
| 24 | + re.IGNORECASE, |
| 25 | +) |
| 26 | +_UPDATE_RE = re.compile( |
| 27 | + r"my\s+(?P<key>[a-z][a-z ]{0,30}?)\s+has\s+changed\s+to\s+(?P<value>.+?)[\.\!]?\s*$", |
| 28 | + re.IGNORECASE, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def _extract_entity(content: str) -> Optional[Tuple[str, str]]: |
| 33 | + """ |
| 34 | + Try to extract a (key, value) fact pair from a single message string. |
| 35 | + Returns (normalised_key, value) or None if no pattern matches. |
| 36 | + """ |
| 37 | + for pattern in (_UPDATE_RE, _INJECTION_RE): |
| 38 | + m = pattern.search(content) |
| 39 | + if m: |
| 40 | + key = m.group("key").strip().lower() |
| 41 | + val = m.group("value").strip() |
| 42 | + return key, val |
| 43 | + return None |
| 44 | + |
| 45 | + |
| 46 | +class EntityMemory(BaseMemory): |
| 47 | + """ |
| 48 | + Structured entity-extraction memory backend. |
| 49 | +
|
| 50 | + Facts are stored as a key-value dictionary rather than as raw messages. |
| 51 | + On retrieval the entire entity store is serialised into a concise context |
| 52 | + block so the LLM always sees the *current* value of every known fact. |
| 53 | +
|
| 54 | + Advantages over RAG/Naive for fact-tracking tasks: |
| 55 | + - O(1) update: overwriting a key replaces the fact immediately |
| 56 | + - No stale value can persist once an update is absorbed |
| 57 | + - Token cost is proportional to the number of unique facts, not turns |
| 58 | +
|
| 59 | + Limitations: |
| 60 | + - Only captures facts matching the injection/update templates from the |
| 61 | + benchmark's conversation generator; free-form facts are not extracted |
| 62 | + - Does not retain conversational flow or filler turns |
| 63 | +
|
| 64 | + Reference: Entity-centric memory as described in |
| 65 | + Xu et al. (2021) "Beyond Goldfish Memory" and related work on |
| 66 | + structured dialogue state tracking. |
| 67 | + """ |
| 68 | + |
| 69 | + name = "entity" |
| 70 | + |
| 71 | + def __init__(self, max_facts: int = 64): |
| 72 | + # Ordered so serialisation is deterministic |
| 73 | + self.entities: Dict[str, str] = {} |
| 74 | + self.max_facts = max_facts |
| 75 | + |
| 76 | + def add_message(self, role: str, content: str, turn: int) -> None: |
| 77 | + if role != "user": |
| 78 | + return # only extract from user utterances |
| 79 | + pair = _extract_entity(content) |
| 80 | + if pair: |
| 81 | + key, val = pair |
| 82 | + self.entities[key] = val |
| 83 | + # Enforce max_facts by evicting oldest key when over limit |
| 84 | + if len(self.entities) > self.max_facts: |
| 85 | + oldest_key = next(iter(self.entities)) |
| 86 | + del self.entities[oldest_key] |
| 87 | + |
| 88 | + def get_context(self, query: str, current_turn: int) -> List[Dict]: |
| 89 | + if not self.entities: |
| 90 | + return [] |
| 91 | + |
| 92 | + lines = [f"my {key} is {val}" for key, val in self.entities.items()] |
| 93 | + entity_block = "; ".join(lines) + "." |
| 94 | + return [ |
| 95 | + { |
| 96 | + "role": "system", |
| 97 | + "content": f"[Known facts about the user] {entity_block}", |
| 98 | + } |
| 99 | + ] |
| 100 | + |
| 101 | + def reset(self) -> None: |
| 102 | + self.entities = {} |
0 commit comments