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



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

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
    replace_run_metrics(@run, 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
# File 'app/controllers/completion_kit/runs_controller.rb', line 65

def generate
  @run.update!(status: "generating", progress_current: 0, progress_total: 0, error_message: nil)
  GenerateJob.perform_later(@run.id)
  redirect_to run_path(@run)
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

#judgeObject



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

def judge
  if params[:run]
    @run.update(judge_model: params[:run][:judge_model])
  end
  JudgeJob.perform_later(@run.id)
  redirect_to run_path(@run)
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

#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



79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/completion_kit/runs_controller.rb', line 79

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



91
92
93
94
# File 'app/controllers/completion_kit/runs_controller.rb', line 91

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"))
    replace_run_metrics(new_run, 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))
    replace_run_metrics(@run, 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