Class: CompletionKit::Api::V1::MetricsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/completion_kit/api/v1/metrics_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_few_shotObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 57

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)
  render json: @metric.reload
rescue ActiveRecord::RecordNotFound
  render json: { error: "Calibration not found or not a disagree on this metric." }, status: :not_found
end

#createObject



17
18
19
20
21
22
23
24
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 17

def create
  metric = Metric.new(metric_params)
  if metric.save
    render json: metric, status: :created
  else
    render json: {errors: metric.errors}, status: :unprocessable_entity
  end
end

#destroyObject



34
35
36
37
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 34

def destroy
  @metric.destroy!
  head :no_content
end

#indexObject



7
8
9
10
11
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 7

def index
  scope = Metric.includes(:tags)
  scope = filter_by_tags(scope)
  render json: paginate(scope.order(created_at: :desc))
end

#remove_few_shotObject



77
78
79
80
81
82
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 77

def remove_few_shot
  cal_id = params[:calibration_id].to_i
  remaining = Array(@metric.few_shot_examples).reject { |fs| fs["calibration_id"].to_i == cal_id }
  @metric.update!(few_shot_examples: remaining)
  render json: @metric.reload
end

#showObject



13
14
15
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 13

def show
  render json: @metric
end

#suggest_variantsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/completion_kit/api/v1/metrics_controller.rb', line 39

def suggest_variants
  disagreement_count = Calibration.where(metric_id: @metric.id, verdict: "disagree").count
  if disagreement_count.zero?
    render json: { error: "Mark at least one case as Disagree before asking the model to suggest a change." }, status: :unprocessable_entity
    return
  end

  MetricVersion.drafts.where(metric_id: @metric.id, source: "suggestion").destroy_all
  generator = MetricVariantGenerator.new(@metric, count: params[:count].to_i, model: params[:model])
  variants = generator.call
  if variants.empty?
    render json: { error: "The model returned no usable variants. Try again with a different model." }, status: :unprocessable_entity
    return
  end
  versions = generator.persist!(variants)
  render json: versions, status: :created
end

#updateObject



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

def update
  if @metric.update(metric_params)
    render json: @metric
  else
    render json: {errors: @metric.errors}, status: :unprocessable_entity
  end
end