Base Generator

class codestral_ros2_gen.generators.base_generator.BaseGenerator(config_path=None, generation_attempt_class=None, metrics_handler_class=None, model_client_class=None)[source]

Bases: ABC

Abstract base class for code generation modules in the codestral_ros2_gen framework.

This class implements the template method pattern with a three-phase generation process:
  1. Initialization: Set up the environment, load configuration, and prepare resources.

  2. Generation: Execute the actual code generation with multiple attempts and iterations.

  3. Reporting: Analyze and display metrics about the generation process.

Derived classes must implement the abstract method prepare_prompt() to define the specific prompting strategy for their generation task.

Parameters:
  • config_path (Path | None)

  • generation_attempt_class (Type | None)

  • metrics_handler_class (Type | None)

  • model_client_class (Type | None)

__init__(config_path=None, generation_attempt_class=None, metrics_handler_class=None, model_client_class=None)[source]

Initialize the generator with configurable dependencies.

Parameters:
  • config_path (Optional[Path]) – Path to the configuration file.

  • generation_attempt_class (Optional[Type]) – Class to use for generation attempts (for testing).

  • metrics_handler_class (Optional[Type]) – Class to use for metrics handling (for testing).

  • model_client_class (Optional[Type]) – Class to use for the AI model client (for testing).

GenerationAttemptClass

alias of GenerationAttempt

MetricsHandlerClass

alias of MetricsHandler

ModelClientClass

alias of MistralClient

abstract prepare_prompt(**kwargs)[source]

Prepare the input prompt to be used for code generation.

This abstract method must be implemented by subclasses to define the specific prompting strategy for their generation task.

Parameters:

**kwargs – Additional keyword arguments specific to the generation task (e.g., service_name, node_name for prompting).

Returns:

A formatted prompt string ready for model input.

Return type:

str

_validate_environment()[source]

Validate that the configuration has a ‘generation’ section with required keys, and ensure that the output directory is accessible.

Return type:

None

Checks for required configuration sections and keys:
  • ‘generation’ section with ‘max_attempts’ and ‘evaluation_iterations’

  • ‘output’ section with ‘output_file’

Also attempts to create the output directory if it doesn’t exist.

Raises:

RuntimeError – If configuration is invalid or output directory is inaccessible.

Return type:

None

_check_ros2_workspace()[source]

Check that the current working directory is a valid ROS2 workspace, i.e. contains either a ‘src’ or ‘install’ directory.

Raises:

RuntimeError – If the current directory is not a valid ROS2 workspace.

Return type:

None

_initialization_phase()[source]

Execute the initialization phase of the generation process.

Return type:

None

This phase:
  1. Loads the configuration from the specified file.

  2. Validates the environment and configuration.

  3. Initializes the metrics handler and the model client.

Raises:

RuntimeError – If there’s an issue with configuration or initialization.

Return type:

None

_generation_phase(**kwargs)[source]

Execute the generation phase by performing multiple iterations and attempts.

This phase:
  1. Prepares the prompt using the subclass-specific implementation.

  2. Runs the configured number of iterations.

  3. For each iteration, makes multiple generation attempts until success or max attempts.

  4. Records metrics for each attempt.

The process stops early if a successful generation is achieved.

Parameters:

**kwargs – Additional keyword arguments to be passed to the prompt preparation method.

Raises:

Exception – Any exceptions that might occur during the generation process.

Return type:

None

_report_phase()[source]

Analyze the metrics collected during the generation process and display a summary report.

Return type:

None

This phase:
  1. Processes all collected metrics from generation attempts.

  2. Generates a comprehensive report on the generation performance.

  3. Logs the report at INFO level.

This method is typically called in the finally block to ensure reports are generated even if exceptions occur during generation.

run(**kwargs)[source]

Run the complete generation process (initialization, generation, reporting).

This is the main entry point for executing the generation workflow:
  1. Runs the initialization phase.

  2. Executes the generation phase.

  3. Ensures the reporting phase is executed even if errors occur.

  4. Logs the total execution time.

Parameters:

**kwargs – Additional keyword arguments to prepare prompt (e.g., service_name, node_name) and other parameters for the generation process. These are passed to the generation phase.

Raises:

None – Logs but does not propagate exceptions from the generation process.

Return type:

None