Class: Leva::OptimizationRun

Inherits:
ApplicationRecord show all
Defined in:
app/models/leva/optimization_run.rb

Overview

Tracks the progress and status of prompt optimization runs.

Examples:

Create and track an optimization run

run = OptimizationRun.create!(
  dataset: dataset,
  prompt_name: "My Optimized Prompt",
  mode: "light"
)
run.start!
run.update_progress(step: "bootstrapping", progress: 50, examples_processed: 5)
run.complete!(prompt)

Constant Summary collapse

STEPS =

Defined optimization steps for display

{
  "validating" => { label: "Validating dataset", icon: "check" },
  "splitting_data" => { label: "Splitting data", icon: "scissors" },
  "generating_signature" => { label: "Generating signature", icon: "code" },
  "bootstrapping" => { label: "Bootstrapping examples", icon: "zap" },
  "evaluating" => { label: "Evaluating results", icon: "bar-chart" },
  "building_result" => { label: "Building prompt", icon: "package" },
  "complete" => { label: "Complete", icon: "check-circle" }
}.freeze

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Hash

Returns a hash for JSON API response.

Returns:

  • (Hash)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/leva/optimization_run.rb', line 121

def as_json(options = {})
  {
    id: id,
    status: status,
    current_step: current_step,
    current_step_label: current_step_label,
    progress: progress,
    examples_processed: examples_processed,
    total_examples: total_examples,
    elapsed_time: elapsed_time_formatted,
    error_message: error_message,
    prompt_id: prompt_id,
    prompt_name: prompt_name
  }
end

#complete!(created_prompt) ⇒ void

This method returns an undefined value.

Marks the run as completed with the created prompt.

Parameters:



70
71
72
73
74
75
76
77
# File 'app/models/leva/optimization_run.rb', line 70

def complete!(created_prompt)
  update!(
    status: :completed,
    prompt: created_prompt,
    current_step: "complete",
    progress: 100
  )
end

#current_step_labelString

Returns the human-readable label for the current step.

Returns:

  • (String)


91
92
93
# File 'app/models/leva/optimization_run.rb', line 91

def current_step_label
  STEPS.dig(current_step, :label) || current_step&.humanize || "Initializing"
end

#elapsed_timeActiveSupport::Duration?

Returns elapsed time since the run started.

Returns:

  • (ActiveSupport::Duration, nil)


98
99
100
101
102
# File 'app/models/leva/optimization_run.rb', line 98

def elapsed_time
  return nil unless running? || completed? || failed?

  (completed? || failed? ? updated_at : Time.current) - created_at
end

#elapsed_time_formattedString

Formats elapsed time for display.

Returns:

  • (String)


107
108
109
110
111
112
113
114
115
116
# File 'app/models/leva/optimization_run.rb', line 107

def elapsed_time_formatted
  seconds = elapsed_time&.to_i || 0
  if seconds < 60
    "#{seconds}s"
  elsif seconds < 3600
    "#{seconds / 60}m #{seconds % 60}s"
  else
    "#{seconds / 3600}h #{(seconds % 3600) / 60}m"
  end
end

#fail!(error) ⇒ void

This method returns an undefined value.

Marks the run as failed.

Parameters:

  • error (String, Exception)

    The error message or exception



83
84
85
86
# File 'app/models/leva/optimization_run.rb', line 83

def fail!(error)
  message = error.is_a?(Exception) ? "#{error.class}: #{error.message}" : error.to_s
  update!(status: :failed, error_message: message)
end

#start!void

This method returns an undefined value.

Marks the run as started.



48
49
50
# File 'app/models/leva/optimization_run.rb', line 48

def start!
  update!(status: :running, current_step: "validating", progress: 0)
end

#update_progress(step:, progress:, examples_processed: nil, total: nil) ⇒ void

This method returns an undefined value.

Updates the progress of the optimization run.

Parameters:

  • step (String)

    Current step name

  • progress (Integer)

    Progress percentage (0-100)

  • examples_processed (Integer, nil) (defaults to: nil)

    Number of examples processed

  • total (Integer, nil) (defaults to: nil)

    Total examples to process



59
60
61
62
63
64
# File 'app/models/leva/optimization_run.rb', line 59

def update_progress(step:, progress:, examples_processed: nil, total: nil)
  attrs = { current_step: step, progress: progress }
  attrs[:examples_processed] = examples_processed if examples_processed
  attrs[:total_examples] = total if total
  update!(attrs)
end