2023-06-27 11:26:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
RSpec.describe DiscourseAi::Summarization::FoldContent do
|
|
|
|
subject(:summarizer) { DiscourseAi::Summarization.topic_summary(topic) }
|
|
|
|
|
2023-06-27 11:26:33 -04:00
|
|
|
describe "#summarize" do
|
2024-07-03 20:48:18 -04:00
|
|
|
let!(:llm_model) { assign_fake_provider_to(:ai_summarization_model) }
|
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic, highest_post_number: 2) }
|
|
|
|
fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1, raw: "This is a text") }
|
|
|
|
|
2024-07-03 20:48:18 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.ai_summarization_enabled = true
|
2023-06-27 13:42:33 -04:00
|
|
|
|
2023-06-27 11:26:33 -04:00
|
|
|
# Make sure each content fits in a single chunk.
|
2023-11-23 10:58:54 -05:00
|
|
|
# 700 is the number of tokens reserved for the prompt.
|
2024-07-03 20:48:18 -04:00
|
|
|
model_tokens =
|
2024-10-25 10:51:17 -04:00
|
|
|
700 +
|
|
|
|
DiscourseAi::Tokenizer::OpenAiTokenizer.size(
|
|
|
|
"(1 #{post_1.user.username_lower} said: This is a text ",
|
|
|
|
) + 3
|
2023-06-27 11:26:33 -04:00
|
|
|
|
2024-07-03 20:48:18 -04:00
|
|
|
llm_model.update!(max_prompt_tokens: model_tokens)
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2023-11-21 11:27:35 -05:00
|
|
|
|
2024-10-25 10:51:17 -04:00
|
|
|
let(:single_summary) { "single" }
|
2023-11-23 10:58:54 -05:00
|
|
|
let(:concatenated_summary) { "this is a concatenated summary" }
|
|
|
|
|
2024-11-04 15:48:11 -05:00
|
|
|
fab!(:user)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2023-06-27 11:26:33 -04:00
|
|
|
context "when the content to summarize fits in a single call" do
|
|
|
|
it "does one call to summarize content" do
|
2023-11-23 10:58:54 -05:00
|
|
|
result =
|
2023-11-28 23:17:46 -05:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([single_summary]) do |spy|
|
2024-10-15 12:53:26 -04:00
|
|
|
summarizer.summarize(user).tap { expect(spy.completions).to eq(1) }
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2023-06-27 11:26:33 -04:00
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
expect(result.summarized_text).to eq(single_summary)
|
2023-06-27 11:26:33 -04:00
|
|
|
end
|
|
|
|
end
|
2024-07-03 17:10:31 -04:00
|
|
|
|
|
|
|
context "when the content to summarize doesn't fit in a single call" do
|
2024-10-15 12:53:26 -04:00
|
|
|
fab!(:post_2) { Fabricate(:post, topic: topic, post_number: 2, raw: "This is a text") }
|
2024-07-03 17:10:31 -04:00
|
|
|
|
2024-10-25 10:51:17 -04:00
|
|
|
it "keeps extending the summary until there is nothing else to process" do
|
2024-07-03 17:10:31 -04:00
|
|
|
result =
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(
|
2024-10-25 10:51:17 -04:00
|
|
|
[single_summary, concatenated_summary],
|
|
|
|
) { |spy| summarizer.summarize(user).tap { expect(spy.completions).to eq(2) } }
|
2024-07-03 17:10:31 -04:00
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
expect(result.summarized_text).to eq(concatenated_summary)
|
2024-07-03 17:10:31 -04:00
|
|
|
end
|
|
|
|
end
|
2023-06-27 11:26:33 -04:00
|
|
|
end
|
|
|
|
end
|