ciberlabreport.core module
Module for automated report generation using OpenAI API.
This module defines the ReportGenerator class, which processes JSON input files and produces PDF reports using OpenAI models. It provides flexible handling of input and output paths, including support for absolute and relative paths, single files, and directories.
- The workflow includes:
Validation and normalization of input/output parameters.
Detection of input file types (.json).
Automatic naming of output files following the pattern “<stem>-report.pdf”.
Logging of debug information and processing time measurement.
- Configuration:
ReportGenerator receives runtime configuration explicitly via constructor parameters. For environment-based setups, callers can either map values from os.environ before instantiation or use
ReportGenerator.from_env().- Classes:
ReportGenerator: Handles the logic for file resolution and report generation.
Example
>>> from ciberlabreport.core import ReportGenerator
>>> rg = ReportGenerator(openai_api_key="sk-...")
>>> rg.generate("input_data", "output_dir")
- raises ValueError:
If constructor parameters or input parameters are invalid.
- raises FileNotFoundError:
If the specified input file or directory does not exist.
- raises TypeError:
If invalid data types are provided for parameters.
- class ciberlabreport.core.ReportGenerator(openai_api_key: str | None = None, vt_api_keys: list | None = None, max_files: int = 3, preprocess_limits: PreprocessLimits | None = None, max_completion_tokens: int = 25000, input_path_default: str | Path | None = None, output_path_default: str | Path | None = None, config_path: str | Path | None = None, prompts_path: str | Path | None = None, schemas_path: str | Path | None = None, tmp_path: str | Path | None = None, ai_model: str = 'gpt-5', mode: str = 'PRO', img_threshold: int = 9, families_list: list | None = None)
Bases:
objectHandles the generation of PDF reports from JSON files using OpenAI API.
This class manages the input/output path resolution, validates provided parameters, and coordinates asynchronous and synchronous OpenAI clients to generate reports.
- classmethod from_env(max_files: int = 3, preprocess_limits: PreprocessLimits | None = None, max_completion_tokens: int = 25000, max_keys_list: int = 10) ReportGenerator
Build a ReportGenerator from process environment variables.
- generate(input_data: str, output_data: str = None) dict
Generates one or multiple PDF reports based on the input data.
The method validates the input and output paths, prepares the files, and performs the report generation process using the OpenAI API.
- Parameters:
input_data (str) – Path to a JSON file or directory containing JSON files.
output_data (str, optional) – Output path (absolute, relative, or directory).
- Returns:
Dict object with measurable data.
- Return type:
dict
- Raises:
ValueError – If the input or output parameters are invalid.
FileNotFoundError – If any specified input file does not exist.
- static read_keys_list_env(key: str, max_keys: int = 5) list
Generate a list with all keys related to key parameter in .env file.
- Parameters:
key (str) – Identifier to obtain the keys
max_keys (int, optional) – Limit of keys to read. Defaults to 5.
- Returns:
All keys related to the key parameter.
- Return type:
list