This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 40
FEATURE: Automatically backfill regular summaries. #892
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ::Jobs | ||
| class SummariesBackfill < ::Jobs::Scheduled | ||
| every 5.minutes | ||
| cluster_concurrency 1 | ||
|
|
||
| def execute(_args) | ||
| return if !SiteSetting.discourse_ai_enabled | ||
| return if !SiteSetting.ai_summarization_enabled | ||
| return if SiteSetting.ai_summary_backfill_maximum_topics_per_hour.zero? | ||
|
|
||
| # Split budget in 12 intervals, but make sure is at least one. | ||
| limit_per_job = [SiteSetting.ai_summary_backfill_maximum_topics_per_hour, 12].max / 12 | ||
| budget = [current_budget, limit_per_job].min | ||
|
|
||
| backfill_candidates | ||
| .limit(budget) | ||
| .each do |topic| | ||
| DiscourseAi::Summarization.topic_summary(topic).force_summarize(Discourse.system_user) | ||
| end | ||
| end | ||
|
|
||
| def backfill_candidates | ||
| Topic | ||
| .where("topics.word_count >= ?", SiteSetting.ai_summary_backfill_minimum_word_count) | ||
| .joins( | ||
| "LEFT OUTER JOIN ai_summaries ais ON topics.id = ais.target_id AND ais.target_type = 'Topic'", | ||
| ) | ||
| .where( | ||
| "ais.id IS NULL OR UPPER(ais.content_range) < topics.highest_post_number + 1", | ||
| ) # (1..1) gets stored ad (1..2). | ||
| .order("ais.created_at DESC NULLS FIRST, topics.last_posted_at DESC") | ||
| end | ||
|
|
||
| def current_budget | ||
| base_budget = SiteSetting.ai_summary_backfill_maximum_topics_per_hour | ||
| used_budget = AiSummary.complete.system.where("created_at > ?", 1.hour.ago).count | ||
|
|
||
| base_budget - used_budget | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # frozen_string_literal: true | ||
| class TrackAiSummaryOrigin < ActiveRecord::Migration[7.1] | ||
| def change | ||
| add_column :ai_summaries, :origin, :integer | ||
| end | ||
| end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20241031180044_set_origin_for_existing_ai_summaries.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # frozen_string_literal: true | ||
| class SetOriginForExistingAiSummaries < ActiveRecord::Migration[7.1] | ||
| def up | ||
| DB.exec <<~SQL | ||
| UPDATE ai_summaries | ||
| SET origin = CASE WHEN summary_type = 0 THEN 0 ELSE 1 END | ||
| WHERE origin IS NULL | ||
| SQL | ||
| end | ||
|
|
||
| def down | ||
| raise ActiveRecord::IrreversibleMigration | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Jobs::SummariesBackfill do | ||
| fab!(:topic) { Fabricate(:topic, word_count: 200, highest_post_number: 2) } | ||
| let(:limit) { 24 } # guarantee two summaries per batch | ||
|
|
||
| before do | ||
| assign_fake_provider_to(:ai_summarization_model) | ||
| SiteSetting.ai_summarization_enabled = true | ||
| SiteSetting.ai_summary_backfill_maximum_topics_per_hour = limit | ||
| end | ||
|
|
||
| describe "#current_budget" do | ||
| context "when no summary has been backfilled yet" do | ||
| it "returns the full budget" do | ||
| expect(subject.current_budget).to eq(limit) | ||
| end | ||
|
|
||
| it "ignores summaries generated by users" do | ||
| Fabricate(:ai_summary, target: topic, origin: AiSummary.origins[:human]) | ||
|
|
||
| expect(subject.current_budget).to eq(limit) | ||
| end | ||
|
|
||
| it "only accounts for complete type summaries" do | ||
| Fabricate(:topic_ai_gist, target: topic, origin: AiSummary.origins[:human]) | ||
|
|
||
| expect(subject.current_budget).to eq(limit) | ||
| end | ||
| end | ||
|
|
||
| context "when we already backfilled stuff" do | ||
| fab!(:backfilled_summary) do | ||
| Fabricate(:ai_summary, target: topic, origin: AiSummary.origins[:system]) | ||
| end | ||
|
|
||
| context "if it was within the budget window" do | ||
| it "reduces our budget" do | ||
| expect(subject.current_budget).to eq(limit - 1) | ||
| end | ||
| end | ||
|
|
||
| context "if it wasn't within the budget window" do | ||
| before { freeze_time(2.hours.from_now) } | ||
|
|
||
| it "returns the full budget" do | ||
| freeze_time(2.hours.from_now) | ||
|
|
||
| expect(subject.current_budget).to eq(limit) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#backfill_candidates" do | ||
| it "only selects posts with enough words" do | ||
| topic.update!(word_count: 100) | ||
|
|
||
| expect(subject.backfill_candidates).to be_empty | ||
| end | ||
|
|
||
| it "ignores up to date summaries" do | ||
| Fabricate(:ai_summary, target: topic, content_range: (1..2)) | ||
|
|
||
| expect(subject.backfill_candidates).to be_empty | ||
| end | ||
|
|
||
| it "orders candidates by topic#last_posted_at" do | ||
| topic.update!(last_posted_at: 1.minute.ago) | ||
| topic_2 = Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago) | ||
|
|
||
| expect(subject.backfill_candidates.map(&:id)).to contain_exactly(topic.id, topic_2.id) | ||
| end | ||
|
|
||
| it "prioritizes topics without summaries" do | ||
| topic_2 = | ||
| Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago, highest_post_number: 1) | ||
| topic.update!(last_posted_at: 1.minute.ago) | ||
| Fabricate(:ai_summary, target: topic, content_range: (1..1)) | ||
|
|
||
| expect(subject.backfill_candidates.map(&:id)).to contain_exactly(topic_2.id, topic.id) | ||
| end | ||
| end | ||
|
|
||
| describe "#execute" do | ||
| it "backfills a batch" do | ||
| topic_2 = | ||
| Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago, highest_post_number: 1) | ||
| topic.update!(last_posted_at: 1.minute.ago) | ||
| Fabricate(:ai_summary, target: topic, created_at: 3.hours.ago, content_range: (1..1)) | ||
|
|
||
| summary_1 = "Summary of topic_2" | ||
| summary_2 = "Summary of topic" | ||
|
|
||
| DiscourseAi::Completions::Llm.with_prepared_responses([summary_1, summary_2]) do | ||
| subject.execute({}) | ||
| end | ||
|
|
||
| expect(AiSummary.find_by(target: topic_2).summarized_text).to eq(summary_1) | ||
| expect(AiSummary.find_by(target: topic).summarized_text).to eq(summary_2) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.