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
Merged
Changes from all 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module Summarization | ||
| module Strategies | ||
| class HotTopicGists < Base | ||
| def type | ||
| AiSummary.summary_types[:gist] | ||
| end | ||
|
|
||
| def targets_data | ||
| content = { content_title: target.title, contents: [] } | ||
|
|
||
| op_post_number = 1 | ||
|
|
||
| hot_topics_recent_cutoff = Time.zone.now - SiteSetting.hot_topics_recent_days.days | ||
|
|
||
| recent_hot_posts = | ||
| Post | ||
| .where(topic_id: target.id) | ||
| .where("post_type = ?", Post.types[:regular]) | ||
| .where("NOT hidden") | ||
| .where("created_at >= ?", hot_topics_recent_cutoff) | ||
| .pluck(:post_number) | ||
|
|
||
romanrizzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # It may happen that a topic is hot without any recent posts | ||
| # In that case, we'll just grab the last 20 posts | ||
| # for an useful summary of the current state of the topic | ||
| if recent_hot_posts.empty? | ||
| recent_hot_posts = | ||
| Post | ||
| .where(topic_id: target.id) | ||
| .where("post_type = ?", Post.types[:regular]) | ||
| .where("NOT hidden") | ||
| .order("post_number DESC") | ||
| .limit(20) | ||
| .pluck(:post_number) | ||
| end | ||
| posts_data = | ||
| Post | ||
| .where(topic_id: target.id) | ||
| .joins(:user) | ||
| .where("post_number IN (?)", recent_hot_posts << op_post_number) | ||
| .order(:post_number) | ||
| .pluck(:post_number, :raw, :username) | ||
|
|
||
| posts_data.each do |(pn, raw, username)| | ||
| raw_text = raw | ||
|
|
||
| if pn == 1 && target.topic_embed&.embed_content_cache.present? | ||
| raw_text = target.topic_embed&.embed_content_cache | ||
| end | ||
|
|
||
| content[:contents] << { poster: username, id: pn, text: raw_text } | ||
| end | ||
|
|
||
| content | ||
| end | ||
|
|
||
| def concatenation_prompt(texts_to_summarize) | ||
| prompt = DiscourseAi::Completions::Prompt.new(<<~TEXT.strip) | ||
| You are a summarization bot tasked with creating a single, concise sentence by merging disjointed summaries into a cohesive statement. | ||
| Your response should strictly be this single, comprehensive sentence, without any additional text or comments. | ||
|
|
||
| - Focus on the central theme or issue being addressed, maintaining an objective and neutral tone. | ||
| - Exclude extraneous details or subjective opinions. | ||
| - Use the original language of the text. | ||
| - Begin directly with the main topic or issue, avoiding introductory phrases. | ||
| - Limit the summary to a maximum of 20 words. | ||
| TEXT | ||
|
|
||
| prompt.push(type: :user, content: <<~TEXT.strip) | ||
| THESE are the summaries, each one separated by a newline, all of them inside <input></input> XML tags: | ||
|
|
||
| <input> | ||
| #{texts_to_summarize.join("\n")} | ||
| </input> | ||
| TEXT | ||
|
|
||
| prompt | ||
| end | ||
|
|
||
| def summarize_single_prompt(input, opts) | ||
| statements = input.split(/(?=\d+\) \w+ said:)/) | ||
|
|
||
| prompt = DiscourseAi::Completions::Prompt.new(<<~TEXT.strip) | ||
| You are an advanced summarization bot. Analyze a given conversation and produce a concise, | ||
| single-sentence summary that conveys the main topic and current developments to someone with no prior context. | ||
|
|
||
| ### Guidelines: | ||
|
|
||
| - Emphasize the most recent updates while considering their significance within the original post. | ||
| - Focus on the central theme or issue being addressed, maintaining an objective and neutral tone. | ||
| - Exclude extraneous details or subjective opinions. | ||
| - Use the original language of the text. | ||
| - Begin directly with the main topic or issue, avoiding introductory phrases. | ||
| - Limit the summary to a maximum of 20 words. | ||
| TEXT | ||
|
|
||
| prompt.push(type: :user, content: <<~TEXT.strip) | ||
| ### Context: | ||
|
|
||
| The conversation began with the following statement: | ||
|
|
||
| #{opts[:content_title].present? ? "The discussion title is: " + opts[:content_title] + ".\n" : ""} | ||
|
|
||
| #{statements&.pop} | ||
|
|
||
| Subsequent discussion includes the following: | ||
|
|
||
| #{statements&.join} | ||
|
|
||
| Your task is to focus on these latest messages, capturing their meaning in the context of the initial post. | ||
| TEXT | ||
|
|
||
| prompt | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file was deleted.
Oops, something went wrong.
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 |
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.