File size: 15,015 Bytes
8360ec7
5f0301f
8360ec7
0c6ddb2
5f0301f
0c6ddb2
8360ec7
0d800ab
5f0301f
0c6ddb2
 
ec53a03
0d800ab
 
 
 
0c6ddb2
 
 
0d800ab
 
 
 
0c6ddb2
 
0d800ab
5f0301f
0c6ddb2
 
 
 
5f0301f
8360ec7
 
0d800ab
 
 
ec53a03
0d800ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6ddb2
 
 
 
0d800ab
0c6ddb2
 
 
 
8360ec7
0c6ddb2
ec53a03
5f0301f
0c6ddb2
 
 
ec53a03
5f0301f
8360ec7
d525453
0c6ddb2
8360ec7
0c6ddb2
 
 
 
 
 
d525453
0c6ddb2
8360ec7
5f0301f
0d800ab
ec53a03
0d800ab
ec53a03
0d800ab
ec53a03
0d800ab
 
ec53a03
0d800ab
 
0c6ddb2
 
0d800ab
 
0c6ddb2
0d800ab
 
0c6ddb2
 
0d800ab
0c6ddb2
 
 
0d800ab
 
0c6ddb2
 
 
 
 
0d800ab
0c6ddb2
 
 
0d800ab
 
0c6ddb2
 
0d800ab
 
0c6ddb2
 
 
0d800ab
 
0c6ddb2
0d800ab
0c6ddb2
 
0d800ab
 
 
af30b7c
 
 
0d800ab
af30b7c
 
0c6ddb2
0d800ab
0c6ddb2
0d800ab
0c6ddb2
0d800ab
eca534f
0c6ddb2
 
 
0d800ab
 
0c6ddb2
 
 
 
d525453
0c6ddb2
0d800ab
0c6ddb2
 
 
 
0d800ab
 
0c6ddb2
0d800ab
0c6ddb2
d525453
ad69f02
0d800ab
 
0c6ddb2
 
 
0d800ab
0c6ddb2
0d800ab
0c6ddb2
 
5f0301f
 
ec53a03
 
0c6ddb2
5f0301f
ec53a03
 
0c6ddb2
0d800ab
 
 
0c6ddb2
5f0301f
8360ec7
 
0c6ddb2
0d800ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6ddb2
0d800ab
 
0c6ddb2
0d800ab
 
d525453
 
 
0d800ab
0c6ddb2
0d800ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6ddb2
0d800ab
8360ec7
 
0c6ddb2
 
 
 
8360ec7
 
 
0c6ddb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8360ec7
0c6ddb2
 
 
 
 
 
 
 
 
 
 
 
 
 
ec53a03
0c6ddb2
 
 
8360ec7
 
ec53a03
0c6ddb2
ec53a03
0c6ddb2
8360ec7
ec53a03
 
0c6ddb2
8360ec7
ec53a03
 
8360ec7
ec53a03
 
8360ec7
 
 
 
0c6ddb2
 
 
 
8360ec7
 
0c6ddb2
8360ec7
 
 
 
 
 
 
 
0d800ab
 
0c6ddb2
0d800ab
 
 
8360ec7
0c6ddb2
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import os
import json
import yaml
from typing import Any
from pathlib import Path
from dotenv import load_dotenv
from collections import namedtuple
from importlib import resources as pkg_resources


from openfactcheck.utils.logging import get_logger, set_verbosity
from openfactcheck.errors import ConfigValidationError
from openfactcheck import templates as solver_config_templates_dir
from openfactcheck import solvers as solver_templates_dir

# Import solver configuration templates
solver_config_templates_path = str(pkg_resources.files(solver_config_templates_dir) / "solver_configs")
solver_config_template_files = [str(f) for f in Path(solver_config_templates_path).iterdir()]


# Import default solvers
# TODO: Currently, only webservice solvers are supported as default solvers
solver_templates_paths = [
    str(pkg_resources.files(solver_templates_dir) / "webservice"),
    str(pkg_resources.files(solver_templates_dir) / "factool"),
]

