Class: CompletionKit::Api::V1::RunsController

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

Instance Method Summary collapse

Instance Method Details

#compareObject



105
106
107
108
109
110
111
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 105

def compare
  other = Run.find(params[:with])
  comparison = build_run_comparison(@run, other)
  render json: { left_run_id: @run.id, right_run_id: other.id, metric_ids: comparison[:metric_ids], rows: comparison[:rows] }
rescue ActiveRecord::RecordNotFound
  render json: { error: "Other run not found. Pass ?with=<run_id>." }, status: :not_found
end

#createObject



20
21
22
23
24
25
26
27
28
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 20

def create
  run = Run.new(run_params.except(:metric_ids))
  if run.save
    run.replace_metrics!(params[:metric_ids])
    render json: run.reload, status: :created
  else
    render json: {errors: run.errors}, status: :unprocessable_entity
  end
end

#destroyObject



39
40
41
42
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 39

def destroy
  @run.destroy!
  head :no_content
end

#generateObject



44
45
46
47
48
49
50
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 44

def generate
  if @run.start!
    render json: @run.reload, status: :accepted
  else
    render json: { errors: [@run.failure_summary || @run.errors.full_messages.to_sentence] }, status: :unprocessable_entity
  end
end

#indexObject



7
8
9
10
11
12
13
14
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 7

def index
  scope = Run.includes(:tags)
  scope = scope.where(status: params[:status]) if params[:status].present?
  scope = scope.where(prompt_id: params[:prompt_id]) if params[:prompt_id].present?
  scope = scope.where(dataset_id: params[:dataset_id]) if params[:dataset_id].present?
  scope = filter_by_tags(scope)
  render json: paginate(scope.order(created_at: :desc))
end

#regradeObject



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

def regrade
  if @run.regrade!
    render json: @run.reload, status: :accepted
  else
    render json: { error: "Nothing to re-grade. The run has no succeeded responses or no metrics attached." }, status: :unprocessable_entity
  end
end

#rerunObject



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

def rerun
  new_run = Run.create!(
    prompt_id: @run.prompt_id,
    dataset_id: @run.dataset_id,
    judge_model: @run.judge_model,
    temperature: @run.temperature,
    output_column: @run.output_column,
    tag_names: @run.tag_names,
    status: "pending"
  )
  new_run.replace_metrics!(@run.metric_ids)
  if new_run.start!
    render json: new_run.reload, status: :accepted
  else
    render json: { errors: [new_run.failure_summary || "Could not start the new run."] }, status: :unprocessable_entity
  end
end

#retry_failuresObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 52

def retry_failures
  if @run.stale_review_summary.any?
    return render(json: { error: "Judge has changed since this run executed. Retry would mix versions in the same run; use POST /api/v1/runs/:id/rerun instead." }, status: :conflict)
  end

  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)
    CompletionKit::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| CompletionKit::GenerateRowJob.perform_later(@run.id, rid) }
  end

  render json: @run.reload, status: :accepted
end

#showObject



16
17
18
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 16

def show
  render json: @run
end

#updateObject



30
31
32
33
34
35
36
37
# File 'app/controllers/completion_kit/api/v1/runs_controller.rb', line 30

def update
  if @run.update(run_params.except(:metric_ids))
    @run.replace_metrics!(params[:metric_ids]) if params.key?(:metric_ids)
    render json: @run.reload
  else
    render json: {errors: @run.errors}, status: :unprocessable_entity
  end
end