Class: CompletionKit::RunsController

Inherits:
ApplicationController show all
Includes:
TagFiltering
Defined in:
app/controllers/completion_kit/runs_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::ONBOARDING_DISMISS_COOKIE

Instance Method Summary collapse

Instance Method Details

#compareObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/completion_kit/runs_controller.rb', line 84

def compare
  other_id = params[:with]
  if other_id.blank?
    @other_runs = Run.where(dataset_id: @run.dataset_id, prompt_id: @run.prompt_id)
                      .where.not(id: @run.id)
                      .order(created_at: :desc)
                      .limit(50)
    return render(:compare_picker)
  end

  @other_run = Run.find(other_id)
  @comparison = build_run_comparison(@run, @other_run)
  render(:compare)
end

#createObject



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/completion_kit/runs_controller.rb', line 44

def create
  @run = Run.new(run_params.except(:metric_ids))
  if @run.save
    @run.replace_metrics!(params[:run][:metric_ids])
    redirect_to run_path(@run), notice: "Run was successfully created."
  else
    load_form_collections
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



71
72
73
74
# File 'app/controllers/completion_kit/runs_controller.rb', line 71

def destroy
  @run.destroy
  redirect_to runs_path, notice: "Run was successfully destroyed."
end

#editObject



41
42
# File 'app/controllers/completion_kit/runs_controller.rb', line 41

def edit
end

#generateObject



76
77
78
79
80
81
82
# File 'app/controllers/completion_kit/runs_controller.rb', line 76

def generate
  if @run.start!
    redirect_to run_path(@run)
  else
    redirect_to run_path(@run), alert: @run.failure_summary || @run.errors.full_messages.to_sentence
  end
end

#indexObject



7
8
9
10
11
12
# File 'app/controllers/completion_kit/runs_controller.rb', line 7

def index
  scope = Run.includes(:prompt, :dataset, :tags, responses: :reviews).order(created_at: :desc)
  index_scope = CompletionKit.config.runs_index_scope
  scope = scope.instance_exec(&index_scope) if index_scope
  @runs = apply_tag_filter(scope)
end

#newObject



32
33
34
35
36
37
38
39
# File 'app/controllers/completion_kit/runs_controller.rb', line 32

def new
  @run = Run.new(prompt_id: params[:prompt_id])
  prompt = Prompt.find_by(id: @run.prompt_id)
  if prompt
    last_run = Run.where(prompt_id: prompt.family_versions.ids).order(created_at: :desc).first
    @run.tag_names = last_run.tag_names if last_run
  end
end

#refresh_statusObject



125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/completion_kit/runs_controller.rb', line 125

def refresh_status
  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace(
        "run_status_header",
        partial: "completion_kit/runs/status_header",
        locals: { run: @run }
      )
    end
  end
end

#regradeObject



99
100
101
102
103
104
105
# File 'app/controllers/completion_kit/runs_controller.rb', line 99

def regrade
  if @run.regrade!
    redirect_to run_path(@run), notice: "Re-grading existing responses against the current metrics."
  else
    redirect_to run_path(@run), alert: "Nothing to re-grade. The run has no succeeded responses or no metrics attached."
  end
end

#rerunObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/completion_kit/runs_controller.rb', line 107

def rerun
  new_run = Run.create!(
    prompt_id: @run.prompt_id,
    dataset_id: @run.dataset_id,
    judge_model: @run.judge_model,
    temperature: @run.temperature,
    output_column: @run.output_column,
    tag_names: @run.tag_names,
    status: "pending"
  )
  new_run.replace_metrics!(@run.metric_ids)
  if new_run.start!
    redirect_to run_path(new_run), notice: "Re-running with the same configuration."
  else
    redirect_to run_path(new_run), alert: new_run.failure_summary || "Could not start the new run."
  end
end

#retry_failuresObject



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
# File 'app/controllers/completion_kit/runs_controller.rb', line 152

def retry_failures
  if @run.stale_review_summary.any?
    redirect_to run_path(@run),
                alert: "A metric has a newer version than the one this run was scored against. Retrying failed cases would mix scores from two versions in the same run. Use 'Re-run from scratch' to refresh everything against the current metrics."
    return
  end

  scope = @run.responses.where(status: "failed")
  scope = scope.where(id: params[:only]) if params[:only].present?

  ActiveRecord::Base.transaction do
    failed_response_ids = scope.pluck(:id)
    Review.where(response_id: failed_response_ids, status: "failed").update_all(
      status: "pending",
      attempts: 0,
      error_provider: nil, error_class: nil, error_status: nil, error_message: nil,
      ai_score: nil, ai_feedback: nil
    )
    scope.update_all(
      status: "pending",
      attempts: 0,
      error_provider: nil, error_class: nil, error_status: nil, error_message: nil,
      response_text: nil
    )
    @run.update!(status: "running")
    failed_response_ids.each { |rid| GenerateRowJob.perform_later(@run.id, rid) }
  end

  @run.broadcast_ui
  redirect_to run_path(@run)
end

#showObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/completion_kit/runs_controller.rb', line 14

def show
  @responses = if @run.judge_configured? && params[:sort] == "score_asc"
                 @run.responses
                   .left_joins(:reviews)
                   .includes(:reviews)
                   .group("completion_kit_responses.id")
                   .order(Arel.sql("AVG(completion_kit_reviews.ai_score) ASC NULLS LAST"))
               elsif @run.judge_configured?
                 @run.responses
                   .left_joins(:reviews)
                   .includes(:reviews)
                   .group("completion_kit_responses.id")
                   .order(Arel.sql("AVG(completion_kit_reviews.ai_score) DESC NULLS LAST"))
               else
                 @run.responses.includes(:reviews).order(:id)
               end
end

#suggestObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/completion_kit/runs_controller.rb', line 137

def suggest
  if @run.prompt.nil?
    redirect_to run_path(@run), alert: "Judge-only runs don't have a prompt to improve."
    return
  end

  suggestion = @run.suggestions.create!(
    prompt: @run.prompt,
    original_template: @run.prompt.template,
    status: "pending"
  )
  PromptSuggestionJob.perform_later(suggestion.id)
  redirect_to suggestion_path(suggestion, from: "run")
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/completion_kit/runs_controller.rb', line 55

def update
  if @run.responses.any? && run_generation_changed?
    attrs = run_params.except(:metric_ids).to_h
    attrs.delete("name") if attrs["name"].to_s == @run.name.to_s
    new_run = Run.create!(attrs.merge(status: "pending"))
    new_run.replace_metrics!(params[:run][:metric_ids]) if params[:run].key?(:metric_ids)
    redirect_to run_path(new_run), notice: "Saved as a new run. The previous run and its results are preserved."
  elsif @run.update(run_params.except(:metric_ids))
    @run.replace_metrics!(params[:run][:metric_ids]) if params[:run].key?(:metric_ids)
    redirect_to run_path(@run), notice: "Run saved."
  else
    load_form_collections
    render :edit, status: :unprocessable_entity
  end
end