Skip to content

Codify Service

Bases: Service

A service responsible for validating and extracting engrams from AI model responses using a TOML-based validation pipeline.

This service listens for prompts that have completed processing, and if the system is in training mode, it fetches related engrams and metadata, applies an LLM-based validation process, and stores structured observations. It tracks metrics related to its activity and supports training workflows.

Attributes:

Name Type Description
plugin_manager PluginManager

Manages access to system plugins such as the LLM and document DB.

llm_validate dict

Plugin for LLM-based validation.

db_document_plugin dict

Plugin for document database access.

engram_repository EngramRepository

Repository for accessing and managing engram data.

meta_repository MetaRepository

Repository for associated metadata retrieval.

observation_repository ObservationRepository

Handles validation and normalization of observation data.

prompt Prompt

Default prompt object used during validation.

metrics_tracker MetricsTracker

Tracks custom CodifyMetric metrics.

training_mode bool

Flag indicating whether the system is in training mode.

Methods:

Name Description
start

Subscribes the service to key topics.

stop

Stops the service.

init_async

Initializes async components, including DB connections.

on_codify_response

dict[str, Any]) -> None: Handles on-demand codification requests for specific response IDs.

_fetch_history

str, repo_ids_filters: list[str]) -> list[dict[str, Any]]: Asynchronously fetches history for a specific response ID.

_on_fetch_history_codify

Future[Any]) -> None: Callback that processes fetched history and triggers codification.

on_main_prompt_complete

dict[str, Any], *, is_on_demand: bool = False) -> None: Main entry point triggered after a model completes a prompt.

_fetch_engrams

Response) -> dict[str, Any]: Asynchronously fetches engrams associated with a response.

on_fetch_engram_complete

Future[Any]) -> None: Callback that processes fetched engrams and triggers metadata retrieval.

_fetch_meta

list[Engram], meta_id_array: list[str], response: Response) -> dict[str, Any]: Asynchronously fetches metadata for given engrams.

on_fetch_meta_complete

Future[Any]) -> None: Callback that begins the validation process after fetching metadata.

_validate

list[Engram], meta_array: list[Meta], response: Response) -> dict[str, Any]: Runs the validation plugin on the response and returns an observation.

on_validate_complete

Future[Any]) -> None: Final step that emits the completed observation to other systems.

on_acknowledge

str) -> None: Responds to ACK messages by reporting and resetting metrics.

Source code in src/engramic/application/codify/codify_service.py
 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
