Class: Leva::SignatureGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/leva/signature_generator.rb

Overview

Generates DSPy signatures from Leva dataset records.

This service analyzes the structure of dataset records and generates a dynamic DSPy::Signature class that matches the input/output schema.

Examples:

Generate a signature from a dataset

generator = Leva::SignatureGenerator.new(dataset)
signature_class = generator.generate
predictor = DSPy::Predict.new(signature_class)

Instance Method Summary collapse

Constructor Details

#initialize(dataset, description: nil) ⇒ SignatureGenerator

Returns a new instance of SignatureGenerator.

Parameters:

  • dataset (Leva::Dataset)

    The dataset to analyze

  • description (String, nil) (defaults to: nil)

    Optional description for the signature



16
17
18
19
20
# File 'app/services/leva/signature_generator.rb', line 16

def initialize(dataset, description: nil)
  @dataset = dataset
  @description = description
  @sample_record = dataset.dataset_records.first&.recordable
end

Instance Method Details

#generateClass?

Generates a DSPy::Signature class based on the dataset structure.

Returns:

  • (Class, nil)

    A dynamically generated DSPy::Signature subclass, or nil if no sample



25
26
27
28
29
30
31
32
33
# File 'app/services/leva/signature_generator.rb', line 25

def generate
  return nil unless @sample_record

  input_fields = extract_input_fields
  output_type = infer_output_type(@sample_record.ground_truth)
  description = @description || generate_description

  build_signature_class(input_fields, output_type, description)
end

#input_field_namesArray<Symbol>

Returns the input field names that will be used in the signature.

Returns:

  • (Array<Symbol>)

    Array of input field names



38
39
40
41
42
# File 'app/services/leva/signature_generator.rb', line 38

def input_field_names
  return [] unless @sample_record

  extract_input_fields.keys
end