sinapsis_core 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. sinapsis_core/__init__.py +0 -0
  2. sinapsis_core/agent/__init__.py +5 -0
  3. sinapsis_core/agent/agent.py +362 -0
  4. sinapsis_core/agent/agent_helpers.py +112 -0
  5. sinapsis_core/agent/base_models.py +361 -0
  6. sinapsis_core/agent/builder.py +112 -0
  7. sinapsis_core/agent/profiler_agent.py +117 -0
  8. sinapsis_core/cli/__init__.py +0 -0
  9. sinapsis_core/cli/arg_parser.py +29 -0
  10. sinapsis_core/cli/display_template_info.py +202 -0
  11. sinapsis_core/cli/run_agent_from_config.py +40 -0
  12. sinapsis_core/context_managers/__init__.py +0 -0
  13. sinapsis_core/context_managers/env_var_override_context_manager.py +39 -0
  14. sinapsis_core/context_managers/working_dir_context_manager.py +57 -0
  15. sinapsis_core/data_containers/__init__.py +0 -0
  16. sinapsis_core/data_containers/annotations.py +152 -0
  17. sinapsis_core/data_containers/data_packet.py +364 -0
  18. sinapsis_core/data_containers/model_types/__init__.py +38 -0
  19. sinapsis_core/data_containers/model_types/_asr_base_models.py +39 -0
  20. sinapsis_core/data_containers/model_types/llm_types.py +189 -0
  21. sinapsis_core/exceptions/agent_exceptions.py +2 -0
  22. sinapsis_core/hooks/__init__.py +15 -0
  23. sinapsis_core/hooks/pre_init_hook_registry.py +225 -0
  24. sinapsis_core/template_base/__init__.py +10 -0
  25. sinapsis_core/template_base/base_models.py +59 -0
  26. sinapsis_core/template_base/copy_data_container.py +46 -0
  27. sinapsis_core/template_base/dynamic_template.py +169 -0
  28. sinapsis_core/template_base/dynamic_template_factory.py +454 -0
  29. sinapsis_core/template_base/dynamic_template_helpers/__init__.py +0 -0
  30. sinapsis_core/template_base/dynamic_template_helpers/builder_methods.py +92 -0
  31. sinapsis_core/template_base/dynamic_template_helpers/signature_parser.py +625 -0
  32. sinapsis_core/template_base/empty_data_container.py +23 -0
  33. sinapsis_core/template_base/input_template.py +8 -0
  34. sinapsis_core/template_base/merge_data_flow.py +33 -0
  35. sinapsis_core/template_base/multi_execute_template.py +65 -0
  36. sinapsis_core/template_base/output_template.py +8 -0
  37. sinapsis_core/template_base/split_data_flow.py +50 -0
  38. sinapsis_core/template_base/template.py +292 -0
  39. sinapsis_core/template_base/transfer_data_container.py +13 -0
  40. sinapsis_core/utils/__init__.py +0 -0
  41. sinapsis_core/utils/base_model_updater.py +148 -0
  42. sinapsis_core/utils/doc_string_generator.py +20 -0
  43. sinapsis_core/utils/env_var_keys.py +249 -0
  44. sinapsis_core/utils/env_var_utils.py +6 -0
  45. sinapsis_core/utils/logging_utils.py +53 -0
  46. sinapsis_core-1.0.0.dist-info/METADATA +239 -0
  47. sinapsis_core-1.0.0.dist-info/RECORD +50 -0
  48. sinapsis_core-1.0.0.dist-info/WHEEL +4 -0
  49. sinapsis_core-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
  50. sinapsis_core-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
