2024-10-15 12:53:26 -04:00
# frozen_string_literal: true
module DiscourseAi
module Summarization
module Strategies
class TopicSummary < Base
def type
AiSummary . summary_types [ :complete ]
end
def targets_data
posts_data =
( target . has_summary? ? best_replies : pick_selection ) . pluck (
:post_number ,
:raw ,
:username ,
)
2024-10-25 10:51:17 -04:00
posts_data . reduce ( [ ] ) do | memo , ( pn , raw , username ) |
2024-10-15 12:53:26 -04:00
raw_text = raw
if pn == 1 && target . topic_embed & . embed_content_cache . present?
raw_text = target . topic_embed & . embed_content_cache
end
2024-10-25 10:51:17 -04:00
memo << { poster : username , id : pn , text : raw_text }
2024-10-15 12:53:26 -04:00
end
end
2024-10-31 11:17:42 -04:00
def summary_extension_prompt ( summary , contents )
2024-10-25 10:51:17 -04:00
resource_path = " #{ Discourse . base_path } /t/-/ #{ target . id } "
content_title = target . title
input =
contents . map { | item | " ( #{ item [ :id ] } #{ item [ :poster ] } said: #{ item [ :text ] } ) " } . join
prompt = DiscourseAi :: Completions :: Prompt . new ( << ~ TEXT )
You are an advanced summarization bot tasked with enhancing an existing summary by incorporating additional posts .
### Guidelines:
- Only include the enhanced summary , without any additional commentary .
- Understand and generate Discourse forum Markdown ; including links , _italics_ , ** bold ** .
- Maintain the original language of the text being summarized .
- Aim for summaries to be 400 words or less .
- Each new post is formatted as " <POST_NUMBER>) <USERNAME> <MESSAGE> "
2024-12-02 15:23:31 -05:00
- Cite specific noteworthy posts using the format [ DESCRIPTION ] ( #{resource_path}/POST_NUMBER)
- Example : links to the 3 rd and 6 th posts by sam : sam ( [ #3](#{resource_path}/3), [#6](#{resource_path}/6))
2024-10-25 10:51:17 -04:00
- Example : link to the 6 th post by jane : [ agreed with ] ( #{resource_path}/6)
2024-12-02 15:23:31 -05:00
- Example : link to the 13 th post by joe : [ joe ] ( #{resource_path}/13)
2024-10-25 10:51:17 -04:00
- When formatting usernames either use @USERNAME or [ USERNAME ] ( #{resource_path}/POST_NUMBER)
2024-10-15 12:53:26 -04:00
TEXT
prompt . push ( type : :user , content : << ~ TEXT . strip )
2024-10-25 10:51:17 -04:00
### Context:
#{content_title.present? ? "The discussion title is: " + content_title + ".\n" : ""}
2024-12-02 15:23:31 -05:00
2024-10-25 10:51:17 -04:00
Here is the existing summary :
2024-12-02 15:23:31 -05:00
2024-10-25 10:51:17 -04:00
#{summary}
2024-12-02 15:23:31 -05:00
2024-10-25 10:51:17 -04:00
Here are the new posts , inside < input > < / input> XML tags:
2024-10-15 12:53:26 -04:00
< input >
2024-10-25 10:51:17 -04:00
#{input}
2024-10-15 12:53:26 -04:00
< / input>
2024-10-25 10:51:17 -04:00
Integrate the new information to generate an enhanced concise and coherent summary .
2024-10-15 12:53:26 -04:00
TEXT
prompt
end
2024-10-31 11:17:42 -04:00
def first_summary_prompt ( contents )
2024-10-25 10:51:17 -04:00
resource_path = " #{ Discourse . base_path } /t/-/ #{ target . id } "
content_title = target . title
input =
contents . map { | item | " ( #{ item [ :id ] } #{ item [ :poster ] } said: #{ item [ :text ] } " } . join
prompt = DiscourseAi :: Completions :: Prompt . new ( << ~ TEXT . strip )
You are an advanced summarization bot that generates concise , coherent summaries of provided text .
- Only include the summary , without any additional commentary .
- You understand and generate Discourse forum Markdown ; including links , _italics_ , ** bold ** .
- Maintain the original language of the text being summarized .
- Aim for summaries to be 400 words or less .
- Each post is formatted as " <POST_NUMBER>) <USERNAME> <MESSAGE> "
2024-12-02 15:23:31 -05:00
- Cite specific noteworthy posts using the format [ DESCRIPTION ] ( #{resource_path}/POST_NUMBER)
- Example : links to the 3 rd and 6 th posts by sam : sam ( [ #3](#{resource_path}/3), [#6](#{resource_path}/6))
2024-10-25 10:51:17 -04:00
- Example : link to the 6 th post by jane : [ agreed with ] ( #{resource_path}/6)
2024-12-02 15:23:31 -05:00
- Example : link to the 13 th post by joe : [ joe ] ( #{resource_path}/13)
2024-10-25 10:51:17 -04:00
- When formatting usernames either use @USERNMAE OR [ USERNAME ] ( #{resource_path}/POST_NUMBER)
TEXT
prompt . push (
type : :user ,
content :
" Here are the posts inside <input></input> XML tags: \n \n <input>1) user1 said: I love Mondays 2) user2 said: I hate Mondays</input> \n \n Generate a concise, coherent summary of the text above maintaining the original language. " ,
)
prompt . push (
type : :model ,
content :
" Two users are sharing their feelings toward Mondays. [user1]( #{ resource_path } /1) hates them, while [user2]( #{ resource_path } /2) loves them. " ,
)
2024-10-15 12:53:26 -04:00
prompt . push ( type : :user , content : << ~ TEXT . strip )
2024-10-25 10:51:17 -04:00
#{content_title.present? ? "The discussion title is: " + content_title + ".\n" : ""}
Here are the posts , inside < input > < / input> XML tags:
2024-10-15 12:53:26 -04:00
2024-10-25 10:51:17 -04:00
< input >
#{input}
< / input>
2024-10-15 12:53:26 -04:00
2024-10-25 10:51:17 -04:00
Generate a concise , coherent summary of the text above maintaining the original language .
TEXT
2024-10-15 12:53:26 -04:00
prompt
end
private
attr_reader :topic
def best_replies
Post
. summary ( target . id )
. where ( " post_type = ? " , Post . types [ :regular ] )
. where ( " NOT hidden " )
. joins ( :user )
. order ( :post_number )
end
def pick_selection
posts =
Post
. where ( topic_id : target . id )
. where ( " post_type = ? " , Post . types [ :regular ] )
. where ( " NOT hidden " )
. order ( :post_number )
post_numbers = posts . limit ( 5 ) . pluck ( :post_number )
post_numbers += posts . reorder ( " posts.score desc " ) . limit ( 50 ) . pluck ( :post_number )
post_numbers += posts . reorder ( " post_number desc " ) . limit ( 5 ) . pluck ( :post_number )
Post
. where ( topic_id : target . id )
. joins ( :user )
. where ( " post_number in (?) " , post_numbers )
. order ( :post_number )
end
end
end
end
end