# Load environment variables from .env file
load_dotenv()


class OpenFactCheckConfig:
    """
    Class to load the OpenFactCheck configuration from a JSON or YAML file.

    Parameters
    ----------
    filename_or_path: str or path object
        The path to the configuration file.

    Attributes
    ----------
    retries: int
        Number of retries for the pipeline components.
    pipeline: namedtuple
        Namedtuple containing the pipeline components.
    solver_configs: dict
        Dictionary containing the solver configurations.
    solver_paths: dict
        Dictionary containing the paths to the solver models.
    output_path: str
        Path to the output directory.
    secrets: namedtuple
        Namedtuple containing the API keys.
    verbose: str
        The verbosity level for the logger.

    Methods
    -------
    solver_configuration(solver: str = None) -> dict:
        Get the solver configuration for a specific solver or all solvers.
    validate():
        Validate the configuration file

    Examples
    --------
    For loading the default configuration file 'config.json':
    >>> config = OpenFactCheckConfig()

    For loading the configuration file from a specific path or filename:
    >>> config = OpenFactCheckConfig("config.json")

    For loading the configuration file and validating it:
    >>> config = OpenFactCheckConfig("config.json")
    >>> config.validate()
    """

    def __init__(self, filename_or_path: str | Path = "config.json"):
        # Setup Logger
        self.logger = get_logger()

        # Set the filename or path
        self.filename_or_path = filename_or_path

        # Define namedtuple structures
        Secrets = namedtuple("Secrets", ["openai_api_key", "serper_api_key", "scraper_api_key"])

        # Define Attributes
        self.config: dict = {}
        self.retries: int = 3
        self.pipeline: list = []
        self.solver_configs: dict[Any, Any] = SolversConfig(solver_config_template_files)()
        self.solver_paths: dict[str, list[str]] = {"default": solver_templates_paths, "user_defined": []}
        self.output_path: str = "tmp/output"
        self.secrets: Secrets = Secrets(openai_api_key=None, serper_api_key=None, scraper_api_key=None)
        self.verbose = "WARNING"

        try:
            # Check if the file exists
            if Path(self.filename_or_path).exists():
                # Loading Config File
                with open(self.filename_or_path, encoding="utf-8") as file:
                    self.config = json.load(file)
                    self.logger.info(f"Config file loaded successfully from {self.filename_or_path}")
            else:
                # Create a dummy configuration file
                self.logger.warning(f"Config file not found: {self.filename_or_path}")

            # Initialize Retries
            if "retries" in self.config:
                self.retries = self.config["retries"]
            else:
                self.logger.warning("No retries found in the configuration file. Using default value of 3.")

            # Initialize template solvers along with the user-defined solvers
            # User defined solvers will override the template solvers
            if "solver_configs" in self.config:
                self.solver_configs = SolversConfig(solver_config_template_files + self.config["solver_configs"])()
            else:
                self.logger.warning(
                    "No solver configurations found in the configuration file. Using default templates only."
                )

            # Initialize template solver paths along with the user-defined solver paths
            if "solver_paths" in self.config:
                self.solver_paths = {
                    "default": solver_templates_paths,
                    "user_defined": self.config["solver_paths"],
                }
            else:
                self.logger.warning(
                    "No solver paths found in the configuration file. Using default solver paths only."
                )

            # Initialize Output Path
            if "output_path" in self.config:
                self.output_path = self.config["output_path"]
                os.makedirs(self.output_path, exist_ok=True)
            else:
                self.logger.warning(
                    "No output path found in the configuration file. Using default output path 'tmp/output'."
                )
                self.output_path = "tmp/output"
                os.makedirs(self.output_path, exist_ok=True)

            # Initialize Pipeline config
            if "pipeline" in self.config:
                self.pipeline = self.config["pipeline"]
            else:
                if self.solver_configs:
                    solvers = list(self.solver_configs.keys())
                    claimprocessor = "factool_claimprocessor" if "factool_claimprocessor" in solvers else None
                    retriever = "factool_retriever" if "factool_retriever" in solvers else None
                    verifier = "factcheckgpt_verifier" if "factcheckgpt_verifier" in solvers else None
                    for solver in solvers:
                        if claimprocessor and retriever and verifier:
                            break
                        if "claimprocessor" in solver:
                            claimprocessor = solver
                        if "retriever" in solver:
                            retriever = solver
                        if "verifier" in solver:
                            verifier = solver
                    self.pipeline = [claimprocessor, retriever, verifier]
                    self.logger.warning(
                        f"No pipeline found in the configuration file. Using first solver as default pipeline. ClaimProcessor: {claimprocessor}, Retriever: {retriever}, Verifier: {verifier}"
                    )

            # Initialize Secrets config
            if "secrets" in self.config:
                self.secrets = Secrets(
                    openai_api_key=self.config["secrets"]["openai_api_key"],
                    serper_api_key=self.config["secrets"]["serper_api_key"],
                    scraper_api_key=self.config["secrets"]["scraper_api_key"],
                )
            else:
                self.logger.warning(
                    "No secrets found in the configuration file. Make sure to set the environment variables."
                )

            # Initialize Environment Variables
            if self.secrets.openai_api_key:
                os.environ["OPENAI_API_KEY"] = self.secrets.openai_api_key
            if self.secrets.serper_api_key:
                os.environ["SERPER_API_KEY"] = self.secrets.serper_api_key
            if self.secrets.scraper_api_key:
                os.environ["SCRAPER_API_KEY"] = self.secrets.scraper_api_key

            # Initialize Verbose
            if "verbose" in self.config:
                self.verbose = self.config["verbose"]
                set_verbosity(self.verbose)
            else:
                self.logger.warning("No verbose level found in the configuration file. Using default level 'WARNING'.")

            # Validate Configuration
            self.validate()

        except FileNotFoundError:
            self.logger.error(f"Config file not found: {self.filename_or_path}")
            raise FileNotFoundError(f"Config file not found: {self.filename_or_path}")

        except json.JSONDecodeError:
            self.logger.error(f"Invalid JSON in config file: {self.filename_or_path}")
            raise ValueError(f"Invalid JSON in config file: {self.filename_or_path}")

        except ConfigValidationError as e:
            self.logger.error(f"Configuration validation failed: {e}")
            raise ConfigValidationError(f"Configuration validation failed: {e}")

        except Exception as e:
            self.logger.error(f"Unexpected error loading config file: {e}")
            raise Exception(f"Unexpected error loading config file: {e}")

    def validate(self):
        """
        Validate the configuration file.

        Raises
        ------
        ValueError
            If the configuration file is invalid.

        Examples
        --------
        >>> config = OpenFactCheckConfig("config.json")
        >>> config.validate()
        """
        # Check for environment variables
        if "OPENAI_API_KEY" not in os.environ:
            self.logger.warning("OPENAI_API_KEY environment variable not found.")
            raise ConfigValidationError("OPENAI_API_KEY environment variable not found.")
        if "SERPER_API_KEY" not in os.environ:
            self.logger.warning("SERPER_API_KEY environment variable not found.")
            raise ConfigValidationError("SERPER_API_KEY environment variable not found.")
        if "SCRAPER_API_KEY" not in os.environ:
            self.logger.warning("SCRAPER_API_KEY environment variable not found.")
            raise ConfigValidationError("SCRAPER_API_KEY environment variable not found.")

    def solver_configuration(self, solver: str | None = None) -> dict:
        """
        Get the solver configuration for a specific solver or all solvers.

        Parameters
        ----------
        solver: str
            The name of the solver to get the configuration for.
            If not provided, returns the configuration for all solvers.

        Returns
        -------
        dict
            The configuration for the specified solver or all solvers.

        Raises
        ------
        ConfigValidationError
            If the configuration validation fails.

        Examples
        --------
        >>> config = OpenFactCheckConfig("config.json")
        >>> config.solver_configuration()
        >>> config.solver_configuration("factcheckgpt_claimprocessor")
        """
        if solver:
            if solver in self.solver_configs:
                return self.solver_configs[solver]
            else:
                self.logger.error(f"Solver not found: {solver}")
                raise ValueError(f"Solver not found: {solver}")
        else:
            return self.solver_configs


