popoto.recipes.subconscious_memory¶
popoto.recipes.subconscious_memory
¶
SubconsciousMemory -- Automatic memory injection and extraction around LLM turns.
Wraps an existing chat flow with: - Pre-turn: assemble relevant memories, inject as system context - Post-turn: extract facts/observations from LLM response, save as Memory - Outcome: report how injected memories were used
Architecture::
User message
|
v
[Pre-turn hook: ContextAssembler.assemble() -> inject into messages]
|
v
[LLM inference]
|
v
[Post-turn hook: extract observations from response -> save as Memory records]
|
v
[Outcome hook: report acted/dismissed/contradicted via ObservationProtocol]
|
v
Agent response
The recipe is framework-agnostic -- it works with plain list[dict]
messages. The guide shows how to wire it into PydanticAI or the OpenAI SDK,
but the recipe itself has no framework dependencies.
Dependencies
ContextAssembler (from popoto.recipes) ObservationProtocol (from popoto.fields.observation) A Popoto Model class with at least Level 1 fields (DecayingSortedField)
Example
from popoto.recipes.subconscious_memory import SubconsciousMemory
sm = SubconsciousMemory( model_class=Memory, agent_id="agent-1", score_weights={"relevance": 0.6, "confidence": 0.3}, )
Pre-turn: inject context¶
messages, assembly_result = sm.inject_context(messages)
... call LLM with messages ...¶
Post-turn: extract and save memories¶
new_memories = sm.extract_memories(response_text, importance=0.6)
Outcome: report usage¶
sm.report_outcomes(assembly_result)
DEFAULT_EXTRACTION_MIN_LENGTH = 10
module-attribute
¶
Minimum sentence length (chars) to be considered a fact worth saving.
DEFAULT_SYSTEM_PREAMBLE = 'You are a helpful assistant.'
module-attribute
¶
Default system message preamble when no system message exists.
SubconsciousMemory
¶
Automatic memory injection and extraction around LLM turns.
Wraps an existing chat flow with: - Pre-turn: assemble relevant memories, inject as system context - Post-turn: extract facts/observations from LLM response, save as Memory - Outcome: report how injected memories were used
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
Popoto Model class (any level from the quickstart guide). |
required | |
agent_id
|
Identifier for the agent whose memories to query/save. |
required | |
score_weights
|
Dict mapping field names to weights for ContextAssembler (e.g., {"relevance": 0.6, "confidence": 0.3}). |
required | |
max_items
|
Maximum memory records to inject per turn. Default 10. |
10
|
|
max_tokens
|
Soft token budget for injected context. Default 4000. |
4000
|
|
extraction_min_length
|
Minimum characters for a sentence to be extracted as a memory. Default 10. |
DEFAULT_EXTRACTION_MIN_LENGTH
|
|
system_preamble
|
System message prefix used when injecting context. Default "You are a helpful assistant." |
DEFAULT_SYSTEM_PREAMBLE
|
|
content_field
|
Name of the field on model_class that stores the text content. Default "content". |
'content'
|
|
importance_field
|
Name of the field on model_class that stores importance score. Default "importance". |
'importance'
|
|
agent_id_field
|
Name of the KeyField for agent partitioning. Default "agent_id". |
'agent_id'
|
Source code in src/popoto/recipes/subconscious_memory.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
inject_context(messages)
¶
Pre-turn: assemble memories and inject into the messages array.
Finds or creates a system message at index 0 and appends assembled memory context to it. Returns the modified messages list and the AssemblyResult for later outcome reporting.
If no memories are found, the messages are returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List of message dicts with "role" and "content" keys. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple of (modified_messages, AssemblyResult). The messages list |
|
|
is modified in-place for convenience but also returned. |
Source code in src/popoto/recipes/subconscious_memory.py
extract_memories(response_text, importance=0.5)
¶
Post-turn: extract facts from LLM response and save as Memory records.
Uses a simple heuristic: split response into sentences, filter by minimum length, and save each as a separate Memory record.
For LLM-based extraction (more accurate but requires an API call), override this method or pass extracted facts directly to your model_class.save().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_text
|
The LLM's response text. |
required | |
importance
|
Default importance score for extracted memories. Float between 0.0 and 1.0. Default 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
|
List of saved model instances. Empty list if response_text |
|
|
is empty or contains no extractable facts. |
Source code in src/popoto/recipes/subconscious_memory.py
report_outcomes(assembly_result, outcome='acted')
¶
Outcome hook: report how injected memories were used.
Calls ObservationProtocol.on_context_used() for all records in the assembly result with the specified outcome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly_result
|
AssemblyResult from inject_context(). |
required | |
outcome
|
How the agent used the memories. One of "acted", "dismissed", "contradicted", "deferred". Default "acted". |
'acted'
|