Bases: ABC
Common initializer for embedding, augmentation and evaluation processes.
Multiple components are used in the embedding, augmentation and evaluation processes.
To avoid code duplication, this initializer is used to bind the components to the injector.
It is intended to be subclassed by the specific initializers for each process.
Attributes: |
-
configuration
–
-
configuration_json
–
Configuration JSON string
|
Source code in src/common/bootstrap/initializer.py
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 | class CommonInitializer(ABC):
"""Common initializer for embedding, augmentation and evaluation processes.
Multiple components are used in the embedding, augmentation and evaluation processes.
To avoid code duplication, this initializer is used to bind the components to the injector.
It is intended to be subclassed by the specific initializers for each process.
Attributes:
configuration: Configuration object
configuration_json: Configuration JSON string
"""
def __init__(self):
"""Initialize the CommonInitializer.
Parse the command line arguments and read the configuration file.
Setup the logger configuration.
"""
args = self._parse_args()
configuration_filepath, secrets_filepath = (
self._get_configuration_and_secrets_filepaths(args)
)
build_name = args.build_name
environment = args.env
with open(configuration_filepath) as f:
self.configuration_json = f.read()
self.configuration = Configuration.model_validate_json(
self.configuration_json, context={"secrets_file": secrets_filepath}
)
self.configuration.metadata.build_name = build_name
self.configuration.metadata.environment = EnvironmentName(environment)
print(f"::{configuration_filepath}")
print(self.configuration.model_dump_json(indent=4))
LoggerConfiguration.config() # TODO: Pass log level from configuration
LoggerConfiguration.filterwarnings()
def init_injector(self) -> Injector:
"""Bind components to the injector based on the configuration and return the injector.
Returns:
Injector: Injector with components bound"""
return Injector([self._bind])
def _bind(self, binder: Binder) -> None:
"""Bind common components to the injector based on the configuration.
Args:
binder: Injector binder
"""
binder.bind(Configuration, to=self.configuration, scope=singleton)
EmbeddingModelBinder(
configuration=self.configuration, binder=binder
).bind()
VectorStoreBinder(
configuration=self.configuration, binder=binder
).bind()
def _parse_args(self):
"""Parse the command line arguments.
Returns:
argparse.Namespace: Parsed command line arguments
"""
parser = argparse.ArgumentParser(
description="Run the embedding process."
)
parser.add_argument(
"--env",
type=str,
help="Runtime environment.",
default="default",
)
parser.add_argument(
"--build-name",
type=str,
help="The name of the build.",
default=f"build-local-{time.time()}", # Removed trailing comma
)
return parser.parse_args()
def _get_configuration_and_secrets_filepaths(
self, args: argparse.Namespace
) -> Tuple[str, str]:
"""Get the configuration and secrets files from the command line arguments.
Args:
args: Parsed command line arguments
Returns:
Tuple[str, str]: Configuration and secrets filepaths
"""
configuration_filepath = f"configurations/configuration.{args.env}.json"
secrets_filepath = f"configurations/secrets.{args.env}.env"
return configuration_filepath, secrets_filepath
|
__init__()
Initialize the CommonInitializer.
Parse the command line arguments and read the configuration file.
Setup the logger configuration.
Source code in src/common/bootstrap/initializer.py
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 | def __init__(self):
"""Initialize the CommonInitializer.
Parse the command line arguments and read the configuration file.
Setup the logger configuration.
"""
args = self._parse_args()
configuration_filepath, secrets_filepath = (
self._get_configuration_and_secrets_filepaths(args)
)
build_name = args.build_name
environment = args.env
with open(configuration_filepath) as f:
self.configuration_json = f.read()
self.configuration = Configuration.model_validate_json(
self.configuration_json, context={"secrets_file": secrets_filepath}
)
self.configuration.metadata.build_name = build_name
self.configuration.metadata.environment = EnvironmentName(environment)
print(f"::{configuration_filepath}")
print(self.configuration.model_dump_json(indent=4))
LoggerConfiguration.config() # TODO: Pass log level from configuration
LoggerConfiguration.filterwarnings()
|
_bind(binder)
Bind common components to the injector based on the configuration.
Source code in src/common/bootstrap/initializer.py
84
85
86
87
88
89
90
91
92
93
94
95
96 | def _bind(self, binder: Binder) -> None:
"""Bind common components to the injector based on the configuration.
Args:
binder: Injector binder
"""
binder.bind(Configuration, to=self.configuration, scope=singleton)
EmbeddingModelBinder(
configuration=self.configuration, binder=binder
).bind()
VectorStoreBinder(
configuration=self.configuration, binder=binder
).bind()
|
_get_configuration_and_secrets_filepaths(args)
Get the configuration and secrets files from the command line arguments.
Parameters: |
-
args
(Namespace )
–
Parsed command line arguments
|
Returns: |
-
Tuple[str, str]
–
Tuple[str, str]: Configuration and secrets filepaths
|
Source code in src/common/bootstrap/initializer.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 | def _get_configuration_and_secrets_filepaths(
self, args: argparse.Namespace
) -> Tuple[str, str]:
"""Get the configuration and secrets files from the command line arguments.
Args:
args: Parsed command line arguments
Returns:
Tuple[str, str]: Configuration and secrets filepaths
"""
configuration_filepath = f"configurations/configuration.{args.env}.json"
secrets_filepath = f"configurations/secrets.{args.env}.env"
return configuration_filepath, secrets_filepath
|
_parse_args()
Parse the command line arguments.
Returns: |
-
–
argparse.Namespace: Parsed command line arguments
|
Source code in src/common/bootstrap/initializer.py
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 | def _parse_args(self):
"""Parse the command line arguments.
Returns:
argparse.Namespace: Parsed command line arguments
"""
parser = argparse.ArgumentParser(
description="Run the embedding process."
)
parser.add_argument(
"--env",
type=str,
help="Runtime environment.",
default="default",
)
parser.add_argument(
"--build-name",
type=str,
help="The name of the build.",
default=f"build-local-{time.time()}", # Removed trailing comma
)
return parser.parse_args()
|
init_injector()
Bind components to the injector based on the configuration and return the injector.
Returns: |
-
Injector ( Injector
) –
Injector with components bound
|
Source code in src/common/bootstrap/initializer.py
| def init_injector(self) -> Injector:
"""Bind components to the injector based on the configuration and return the injector.
Returns:
Injector: Injector with components bound"""
return Injector([self._bind])
|