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 | class CodifyService(Service):
"""
CodifyService is a system-level service responsible for validating and extracting engrams (memories) 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.
Key Responsibilities:
- Subscribes to relevant service events like `MAIN_PROMPT_COMPLETE` and `ACKNOWLEDGE`.
- Fetches engrams and their associated metadata based on a completed model response.
- Uses a validation plugin to process model responses and extract structured observation data.
- Validates and loads TOML-encoded responses into structured Observation objects.
- Merges observations when applicable and sends results asynchronously to downstream systems.
- Tracks system-level metrics for observability and debugging.
Attributes:
plugin_manager (PluginManager): Manages access to system plugins such as the LLM and document DB.
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(): Subscribes the service to key topics.
stop(): Stops the service.
init_async(): Initializes async components, including DB connections.
on_set_training_mode(message_in): Sets training mode flag based on incoming message.
on_main_prompt_complete(response_dict): Main entry point triggered after a model completes a prompt.
fetch_engrams(response): Asynchronously fetches engrams associated with a response.
on_fetch_engram_complete(fut): Callback that processes fetched engrams and triggers metadata retrieval.
fetch_meta(engram_array, meta_id_array, response): Asynchronously fetches metadata for given engrams.
on_fetch_meta_complete(fut): Callback that begins the validation process after fetching metadata.
validate(engram_array, meta_array, response): Runs the validation plugin on the response and returns an observation.
on_validate_complete(fut): Final step that emits the completed observation to other systems.
on_acknowledge(message_in): 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.SET_TRAINING_MODE, self.on_set_training_mode)
def stop(self) -> None:
super().stop()
def init_async(self) -> None:
self.db_document_plugin['func'].connect(args=None)
return super().init_async()
def on_set_training_mode(self, message_in: dict[str, Any]) -> None:
self.training_mode = message_in['training_mode']
def on_main_prompt_complete(self, response_dict: dict[str, Any]) -> None:
if __debug__:
self.host.update_mock_data_input(self, response_dict)
if not self.training_mode:
return
prompt_str = response_dict['prompt_str']
model = response_dict['model']
analysis = PromptAnalysis(**response_dict['analysis'])
retrieve_result = RetrieveResult(**response_dict['retrieve_result'])
response = Response(
response_dict['id'], response_dict['response'], retrieve_result, prompt_str, analysis, model
)
self.metrics_tracker.increment(CodifyMetric.RESPONSE_RECIEVED)
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_str, input_data=input_data)
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)
)
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:
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 be merged the sources into it's meta.
if len(engram_array) > 0:
return_observation_merged: Observation = return_observation.merge_observation(
return_observation,
CodifyService.ACCURACY_CONSTANT,
CodifyService.RELEVANCY_CONSTANT,
self.engram_repository,
)
return {'return_observation': return_observation_merged}
self.metrics_tracker.increment(CodifyMetric.ENGRAM_VALIDATED)
return {'return_observation': return_observation}
def on_validate_complete(self, fut: Future[Any]) -> None:
ret = fut.result()
if ret['return_observation'] is not None:
self.send_message_async(Service.Topic.OBSERVATION_COMPLETE, asdict(ret['return_observation']))
if __debug__:
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},
)
|