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!(
|
2024-01-10 17:23:07 -05:00
|
|
|
"#{endpoint}/api/v1/classify",
|
2023-11-23 10:58:54 -05:00
|
|
|
completion_model.model,
|
|
|
|
prompt,
|
|
|
|
SiteSetting.ai_summarization_discourse_service_api_key,
|
|
|
|
).dig(:summary_text)
|
|
|
|
end
|
2024-01-10 17:23:07 -05:00
|
|
|
|
|
|
|
def endpoint
|
|
|
|
if SiteSetting.ai_summarization_discourse_service_api_endpoint_srv.present?
|
|
|
|
service =
|
|
|
|
DiscourseAi::Utils::DnsSrv.lookup(
|
|
|
|
SiteSetting.ai_summarization_discourse_service_api_endpoint_srv,
|
|
|
|
)
|
|
|
|
"https://#{service.target}:#{service.port}"
|
|
|
|
else
|
|
|
|
SiteSetting.ai_summarization_discourse_service_api_endpoint
|
|
|
|
end
|
|
|
|
end
|
2023-06-27 11:26:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|