Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Evidence::ActivatedLLMPrompt (#12592) #12597

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions services/QuillLMS/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
# id :integer not null, primary key
# account_type :string default("unknown")
# active :boolean default(FALSE)
# classcode :string
# email :string
# classcode :string(255)
# email :string(255)
# flags :string default([]), not null, is an Array
# flagset :string default("production"), not null
# ip_address :inet
# last_active :datetime
# last_sign_in :datetime
# name :string
# password_digest :string
# role :string default("user")
# name :string(255)
# password_digest :string(255)
# role :string(255) default("user")
# send_newsletter :boolean default(FALSE)
# signed_up_with_google :boolean default(FALSE)
# time_zone :string
# title :string
# token :string
# username :string
# created_at :datetime
# updated_at :datetime
# clever_id :string
# token :string(255)
# username :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# clever_id :string(255)
# google_id :string
# stripe_customer_id :string
#
Expand All @@ -50,10 +50,16 @@
# username_idx (username) USING gin
# users_to_tsvector_idx (to_tsvector('english'::regconfig, (name)::text)) USING gin
# users_to_tsvector_idx1 (to_tsvector('english'::regconfig, (email)::text)) USING gin
# users_to_tsvector_idx10 (to_tsvector('english'::regconfig, (username)::text)) USING gin
# users_to_tsvector_idx11 (to_tsvector('english'::regconfig, split_part((ip_address)::text, '/'::text, 1))) USING gin
# users_to_tsvector_idx2 (to_tsvector('english'::regconfig, (role)::text)) USING gin
# users_to_tsvector_idx3 (to_tsvector('english'::regconfig, (classcode)::text)) USING gin
# users_to_tsvector_idx4 (to_tsvector('english'::regconfig, (username)::text)) USING gin
# users_to_tsvector_idx5 (to_tsvector('english'::regconfig, split_part((ip_address)::text, '/'::text, 1))) USING gin
# users_to_tsvector_idx6 (to_tsvector('english'::regconfig, (name)::text)) USING gin
# users_to_tsvector_idx7 (to_tsvector('english'::regconfig, (email)::text)) USING gin
# users_to_tsvector_idx8 (to_tsvector('english'::regconfig, (role)::text)) USING gin
# users_to_tsvector_idx9 (to_tsvector('english'::regconfig, (classcode)::text)) USING gin
#

# rubocop:disable Metrics/ClassLength
Expand Down
1 change: 1 addition & 0 deletions services/QuillLMS/config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rails.autoloaders.each do |autoloader|
autoloader.inflector = Zeitwerk::Inflector.new
autoloader.inflector.inflect(
'activated_llm_prompt' => 'ActivatedLLMPrompt',
'auto_ml' => 'AutoML',
'build_llm_example_worker' => 'BuildLLMExampleWorker',
'concept_replacement_cms_worker' => 'ConceptReplacementCMSWorker',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# This migration comes from evidence (originally 20241122181141)
class CreateActivatedLLMPrompt < ActiveRecord::Migration[7.1]
def change
create_table :evidence_activated_llm_prompts do |t|
t.integer :prompt_id, null: false
t.integer :llm_prompt_id, null: false
t.boolean :active, null: false

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# This migration comes from evidence (originally 20241122195610)
class AddFavoriteToLLMPrompt < ActiveRecord::Migration[7.1]
def change
add_column :evidence_research_gen_ai_llm_prompts, :favorite, :boolean, default: false
end
end
67 changes: 59 additions & 8 deletions services/QuillLMS/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ CREATE FUNCTION public.timespent_question(act_sess integer, question character v
item timestamp;
BEGIN
SELECT created_at INTO as_created_at FROM activity_sessions WHERE id = act_sess;

-- backward compatibility block
IF as_created_at IS NULL OR as_created_at < timestamp '2013-08-25 00:00:00.000000' THEN
SELECT SUM(
Expand All @@ -160,11 +160,11 @@ CREATE FUNCTION public.timespent_question(act_sess integer, question character v
'epoch' FROM (activity_sessions.completed_at - activity_sessions.started_at)
)
END) INTO time_spent FROM activity_sessions WHERE id = act_sess AND state='finished';

RETURN COALESCE(time_spent,0);
END IF;


first_item := NULL;
last_item := NULL;
max_item := NULL;
Expand All @@ -188,11 +188,11 @@ CREATE FUNCTION public.timespent_question(act_sess integer, question character v

END IF;
END LOOP;

IF max_item IS NOT NULL AND first_item IS NOT NULL THEN
time_spent := time_spent + EXTRACT( EPOCH FROM max_item - first_item );
END IF;

RETURN time_spent;
END;
$$;
Expand All @@ -207,7 +207,7 @@ CREATE FUNCTION public.timespent_student(student integer) RETURNS bigint
AS $$
SELECT COALESCE(SUM(time_spent),0) FROM (
SELECT id,timespent_activity_session(id) AS time_spent FROM activity_sessions
WHERE activity_sessions.user_id = student
WHERE activity_sessions.user_id = student
GROUP BY id) as as_ids;

$$;
Expand Down Expand Up @@ -2677,6 +2677,39 @@ CREATE SEQUENCE public.english_texts_id_seq
ALTER SEQUENCE public.english_texts_id_seq OWNED BY public.english_texts.id;


--
-- Name: evidence_activated_llm_prompts; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.evidence_activated_llm_prompts (
id bigint NOT NULL,
prompt_id integer NOT NULL,
llm_prompt_id integer NOT NULL,
active boolean NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


--
-- Name: evidence_activated_llm_prompts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.evidence_activated_llm_prompts_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: evidence_activated_llm_prompts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.evidence_activated_llm_prompts_id_seq OWNED BY public.evidence_activated_llm_prompts.id;


--
-- Name: evidence_activity_healths; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3432,7 +3465,8 @@ CREATE TABLE public.evidence_research_gen_ai_llm_prompts (
suboptimal_guidelines_count integer NOT NULL,
optimal_examples_count integer NOT NULL,
suboptimal_examples_count integer NOT NULL,
locked boolean NOT NULL
locked boolean NOT NULL,
favorite boolean DEFAULT false
);


Expand Down Expand Up @@ -7042,6 +7076,13 @@ ALTER TABLE ONLY public.email_subscriptions ALTER COLUMN id SET DEFAULT nextval(
ALTER TABLE ONLY public.english_texts ALTER COLUMN id SET DEFAULT nextval('public.english_texts_id_seq'::regclass);


--
-- Name: evidence_activated_llm_prompts id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.evidence_activated_llm_prompts ALTER COLUMN id SET DEFAULT nextval('public.evidence_activated_llm_prompts_id_seq'::regclass);


--
-- Name: evidence_activity_healths id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -8371,6 +8412,14 @@ ALTER TABLE ONLY public.english_texts
ADD CONSTRAINT english_texts_pkey PRIMARY KEY (id);


--
-- Name: evidence_activated_llm_prompts evidence_activated_llm_prompts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.evidence_activated_llm_prompts
ADD CONSTRAINT evidence_activated_llm_prompts_pkey PRIMARY KEY (id);


--
-- Name: evidence_activity_healths evidence_activity_healths_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -11825,6 +11874,8 @@ ALTER TABLE ONLY public.learn_worlds_account_course_events
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20241122195738'),
('20241122185259'),
('20241122002716'),
('20241122002645'),
('20241121214425'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: evidence_activated_llm_prompts
#
# id :bigint not null, primary key
# active :boolean not null
# created_at :datetime not null
# updated_at :datetime not null
# llm_prompt_id :integer not null
# prompt_id :integer not null
#
module Evidence
class ActivatedLLMPrompt < ApplicationRecord
belongs_to :llm_prompt, class_name: 'Evidence::Research::GenAI::LLMPrompt'
belongs_to :prompt

validates :llm_prompt, presence: true
validates :prompt, presence: true

attr_readonly :llm_prompt_id, :prompt_id, :active

scope :active, -> { where(active: true) }

validates :llm_prompt_id, uniqueness: { scope: :prompt_id }, if: :active?

before_save :deactivate_other_prompts, if: :active?

private def deactivate_other_prompts
self
.class
.where(prompt_id:)
.where.not(id:)
.update_all(active: false)
end
end
end
40 changes: 20 additions & 20 deletions services/QuillLMS/engines/evidence/app/models/evidence/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Prompt < ApplicationRecord
has_many :active_labels, through: :active_automl_rules, source: :label
has_many :misc_labels, -> { misc }, through: :active_automl_rules, source: :label
has_many :stem_vaults, inverse_of: :prompt, class_name: 'Evidence::Research::GenAI::StemVault'
has_many :activated_llm_prompts, dependent: :destroy
has_many :llm_prompts, through: :activated_llm_prompts

after_create :assign_universal_rules
before_validation :downcase_conjunction
Expand All @@ -68,28 +70,18 @@ def serializable_hash(options = nil)
))
end

def change_log_name
'Evidence Stem'
end

def url
activity.url
end

def conjunctions
[conjunction]
end

def change_log_name = 'Evidence Stem'
def url = activity.url
def conjunctions = [conjunction]
def label_names = labels.map(&:name)

def first_passage = activity.passages.first
def plagiarism_texts = plagiarism_rule&.plagiarism_texts&.map(&:text) || []

def plagiarism_texts
plagiarism_rule&.plagiarism_texts&.map(&:text) || []
end

private def plagiarism_rule
rules&.find_by(rule_type: Evidence::Rule::TYPE_PLAGIARISM)
def active_llm_prompt
llm_prompts
.joins(:activated_llm_prompts)
.merge(Evidence::ActivatedLLMPrompt.active)
.first
end

def distinct_automl_highlight_arrays
Expand Down Expand Up @@ -120,9 +112,17 @@ def example_sets(optimal: true, limit: 2, offset: SUBOPTIMAL_SAMPLE_COUNT)

def optimal_label_feedback
# we can just grab the first feedback here because all optimal feedback text strings will be the same for any given prompt
rules.where(optimal: true, rule_type: Evidence::Rule::TYPE_AUTOML).joins('JOIN comprehension_feedbacks ON comprehension_feedbacks.rule_id = comprehension_rules.id').first&.feedbacks&.first&.text
rules
.where(optimal: true, rule_type: Evidence::Rule::TYPE_AUTOML)
.joins('JOIN comprehension_feedbacks ON comprehension_feedbacks.rule_id = comprehension_rules.id')
.first
&.feedbacks
&.first
&.text
end

private def plagiarism_rule = rules&.find_by(rule_type: Evidence::Rule::TYPE_PLAGIARISM)

private def downcase_conjunction
self.conjunction = conjunction&.downcase
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: evidence_research_gen_ai_llm_prompts
#
# id :bigint not null, primary key
# favorite :boolean default(FALSE)
# locked :boolean not null
# optimal_examples_count :integer not null
# optimal_guidelines_count :integer not null
Expand All @@ -30,6 +31,7 @@ class LLMPrompt < ApplicationRecord

has_many :llm_prompt_guidelines, dependent: :destroy
has_many :guidelines, through: :llm_prompt_guidelines
has_many :activated_llm_prompts, class_name: 'Evidence::ActivatedLLMPrompt', dependent: :destroy

validates :prompt, presence: true
validates :llm_prompt_template_id, presence: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rails.autoloaders.each do |autoloader|
autoloader.inflector = Zeitwerk::Inflector.new
autoloader.inflector.inflect(
'activated_llm_prompt' => 'ActivatedLLMPrompt',
'auto_ml' => 'AutoML',
'build_llm_example_worker' => 'BuildLLMExampleWorker',
'gen_ai' => 'GenAI',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateActivatedLLMPrompt < ActiveRecord::Migration[7.1]
def change
create_table :evidence_activated_llm_prompts do |t|
t.integer :prompt_id, null: false
t.integer :llm_prompt_id, null: false
t.boolean :active, null: false

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddFavoriteToLLMPrompt < ActiveRecord::Migration[7.1]
def change
add_column :evidence_research_gen_ai_llm_prompts, :favorite, :boolean, default: false
end
end
Loading