Initializer

This module contains functionality related to the the initializer module for common.bootstrap.

Initializer

AugmentationInitializer

Bases: CommonInitializer

Initializer for the augmentation process.

Bind the components required for the augmentation process to the injector.

Source code in src/common/bootstrap/initializer.py
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
class AugmentationInitializer(CommonInitializer):
    """Initializer for the augmentation process.

    Bind the components required for the augmentation process to the injector.
    """

    def init_injector(self) -> Injector:
        """Bind components to the injector based on the configuration and return the injector.

        Returns:
            Injector: Injector with components bound"""
        injector = super().init_injector()
        cl_data._data_layer = injector.get(ChainlitService)
        return injector

    def _bind(self, binder: Binder) -> None:
        """Bind components required for the augmentation process to the injector.

        Args:
            binder: Injector binder
        """
        super()._bind(binder)
        LangfuseBinder(configuration=self.configuration, binder=binder).bind()
        QueryEngineBinder(
            configuration=self.configuration, binder=binder
        ).bind()
        ChainlitBinder(configuration=self.configuration, binder=binder).bind()

_bind(binder)

Bind components required for the augmentation process to the injector.

Parameters:
  • binder (Binder) –

    Injector binder

Source code in src/common/bootstrap/initializer.py
171
172
173
174
175
176
177
178
179
180
181
182
def _bind(self, binder: Binder) -> None:
    """Bind components required for the augmentation process to the injector.

    Args:
        binder: Injector binder
    """
    super()._bind(binder)
    LangfuseBinder(configuration=self.configuration, binder=binder).bind()
    QueryEngineBinder(
        configuration=self.configuration, binder=binder
    ).bind()
    ChainlitBinder(configuration=self.configuration, binder=binder).bind()

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
162
163
164
165
166
167
168
169
def init_injector(self) -> Injector:
    """Bind components to the injector based on the configuration and return the injector.

    Returns:
        Injector: Injector with components bound"""
    injector = super().init_injector()
    cl_data._data_layer = injector.get(ChainlitService)
    return injector

CommonInitializer

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 object

  • 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.

Parameters:
  • binder (Binder) –

    Injector binder

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
77
78
79
80
81
82
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])

EmbeddingInitializer

Bases: CommonInitializer

Initializer for the embedding process.

Bind the components required for the embedding process to the injector.

Source code in src/common/bootstrap/initializer.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class EmbeddingInitializer(CommonInitializer):
    """Initializer for the embedding process.

    Bind the components required for the embedding process to the injector.
    """

    def _bind(self, binder: Binder) -> None:
        """Bind components required for the embedding process to the injector.

        Args:
            binder: Injector binder
        """
        super()._bind(binder)
        DatasourcesBinder(
            configuration=self.configuration, binder=binder
        ).bind()

_bind(binder)

Bind components required for the embedding process to the injector.

Parameters:
  • binder (Binder) –

    Injector binder

Source code in src/common/bootstrap/initializer.py
144
145
146
147
148
149
150
151
152
153
def _bind(self, binder: Binder) -> None:
    """Bind components required for the embedding process to the injector.

    Args:
        binder: Injector binder
    """
    super()._bind(binder)
    DatasourcesBinder(
        configuration=self.configuration, binder=binder
    ).bind()

EvaluationInitializer

Bases: AugmentationInitializer

Initializer for the evaluation process.

Bind the components required for the evaluation process to the injector.

Source code in src/common/bootstrap/initializer.py
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
class EvaluationInitializer(AugmentationInitializer):
    """Initializer for the evaluation process.

    Bind the components required for the evaluation process to the injector."""

    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 components required for the evaluation process to the injector.

        Args:
            binder: Injector binder
        """
        super()._bind(binder)
        EvaluationBinder(configuration=self.configuration, binder=binder).bind()

_bind(binder)

Bind components required for the evaluation process to the injector.

Parameters:
  • binder (Binder) –

    Injector binder

Source code in src/common/bootstrap/initializer.py
197
198
199
200
201
202
203
204
def _bind(self, binder: Binder) -> None:
    """Bind components required for the evaluation process to the injector.

    Args:
        binder: Injector binder
    """
    super()._bind(binder)
    EvaluationBinder(configuration=self.configuration, binder=binder).bind()

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
190
191
192
193
194
195
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])