@@ -0,0 +1,361 @@
1
+ # -*- coding: utf-8 -*-
2
+ from enum import Enum
3
+ from functools import cached_property
4
+ from pathlib import PosixPath
5
+ from typing import Annotated, Any, Generator, Literal, TypeVar
6
+ from uuid import uuid4
7
+
8
+ from pydantic import BaseModel, ConfigDict, Field, computed_field, field_validator, model_validator
9
+ from pydantic.dataclasses import dataclass
10
+ from pydantic.types import StrictStr
11
+ from pydantic_core.core_schema import FieldValidationInfo
12
+
13
+ from sinapsis_core.agent.agent_helpers import get_template_neighbors_mapping, sort_agent_execution
14
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
15
+
16
+ ATTRIBUTE_TYPE = dict[str, Any]
17
+
18
+ AGENT_MODE_TYPE = Literal["loop_forever", "generator", "single", "forward"]
19
+
20
+ ConstantIntType = Annotated[int, Field(ge=0)]
21
+
22
+
23
+ class TemplateTypes(Enum):
24
+ INPUT_TEMPLATE = 1
25
+ OUTPUT_TEMPLATE = 2
26
+ STANDARD_TEMPLATE = 3
27
+ DATA_SPLIT_TEMPLATE = 4
28
+ DATA_JOIN_TEMPLATE = 5
29
+
30
+
31
+ @dataclass
32
+ class AgentMode:
33
+ LOOP_FOREVER: str = "loop_forever"
34
+ GENERATOR: str = "generator"
35
+ SINGLE: str = "single"
36
+ FORWARD: str = "forward"
37
+
38
+
39
+ class TemplateNames:
40
+ input_template: str = "InputTemplate"
41
+ output_template: str = "OutputTemplate"
42
+ merge_data_flow: str = "MergeDataFlow"
43
+ mpi_merge_data_flow: str = "MPIMergeDataFlow"
44
+
45
+
46
+ class TemplateKeys:
47
+ class_name: str = "class_name"
48
+ template_input: str = "template_input"
49
+ template_name: str = "template_name"
50
+
51
+
52
+ class AgentAttributes(BaseModel):
53
+ """
54
+ template_overrides: Used to update a template attribute. To be given as
55
+ {template_name: {attribute_name, attribute_value}}
56
+ env_var_overrides: Used to update a template environment variable. To be given as
57
+ {env_var_name: envar_value}.
58
+ lazy_init: Indicates if templates are to be initialized lazily, i.e. only during the
59
+ first execute call.
60
+ agent_mode: Define the operation mode for agents ("loop_forever", "generator", "single", forward).
61
+ Defaults to "single".
62
+ shut_down_templates: Enable calling the shut_down method of templates when the agent finish its execution.
63
+ Defaults to "True".
64
+ """
65
+
66
+ template_attribute_overrides: dict[str, ATTRIBUTE_TYPE] = Field(default_factory=dict)
67
+ env_var_overrides: ATTRIBUTE_TYPE = Field(default_factory=dict)
68
+ # lazy_init: bool = False
69
+ description: str = ""
70
+ agent_mode: AGENT_MODE_TYPE = "single"
71
+ shut_down_templates: bool = True
72
+ user_id: str | None = None
73
+ agent_id: str | None = None
74
+
75
+
76
+ class TemplateSchema(BaseModel):
77
+ template_name: StrictStr
78
+ class_name: StrictStr
79
+ template_input: StrictStr
80
+ attributes: ATTRIBUTE_TYPE
81
+ model_config = ConfigDict(extra="forbid")
82
+ template_type: TemplateTypes = TemplateTypes.STANDARD_TEMPLATE
83
+
84
+ @field_validator("template_input")
85
+ def validate_template_input(cls, v: StrictStr, info: FieldValidationInfo) -> StrictStr:
86
+ template_name = info.data[TemplateKeys.template_name]
87
+ class_name = info.data[TemplateKeys.class_name]
88
+ input_templates = [TemplateNames.input_template]
89
+
90
+ log_msg = f"""Invalid template: {template_name} of class: {class_name}. Only {input_templates} templates are
91
+ allowed to have no input"""
92
+ if v is None and class_name not in input_templates:
93
+ raise TypeError(log_msg)
94
+
95
+ if v is not None and class_name in input_templates:
96
+ raise TypeError(f"Invalid template: {template_name} of class: {class_name}, template_input must be None")
97
+
98
+ return v
99
+
100
+ @field_validator("template_input")
101
+ def merge_input_type(cls, v: StrictStr, info: FieldValidationInfo) -> StrictStr:
102
+ template_name = info.data[TemplateKeys.template_name]
103
+ class_name = info.data[TemplateKeys.class_name]
104
+ merge_templates = ["MergeDataFlow", "MPIMergeDataFlow"]
105
+
106
+ log_msg = f"""Invalid template_input type: {type(v)} in Template: {template_name} of class {class_name}.
107
+ Template must received a template_input of type: list[str]"""
108
+ if class_name in merge_templates and not isinstance(v, list):
109
+ raise TypeError(log_msg)
110
+ return v
111
+
112
+
113
+ class MergeDataFlowSchema(TemplateSchema):
114
+ """
115
+ DataFlow templates are the only templates that allow more than one input.
116
+ Particularly those used to merge containers.
117
+ """
118
+
119
+ template_input: list[StrictStr] # type:ignore[assignment]
120
+ template_type: TemplateTypes = TemplateTypes.DATA_JOIN_TEMPLATE
121
+
122
+ @field_validator(TemplateKeys.template_input)
123
+ def null_input(cls, v: list[StrictStr], info: FieldValidationInfo) -> list[StrictStr]:
124
+ class_name = info.data[TemplateKeys.class_name] # .lower()
125
+ template_name = info.data[TemplateKeys.template_name]
126
+
127
+ merge_templates = ["MergeDataFlow", "MPIMergeDataFlow"]
128
+ if class_name not in merge_templates and isinstance(v, list):
129
+ log_msg = f"""Invalid template: {template_name}. Only {merge_templates} templates are allowed to have more
130
+ than one input, got {class_name}"""
131
+ raise TypeError(log_msg)
132
+
133
+ return v
134
+
135
+
136
+ class InputSchema(TemplateSchema):
137
+ """
138
+ Only input templates are allowed to have no inputs.
139
+ """
140
+
141
+ template_input: None = None # type:ignore[assignment]
142
+ attributes: ATTRIBUTE_TYPE = Field(default_factory=dict) # type:ignore[assignment]
143
+ template_type: TemplateTypes = TemplateTypes.INPUT_TEMPLATE
144
+
145
+ @field_validator(TemplateKeys.class_name)
146
+ def null_input(cls, v: StrictStr, info: FieldValidationInfo) -> StrictStr:
147
+ template_name = info.data[TemplateKeys.template_name]
148
+ input_templates = [TemplateNames.input_template]
149
+ if v not in input_templates:
150
+ log_msg = (
151
+ f"Invalid template: {template_name}. Only {input_templates} templates are allowed to have no input"
152
+ )
153
+ raise TypeError(log_msg)
154
+ return v
155
+
156
+
157
+ class SplitDataFlowSchema(TemplateSchema):
158
+ template_type: TemplateTypes = TemplateTypes.DATA_SPLIT_TEMPLATE
159
+
160
+
161
+ class OutputSchema(TemplateSchema):
162
+ class_name: StrictStr = TemplateNames.output_template
163
+ attributes: ATTRIBUTE_TYPE = Field(default_factory=dict)
164
+ template_type: TemplateTypes = TemplateTypes.OUTPUT_TEMPLATE
165
+
166
+
167
+ class AgentDef(BaseModel):
168
+ name: StrictStr = str(uuid4())
169
+ attributes: AgentAttributes = Field(default_factory=AgentAttributes)
170
+
171
+
172
+ AgentSchemaType = TypeVar("AgentSchemaType", bound="AgentSchema")
173
+ MPIAgentSchemaType = TypeVar("MPIAgentSchemaType", bound="MPIAgentSchema")
174
+
175
+
176
+ TemplateSchemas = Annotated[
177
+ TemplateSchema | MergeDataFlowSchema | SplitDataFlowSchema | OutputSchema | InputSchema,
178
+ Field(union_mode="left_to_right"),
179
+ ]
180
+
181
+
182
+ class AgentSchema(BaseModel):
183
+ agent: AgentDef
184
+ templates: list[TemplateSchemas]
185
+
186
+ @model_validator(mode="after")
187
+ def unique_names(self: AgentSchemaType) -> AgentSchemaType:
188
+ template_names = [template.template_name for template in self.templates]
189
+ if len(set(template_names)) != len(template_names):
190
+ raise ValueError("Agent templates must have unique names")
191
+ return self
192
+
193
+ @model_validator(mode="before")
194
+ @classmethod
195
+ def output_template_is_present(cls, values: dict[str, Any]) -> dict[str, Any]:
196
+ raw_templates = values.get("templates", [])
197
+ template_as_dict = []
198
+ for t in raw_templates:
199
+ if isinstance(t, dict):
200
+ template_as_dict.append(t)
201
+ elif isinstance(t, TemplateSchema):
202
+ template_as_dict.append(t.model_dump())
203
+ template_names = {t.get(TemplateKeys.template_name) for t in template_as_dict}
204
+ template_inputs = {t.get(TemplateKeys.template_input) for t in template_as_dict}
205
+ last_template = (template_names - template_inputs).pop()
206
+ output_template_is_missing = all(
207
+ template.get(TemplateKeys.class_name) != TemplateNames.output_template for template in template_as_dict
208
+ )
209
+
210
+ if output_template_is_missing:
211
+ sinapsis_logger.warning("OutputTemplate not present in agent schema, adding...")
212
+ output_template = {
213
+ TemplateKeys.template_name: TemplateNames.output_template,
214
+ TemplateKeys.class_name: TemplateNames.output_template,
215
+ TemplateKeys.template_input: last_template,
216
+ "attributes": {},
217
+ }
218
+ raw_templates.append(output_template)
219
+ sinapsis_logger.warning("OutputTemplate added to agent schema")
220
+ values["templates"] = raw_templates
221
+ return values
222
+
223
+ @computed_field
224
+ @cached_property
225
+ def template_mapping_dict(self) -> dict[str, Any]:
226
+ return get_template_neighbors_mapping(self.templates)
227
+
228
+ @computed_field
229
+ @cached_property
230
+ def sorted_templates(self) -> list[TemplateSchema]:
231
+ ordered_template_names = sort_agent_execution(self.template_mapping_dict)
232
+ templates = [
233
+ find_template_in_list_by_name(template_name, self.templates) for template_name in ordered_template_names
234
+ ]
235
+ return templates
236
+
237
+ @computed_field
238
+ @cached_property
239
+ def sorted_templates_as_dict(self) -> dict[str, TemplateSchema]:
240
+ ordered_template_names = sort_agent_execution(self.template_mapping_dict)
241
+ templates = [
242
+ find_template_in_list_by_name(template_name, self.templates) for template_name in ordered_template_names
243
+ ]
244
+ return {t.template_name: t for t in templates}
245
+
246
+
247
+ class MPIAgentSchema(AgentSchema):
248
+ @model_validator(mode="after")
249
+ def one_data_split(self: MPIAgentSchemaType) -> MPIAgentSchemaType:
250
+ mpi_split_class_name = "MPISplitDataFlow"
251
+ mpi_merge_class_name = "MPIMergeDataFlow"
252
+
253
+ split_templates = []
254
+ join_templates = []
255
+ for template in self.templates:
256
+ if template.class_name == mpi_split_class_name:
257
+ split_templates.append(template)
258
+ if template.class_name == mpi_merge_class_name:
259
+ join_templates.append(template)
260
+
261
+ if len(split_templates) != 1:
262
+ raise ValueError("MPI agents must have exactly one MPISplitDataFlow template")
263
+ if len(join_templates) != 1:
264
+ raise ValueError("MPI agents must have exactly one MPISplitDataFlow template")
265
+ return self
266
+
267
+ @computed_field
268
+ @cached_property
269
+ def branches(
270
+ self,
271
+ ) -> tuple[list[TemplateSchema], list[TemplateSchema], list[list[TemplateSchema]]]:
272
+ def find_data_node(node_type: str) -> str:
273
+ for template_name in self.template_mapping_dict:
274
+ if node_type in template_name.lower():
275
+ return template_name
276
+ raise ValueError("Agent templates must have one data split")
277
+
278
+ data_split_node_name = find_data_node("split")
279
+ data_join_node_name = find_data_node("join")
280
+
281
+ def find_template_index(t_name: str) -> int:
282
+ for idx, template in enumerate(self.sorted_templates):
283
+ if template.template_name == t_name:
284
+ return idx
285
+ return -1
286
+
287
+ branches = []
288
+ start_branch = self.sorted_templates[0 : find_template_index(data_split_node_name) + 1]
289
+ end_branch = self.sorted_templates[find_template_index(data_join_node_name) :]
290
+ branches.append(start_branch)
291
+ branches.append(end_branch)
292
+ start_branch = self.sorted_templates[0 : find_template_index(data_split_node_name) + 1]
293
+ end_branch = self.sorted_templates[find_template_index(data_join_node_name) :]
294
+
295
+ for branch in find_parallel_branch_paths(self.template_mapping_dict, data_split_node_name, data_join_node_name):
296
+ template_branch = [
297
+ find_template_in_list_by_name(template_name, self.templates) for template_name in branch[1:-1]
298
+ ]
299
+ branches.append(template_branch)
300
+ return start_branch, end_branch, branches
301
+
302
+
303
+ class ProfilerAgentAttributes(AgentAttributes):
304
+ profile_execution_time: bool = True
305
+ profile_memory_usage: bool = False
306
+ templates_to_profile: list[str] | str = "all"
307
+ warm_up_iterations: ConstantIntType = 0
308
+ disregard_calls_on_finished: bool = True
309
+ print_stats_on_exit: bool = True
310
+
311
+ @field_validator("templates_to_profile")
312
+ def templates_to_profile_as_list(cls, v: list[str] | str) -> list[str] | str:
313
+ if isinstance(v, str) and v != "all":
314
+ v = [v]
315
+ return v
316
+
317
+
318
+ class ProfilerAgentDef(AgentDef):
319
+ attributes: ProfilerAgentAttributes = Field(default_factory=ProfilerAgentAttributes)
320
+
321
+
322
+ class ProfilerAgentSchema(AgentSchema):
323
+ agent: ProfilerAgentDef
324
+
325
+
326
+ AgentDefinitionType = dict | ProfilerAgentSchema | AgentSchema | MPIAgentSchema | str | PosixPath
327
+
328
+
329
+ def find_template_in_list_by_name(template_name: str, template_list: list) -> TemplateSchema:
330
+ """Finds the template in the list of templates from the AgentSchema through the template name
331
+ If the template is found, return the schema.
332
+ Args:
333
+ template_name (str): Name of the template to search
334
+ template_list (list): List of templates in the Schema.
335
+ Raise:
336
+ AttributeError: If the template_list has no template with template_name
337
+ """
338
+ for template in template_list:
339
+ if template.template_name == template_name:
340
+ return template
341
+ raise AttributeError(f"Template list has no template with name {template_name}")
342
+
343
+
344
+ def find_parallel_branch_paths(graph: dict, start: str, end: str) -> Generator:
345
+ stack = [(start, [start])]
346
+ while stack:
347
+ (vertex, path) = stack.pop()
348
+ for neighbor in graph[vertex]:
349
+ if neighbor == end:
350
+ yield [*path, neighbor]
351
+ else:
352
+ stack.append((neighbor, [*path, neighbor]))
353
+
354
+
355
+ class SinapsisStatusCodes(str, Enum):
356
+ READY = "ready"
357
+ IDLE = "idle"
358
+ PROCESSING = "processing"
359
+ DONE = "done"
360
+ FAILED = "failed"
361
+ CANCELLED = "cancelled"
@@ -0,0 +1,112 @@
1
+ # -*- coding: utf-8 -*-
2
+ from pathlib import PosixPath
3
+ from typing import Callable
4
+
5
+ import yaml
6
+
7
+ from sinapsis_core.agent.base_models import (
8
+ AgentDefinitionType,
9
+ AgentSchema,
10
+ MPIAgentSchema,
11
+ ProfilerAgentSchema,
12
+ TemplateSchema,
13
+ )
14
+ from sinapsis_core.template_base.template import Template
15
+ from sinapsis_core.utils.logging_utils import sinapsis_logger
16
+
17
+
18
+ def lazy_import_templates_module() -> Callable:
19
+ """Imports all available sinapsis modules as defined in the template_lookup
20
+ Raise:
21
+ ImportError : In case the templates cannot be imported, stop the execution
22
+ """
23
+ try:
24
+ from sinapsis import templates as templates_module
25
+ except ImportError as err:
26
+ sinapsis_logger.error("Unable to import templates module. Please make sure that you have `sinapsis` installed")
27
+ raise err
28
+ return templates_module
29
+
30
+
31
+ def instantiate_template(
32
+ template_schema: TemplateSchema,
33
+ templates_module: Callable | None = None,
34
+ decorator_function: Callable | None = None,
35
+ ) -> Template:
36
+ """
37
+ Creates a class instance from a class name, module path and attributes dataclass
38
+ Args:
39
+ template_schema (TemplateSchema): Template schema
40
+ templates_module: module to import templates from
41
+ decorator_function: (Callable | None): optional decorator function to apply
42
+
43
+ Raises:
44
+ ValueError: Class class_name not found in global
45
+
46
+ Returns:
47
+ class instance
48
+ """
49
+ if templates_module is None:
50
+ templates_module = lazy_import_templates_module()
51
+ try:
52
+ template_instance = getattr(templates_module, template_schema.class_name)
53
+
54
+ if decorator_function is not None:
55
+ return decorator_function(template_instance)(template_schema.attributes)
56
+
57
+ return template_instance(template_schema.attributes)
58
+ except AttributeError as err:
59
+ raise ModuleNotFoundError(f"Module: templates.{template_schema.class_name} not found.") from err
60
+
61
+
62
+ def load_yaml_config(config_file_path: str) -> dict:
63
+ """
64
+ Reads a string to a config ile and returns the content in a dictionary
65
+ Args:
66
+ config_file_path (str): Path to the config file to be read
67
+ Returns:
68
+ dict : The dictionary contained in the config file
69
+ """
70
+ with open(config_file_path, "r", encoding="utf-8") as config_file:
71
+ config_dict: dict = yaml.safe_load(config_file)
72
+ return config_dict
73
+
74
+
75
+ def make_schema_from_dict(config_dict: dict, enable_profiler: bool) -> AgentSchema:
76
+ """
77
+ From the dictionary containing the configuration for the agent, build the Schema
78
+ Args:
79
+ config_dict (dict): the dictionary with the configuration for the agent
80
+ enable_profiler (bool): boolean to enable profiling
81
+ Returns:
82
+ AgentSchema : The basemodel with the Schema for the Agent.
83
+ """
84
+ if enable_profiler:
85
+ return ProfilerAgentSchema(**config_dict)
86
+ return AgentSchema(**config_dict)
87
+
88
+
89
+ def generic_agent_schema_builder(agent_definition: AgentDefinitionType, enable_profiler: bool = False) -> AgentSchema:
90
+ """
91
+ Generic method to build the AgentSchema.
92
+ The method checks which type of object has the agent_definition variable.
93
+ If it's already of type AgentSchema, ProfilerAgentSchema or MPIAgentSchema, it returns as is,
94
+ if the agent definition is a string (pointing to a config file), a dictionary or a Path to a config file,
95
+ execute the 'make_schema_from_dict' method
96
+ agent_definition (AgentDefinitionType): The definition of the agent. This includes the templates and attributes.
97
+ enable_profiler (bool): boolean to enable profiling
98
+ Returns:
99
+ AgentSchema: The basemodel with the Schema for the Agent.
100
+ Raise:
101
+ ValueError: if the agent_definition type is not allowed, AgentSchema cannot be built
102
+ """
103
+ if isinstance(agent_definition, (AgentSchema, ProfilerAgentSchema, MPIAgentSchema)):
104
+ return agent_definition
105
+ elif isinstance(agent_definition, str):
106
+ return make_schema_from_dict(load_yaml_config(agent_definition), enable_profiler)
107
+ elif isinstance(agent_definition, PosixPath):
108
+ return make_schema_from_dict(load_yaml_config(str(agent_definition.resolve())), enable_profiler)
109
+ elif isinstance(agent_definition, dict):
110
+ return make_schema_from_dict(agent_definition, enable_profiler)
111
+ else:
112
+ raise ValueError("Unknown agent definition type given.")
@@ -0,0 +1,117 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import atexit
4
+ import time
5
+ from statistics import median
6
+
7
+ from sinapsis_core.agent.agent import Agent, AgentTemplateEntry
8
+ from sinapsis_core.agent.base_models import AgentDefinitionType, ProfilerAgentAttributes
9
+ from sinapsis_core.data_containers.data_packet import DataContainer
10
+
11
+ PROFILE_ALL_KEY = "all"
12
+
13
+
14
+ class AgentProfiler(Agent):
15
+ """
16
+ The Agent establishes and manages the execution of templates
17
+ by defining a structure that includes the Agent attributes and the
18
+ list of templates to be executed. The agent already organizes the way
19
+ templates should be executed by inspecting the template_input field
20
+ and establishes how the data should be transported from template to template.
21
+ The Agent profiler provides average execution times for the given agent.
22
+ """
23
+
24
+ PROFILER_ENABLED = True
25
+
26
+ def __init__(self, build_from: AgentDefinitionType) -> None:
27
+ super().__init__(build_from)
28
+ if not self.agent_attributes:
29
+ self.logger.warning(
30
+ "AgentProfiler was requested but no agent attributes were defined. Will fall back to default values."
31
+ )
32
+ self.agent_schema.agent.attributes = ProfilerAgentAttributes()
33
+
34
+ if self.agent_attributes.print_stats_on_exit:
35
+ atexit.register(self.print_stats)
36
+
37
+ self.templates_to_profile = self.agent_attributes.templates_to_profile
38
+ if self.templates_to_profile == PROFILE_ALL_KEY:
39
+ self.templates_to_profile = list(self.agent_schema.sorted_templates_as_dict.keys())
40
+ else:
41
+ self._validate_template_names()
42
+
43
+ self.execution_times: dict[str, list[float]] = {t_name: [] for t_name in self.templates_to_profile}
44
+ self.memory_usages: dict[str, list[float]] = {t_name: [] for t_name in self.templates_to_profile}
45
+ self.agent_execution_times: list[float] = []
46
+
47
+ def _validate_template_names(self) -> None:
48
+ # consider moving to ProfilerAgentSchema?
49
+ valid_template_list = []
50
+ if self.LAZY_INITIALIZATION:
51
+ template_names = [template_schema.template_name for template_schema in self.agent_schema.templates]
52
+ else:
53
+ template_names = self.topological_sort.keys()
54
+
55
+ for template_name in self.templates_to_profile:
56
+ if template_name in template_names:
57
+ valid_template_list.append(template_name)
58
+ else:
59
+ self.logger.warning(f"Will not profile {template_name}, invalid template name")
60
+ if not valid_template_list:
61
+ self.logger.debug("No valid template names given. Will only profile the entire agent.")
62
+ self.templates_to_profile = valid_template_list
63
+
64
+ def profile_memory(self, template_entry: AgentTemplateEntry) -> None:
65
+ raise NotImplementedError
66
+
67
+ def _single_execute(self, container: DataContainer, is_production: bool = False) -> DataContainer:
68
+ start = time.perf_counter()
69
+ out = super()._single_execute(container, is_production)
70
+ end = time.perf_counter() - start
71
+ self.agent_execution_times.append(end)
72
+ return out
73
+
74
+ def _execute_template(self, template_entry: AgentTemplateEntry, container: DataContainer) -> None:
75
+ start = time.perf_counter()
76
+ super()._execute_template(template_entry, container)
77
+ end = time.perf_counter() - start
78
+ if template_entry.metadata.template_name in self.templates_to_profile:
79
+ if self.agent_attributes.profile_execution_time:
80
+ self.execution_times[template_entry.metadata.template_name].append(end)
81
+ if self.agent_attributes.profile_memory_usage:
82
+ self.profile_memory(template_entry)
83
+
84
+ def print_stats(self) -> None:
85
+ for template_name, template_stats in self.execution_times.items():
86
+ self.logger.info(
87
+ f"template: {template_name}, "
88
+ f"average_time: {self.get_template_timing_average(template_name):.3f}, "
89
+ f"median: {median(template_stats) * 1000:.3f}, "
90
+ f"num_executions: {len(template_stats)}"
91
+ )
92
+ if self.agent_execution_times:
93
+ self.logger.info(
94
+ f"Agent times: {sum(self.agent_execution_times) / len(self.agent_execution_times) * 1000:.3f} "
95
+ f"median: {median(self.agent_execution_times) * 1000:.3f} "
96
+ )
97
+ else:
98
+ self.logger.info("No agent execution times to show")
99
+
100
+ def _ignore_warm_up_steps(self, profiling_metrics: list[float]) -> list[float]:
101
+ metrics = profiling_metrics[self.agent_attributes.warm_up_iterations :]
102
+ if not metrics and len(profiling_metrics) < self.agent_attributes.warm_up_iterations:
103
+ self.logger.warning("The number of warmup iterations is greater than the execution times")
104
+ return metrics
105
+
106
+ def get_template_timing_average(self, template_name: str) -> int | float:
107
+ """
108
+ Return the average execution time of a given template
109
+ Args:
110
+ template_name (str): Name of the template to get the average execution time
111
+ Returns:
112
+ the average time
113
+ """
114
+ execution_timings: list[float] = self._ignore_warm_up_steps(self.execution_times[template_name])
115
+ if not execution_timings:
116
+ return -1
117
+ return (sum(execution_timings) / len(execution_timings)) * 1000
File without changes
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+ import argparse
3
+
4
+
5
+ def args_parser() -> argparse.Namespace:
6
+ """
7
+ Arg parser for the Sinapsis CLI. Each command should be added as a
8
+ subparser with its corresponding required and optional arguments.
9
+ """
10
+ cli_parser = argparse.ArgumentParser(description="Sinapsis CLI")
11
+ subparsers = cli_parser.add_subparsers(dest="action", description="CLI commands: run, help")
12
+
13
+ # execute agent parser
14
+ run_parser = subparsers.add_parser("run", help="Run a Sinapsis agent with a yaml config file")
15
+ run_parser.add_argument("agent_config")
16
+ run_parser.add_argument("--enable-profiler", default=False, action="store_true", help="Run agent in profiling mode")
17
+
18
+ # display info parser
19
+ help_parser = subparsers.add_parser("info", help="Get information about one or all Sinapsis template")
20
+ help_parser.add_argument("--template", type=str, help="Template name to display info for", metavar="TEMPLATE_NAME")
21
+ help_parser.add_argument("--all", action="store_true", help="Get info about all Sinapsis templates")
22
+ help_parser.add_argument("--all-template-names", action="store_true", help="Display all template names")
23
+ help_parser.add_argument(
24
+ "--example-template-config",
25
+ type=str,
26
+ help="Display an example config for the specified template",
27
+ metavar="TEMPLATE_NAME",
28
+ )
29
+ return cli_parser.parse_args()