34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 | class ConsolidateService(Service):
"""
The ConsolidateService orchestrates the post-processing pipeline for completed observations,
coordinating summarization, engram generation, index generation, and embedding creation.
This service is triggered when an observation is marked complete and is responsible for the following:
1. **Summarization** - Generates a natural language summary from the observation using an LLM plugin.
2. **Embedding Summaries** - Uses an embedding plugin to create vector embeddings of the summary text.
3. **Engram Generation** - Extracts or constructs engrams from the observation's content.
4. **Index Generation** - Applies an LLM to generate meaningful textual indices for each engram.
5. **Embedding Indices** - Uses an embedding plugin to convert each index into a vector representation.
6. **Publishing Results** - Emits messages like `ENGRAM_COMPLETE`, `META_COMPLETE`, and `INDEX_COMPLETE` at various stages to notify downstream systems.
Metrics are tracked throughout the pipeline using a `MetricsTracker` and returned on demand via the
`on_acknowledge` method.
Attributes:
plugin_manager (PluginManager): Manages access to all system plugins.
llm_summary (dict): Plugin used for generating summaries.
llm_gen_indices (dict): Plugin used for generating indices from engrams.
embedding_gen_embed (dict): Plugin used for generating embeddings for summaries and indices.
db_document (dict): Plugin for document-level database access.
observation_repository (ObservationRepository): Handles deserialization of incoming observations.
engram_builder (dict[str, Engram]): In-memory store of engrams awaiting completion.
index_builder (dict[str, Index]): In-memory store of indices being constructed.
metrics_tracker (MetricsTracker): Tracks metrics across each processing stage.
Methods:
start(): Subscribes the service to message topics.
stop(): Stops the service and clears subscriptions.
on_observation_complete(observation_dict): Handles post-processing when an observation completes.
generate_summary(observation): Creates a summary of the observation content.
on_summary(summary_fut): Callback after summary generation completes.
generate_summary_embeddings(meta): Generates and attaches embeddings for a summary.
generate_engrams(observation): Constructs engrams from observation data.
on_engrams(engram_list_fut): Callback after engram generation; handles index and embedding creation.
gen_indices(index, id_in, engram): Uses an LLM to create indices from an engram.
gen_embeddings(id_and_index_dict, process_index): Creates embeddings for generated indices.
on_acknowledge(message_in): Sends a metrics snapshot for observability/debugging.
"""
def __init__(self, host: Host) -> None:
super().__init__(host)
self.plugin_manager: PluginManager = host.plugin_manager
self.llm_summary: dict[str, Any] = self.plugin_manager.get_plugin('llm', 'summary')
self.llm_gen_indices: dict[str, Any] = self.plugin_manager.get_plugin('llm', 'gen_indices')
self.embedding_gen_embed: dict[str, Any] = self.plugin_manager.get_plugin('embedding', 'gen_embed')
self.db_document: dict[str, Any] = self.plugin_manager.get_plugin('db', 'document')
self.observation_repository = ObservationRepository(self.db_document)
self.engram_builder: dict[str, Engram] = {}
self.index_builder: dict[str, Index] = {}
self.metrics_tracker: MetricsTracker[ConsolidateMetric] = MetricsTracker[ConsolidateMetric]()
def start(self) -> None:
self.subscribe(Service.Topic.OBSERVATION_COMPLETE, self.on_observation_complete)
self.subscribe(Service.Topic.ACKNOWLEDGE, self.on_acknowledge)
def stop(self) -> None:
super().stop()
def on_observation_complete(self, observation_dict: dict[str, Any]) -> None:
if __debug__:
self.host.update_mock_data_input(self, observation_dict)
# should run a task for this.
observation = self.observation_repository.load_dict(observation_dict)
self.metrics_tracker.increment(ConsolidateMetric.OBSERVATIONS_RECIEVED)
summary_observation = self.run_task(self._generate_summary(observation))
summary_observation.add_done_callback(self.on_summary)
generate_engrams = self.run_task(self._generate_engrams(observation))
generate_engrams.add_done_callback(self.on_engrams)
"""
### Summarize
Will be used in the future when we pull in data from other sources.
"""
async def _generate_summary(self, observation: Observation) -> Meta:
if (
observation.meta.summary_full is not None and not observation.meta.summary_full.text
): # native LLM observations have a summary already.
not_test = 'not tested yet'
raise NotImplementedError(not_test)
self.metrics_tracker.increment(ConsolidateMetric.SUMMARIES_GENERATED)
return observation.meta
def on_summary(self, summary_fut: Future[Any]) -> None:
result = summary_fut.result()
self.run_task(self._generate_summary_embeddings(result))
async def _generate_summary_embeddings(self, meta: Meta) -> None:
if meta.summary_full is None:
error = 'Summary full is none.'
raise ValueError(error)
plugin = self.embedding_gen_embed
embedding_list_ret = await asyncio.to_thread(
plugin['func'].gen_embed, strings=[meta.summary_full.text], args=self.host.mock_update_args(plugin)
)
self.host.update_mock_data(plugin, embedding_list_ret)
embedding_list = embedding_list_ret[0]['embeddings_list']
meta.summary_full.embedding = embedding_list[0]
self.send_message_async(Service.Topic.META_COMPLETE, asdict(meta))
"""
### Generate Engrams
Create engrams from the observation.
"""
async def _generate_engrams(self, observation: Observation) -> list[Engram]:
self.metrics_tracker.increment(ConsolidateMetric.ENGRAMS_GENERATED, len(observation.engram_list))
return observation.engram_list
def on_engrams(self, engram_list_fut: Future[Any]) -> None:
engram_list = engram_list_fut.result()
# Keep references so we can fill them in later
for engram in engram_list:
logging.debug('Engram Ready: %s', engram.id)
if self.engram_builder.get(engram.id) is None:
self.engram_builder[engram.id] = engram
else:
error = 'Engram ID Collision. During conslidation, two Engrams with the same IDs were detected.'
raise RuntimeError(error)
# 1) Generate indices for each engram
index_tasks = [self._gen_indices(i, engram.id, engram) for i, engram in enumerate(engram_list)]
indices_future = self.run_tasks(index_tasks)
# Once all indices are generated, generate embeddings
def on_indices_done(indices_list_fut: Future[Any]) -> None:
# This is the accumulated result of each gen_indices(...) call
indices_list: dict[str, Any] = indices_list_fut.result()
# indices_list should have a key like 'gen_indices' -> list[dict[str, Any]]
index_sets: list[dict[str, Any]] = indices_list['_gen_indices']
# 2) Generate embeddings for each index set
embed_tasks = [self._gen_embeddings(index_set, i) for i, index_set in enumerate(index_sets)]
logging.debug('index_sets %s', len(index_sets))
embed_future = self.run_tasks(embed_tasks)
# Once embeddings are generated, then we're truly done
def on_embeddings_done(embed_fut: Future[Any]) -> None:
ret = embed_fut.result() # ret should have 'gen_embeddings' -> list of engram IDs
ids = ret['_gen_embeddings'] # which IDs got their embeddings updated
# 3) Now that embeddings exist, we can send "ENGRAM_COMPLETE" for each
engram_dict: list[dict[str, Any]] = []
for eid in ids:
logging.debug('Done: %s', eid)
engram_dict.append(asdict(self.engram_builder[eid]))
self.send_message_async(Service.Topic.ENGRAM_COMPLETE, {'engram_array': engram_dict})
if __debug__:
self.host.update_mock_data_output(self, {'engram_array': engram_dict})
for eid in ids:
logging.debug('Deleting: %s', eid)
del self.engram_builder[eid]
embed_future.add_done_callback(on_embeddings_done)
indices_future.add_done_callback(on_indices_done)
async def _gen_indices(self, index: int, id_in: str, engram: Engram) -> dict[str, Any]:
data_input = {'engram': engram}
prompt = PromptGenIndices(prompt_str='', input_data=data_input)
plugin = self.llm_gen_indices
response_schema = {'index_text_array': list[str]}
indices = await asyncio.to_thread(
plugin['func'].submit,
prompt=prompt,
structured_schema=response_schema,
args=self.host.mock_update_args(plugin, index),
)
self.host.update_mock_data(plugin, indices, index)
self.metrics_tracker.increment(ConsolidateMetric.INDICES_GENERATED, len(indices))
response_json = json.loads(indices[0]['llm_response'])
return {'id': id_in, 'indices': response_json['index_text_array']}
async def _gen_embeddings(self, id_and_index_dict: dict[str, Any], process_index: int) -> str:
logging.debug('gen_embeddings: indices in %s', len(id_and_index_dict['indices']))
indices = id_and_index_dict['indices']
engram_id: str = id_and_index_dict['id']
plugin = self.embedding_gen_embed
embedding_list_ret = await asyncio.to_thread(
plugin['func'].gen_embed, strings=indices, args=self.host.mock_update_args(plugin, process_index)
)
self.host.update_mock_data(plugin, embedding_list_ret, process_index)
embedding_list = embedding_list_ret[0]['embeddings_list']
self.metrics_tracker.increment(ConsolidateMetric.EMBEDDINGS_GENERATED, len(embedding_list))
# Convert raw embeddings to Index objects and attach them
index_array: list[Index] = []
for i, vec in enumerate(embedding_list):
index = Index(indices[i], vec)
index_array.append(index)
self.engram_builder[engram_id].indices = index_array
serialized_index_array = [asdict(index) for index in index_array]
# We can optionally notify about newly attached indices
self.send_message_async(Service.Topic.INDEX_COMPLETE, {'index': serialized_index_array, 'engram_id': engram_id})
# Return the ID so we know which engram was updated
return engram_id
"""
### Acknowledge
Acknowledge and return metrics
"""
def on_acknowledge(self, message_in: str) -> None:
del message_in
metrics_packet: MetricPacket = self.metrics_tracker.get_and_reset_packet()
self.send_message_async(
Service.Topic.STATUS,
{'id': self.id, 'name': self.__class__.__name__, 'timestamp': time.time(), 'metrics': metrics_packet},
)
|