Skip to content

Commit

Permalink
FIX: Truncate OP for gists to help the model focus on the latest posts (
Browse files Browse the repository at this point in the history
  • Loading branch information
romanrizzi authored Oct 31, 2024
1 parent 32fb023 commit e8eed71
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/summarization/fold_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def fold(items, summary, cursor, user, &on_partial_blk)
prompt =
(
if summary.blank?
strategy.first_summary_prompt(iteration_content)
strategy.first_summary_prompt(iteration_content, tokenizer)
else
strategy.summary_extension_prompt(summary, iteration_content)
strategy.summary_extension_prompt(summary, iteration_content, tokenizer)
end
)

Expand Down
4 changes: 2 additions & 2 deletions lib/summarization/strategies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def targets_data
end

# @returns { DiscourseAi::Completions::Prompt } - Prompt passed to the LLM when extending an existing summary.
def summary_extension_prompt(_summary, _texts_to_summarize)
def summary_extension_prompt(_summary, _texts_to_summarize, _tokenizer)
raise NotImplementedError
end

# @returns { DiscourseAi::Completions::Prompt } - Prompt passed to the LLM for summarizing a single chunk of content.
def first_summary_prompt(_input)
def first_summary_prompt(_input, _tokenizer)
raise NotImplementedError
end

Expand Down
4 changes: 2 additions & 2 deletions lib/summarization/strategies/chat_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def targets_data
.map { { id: _1, poster: _2, text: _3 } }
end

def summary_extension_prompt(summary, contents)
def summary_extension_prompt(summary, contents, _tokenizer)
input =
contents
.map { |item| "(#{item[:id]} #{item[:poster]} said: #{item[:text]} " }
Expand Down Expand Up @@ -63,7 +63,7 @@ def summary_extension_prompt(summary, contents)
prompt
end

def first_summary_prompt(contents)
def first_summary_prompt(contents, _tokenizer)
content_title = target.name
input =
contents.map { |item| "(#{item[:id]} #{item[:poster]} said: #{item[:text]} " }.join
Expand Down
17 changes: 14 additions & 3 deletions lib/summarization/strategies/hot_topic_gists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def targets_data
end
end

def summary_extension_prompt(summary, contents)
def summary_extension_prompt(summary, contents, _tokenizer)
statements =
contents
.to_a
Expand Down Expand Up @@ -98,11 +98,22 @@ def summary_extension_prompt(summary, contents)
prompt
end

def first_summary_prompt(contents)
def first_summary_prompt(contents, tokenizer)
content_title = target.title
statements =
contents.to_a.map { |item| "(#{item[:id]} #{item[:poster]} said: #{item[:text]} " }

op_statement = statements.shift.to_s
split_1, split_2 =
[op_statement[0, op_statement.size / 2], op_statement[(op_statement.size / 2)..-1]]

truncation_length = 500

op_statement = [
tokenizer.truncate(split_1, truncation_length),
tokenizer.truncate(split_2.reverse, truncation_length).reverse,
].join(" ")

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.
Expand All @@ -127,7 +138,7 @@ def first_summary_prompt(contents)
The conversation began with the following statement:
#{statements.shift}\n
#{op_statement}\n
TEXT

if statements.present?
Expand Down
4 changes: 2 additions & 2 deletions lib/summarization/strategies/topic_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def targets_data
end
end

def summary_extension_prompt(summary, contents)
def summary_extension_prompt(summary, contents, _tokenizer)
resource_path = "#{Discourse.base_path}/t/-/#{target.id}"
content_title = target.title
input =
Expand Down Expand Up @@ -70,7 +70,7 @@ def summary_extension_prompt(summary, contents)
prompt
end

def first_summary_prompt(contents)
def first_summary_prompt(contents, _tokenizer)
resource_path = "#{Discourse.base_path}/t/-/#{target.id}"
content_title = target.title
input =
Expand Down

0 comments on commit e8eed71

Please sign in to comment.