Skip to content

Commit

Permalink
feat: Add actual_result column to LevaRunnerResults and LevaDatasetRe…
Browse files Browse the repository at this point in the history
…cords

This commit adds the `actual_result` column to the `leva_runner_results` and `leva_dataset_records` tables in the database. The `actual_result` column will store the actual result of each dataset record, allowing for better analysis and comparison with the predicted results. This change improves the functionality and accuracy of the Leva application.

Related to recent code changes that added the `actual_result` column to the `Leva::RunnerResult` and `Leva::DatasetRecord` models, and updated the migrations and schema file to reflect these changes. Also aligns with recent repository commits that updated foreign key references in migration files and added the `actual_result` attribute to the `Leva::RunnerResult` and `Leva::DatasetRecord` models.
  • Loading branch information
kieranklaassen committed Aug 21, 2024
1 parent b3a0e40 commit 7931e4e
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 17 deletions.
8 changes: 8 additions & 0 deletions app/controllers/leva/runner_results_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Leva
class RunnerResultsController < ApplicationController
def show
@experiment = Experiment.find(params[:experiment_id])
@runner_result = @experiment.runner_results.find(params[:id])
end
end
end
3 changes: 3 additions & 0 deletions app/models/leva/dataset_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# updated_at :datetime not null
# dataset_id :integer not null
# recordable_id :integer not null
# actual_result :text
#
# Indexes
#
Expand All @@ -26,6 +27,8 @@ class DatasetRecord < ApplicationRecord
has_many :runner_results, dependent: :destroy
has_many :evaluation_results, dependent: :destroy, through: :runner_results

validates :actual_result, presence: true

# @return [Hash] A hash of attributes to be displayed in the dataset records index
def index_attributes
if recordable.respond_to?(:index_attributes)
Expand Down
2 changes: 2 additions & 0 deletions app/models/leva/runner_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: leva_runner_results
#
# id :integer not null, primary key
# actual_result :text
# prediction :text
# prompt_version :integer
# created_at :datetime not null
Expand Down Expand Up @@ -31,6 +32,7 @@ class RunnerResult < ApplicationRecord
has_many :evaluation_results, dependent: :destroy

