|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module DiscourseAi |
| 4 | + module Summarization |
| 5 | + module Strategies |
| 6 | + class HotTopicGists < Base |
| 7 | + def type |
| 8 | + AiSummary.summary_types[:gist] |
| 9 | + end |
| 10 | + |
| 11 | + def targets_data |
| 12 | + content = { content_title: target.title, contents: [] } |
| 13 | + |
| 14 | + op_post_number = 1 |
| 15 | + |
| 16 | + hot_topics_recent_cutoff = Time.zone.now - SiteSetting.hot_topics_recent_days.days |
| 17 | + |
| 18 | + recent_hot_posts = |
| 19 | + Post |
| 20 | + .where(topic_id: target.id) |
| 21 | + .where("post_type = ?", Post.types[:regular]) |
| 22 | + .where("NOT hidden") |
| 23 | + .where("created_at >= ?", hot_topics_recent_cutoff) |
| 24 | + .pluck(:post_number) |
| 25 | + |
| 26 | + # It may happen that a topic is hot without any recent posts |
| 27 | + # In that case, we'll just grab the last 20 posts |
| 28 | + # for an useful summary of the current state of the topic |
| 29 | + if recent_hot_posts.empty? |
| 30 | + recent_hot_posts = |
| 31 | + Post |
| 32 | + .where(topic_id: target.id) |
| 33 | + .where("post_type = ?", Post.types[:regular]) |
| 34 | + .where("NOT hidden") |
| 35 | + .order("post_number DESC") |
| 36 | + .limit(20) |
| 37 | + .pluck(:post_number) |
| 38 | + end |
| 39 | + posts_data = |
| 40 | + Post |
| 41 | + .where(topic_id: target.id) |
| 42 | + .joins(:user) |
| 43 | + .where("post_number IN (?)", recent_hot_posts << op_post_number) |
| 44 | + .order(:post_number) |
| 45 | + .pluck(:post_number, :raw, :username) |
| 46 | + |
| 47 | + posts_data.each do |(pn, raw, username)| |
| 48 | + raw_text = raw |
| 49 | + |
| 50 | + if pn == 1 && target.topic_embed&.embed_content_cache.present? |
| 51 | + raw_text = target.topic_embed&.embed_content_cache |
| 52 | + end |
| 53 | + |
| 54 | + content[:contents] << { poster: username, id: pn, text: raw_text } |
| 55 | + end |
| 56 | + |
| 57 | + content |
| 58 | + end |
| 59 | + |
| 60 | + def concatenation_prompt(texts_to_summarize) |
| 61 | + prompt = DiscourseAi::Completions::Prompt.new(<<~TEXT.strip) |
| 62 | + You are a summarization bot tasked with creating a single, concise sentence by merging disjointed summaries into a cohesive statement. |
| 63 | + Your response should strictly be this single, comprehensive sentence, without any additional text or comments. |
| 64 | +
|
| 65 | + - Focus on the central theme or issue being addressed, maintaining an objective and neutral tone. |
| 66 | + - Exclude extraneous details or subjective opinions. |
| 67 | + - Use the original language of the text. |
| 68 | + - Begin directly with the main topic or issue, avoiding introductory phrases. |
| 69 | + - Limit the summary to a maximum of 20 words. |
| 70 | + TEXT |
| 71 | + |
| 72 | + prompt.push(type: :user, content: <<~TEXT.strip) |
| 73 | + THESE are the summaries, each one separated by a newline, all of them inside <input></input> XML tags: |
| 74 | +
|
| 75 | + <input> |
| 76 | + #{texts_to_summarize.join("\n")} |
| 77 | + </input> |
| 78 | + TEXT |
| 79 | + |
| 80 | + prompt |
| 81 | + end |
| 82 | + |
| 83 | + def summarize_single_prompt(input, opts) |
| 84 | + statements = input.split(/(?=\d+\) \w+ said:)/) |
| 85 | + |
| 86 | + prompt = DiscourseAi::Completions::Prompt.new(<<~TEXT.strip) |
| 87 | + You are an advanced summarization bot. Analyze a given conversation and produce a concise, |
| 88 | + single-sentence summary that conveys the main topic and current developments to someone with no prior context. |
| 89 | +
|
| 90 | + ### Guidelines: |
| 91 | + |
| 92 | + - Emphasize the most recent updates while considering their significance within the original post. |
| 93 | + - Focus on the central theme or issue being addressed, maintaining an objective and neutral tone. |
| 94 | + - Exclude extraneous details or subjective opinions. |
| 95 | + - Use the original language of the text. |
| 96 | + - Begin directly with the main topic or issue, avoiding introductory phrases. |
| 97 | + - Limit the summary to a maximum of 20 words. |
| 98 | + TEXT |
| 99 | + |
| 100 | + prompt.push(type: :user, content: <<~TEXT.strip) |
| 101 | + ### Context: |
| 102 | + |
| 103 | + The conversation began with the following statement: |
| 104 | +
|
| 105 | + #{opts[:content_title].present? ? "The discussion title is: " + opts[:content_title] + ".\n" : ""} |
| 106 | + |
| 107 | + #{statements&.pop} |
| 108 | + |
| 109 | + Subsequent discussion includes the following: |
| 110 | +
|
| 111 | + #{statements&.join} |
| 112 | + |
| 113 | + Your task is to focus on these latest messages, capturing their meaning in the context of the initial post. |
| 114 | + TEXT |
| 115 | + |
| 116 | + prompt |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments