2023-06-27 11:26:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Summarization
|
|
|
|
module Strategies
|
|
|
|
class TruncateContent < ::Summarization::Base
|
|
|
|
def initialize(completion_model)
|
|
|
|
@completion_model = completion_model
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :completion_model
|
|
|
|
|
|
|
|
delegate :correctly_configured?,
|
|
|
|
:display_name,
|
|
|
|
:configuration_hint,
|
|
|
|
:model,
|
|
|
|
to: :completion_model
|
|
|
|
|
2023-11-21 11:27:35 -05:00
|
|
|
def summarize(content, _user, &on_partial_blk)
|
2023-06-27 11:26:33 -04:00
|
|
|
opts = content.except(:contents)
|
|
|
|
|
|
|
|
{
|
2023-12-01 08:17:24 -05:00
|
|
|
summary: summarize_with_truncation(content[:contents], opts, &on_partial_blk),
|
2023-06-27 11:26:33 -04:00
|
|
|
chunks: [],
|
|
|
|
}
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def format_content_item(item)
|
|
|
|
"(#{item[:id]} #{item[:poster]} said: #{item[:text]} "
|
|
|
|
end
|
|
|
|
|
|
|
|
def summarize_with_truncation(contents, opts)
|
|
|
|
text_to_summarize = contents.map { |c| format_content_item(c) }.join
|
|
|
|
truncated_content =
|
|
|
|
::DiscourseAi::Tokenizer::BertTokenizer.truncate(
|
|
|
|
text_to_summarize,
|
|
|
|
completion_model.available_tokens,
|
|
|
|
)
|
|
|
|
|
|
|
|
completion(truncated_content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def completion(prompt)
|
|
|
|
::DiscourseAi::Inference::DiscourseClassifier.perform!(
|
|
|
|
"#{SiteSetting.ai_summarization_discourse_service_api_endpoint}/api/v1/classify",
|
|
|
|
completion_model.model,
|
|
|
|
prompt,
|
|
|
|
SiteSetting.ai_summarization_discourse_service_api_key,
|
|
|
|
).dig(:summary_text)
|
|
|
|
end
|
2023-06-27 11:26:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|