From f44743a245441ca19a43c6fab12e6b4f0ba56b64 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Sun, 17 Mar 2024 18:44:13 -0400 Subject: [PATCH 1/7] copy pasta error --- test/integration/import_book_job.rb | 18 ------------------ test/integration/show.json.jbuilder | 3 --- 2 files changed, 21 deletions(-) delete mode 100644 test/integration/import_book_job.rb delete mode 100644 test/integration/show.json.jbuilder diff --git a/test/integration/import_book_job.rb b/test/integration/import_book_job.rb deleted file mode 100644 index 30b4c9f6d..000000000 --- a/test/integration/import_book_job.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -class ImportBookJob < ApplicationJob - queue_as :default - - def perform book - options = {} - - # Read and parse the JSON data - json_data = JSON.parse(book.import_file.download) - - service = ::BookImporter.new book, json_data.deep_symbolize_keys, options - - service.import - book.import_file.purge - book.save - end -end diff --git a/test/integration/show.json.jbuilder b/test/integration/show.json.jbuilder deleted file mode 100644 index d0e4b27d4..000000000 --- a/test/integration/show.json.jbuilder +++ /dev/null @@ -1,3 +0,0 @@ -# frozen_string_literal: true - -json.partial! 'book', book: @book From eecfdf68c6e2633ad86ae60500535811d9aec7dc Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Sun, 17 Mar 2024 22:31:13 -0400 Subject: [PATCH 2/7] have a bare bones judge judy --- .../api/v1/bulk_ratings_controller.rb | 2 +- .../api/v1/queries/ratings_controller.rb | 2 +- app/controllers/books_controller.rb | 14 +-- app/jobs/populate_book_job.rb | 4 +- app/jobs/run_judge_judy_job.rb | 22 ++++ app/models/book.rb | 4 + app/services/ratings_manager.rb | 6 +- .../20240317233035_add_ai_judge_to_book.rb | 5 + db/schema.rb | 3 +- test/fixtures/books.yml | 1 + .../case_to_book_to_case_flow_test.rb | 2 - test/integration/judge_judy_flow_test.rb | 119 ++++++++++++++++++ test/models/book_test.rb | 1 + 13 files changed, 168 insertions(+), 17 deletions(-) create mode 100644 app/jobs/run_judge_judy_job.rb create mode 100644 db/migrate/20240317233035_add_ai_judge_to_book.rb create mode 100644 test/integration/judge_judy_flow_test.rb diff --git a/app/controllers/api/v1/bulk_ratings_controller.rb b/app/controllers/api/v1/bulk_ratings_controller.rb index c5a3ca491..e12fa1694 100644 --- a/app/controllers/api/v1/bulk_ratings_controller.rb +++ b/app/controllers/api/v1/bulk_ratings_controller.rb @@ -15,7 +15,7 @@ def update if params[:doc_ids].present? params[:doc_ids].each do |doc_id| rating = @query.ratings.find_or_create_by doc_id: doc_id - rating.user = @current_user + # rating.user = @current_user # rating.update rating_params rating.rating = pluck_out_just_rating_param diff --git a/app/controllers/api/v1/queries/ratings_controller.rb b/app/controllers/api/v1/queries/ratings_controller.rb index 1567222fb..8ba0ff20e 100644 --- a/app/controllers/api/v1/queries/ratings_controller.rb +++ b/app/controllers/api/v1/queries/ratings_controller.rb @@ -8,7 +8,7 @@ class RatingsController < Api::V1::Queries::ApplicationController def update @rating = @query.ratings.find_or_create_by doc_id: @doc_id - @rating.user = @current_user + # @rating.user = @current_user if @rating.update rating_params Analytics::Tracker.track_rating_created_event current_user, @rating diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index a872fc15c..0566eaaa5 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -167,7 +167,6 @@ def combine # rubocop:enable Metrics/PerceivedComplexity # rubocop:enable Layout/LineLength - # rubocop:disable Metrics/MethodLength def assign_anonymous # assignee = @book.team.members.find_by(id: params[:assignee_id]) assignee = User.find_by(id: params[:assignee_id]) @@ -181,17 +180,16 @@ def assign_anonymous judgement.save! end end - @book.cases.each do |kase| - kase.ratings.where(user: nil).find_each do |rating| - rating.user = assignee - rating.save! - end - end + # @book.cases.each do |kase| + # kase.ratings.where(user: nil).find_each do |rating| + # rating.user = assignee + # rating.save! + # end + # end UpdateCaseJob.perform_later @book redirect_to book_path(@book), :notice => "Assigned #{assignee.fullname} to ratings and judgements." end - # rubocop:enable Metrics/MethodLength def delete_ratings_by_assignee judgements_to_delete = @book.judgements.where(user: @user) diff --git a/app/jobs/populate_book_job.rb b/app/jobs/populate_book_job.rb index 17e81a784..1d85e1b0b 100644 --- a/app/jobs/populate_book_job.rb +++ b/app/jobs/populate_book_job.rb @@ -37,7 +37,7 @@ def perform user, book, kase # we are smart and just look up the correct user id from rating.user_id via the database, no API data needed. judgement = query_doc_pair.judgements.find_or_create_by user_id: rating.user_id judgement.rating = pair[:rating] - judgement.user = rating.user + judgement.user = User.find(pair[:user_id]) # rating.user judgement.save! end @@ -46,6 +46,8 @@ def perform user, book, kase book.populate_file.purge book.save + RunJudgeJudyJob.perform_later book + Analytics::Tracker.track_query_doc_pairs_bulk_updated_event user, book, is_book_empty end # rubocop:enable Security/MarshalLoad diff --git a/app/jobs/run_judge_judy_job.rb b/app/jobs/run_judge_judy_job.rb new file mode 100644 index 000000000..7f85dc203 --- /dev/null +++ b/app/jobs/run_judge_judy_job.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class RunJudgeJudyJob < ApplicationJob + queue_as :default + sidekiq_options log_level: :warn + + def perform book + judge = book.ai_judge + + loop do + query_doc_pair = SelectionStrategy.random_query_doc_based_on_strategy(book, judge) + break if query_doc_pair.nil? + + judgement = Judgement.new(query_doc_pair: query_doc_pair, user: judge, updated_at: Time.zone.now) + judgement.rating = 4 + judgement.explanation = "Eric writing code. Judge is #{judge.email}" + judgement.save! + # UpdateCaseRatingsJob.perform_later query_doc_pair + UpdateCaseJob.perform_later book + end + end +end diff --git a/app/models/book.rb b/app/models/book.rb index 455b8ee8c..edd61ba9f 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -10,6 +10,7 @@ # support_implicit_judgements :boolean # created_at :datetime not null # updated_at :datetime not null +# ai_judge_id :integer # owner_id :integer # scorer_id :integer # selection_strategy_id :bigint not null @@ -32,6 +33,9 @@ class Book < ApplicationRecord belongs_to :owner, class_name: 'User', optional: true + belongs_to :ai_judge, + class_name: 'User', optional: true + belongs_to :selection_strategy belongs_to :scorer has_many :query_doc_pairs, dependent: :destroy, autosave: true diff --git a/app/services/ratings_manager.rb b/app/services/ratings_manager.rb index 978309816..0f07cec8a 100644 --- a/app/services/ratings_manager.rb +++ b/app/services/ratings_manager.rb @@ -30,7 +30,6 @@ def sync_ratings_for_case kase # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/PerceivedComplexity - # rubocop:disable Metrics/CyclomaticComplexity def sync_judgements_to_ratings kase, query_doc_pair query = Query.find_or_initialize_by(case: kase, query_text: query_doc_pair.query_text) @@ -53,7 +52,9 @@ def sync_judgements_to_ratings kase, query_doc_pair summed_rating = query_doc_pair.judgements.rateable.sum(&:rating) rating = Rating.find_or_initialize_by(query: query, doc_id: query_doc_pair.doc_id) - rating.user = query_doc_pair.judgements.last.user if rating.user.nil? + # I think the whole rating having a user was bogus becasue we have multiple raters + # on the book side, which kind of messes this us. + # rating.user = query_doc_pair.judgements.last.user if rating.user.nil? rating.rating = if @book.support_implicit_judgements? summed_rating / count_of_judgements else @@ -70,5 +71,4 @@ def sync_judgements_to_ratings kase, query_doc_pair # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/PerceivedComplexity - # rubocop:enable Metrics/CyclomaticComplexity end diff --git a/db/migrate/20240317233035_add_ai_judge_to_book.rb b/db/migrate/20240317233035_add_ai_judge_to_book.rb new file mode 100644 index 000000000..cec3e54b9 --- /dev/null +++ b/db/migrate/20240317233035_add_ai_judge_to_book.rb @@ -0,0 +1,5 @@ +class AddAiJudgeToBook < ActiveRecord::Migration[7.1] + def change + add_column :books, :ai_judge_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 54f7e4251..d61f06dab 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_08_204637) do +ActiveRecord::Schema[7.1].define(version: 2024_03_17_233035) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_bin", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -95,6 +95,7 @@ t.boolean "support_implicit_judgements" t.boolean "show_rank", default: false t.integer "owner_id" + t.integer "ai_judge_id" t.index ["selection_strategy_id"], name: "index_books_on_selection_strategy_id" end diff --git a/test/fixtures/books.yml b/test/fixtures/books.yml index 1fa38b6c5..0409ff6f1 100644 --- a/test/fixtures/books.yml +++ b/test/fixtures/books.yml @@ -8,6 +8,7 @@ # support_implicit_judgements :boolean # created_at :datetime not null # updated_at :datetime not null +# ai_judge_id :integer # owner_id :integer # scorer_id :integer # selection_strategy_id :bigint not null diff --git a/test/integration/case_to_book_to_case_flow_test.rb b/test/integration/case_to_book_to_case_flow_test.rb index 26ebbf276..ca054a28c 100644 --- a/test/integration/case_to_book_to_case_flow_test.rb +++ b/test/integration/case_to_book_to_case_flow_test.rb @@ -52,8 +52,6 @@ class CaseToBookToCaseFlowTest < ActionDispatch::IntegrationTest put api_book_case_refresh_url book, new_case, params: { create_missing_queries: true } - response.parsed_body - new_case.reload assert_not_empty new_case.queries diff --git a/test/integration/judge_judy_flow_test.rb b/test/integration/judge_judy_flow_test.rb new file mode 100644 index 000000000..5e3c94c4a --- /dev/null +++ b/test/integration/judge_judy_flow_test.rb @@ -0,0 +1,119 @@ +# frozen_string_literal: true + +require 'test_helper' + +class JudgeJudyFlowTest < ActionDispatch::IntegrationTest + let(:book) { books(:james_bond_movies) } + let(:acase) { cases(:shared_with_team) } + let(:team) { teams(:shared) } + let(:user) { users(:random) } + + test 'Demonstrate how to work with Judge Judy' do + post users_login_url params: { user: { email: user.email, password: 'password' }, format: :json } + + assert_empty(acase.queries) + assert_empty(acase.ratings) + + assert_includes user.teams, team + assert_includes acase.teams, team + assert_includes book.teams, team + + # Link to a Case and populate it. + put api_case_url acase, params: { case_id: acase.id, case: { book_id: book.id }, format: :json } + assert_response :ok + + acase.reload + + assert_equal acase.book, book + + # Set up Judge Judy. Give her a prompt (and an OPENAI KEY) + + judge_judy = User.new name: 'judge judy', email: 'judgejudy@quepid.com', password: 'password' + + # Add her to the team + judge_judy.teams << team + judge_judy.save! + # Add her to the book as the judge. Like owner_id but it's ai_judge_id + book.ai_judge = judge_judy + book.save! + + # Wait for her to judge + perform_enqueued_jobs do + # patch :update, params: data + RunJudgeJudyJob.perform_later(book) + end + + book.reload + + # make sure every query_doc_pair has a rating by Judge Judy. + assert_equal book.query_doc_pairs.size, book.judgements.where(user: judge_judy).size + + # Add a query to the case --> and then rate it to create rating. + post api_case_queries_url acase, + params: { case_id: acase.id, + query: { query_text: 'what is the average velocity of a swallow?' } } + assert_response :ok + response_json = response.parsed_body + query = Query.find(response_json['query']['query_id']) + + rating = { + doc_id: 'african_swallow', + rating: 1, + } + + put api_case_query_ratings_url acase, query, params: { case_id: acase.id, query_id: query.id, rating: rating } + assert_response :ok + puts response.parsed_body + rating = Rating.find(response.parsed_body['id']) + + # make sure Judge Judy didn't get involved' + # we don' want users tracked at the case rating level + assert_nil rating.user + # assert_equal rating.user, user + assert_equal rating.rating, 1 + + # Here is the big question, if I rate something in the main UI, how is that handled? + # I worry that if a case rating creates/updates a book judgement, and that triggers a + # update the case that we get weird round tripping. Really want ratings to be book --> Case + + data = { + book_id: book.id, + case_id: acase.id, + query_doc_pairs: [ + { + query_text: query.query_text, + doc_id: rating.doc_id, + rating: rating.rating, + user_id: user.id, + position: 0, + document_fields: { + title: 'Monty Python Quest for Holy Grail', + year: '1987', + }, + } + ], + } + + perform_enqueued_jobs do + assert_difference 'book.query_doc_pairs.count' do + put api_book_populate_url book, params: data + assert_response :no_content + end + end + book.reload + assert_equal book.query_doc_pairs.size, book.judgements.where(user: judge_judy).size + + query_doc_pairs = book.query_doc_pairs.where(query_text: query.query_text, doc_id: rating.doc_id) + assert_equal query_doc_pairs.count, 1 + + judgements = query_doc_pairs.first.judgements + assert_equal 2, judgements.count + + rating.reload + + # See that the original case rating has changed to be the average of + # Judge Judy with a 4 and User with 1. (1+4)/2 = 2.5 rounded to 3.0 + assert_equal rating.rating, 3.0 + assert_nil rating.user # I think that having a "last user who rated" wasn't good idea.' + end +end diff --git a/test/models/book_test.rb b/test/models/book_test.rb index bacfcc6f5..0ff6cb002 100644 --- a/test/models/book_test.rb +++ b/test/models/book_test.rb @@ -10,6 +10,7 @@ # support_implicit_judgements :boolean # created_at :datetime not null # updated_at :datetime not null +# ai_judge_id :integer # owner_id :integer # scorer_id :integer # selection_strategy_id :bigint not null From 5b24171893fa680be693ed46c13d5bafc06f93f9 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Mon, 18 Mar 2024 06:50:03 -0700 Subject: [PATCH 3/7] allow ratings from case to make it to a book... --- .../api/v1/bulk_ratings_controller.rb | 2 +- .../api/v1/queries/ratings_controller.rb | 1 + app/jobs/judgement_from_rating_job.rb | 16 ++++++++++++++++ app/jobs/run_judge_judy_job.rb | 19 ++++++++++--------- test/integration/judge_judy_flow_test.rb | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 app/jobs/judgement_from_rating_job.rb diff --git a/app/controllers/api/v1/bulk_ratings_controller.rb b/app/controllers/api/v1/bulk_ratings_controller.rb index e12fa1694..ca2cd483c 100644 --- a/app/controllers/api/v1/bulk_ratings_controller.rb +++ b/app/controllers/api/v1/bulk_ratings_controller.rb @@ -15,11 +15,11 @@ def update if params[:doc_ids].present? params[:doc_ids].each do |doc_id| rating = @query.ratings.find_or_create_by doc_id: doc_id - # rating.user = @current_user # rating.update rating_params rating.rating = pluck_out_just_rating_param rating.save + JudgementFromRatingJob.perform_later current_user, rating end end diff --git a/app/controllers/api/v1/queries/ratings_controller.rb b/app/controllers/api/v1/queries/ratings_controller.rb index 8ba0ff20e..114643244 100644 --- a/app/controllers/api/v1/queries/ratings_controller.rb +++ b/app/controllers/api/v1/queries/ratings_controller.rb @@ -12,6 +12,7 @@ def update if @rating.update rating_params Analytics::Tracker.track_rating_created_event current_user, @rating + JudgementFromRatingJob.perform_later current_user, @rating respond_with @rating else render json: @rating.errors, status: :bad_request diff --git a/app/jobs/judgement_from_rating_job.rb b/app/jobs/judgement_from_rating_job.rb new file mode 100644 index 000000000..fac310007 --- /dev/null +++ b/app/jobs/judgement_from_rating_job.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class JudgementFromRatingJob < ApplicationJob + queue_as :default + + def perform user, rating + query = rating.query + book = query.case.book + if book + query_doc_pair = QueryDocPair.find_or_create_by query_text: rating.query_text, doc_id: rating.doc_id + judgement = query_doc_pair.judgements.find_or_initialize_by(user: user) + judgement.rating = rating.rating + judgment.save! + end + end +end diff --git a/app/jobs/run_judge_judy_job.rb b/app/jobs/run_judge_judy_job.rb index 7f85dc203..cd8dfa813 100644 --- a/app/jobs/run_judge_judy_job.rb +++ b/app/jobs/run_judge_judy_job.rb @@ -6,17 +6,18 @@ class RunJudgeJudyJob < ApplicationJob def perform book judge = book.ai_judge + if judge # only run the job if we have a judge defined + loop do + query_doc_pair = SelectionStrategy.random_query_doc_based_on_strategy(book, judge) + break if query_doc_pair.nil? - loop do - query_doc_pair = SelectionStrategy.random_query_doc_based_on_strategy(book, judge) - break if query_doc_pair.nil? + judgement = Judgement.new(query_doc_pair: query_doc_pair, user: judge, updated_at: Time.zone.now) + judgement.rating = 4 + judgement.explanation = "Eric writing code. Judge is #{judge.email}" + judgement.save! - judgement = Judgement.new(query_doc_pair: query_doc_pair, user: judge, updated_at: Time.zone.now) - judgement.rating = 4 - judgement.explanation = "Eric writing code. Judge is #{judge.email}" - judgement.save! - # UpdateCaseRatingsJob.perform_later query_doc_pair - UpdateCaseJob.perform_later book + UpdateCaseJob.perform_later book + end end end end diff --git a/test/integration/judge_judy_flow_test.rb b/test/integration/judge_judy_flow_test.rb index 5e3c94c4a..6bc7da2c8 100644 --- a/test/integration/judge_judy_flow_test.rb +++ b/test/integration/judge_judy_flow_test.rb @@ -18,7 +18,7 @@ class JudgeJudyFlowTest < ActionDispatch::IntegrationTest assert_includes acase.teams, team assert_includes book.teams, team - # Link to a Case and populate it. + # Link to a Case and Book together and populate it. put api_case_url acase, params: { case_id: acase.id, case: { book_id: book.id }, format: :json } assert_response :ok From 1f3ae61131ee908246264dc37c8db50c884ac897 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Mon, 18 Mar 2024 07:16:55 -0700 Subject: [PATCH 4/7] testing of rating in a case ending up in associated book! --- app/controllers/books_controller.rb | 5 +---- app/jobs/judgement_from_rating_job.rb | 7 +++++-- app/views/books/show.html.erb | 5 ----- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 0566eaaa5..f26231929 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -24,10 +24,7 @@ def index # rubocop:disable Metrics/MethodLength def show @count_of_anonymous_book_judgements = @book.judgements.where(user: nil).count - @count_of_anonymous_case_judgements = 0 - @book.cases.each do |kase| - @count_of_anonymous_case_judgements += kase.ratings.where(user: nil).count - end + @moar_judgements_needed = @book.judgements.where(user: current_user).count < @book.query_doc_pairs.count @cases = @book.cases diff --git a/app/jobs/judgement_from_rating_job.rb b/app/jobs/judgement_from_rating_job.rb index fac310007..55e512e3a 100644 --- a/app/jobs/judgement_from_rating_job.rb +++ b/app/jobs/judgement_from_rating_job.rb @@ -7,10 +7,13 @@ def perform user, rating query = rating.query book = query.case.book if book - query_doc_pair = QueryDocPair.find_or_create_by query_text: rating.query_text, doc_id: rating.doc_id + query_doc_pair = book.query_doc_pairs.find_or_create_by query_text: rating.query.query_text, doc_id: rating.doc_id + judgement = query_doc_pair.judgements.find_or_initialize_by(user: user) judgement.rating = rating.rating - judgment.save! + judgement.save! + + RunJudgeJudyJob.perform_later book end end end diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 97cf1220b..9711a6dd6 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -54,11 +54,6 @@ This book consists of <%= @book.query_doc_pairs.count %> query document pairs an This book has <%= @count_of_anonymous_book_judgements %> anonymous judgements that could be mapped to a user. <% end %> -<% if @count_of_anonymous_case_judgements > 0 %> - -<% end %>

