Class: CompletionKit::RunsController

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

Instance Method Summary collapse

Instance Method Details

#apply_suggestionObject



116
117
118
119
120
121
122
123
124
# File 'app/controllers/completion_kit/runs_controller.rb', line 116

def apply_suggestion
  suggestion = @run.suggestions.order(created_at: :desc).first
  return redirect_to run_path(@run), alert: "No suggestion to apply." unless suggestion

  new_prompt = @run.prompt.clone_as_new_version(template: suggestion.suggested_template)
  new_prompt.publish!
  suggestion.update!(applied_at: Time.current)
  redirect_to prompt_path(new_prompt), notice: "Suggestion applied."
end

#createObject



35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/completion_kit/runs_controller.rb', line 35

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



60
61
62
63
# File 'app/controllers/completion_kit/runs_controller.rb', line 60

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

#editObject



32
33
# File 'app/controllers/completion_kit/runs_controller.rb', line 32

def edit
end

#generateObject



65
66
67
68
69
70
71
# File 'app/controllers/completion_kit/runs_controller.rb', line 65

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



6
7
8
# File 'app/controllers/completion_kit/runs_controller.rb', line 6

def index
  @runs = Run.includes(:prompt, :dataset, responses: :reviews).order(created_at: :desc)
end

#newObject



28
29
30
# File 'app/controllers/completion_kit/runs_controller.rb', line 28

def new
  @run = Run.new(prompt_id: params[:prompt_id])
end

#retry_failuresObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/completion_kit/runs_controller.rb', line 90

def retry_failures
  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.send(:broadcast_ui)
  redirect_to run_path(@run)
end

#showObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/completion_kit/runs_controller.rb', line 10

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



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

def suggest
  service = PromptImprovementService.new(@run)
  result = service.suggest
  @run.suggestions.create!(
    prompt: @run.prompt,
    reasoning: result["reasoning"],
    suggested_template: result["suggested_template"],
    original_template: result["original_template"]
  )
  redirect_to suggestion_run_path(@run)
end

#suggestionObject



85
86
87
88
# File 'app/controllers/completion_kit/runs_controller.rb', line 85

def suggestion
  @suggestion = @run.suggestions.order(created_at: :desc).first
  return redirect_to run_path(@run), alert: "No suggestion available. Generate one first." unless @suggestion
end

#updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/completion_kit/runs_controller.rb', line 46

def update
  if @run.responses.any?
    new_run = Run.create!(run_params.except(:metric_ids).to_h.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