validates :prediction, presence: true
validates :actual_result, presence: true
validates :prompt, presence: true
end
end
16 changes: 4 additions & 12 deletions app/views/leva/experiments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<th class="px-6 py-3 text-left text-xs font-medium text-indigo-300 uppercase tracking-wider">Dataset Record</th>
<th class="px-6 py-3 text-left text-xs font-medium text-indigo-300 uppercase tracking-wider">Prompt</th>
<th class="px-6 py-3 text-left text-xs font-medium text-indigo-300 uppercase tracking-wider">Prediction</th>
<th class="px-6 py-3 text-left text-xs font-medium text-indigo-300 uppercase tracking-wider">Actual Result</th>
<% @experiment.evaluation_results.group_by(&:evaluator_class).keys.each do |evaluator_class| %>
<th class="px-6 py-3 text-left text-xs font-medium text-indigo-300 uppercase tracking-wider"><%= evaluator_class %></th>
<% end %>
Expand All @@ -61,7 +62,8 @@
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300"><%= runner_result.dataset_record.display_name %></td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300"><%= runner_result.prompt.name %> (v<%= runner_result.prompt_version %>)</td>
<td class="px-6 py-4 text-sm text-gray-300"><%= truncate(runner_result.prediction, length: 50) %></td>
<td class="px-6 py-4 text-sm text-gray-300"><%= truncate(runner_result.prediction, length: 30) %></td>
<td class="px-6 py-4 text-sm text-gray-300"><%= truncate(runner_result.actual_result, length: 30) %></td>
<% @experiment.evaluation_results.group_by(&:evaluator_class).keys.each do |evaluator_class| %>
<% eval_result = runner_result.evaluation_results.find_by(evaluator_class: evaluator_class) %>
<td class="px-6 py-4 whitespace-nowrap text-sm">
Expand All @@ -83,24 +85,14 @@
<% end %>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300"><%= runner_result.created_at.strftime("%Y-%m-%d %H:%M:%S") %></td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300">
<%= link_to 'View Details', experiment_runner_result_path(@experiment, runner_result), class: 'text-indigo-400 hover:underline mr-2' %>
<%= link_to 'Experiment', workbench_index_path(prompt_id: runner_result.prompt_id, dataset_record_id: runner_result.dataset_record_id, runner: @experiment.runner_class), class: 'text-indigo-400 hover:underline' %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="mt-4 text-sm text-gray-400">
<p>Score color legend:</p>
<ul class="list-disc list-inside">
<li><span class="text-red-500 font-semibold">0.00 - 0.19</span>: Very Poor</li>
<li><span class="text-orange-500 font-semibold">0.20 - 0.39</span>: Poor</li>
<li><span class="text-yellow-500 font-semibold">0.40 - 0.59</span>: Fair</li>
<li><span class="text-lime-500 font-semibold">0.60 - 0.79</span>: Good</li>
<li><span class="text-green-400 font-semibold">0.80 - 0.99</span>: Very Good</li>
<li><span class="text-green-300 font-semibold">1.00+</span>: Excellent</li>
</ul>
</div>
<% else %>
<p class="text-gray-400">No runner results available yet.</p>
<% end %>
Expand Down
59 changes: 59 additions & 0 deletions app/views/leva/runner_results/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<% content_for :title, "Runner Result Details" %>
<div class="container mx-auto px-4 py-8 bg-gray-950 text-white">
<div class="mb-8">
<h1 class="text-3xl font-bold text-indigo-400 mb-2">Runner Result Details</h1>
<%= link_to "Back to Experiment", experiment_path(@experiment), class: "text-indigo-400 hover:underline" %>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
<div class="bg-gray-800 rounded-lg shadow-lg p-6">
<h2 class="text-2xl font-semibold text-indigo-300 mb-4">Details</h2>
<p class="text-gray-400">
<strong class="text-indigo-300">Dataset Record:</strong>
<%= link_to @runner_result.dataset_record.display_name, dataset_dataset_record_path(@runner_result.dataset_record.dataset, @runner_result.dataset_record), class: "text-indigo-400 hover:underline" %>
</p>
<p class="text-gray-400">
<strong class="text-indigo-300">Prompt:</strong>
<%= link_to "#{@runner_result.prompt.name} (v#{@runner_result.prompt_version})", prompt_path(@runner_result.prompt), class: "text-indigo-400 hover:underline" %>
</p>
<p class="text-gray-400"><strong class="text-indigo-300">Created At:</strong> <%= @runner_result.created_at.strftime("%Y-%m-%d %H:%M:%S") %></p>
<%= link_to 'Run in Workbench', workbench_index_path(prompt_id: @runner_result.prompt_id, dataset_record_id: @runner_result.dataset_record_id, runner: @experiment.runner_class), class: 'mt-4 inline-block px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700 transition-colors duration-200' %>
</div>
<div class="bg-gray-800 rounded-lg shadow-lg p-6">
<h2 class="text-2xl font-semibold text-indigo-300 mb-4">Evaluation Results</h2>
<% if @runner_result.evaluation_results.any? %>
<div class="space-y-4">
<% @runner_result.evaluation_results.each do |eval_result| %>
<div class="bg-gray-700 rounded-lg p-4">
<h3 class="text-lg font-semibold text-indigo-200 mb-2"><%= eval_result.evaluator_class %></h3>
<% score = eval_result.score %>
<% color_class = case score
when 0...0.2 then 'text-red-500'
when 0.2...0.4 then 'text-orange-500'
when 0.4...0.6 then 'text-yellow-500'
when 0.6...0.8 then 'text-lime-500'
when 0.8...1.0 then 'text-green-400'
else 'text-green-300'
end %>
<p class="text-xl font-bold <%= color_class %>"><%= sprintf('%.2f', score) %></p>
</div>
<% end %>
</div>
<% else %>
<p class="text-gray-400">No evaluation results available.</p>
<% end %>
</div>
</div>
<div class="bg-gray-800 rounded-lg shadow-lg p-6 mb-8">
<h2 class="text-2xl font-semibold text-indigo-300 mb-4">Prediction and Actual Result</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<h3 class="text-xl font-semibold text-indigo-200 mb-2">Prediction</h3>
<pre class="bg-gray-700 p-4 rounded-lg mt-2 text-sm text-gray-300 whitespace-pre-wrap"><%= @runner_result.prediction %></pre>
</div>
<div>
<h3 class="text-xl font-semibold text-indigo-200 mb-2">Actual Result</h3>
<pre class="bg-gray-700 p-4 rounded-lg mt-2 text-sm text-gray-300 whitespace-pre-wrap"><%= @runner_result.actual_result %></pre>
</div>
</div>
</div>
</div>
13 changes: 11 additions & 2 deletions app/views/leva/workbench/_results_section.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@
<% if @dataset_record %>
<% evaluation_result = @dataset_record.evaluation_results.for_evaluator(evaluator_class).last %>
<% if evaluation_result %>
<div class="text-sm text-gray-300">
Score: <%= evaluation_result.score %>
<% score = evaluation_result.score %>
<% color_class = case score
when 0...0.2 then 'text-red-500'
when 0.2...0.4 then 'text-orange-500'
when 0.4...0.6 then 'text-yellow-500'
when 0.6...0.8 then 'text-lime-500'
when 0.8...1.0 then 'text-green-400'
else 'text-green-300'
end %>
<div class="text-sm <%= color_class %> font-semibold">
Score: <%= sprintf('%.2f', score) %>
</div>
<% else %>
<div class="text-sm text-gray-500">
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
resources :datasets do
resources :dataset_records, path: 'records', only: [:index, :show]
end
resources :experiments, except: [:destroy]
resources :experiments, except: [:destroy] do
resources :runner_results, only: [:show]
end
resources :prompts
resources :workbench, only: [:index, :new, :create, :edit, :update] do
collection do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddActualResultToLevaDatasetRecords < ActiveRecord::Migration[7.2]
def change
add_column :leva_runner_results, :actual_result, :text
end
end
3 changes: 2 additions & 1 deletion lib/leva.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def execute_and_store(experiment, dataset_record, prompt)
experiment: experiment,
dataset_record: dataset_record,
prompt: prompt,
prediction: result
prediction: result,
actual_result: dataset_record.actual_result
)
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_08_21_183153) do
ActiveRecord::Schema[7.2].define(version: 2024_08_21_191713) do
create_table "leva_dataset_records", force: :cascade do |t|
t.integer "dataset_id", null: false
t.string "recordable_type", null: false
t.integer "recordable_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "actual_result"
t.index ["dataset_id"], name: "index_leva_dataset_records_on_dataset_id"
t.index ["recordable_type", "recordable_id"], name: "index_leva_dataset_records_on_recordable"
end
Expand Down Expand Up @@ -74,6 +75,7 @@
t.datetime "updated_at", null: false
t.integer "prompt_version"
t.integer "prompt_id", null: false
t.text "actual_result"
t.index ["dataset_record_id"], name: "index_leva_runner_results_on_dataset_record_id"
t.index ["experiment_id"], name: "index_leva_runner_results_on_experiment_id"
t.index ["prompt_id"], name: "index_leva_runner_results_on_prompt_id"
Expand Down
1 change: 1 addition & 0 deletions test/models/leva/dataset_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: leva_dataset_records
#
# id :integer not null, primary key
# actual_result :text
# recordable_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
1 change: 1 addition & 0 deletions test/models/leva/runner_result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: leva_runner_results
#
# id :integer not null, primary key
# actual_result :text
# prediction :text
# prompt_version :integer
# created_at :datetime not null
Expand Down

0 comments on commit 7931e4e

Please sign in to comment.