Teams: From 84f39f80d33d33a8a017a0feb70ba032926ab955 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Thu, 21 Mar 2024 09:02:58 -0400 Subject: [PATCH 5/7] making progress on integrating judge judy --- app/controllers/books_controller.rb | 12 ++++++++---- app/jobs/judgement_from_rating_job.rb | 2 +- app/models/user.rb | 3 +++ app/views/books/_form.html.erb | 10 ++++++++++ app/views/books/show.html.erb | 7 +++++++ config/routes.rb | 1 + .../20240318135613_add_judge_attributes_to_users.rb | 6 ++++++ db/schema.rb | 4 +++- test/fixtures/users.yml | 2 ++ test/models/user_test.rb | 2 ++ 10 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20240318135613_add_judge_attributes_to_users.rb diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index f26231929..15c677b22 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -5,11 +5,11 @@ class BooksController < ApplicationController before_action :set_book, only: [ :show, :edit, :update, :destroy, :combine, :assign_anonymous, :delete_ratings_by_assignee, :reset_unrateable, :reset_judge_later, :delete_query_doc_pairs_below_position, - :eric_steered_us_wrong ] + :eric_steered_us_wrong, :run_judge_judy ] before_action :check_book, only: [ :show, :edit, :update, :destroy, :combine, :assign_anonymous, :delete_ratings_by_assignee, :reset_unrateable, :reset_judge_later, :delete_query_doc_pairs_below_position, - :eric_steered_us_wrong ] + :eric_steered_us_wrong, :run_judge_judy ] before_action :find_user, only: [ :reset_unrateable, :reset_judge_later, :delete_ratings_by_assignee ] @@ -25,7 +25,6 @@ def index def show @count_of_anonymous_book_judgements = @book.judgements.where(user: nil).count - @moar_judgements_needed = @book.judgements.where(user: current_user).count < @book.query_doc_pairs.count @cases = @book.cases @leaderboard_data = [] @@ -158,6 +157,11 @@ def combine :alert => "Could not merge due to errors: #{@book.errors.full_messages.to_sentence}. #{query_doc_pair_count} query/doc pairs." end end + + def run_judge_judy + RunJudgeJudyJob.perform_later(@book) + redirect_to book_path(@book), :notice => "Started #{@book.ai_judge.fullname} judging query/doc pairs." + end # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/CyclomaticComplexity @@ -256,7 +260,7 @@ def find_user def book_params params_to_use = params.require(:book).permit(:scorer_id, :selection_strategy_id, :name, :support_implicit_judgements, - :show_rank, team_ids: []) + :show_rank, :ai_judge_id, team_ids: []) # Crafting a book[team_ids] parameter from the AngularJS side didn't work, so using top level parameter params_to_use[:team_ids] = params[:team_ids] if params[:team_ids] diff --git a/app/jobs/judgement_from_rating_job.rb b/app/jobs/judgement_from_rating_job.rb index 55e512e3a..0bf9a5d18 100644 --- a/app/jobs/judgement_from_rating_job.rb +++ b/app/jobs/judgement_from_rating_job.rb @@ -12,7 +12,7 @@ def perform user, rating judgement = query_doc_pair.judgements.find_or_initialize_by(user: user) judgement.rating = rating.rating judgement.save! - + RunJudgeJudyJob.perform_later book end end diff --git a/app/models/user.rb b/app/models/user.rb index ae69f87b8..859a5c91e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,8 +22,10 @@ # locked_at :datetime # name :string(255) # num_logins :integer +# openai_key :string(255) # password :string(120) # profile_pic :string(4000) +# prompt :string(4000) # reset_password_sent_at :datetime # reset_password_token :string(255) # stored_raw_invitation_token :string(255) @@ -198,6 +200,7 @@ def store_raw_invitation_token # Scopes # default_scope -> { includes(:permissions) } + scope :ai_judges, -> { where('`users`.`openai_key` IS NOT NULL') } def num_queries queries.count diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb index 3f8f4743c..eb0f4e85d 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/books/_form.html.erb @@ -39,6 +39,16 @@ <%= form.label :selection_strategy_id, class: 'form-label' %> <%= form.collection_select(:selection_strategy_id, SelectionStrategy.all, :id, :descriptive_name, { prompt: true }, required: true, class: 'form-control') %> + +

+ <%= form.label :ai_judge_id, class: 'form-label' %> + <% + ai_judges = book.teams.collect{|team| team.members.ai_judges }.flatten + %> + <%= form.collection_select(:ai_judge_id, ai_judges, :id, :name, { include_blank: true, prompt: true }, required: false, class: 'form-control') %> +
You can specify an AI powered Judge to evaluate this book. This list is derived from Judges created for your teams, so you must associate the book with a team first!
+
+
<%= form.label :show_rank, class: 'form-check-label' %> <%= form.check_box :show_rank, class: 'form-check-input' %> diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 9711a6dd6..2760fd2af 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -17,6 +17,13 @@ This book consists of <%= @book.query_doc_pairs.count %> query document pairs an <%= render 'judgements/moar_judgements_needed', book: @book %> +<% if @book.ai_judge %> + We have an AI Judge, <%= @book.ai_judge.fullname %> helping us. +
+ <%= button_to 'Run Judgment Process', run_judge_judy_book_path(@book), method: :patch %> +

+<% end %> + <% if @book.import_file.attached? %>