class CodifyService(Service):
    """
    A service responsible for validating and extracting engrams from AI model responses using a TOML-based validation pipeline.

    This service listens for prompts that have completed processing, and if the system is in training mode, it fetches related engrams and metadata, applies an LLM-based validation process, and stores structured observations. It tracks metrics related to its activity and supports training workflows.

    Attributes:
        plugin_manager (PluginManager): Manages access to system plugins such as the LLM and document DB.
        llm_validate (dict): Plugin for LLM-based validation.
        db_document_plugin (dict): Plugin for document database access.
        engram_repository (EngramRepository): Repository for accessing and managing engram data.
        meta_repository (MetaRepository): Repository for associated metadata retrieval.
        observation_repository (ObservationRepository): Handles validation and normalization of observation data.
        prompt (Prompt): Default prompt object used during validation.
        metrics_tracker (MetricsTracker): Tracks custom CodifyMetric metrics.
        training_mode (bool): Flag indicating whether the system is in training mode.

    Methods:
        start() -> None:
            Subscribes the service to key topics.
        stop() -> None:
            Stops the service.
        init_async() -> None:
            Initializes async components, including DB connections.
        on_codify_response(msg: dict[str, Any]) -> None:
            Handles on-demand codification requests for specific response IDs.
        _fetch_history(response_id: str, repo_ids_filters: list[str]) -> list[dict[str, Any]]:
            Asynchronously fetches history for a specific response ID.
        _on_fetch_history_codify(fut: Future[Any]) -> None:
            Callback that processes fetched history and triggers codification.
        on_main_prompt_complete(response_dict: dict[str, Any], *, is_on_demand: bool = False) -> None:
            Main entry point triggered after a model completes a prompt.
        _fetch_engrams(response: Response) -> dict[str, Any]:
            Asynchronously fetches engrams associated with a response.
        on_fetch_engram_complete(fut: Future[Any]) -> None:
            Callback that processes fetched engrams and triggers metadata retrieval.
        _fetch_meta(engram_array: list[Engram], meta_id_array: list[str], response: Response) -> dict[str, Any]:
            Asynchronously fetches metadata for given engrams.
        on_fetch_meta_complete(fut: Future[Any]) -> None:
            Callback that begins the validation process after fetching metadata.
        _validate(engram_array: list[Engram], meta_array: list[Meta], response: Response) -> dict[str, Any]:
            Runs the validation plugin on the response and returns an observation.
        on_validate_complete(fut: Future[Any]) -> None:
            Final step that emits the completed observation to other systems.
        on_acknowledge(message_in: str) -> None:
            Responds to ACK messages by reporting and resetting metrics.
    """

    ACCURACY_CONSTANT = 3
    RELEVANCY_CONSTANT = 3

    def __init__(self, host: Host) -> None:
        super().__init__(host)
        self.plugin_manager: PluginManager = host.plugin_manager
        self.llm_validate = self.plugin_manager.get_plugin('llm', 'validate')
        self.db_document_plugin = self.plugin_manager.get_plugin('db', 'document')
        self.engram_repository: EngramRepository = EngramRepository(self.db_document_plugin)
        self.meta_repository: MetaRepository = MetaRepository(self.db_document_plugin)
        self.observation_repository: ObservationRepository = ObservationRepository(self.db_document_plugin)

        self.prompt = Prompt('Validate the llm.')
        self.metrics_tracker: MetricsTracker[CodifyMetric] = MetricsTracker[CodifyMetric]()
        self.training_mode = False

    def start(self) -> None:
        self.subscribe(Service.Topic.ACKNOWLEDGE, self.on_acknowledge)
        self.subscribe(Service.Topic.MAIN_PROMPT_COMPLETE, self.on_main_prompt_complete)
        self.subscribe(Service.Topic.CODIFY_RESPONSE, self.on_codify_response)
        super().start()

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

    def init_async(self) -> None:
        self.db_document_plugin['func'].connect(args=None)
        return super().init_async()

    #################
    # Start codify when the user is starting from a response id

    def on_codify_response(self, msg: dict[str, Any]) -> None:
        response_id = msg['response_id']
        repo_ids_filters = msg['repo_ids_filters']
        fut = self.run_task(self._fetch_history(response_id, repo_ids_filters))
        fut.add_done_callback(self._on_fetch_history_codify)

    async def _fetch_history(self, response_id: str, repo_ids_filters: list[str]) -> list[dict[str, Any]]:
        plugin = self.db_document_plugin
        args = plugin['args']
        args['repo_ids_filters'] = repo_ids_filters
        args['history_limit'] = 1

        ret_val = await asyncio.to_thread(plugin['func'].fetch, table=DB.DBTables.HISTORY, ids=[response_id], args=args)
        history_dict: list[dict[str, Any]] = ret_val[0]
        return history_dict

    def _on_fetch_history_codify(self, fut: Future[Any]) -> None:
        ret = fut.result()
        response = ret['history'][0]
        prompt = response['prompt']
        prompt['training_mode'] = True
        prompt['is_on_demand'] = True

        self.on_main_prompt_complete(ret['history'][0], is_on_demand=True)

    #################
    # Start codify when continuing from main prompt completion.

    def on_main_prompt_complete(self, response_dict: dict[str, Any], *, is_on_demand: bool = False) -> None:
        if __debug__:
            self.host.update_mock_data_input(self, response_dict)

        prompt = Prompt(**response_dict['prompt'])
        if not prompt.training_mode:
            return

        parent_id: str | None = prompt.prompt_id
        tracking_id = prompt.tracking_id
        if is_on_demand:
            parent_id = None
            tracking_id = str(uuid.uuid4())

        model = response_dict['model']
        analysis = PromptAnalysis(**response_dict['analysis'])
        retrieve_result = RetrieveResult(**response_dict['retrieve_result'])
        response = Response(
            response_dict['id'],
            response_dict['source_id'],
            response_dict['response'],
            retrieve_result,
            prompt,
            analysis,
            model,
        )

        self.send_message_async(
            Service.Topic.CODIFY_CREATED, {'id': response.id, 'parent_id': parent_id, 'tracking_id': tracking_id}
        )

        self.metrics_tracker.increment(CodifyMetric.RESPONSE_RECEIVED)
        fetch_engram_step = self.run_task(self._fetch_engrams(response))
        fetch_engram_step.add_done_callback(self.on_fetch_engram_complete)

    """
    ### Fetch Engrams & Meta

    Fetch engrams based on retrieved results.
    """

    async def _fetch_engrams(self, response: Response) -> dict[str, Any]:
        engram_array: list[Engram] = await asyncio.to_thread(
            self.engram_repository.load_batch_retrieve_result, response.retrieve_result
        )

        self.metrics_tracker.increment(CodifyMetric.ENGRAM_FETCHED, len(engram_array))

        meta_array: set[str] = set()
        for engram in engram_array:
            if engram.meta_ids is not None:
                meta_array.update(engram.meta_ids)

        return {'engram_array': engram_array, 'meta_array': list(meta_array), 'response': response}

    def on_fetch_engram_complete(self, fut: Future[Any]) -> None:
        ret = fut.result()
        fetch_meta_step = self.run_task(self._fetch_meta(ret['engram_array'], ret['meta_array'], ret['response']))
        fetch_meta_step.add_done_callback(self.on_fetch_meta_complete)

    async def _fetch_meta(
        self, engram_array: list[Engram], meta_id_array: list[str], response: Response
    ) -> dict[str, Any]:
        meta_array: list[Meta] = await asyncio.to_thread(self.meta_repository.load_batch, meta_id_array)
        # assembled main_prompt, render engrams.

        return {'engram_array': engram_array, 'meta_array': meta_array, 'response': response}

    def on_fetch_meta_complete(self, fut: Future[Any]) -> None:
        ret = fut.result()
        fetch_meta_step = self.run_task(self._validate(ret['engram_array'], ret['meta_array'], ret['response']))
        fetch_meta_step.add_done_callback(self.on_validate_complete)

    """
    ### Validate

    Validates and extracts engrams (i.e. memories) from responses.
    """

    async def _validate(self, engram_array: list[Engram], meta_array: list[Meta], response: Response) -> dict[str, Any]:
        # insert prompt engineering

        del meta_array

        input_data = {
            'engram_list': engram_array,
            'response': response.response,
        }

        prompt = PromptValidatePrompt(
            response.prompt.prompt_str,
            input_data=input_data,
            is_lesson=response.prompt.is_lesson,
            is_on_demand=response.prompt.is_on_demand,
            training_mode=response.prompt.training_mode,
        )

        plugin = self.llm_validate
        validate_response = await asyncio.to_thread(
            plugin['func'].submit,
            prompt=prompt,
            structured_schema=None,
            args=self.host.mock_update_args(plugin),
            images=None,
        )

        self.host.update_mock_data(self.llm_validate, validate_response)

        toml_data = None

        try:
            if __debug__:
                prompt_render = prompt.render_prompt()
                self.send_message_async(
                    Service.Topic.DEBUG_OBSERVATION_TOML_COMPLETE,
                    {'prompt': prompt_render, 'toml': validate_response[0]['llm_response'], 'response_id': response.id},
                )

            toml_data = tomli.loads(validate_response[0]['llm_response'])

        except tomli.TOMLDecodeError as e:
            logging.exception('TOML decode error: %s', validate_response[0]['llm_response'])
            error = 'Malformed TOML file in codify:validate.'
            raise TypeError(error) from e

        if 'not_memorable' in toml_data:
            # print("not memorable")
            return {'return_observation': None}

        if not self.observation_repository.validate_toml_dict(toml_data):
            error = 'Codify TOML did not pass validation.'
            raise TypeError(error)

        return_observation = self.observation_repository.load_toml_dict(
            self.observation_repository.normalize_toml_dict(toml_data, response)
        )

        # if this observation is from multiple sources, it must merge the sources into it's meta.
        if len(engram_array) > 0:
            merged_data = return_observation.merge_observation(
                return_observation,
                CodifyService.ACCURACY_CONSTANT,
                CodifyService.RELEVANCY_CONSTANT,
                self.engram_repository,
            )

            # Cast merged_data to the same type as return_observation
            return_observation_merged = type(return_observation)(**asdict(merged_data))

            return_observation = return_observation_merged

        self.metrics_tracker.increment(CodifyMetric.ENGRAM_VALIDATED)
        self.send_message_async(
            Service.Topic.OBSERVATION_CREATED, {'id': return_observation.id, 'parent_id': return_observation.parent_id}
        )

        return {'return_observation': return_observation}

    def on_validate_complete(self, fut: Future[Any]) -> None:
        ret = fut.result()

        # print(asdict(ret['return_observation']))

        if ret['return_observation'] is not None:
            self.send_message_async(Service.Topic.OBSERVATION_COMPLETE, asdict(ret['return_observation']))

            # if thinking...
            # self.send_message_async(Service.Topic.META_COMPLETE, asdict(ret['return_observation'].meta))

        if __debug__ and ret['return_observation'] is not None:
            self.host.update_mock_data_output(self, asdict(ret['return_observation']))

    """
    ### Ack

    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},
        )