Skip to content

Consolidate Service

Bases: Service

Orchestrates the post-processing pipeline for completed observations.

This service is triggered when an observation is marked complete and coordinates summarization, engram generation, index generation, and embedding creation through the following pipeline stages:

  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.

Attributes:

Name Type Description
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.

metrics_tracker MetricsTracker

Tracks metrics across each processing stage.

Methods:

Name Description
start

Subscribes the service to message topics.

stop

Stops the service and clears subscriptions.

on_observation_complete

Handles post-processing when an observation completes.

_generate_summary_embeddings

Creates and attaches embeddings for a summary.

process_engrams

Orchestrates the generation of indices and embeddings for engrams.

_gen_indices

Uses an LLM to create indices from an engram.

_gen_embeddings

Creates embeddings for generated indices.

on_acknowledge

Sends a metrics snapshot for observability/debugging.

Source code in src/engramic/application/consolidate/consolidate_service.py
 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
class ConsolidateService(Service):
    """
    Orchestrates the post-processing pipeline for completed observations.

    This service is triggered when an observation is marked complete and coordinates summarization,
    engram generation, index generation, and embedding creation through the following pipeline stages:

    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.

    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.
        metrics_tracker (MetricsTracker): Tracks metrics across each processing stage.

    Methods:
        start() -> None:
            Subscribes the service to message topics.
        stop() -> None:
            Stops the service and clears subscriptions.
        on_observation_complete(observation_dict) -> None:
            Handles post-processing when an observation completes.
        _generate_summary_embeddings(observation) -> Meta:
            Creates and attaches embeddings for a summary.
        process_engrams(observation) -> None:
            Orchestrates the generation of indices and embeddings for engrams.
        _gen_indices(index, engram, repo_ids, tracking_id) -> dict:
            Uses an LLM to create indices from an engram.
        _gen_embeddings(id_and_index_dict, process_index) -> dict:
            Creates embeddings for generated indices.
        on_acknowledge(message_in) -> None:
            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.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)
        super().start()

    async def stop(self) -> None:
        await super().stop()

    def on_observation_complete(self, observation_dict: dict[str, Any]) -> None:
        # print("run consolidate")
        # should run a task for this.
        observation = self.observation_repository.load_dict(observation_dict)

        if __debug__:
            if observation.tracking_id is None:
                error = 'Tracking id is None but expected not to be.'
                raise ValueError(error)

            self.host.update_mock_data_input(self, observation_dict, observation.tracking_id)

        self.metrics_tracker.increment(ConsolidateMetric.OBSERVATIONS_RECIEVED)

        # So, a bit of a race condition here. If meta lags engrams could signal inserterd before the meta.
        # I think this is unlikely to happen practically, but it would be better to fix this and know for sure.
        future = self.run_task(self._generate_summary_embeddings(observation))
        future.add_done_callback(self.on_generate_summary_embeddings)

        self.process_engrams(observation)

    """
    ### Generate meta embeddings
    """

    async def _generate_summary_embeddings(self, observation: Observation) -> Meta:
        if observation.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=[observation.meta.summary_full.text],
            args=self.host.mock_update_args(plugin, 0, str(observation.meta.source_ids)),
        )

        self.host.update_mock_data(plugin, embedding_list_ret, 0, str(observation.meta.source_ids))

        embedding_list = embedding_list_ret[0]['embeddings_list']
        observation.meta.summary_full.embedding = embedding_list[0]
        return observation.meta

    def on_generate_summary_embeddings(self, future: Future[Any]) -> None:
        meta = future.result()
        self.send_message_async(Service.Topic.META_COMPLETE, asdict(meta))

    """
    ### Generate Engrams

    Create engrams from the observation.
    """

    async def _generate_engrams(self, observation: Observation) -> Observation:
        self.metrics_tracker.increment(ConsolidateMetric.ENGRAMS_GENERATED, len(observation.engram_list))

        return observation

    def process_engrams(self, observation: Observation) -> None:
        engram_list = observation.engram_list
        engram_ids = [engram.id for engram in engram_list]

        self.send_message_async(
            Service.Topic.ENGRAMS_CREATED,
            {'engram_id_array': engram_ids, 'parent_id': observation.id, 'tracking_id': observation.tracking_id},
        )

        # Keep references so we can fill them in later
        for engram in engram_list:
            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, engram.repo_ids, observation.tracking_id)
            for i, engram in enumerate(engram_list)
        ]

        indices_future = self.run_tasks(index_tasks)

        indices_future.add_done_callback(self.on_indices_done)

    async def _gen_indices(
        self, index: int, engram: Engram, repo_ids: list[str] | None, tracking_id: str | None
    ) -> 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, str(tracking_id)),
            images=None,
        )

        load_json = json.loads(indices[0]['llm_response'])
        response_json: dict[str, Any] = {'index_text_array': []}

        # generate context
        context_string = 'Context: '

        if engram.context is None:
            error = 'None context found in engram.'
            raise RuntimeError(error)

        for item, key in engram.context.items():
            if key != 'null':
                context_string += f'{item}: {key}\n'

        # add in the context to each index.
        for index_item in load_json['index_text_array']:
            response_json['index_text_array'].append(context_string + ' Content: ' + index_item)

        self.host.update_mock_data(plugin, indices, index, str(tracking_id))

        self.metrics_tracker.increment(ConsolidateMetric.INDICES_GENERATED, len(indices))

        if len(response_json['index_text_array']) == 0:
            error = 'An empty index was created.'
            raise RuntimeError(error)

        return {
            'engram_id': engram.id,
            'indices': response_json['index_text_array'],
            'repo_ids': repo_ids,
            'tracking_id': tracking_id,
        }

    # Once all indices are generated, generate embeddings
    def on_indices_done(self, 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)]

        embed_future = self.run_tasks(embed_tasks)

        embed_future.add_done_callback(self.on_embeddings_done)

    async def _gen_embeddings(self, id_and_index_dict: dict[str, Any], process_index: int) -> dict[str, Any]:
        indices = id_and_index_dict['indices']
        engram_id: str = id_and_index_dict['engram_id']
        repo_ids: str = id_and_index_dict['repo_ids']
        tracking_id: str = id_and_index_dict['tracking_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, tracking_id),
        )

        self.host.update_mock_data(plugin, embedding_list_ret, process_index, tracking_id)

        embedding_list = embedding_list_ret[0]['embeddings_list']

        self.metrics_tracker.increment(ConsolidateMetric.EMBEDDINGS_GENERATED, len(embedding_list))

        index_id_array = []

        # Convert raw embeddings to Index objects and attach them
        try:
            index_array: list[Index] = []
            for i, vec in enumerate(embedding_list):
                index = Index(indices[i], vec)
                index_array.append(index)
                index_id_array.append(index.id)
        except Exception:
            logging.exception('Exception caught.')

        self.send_message_async(
            Service.Topic.INDICES_CREATED,
            {'parent_id': engram_id, 'index_id_array': index_id_array, 'tracking_id': tracking_id},
        )

        self.engram_builder[engram_id].indices = index_array
        serialized_index_array = [asdict(index) for index in index_array]

        # Return the ID so we know which engram was updated
        return {
            'engram_id': engram_id,
            'tracking_id': tracking_id,
            'index_array': serialized_index_array,
            'repo_ids': repo_ids,
        }

    # Once embeddings are generated, then we're truly done
    def on_embeddings_done(self, embed_fut: Future[Any]) -> None:
        ret = embed_fut.result()  # ret should have 'gen_embeddings' -> list of engram IDs

        ret_dict = ret['_gen_embeddings']  # which IDs got their embeddings updated

        # Now that embeddings exist, we can send "ENGRAM_COMPLETE" for each
        engram_dict: list[dict[str, Any]] = []

        for engram in ret_dict:
            builder_data: dict[str, Any] = asdict(self.engram_builder[engram['engram_id']])
            engram_dict.append(builder_data)

            # We can optionally notify about newly attached indices
            self.send_message_async(
                Service.Topic.INDICES_COMPLETE,
                {
                    'index': engram['index_array'],
                    'engram_id': engram['engram_id'],
                    'tracking_id': engram['tracking_id'],
                    'repo_ids': engram['repo_ids'],
                },
            )

        self.send_message_async(
            Service.Topic.ENGRAM_COMPLETE, {'engram_array': engram_dict, 'tracking_id': ret_dict[0]['tracking_id']}
        )

        if __debug__:
            self.host.update_mock_data_output(self, {'engram_array': engram_dict}, ret_dict[0]['tracking_id'])

        for eid in ret_dict:
            del self.engram_builder[eid['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},
        )