Class: CompletionKit::MetricsController

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

Constant Summary

Constants inherited from ApplicationController

ApplicationController::ONBOARDING_DISMISS_COOKIE

Instance Method Summary collapse

Instance Method Details

#add_few_shotObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/completion_kit/metrics_controller.rb', line 97

def add_few_shot
  calibration = Calibration.where(metric_id: @metric.id, verdict: "disagree").find(params[:calibration_id])
  review = calibration.response.reviews.find_by(metric_id: @metric.id)
  examples = Array(@metric.few_shot_examples)
  examples << {
    "input" => calibration.response.input_data.to_s.truncate(2000),
    "response" => calibration.response.response_text.to_s.truncate(2000),
    "judge_score" => review&.ai_score&.to_f,
    "judge_feedback" => review&.ai_feedback.to_s.truncate(1000),
    "human_score" => calibration.corrected_score&.to_f,
    "human_note" => calibration.note.to_s.truncate(1000),
    "calibration_id" => calibration.id,
    "added_at" => Time.current.utc.iso8601
  }
  @metric.update!(few_shot_examples: examples)
  redirect_to metric_path(@metric), notice: "Got it. The judge will remember this next time it grades."
end

#createObject



28
29
30
31
32
33
34
35
36
# File 'app/controllers/completion_kit/metrics_controller.rb', line 28

def create
  @metric = Metric.new(metric_params)

  if @metric.save
    redirect_to metric_path(@metric), notice: "Metric was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



46
47
48
49
# File 'app/controllers/completion_kit/metrics_controller.rb', line 46

def destroy
  @metric.destroy
  redirect_to metrics_path, notice: "Metric was successfully destroyed."
end

#dismiss_suggestionObject



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

def dismiss_suggestion
  draft = JudgeVersion.drafts.where(metric_id: @metric.id, source: "suggestion").find_by(id: params[:draft_id])
  draft&.destroy
  redirect_to metric_path(@metric), notice: "Dismissed."
end

#editObject



25
26
# File 'app/controllers/completion_kit/metrics_controller.rb', line 25

def edit
end

#indexObject



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

def index
  @metrics = apply_tag_filter(Metric.includes(:metric_groups, :tags).order(:name))
end

#newObject



21
22
23
# File 'app/controllers/completion_kit/metrics_controller.rb', line 21

def new
  @metric = Metric.new
end

#publish_draftObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/completion_kit/metrics_controller.rb', line 76

def publish_draft
  scope = JudgeVersion.drafts.where(metric_id: @metric.id)
  draft = params[:draft_id].present? ? scope.find_by(id: params[:draft_id]) : scope.order(created_at: :desc).first

  if draft.nil?
    redirect_to metric_path(@metric), alert: "No draft to publish."
    return
  end

  JudgeVersion.transaction do
    JudgeVersion.where(metric_id: @metric.id, state: "published").update_all(current: false)
    draft.update!(state: "published", current: true)
    @metric.update_columns(
      instruction: draft.instruction,
      rubric_bands: Array(draft.rubric_bands).to_json
    )
  end

  redirect_to metric_path(@metric), notice: "This judge version is now live."
end

#showObject



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/completion_kit/metrics_controller.rb', line 10

def show
  @disagreements = Calibration.where(metric_id: @metric.id, verdict: "disagree")
                              .includes(response: [:reviews, :run])
                              .order(created_at: :desc)
                              .limit(50)
  @edit_draft = JudgeVersion.drafts.where(metric_id: @metric.id, source: "edit").order(created_at: :desc).first
  @published_judge_version = JudgeVersion.published.where(metric_id: @metric.id, current: true).first
  @suggestion_draft = JudgeVersion.drafts.where(metric_id: @metric.id, source: "suggestion").order(created_at: :desc).first
  @improve_disagreement_count = @disagreements.size
end

#suggest_variantsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/completion_kit/metrics_controller.rb', line 51

def suggest_variants
  disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count
  if disagreement_count.zero?
    redirect_to metric_path(@metric), alert: "Mark at least one row as Disagree before asking the model to suggest a change."
    return
  end

  JudgeVersion.drafts.where(metric_id: @metric.id, source: "suggestion").destroy_all

  generator = JudgeVariantGenerator.new(@metric, count: 1)
  variants = generator.call
  if variants.empty?
    redirect_to metric_path(@metric), alert: "The model returned no usable variants. Try again with a different model."
    return
  end
  generator.persist!(variants)
  redirect_to metric_path(@metric), notice: "Drafted a new version. Review it below."
end

#updateObject



38
39
40
41
42
43
44
# File 'app/controllers/completion_kit/metrics_controller.rb', line 38

def update
  if @metric.update(metric_params)
    redirect_to metric_path(@metric), notice: "Metric was successfully updated."
  else
    render :edit, status: :unprocessable_entity
  end
end