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: Generate topic gists for the hot topics list. #837
Merged
+455
−107
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f6cbd7
Display gists in the hot topics list
romanrizzi f1113ac
Adjust hot topics gist strategy and add a job to generate gists
romanrizzi 650a6e8
Replace setting with a configurable batch size
romanrizzi 6932262
Avoid loading summaries for other topic lists
romanrizzi 94b417c
Tweak gist prompt to focus on latest posts in the context of the OP
romanrizzi ecefc93
Remove serializer hack and rely on core change from discourse/discour…
romanrizzi 8d8c7cd
Update lib/summarization/strategies/hot_topic_gists.rb
romanrizzi 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,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ::Jobs | ||
| class HotTopicsGistBatch < ::Jobs::Base | ||
| def execute(args) | ||
| return if !SiteSetting.discourse_ai_enabled | ||
| return if !SiteSetting.ai_summarization_enabled | ||
| return if SiteSetting.ai_summarize_max_hot_topics_gists_per_batch.zero? | ||
|
|
||
| Topic | ||
| .joins("JOIN topic_hot_scores on topics.id = topic_hot_scores.topic_id") | ||
| .order("topic_hot_scores.score DESC") | ||
| .limit(SiteSetting.ai_summarize_max_hot_topics_gists_per_batch) | ||
| .each do |topic| | ||
| summarizer = DiscourseAi::Summarization.topic_gist(topic) | ||
| gist = summarizer.existing_summary | ||
|
|
||
| summarizer.delete_cached_summaries! if gist && gist.outdated | ||
|
|
||
| summarizer.summarize(Discourse.system_user) | ||
| end | ||
| end | ||
| end | ||
| end |
15 changes: 15 additions & 0 deletions
15
assets/javascripts/discourse/connectors/topic-list-main-link-bottom/ai-topic-gist.gjs
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,15 @@ | ||
| import Component from "@glimmer/component"; | ||
|
|
||
| export default class AiTopicGist extends Component { | ||
| static shouldRender(outletArgs) { | ||
| return outletArgs?.topic?.ai_topic_gist && !outletArgs.topic.excerpt; | ||
| } | ||
|
|
||
| <template> | ||
| <div class="ai-topic-gist"> | ||
| <div class="ai-topic-gist__text"> | ||
| {{@outletArgs.topic.ai_topic_gist}} | ||
| </div> | ||
| </div> | ||
| </template> | ||
| } |
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 |
|---|---|---|
|
|
@@ -215,3 +215,11 @@ | |
| opacity: 1; | ||
| } | ||
| } | ||
|
|
||
| .ai-topic-gist { | ||
| margin-top: 0.5em; | ||
|
|
||
| &__text { | ||
| font-size: var(--font-down-2); | ||
| } | ||
| } | ||
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
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,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module TopicExtensions | ||
| extend ActiveSupport::Concern | ||
|
|
||
| prepended { has_many :ai_summaries, as: :target } | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| Fabricator(:ai_summary) do | ||
| summarized_text "complete summary" | ||
| original_content_sha "123" | ||
| algorithm "test" | ||
| target { Fabricate(:topic) } | ||
| summary_type AiSummary.summary_types[:complete] | ||
| end | ||
|
|
||
| Fabricator(:topic_ai_gist, from: :ai_summary) do | ||
| summarized_text "gist" | ||
| summary_type AiSummary.summary_types[:gist] | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Jobs::HotTopicsGistBatch do | ||
| fab!(:topic_1) { Fabricate(:topic) } | ||
| fab!(:post_1) { Fabricate(:post, topic: topic_1, post_number: 1) } | ||
| fab!(:post_2) { Fabricate(:post, topic: topic_1, post_number: 2) } | ||
|
|
||
| before do | ||
| assign_fake_provider_to(:ai_summarization_model) | ||
| SiteSetting.ai_summarization_enabled = true | ||
| SiteSetting.ai_summarize_max_hot_topics_gists_per_batch = 100 | ||
| end | ||
|
|
||
| describe "#execute" do | ||
| context "when there is a topic with a hot score" do | ||
| before { TopicHotScore.create!(topic_id: topic_1.id, score: 0.1) } | ||
|
|
||
| it "does nothing if the plugin is disabled" do | ||
| SiteSetting.discourse_ai_enabled = false | ||
|
|
||
| subject.execute({}) | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist).to be_nil | ||
| end | ||
|
|
||
| it "does nothing if the summarization module is disabled" do | ||
| SiteSetting.ai_summarization_enabled = false | ||
|
|
||
| subject.execute({}) | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist).to be_nil | ||
| end | ||
|
|
||
| it "does nothing if hot topics summarization is disabled" do | ||
| SiteSetting.ai_summarize_max_hot_topics_gists_per_batch = 0 | ||
|
|
||
| subject.execute({}) | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist).to be_nil | ||
| end | ||
|
|
||
| it "creates a gist" do | ||
| gist_result = "I'm a gist" | ||
|
|
||
| DiscourseAi::Completions::Llm.with_prepared_responses([gist_result]) { subject.execute({}) } | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist.summarized_text).to eq(gist_result) | ||
| end | ||
|
|
||
| context "when we already generated a gist of it" do | ||
| fab!(:ai_gist) do | ||
| Fabricate( | ||
| :topic_ai_gist, | ||
| target: topic_1, | ||
| original_content_sha: AiSummary.build_sha("12"), | ||
| ) | ||
| end | ||
|
|
||
| it "does nothing if the gist is up to date" do | ||
| subject.execute({}) | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist.summarized_text).to eq(ai_gist.summarized_text) | ||
| expect(gist.original_content_sha).to eq(ai_gist.original_content_sha) | ||
| end | ||
|
|
||
| it "regenerates it if it's outdated" do | ||
| Fabricate(:post, topic: topic_1, post_number: 3) | ||
| gist_result = "They updated me" | ||
|
|
||
| DiscourseAi::Completions::Llm.with_prepared_responses([gist_result]) do | ||
| subject.execute({}) | ||
| end | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist.summarized_text).to eq(gist_result) | ||
| expect(gist.original_content_sha).to eq(AiSummary.build_sha("123")) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when there is a topic but it doesn't have a hot score" do | ||
| it "does nothing" do | ||
| subject.execute({}) | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "when there are multiple hot topics" do | ||
| fab!(:topic_2) { Fabricate(:topic) } | ||
| fab!(:post_2_1) { Fabricate(:post, topic: topic_2, post_number: 1) } | ||
| fab!(:post_2_2) { Fabricate(:post, topic: topic_2, post_number: 2) } | ||
|
|
||
| before do | ||
| TopicHotScore.create!(topic_id: topic_1.id, score: 0.2) | ||
| TopicHotScore.create!(topic_id: topic_2.id, score: 0.4) | ||
| end | ||
|
|
||
| it "processes them by score order" do | ||
| topic_1_gist = "I'm gist of topic 1" | ||
| topic_2_gist = "I'm gist of topic 2" | ||
|
|
||
| DiscourseAi::Completions::Llm.with_prepared_responses([topic_2_gist, topic_1_gist]) do | ||
| subject.execute({}) | ||
| end | ||
|
|
||
| gist = AiSummary.gist.find_by(target: topic_1) | ||
| expect(gist.summarized_text).to eq(topic_1_gist) | ||
|
|
||
| gist_2 = AiSummary.gist.find_by(target: topic_2) | ||
| expect(gist_2.summarized_text).to eq(topic_2_gist) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.