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



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

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: "Added as a judge few-shot."
end

#createObject



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

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



44
45
46
47
# File 'app/controllers/completion_kit/metrics_controller.rb', line 44

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

#editObject



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

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



19
20
21
# File 'app/controllers/completion_kit/metrics_controller.rb', line 19

def new
  @metric = Metric.new
end

#publish_draftObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/completion_kit/metrics_controller.rb', line 61

def publish_draft
  draft = JudgeVersion.drafts.where(metric_id: @metric.id).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)
  end

  redirect_to metric_path(@metric), notice: "Draft published as the current judge version."
end

#showObject



10
11
12
13
14
15
16
17
# 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)
  @latest_draft = JudgeVersion.drafts.where(metric_id: @metric.id).order(created_at: :desc).first
  @suggestion_drafts = JudgeVersion.drafts.where(metric_id: @metric.id, source: "suggestion").order(created_at: :desc)
end

#suggest_variantsObject



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/completion_kit/metrics_controller.rb', line 49

def suggest_variants
  generator = JudgeVariantGenerator.new(@metric)
  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)
  label = variants.length == 1 ? "judge variant" : "judge variants"
  redirect_to metric_path(@metric), notice: "Generated #{variants.length} #{label} as drafts. Pick one to publish."
end

#updateObject



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

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