class SolversConfig:
    """
    A class to load solver configurations from one or more JSON or YAML files.

    This class reads solver configurations from specified files, merges them,
    and provides access to the combined configuration as a dictionary.

    Parameters
    ----------
    filename_or_paths : str | Path | list[str | Path]
        The path or list of paths to the solver configuration files.

    Attributes
    ----------
    solvers : dict[Any, Any]
        Dictionary containing the merged solver configurations.

    Examples
    --------
    Load solver configurations from a single file:

    >>> solvers = SolversConfig("solvers.yaml")
    >>> config = solvers()

    Load solver configurations from multiple files:

    >>> solvers = SolversConfig(["solvers1.json", "solvers2.yaml"])
    >>> config = solvers()

    Access the solvers dictionary:

    >>> config = solvers()
    """

    def __init__(self, filename_or_path_s: str | Path | list[str] | list[Path]) -> None:
        """
        Initialize the SolversConfig class.

        Parameters
        ----------
        filename_or_path_s: str or path object or list of str or path objects
            The path to the solvers configuration or a list of paths to multiple solvers configurations.
        """
        # Setup Logger
        self.logger = get_logger()

        # Set the filename or path
        self.filename_or_path_or_path_s = filename_or_path_s

        # Define Attributes
        self.solvers: dict[Any, Any] = {}

        try:
            if isinstance(self.filename_or_path_or_path_s, (str, Path)):
                self.__load_config(self.filename_or_path_or_path_s)
            elif isinstance(self.filename_or_path_or_path_s, list):
                self.__load_configs(self.filename_or_path_or_path_s)
            else:
                self.logger.error(f"Invalid filename type: {type(self.filename_or_path_or_path_s)}")
                raise ValueError(f"Invalid filename type: {type(self.filename_or_path_or_path_s)}")

        except FileNotFoundError:
            self.logger.error(f"Solvers file not found: {self.filename_or_path_or_path_s}")
            raise FileNotFoundError(f"Solvers file not found: {self.filename_or_path_or_path_s}")
        except json.JSONDecodeError:
            self.logger.error(f"Invalid JSON in solvers file: {self.filename_or_path_or_path_s}")
            raise ValueError(f"Invalid JSON in solvers file: {self.filename_or_path_or_path_s}")
        except Exception as e:
            self.logger.error(f"Unexpected error loading solvers file: {e}")
            raise Exception(f"Unexpected error loading solvers file: {e}")

    def __load_config(self, filename_or_path: str | Path) -> None:
        # Ensure filename is a string when performing string operations
        filename = str(filename_or_path)

        with open(filename, encoding="utf-8") as file:
            if filename.endswith(".yaml"):
                file_data = yaml.load(file, Loader=yaml.FullLoader)
            elif filename.endswith(".json"):
                file_data = json.load(file)
            else:
                self.logger.error(f"Invalid file format: {filename}")
                raise ValueError(f"Invalid file format: {filename}")

            # Merge current file data into existing solvers dictionary
            self.solvers.update(file_data)

            # Log the loaded configuration pattern
            if "template" in filename:
                self.logger.info(f"Template solver configuration loaded: {filename.split('/')[-1]}")
            else:
                self.logger.info(f"User-defined solver configuration loaded from: {filename}")

    def __load_configs(self, filenames: list[str] | list[Path]) -> None:
        for filename in filenames:
            self.__load_config(filename)

    def __call__(self) -> dict[Any, Any]:
        return self.solvers