Skip to content

A platform for community discussion. Free, open, simple.

DEV: Pass category/tags to LLM for AI summary (#33861)

Mark VanLandingham4e4f18b8b585
diff --git a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
index f06b67abd7a..69e982d424a 100644
--- a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
+++ b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
@@ -41,11 +41,15 @@ module DiscourseAi
def as_llm_messages(contents)
resource_path = "#{Discourse.base_path}/t/-/#{target.id}"
content_title = target.title
+ category_name = target.category&.name
+ tags = target.tags&.map(&:name)&.sort
input =
contents.map { |item| "(#{item[:id]} #{item[:poster]} said: #{item[:text]} " }.join
[{ type: :user, content: <<~TEXT.strip }]
#{content_title.present? ? "The discussion title is: " + content_title + ".\n" : ""}
+ #{category_name.present? ? "Category: " + category_name + ".\n" : ""}
+ #{tags.present? ? "Tags: " + tags.join(", ") + ".\n" : ""}
Here are the posts, inside <input></input> XML tags:
<input>
diff --git a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
index 80ee15b7d9f..89b388b3128 100644
--- a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
@@ -96,4 +96,59 @@ RSpec.describe DiscourseAi::Summarization::Strategies::TopicSummary do
end
end
end
+
+ describe "#as_llm_messages" do
+ let(:contents) do
+ [{ id: 1, poster: "user1", text: "First post content", last_version_at: Time.now }]
+ end
+
+ it "includes the topic title in the message" do
+ topic.title = "Test Topic Title"
+
+ messages = topic_summary.as_llm_messages(contents)
+ content = messages.first[:content]
+
+ expect(content).to include("The discussion title is: Test Topic Title")
+ end
+
+ context "when topic has a category" do
+ fab!(:category) { Fabricate(:category, name: "Test Category") }
+
+ it "includes the category name in the message" do
+ topic.category = category
+
+ messages = topic_summary.as_llm_messages(contents)
+ content = messages.first[:content]
+
+ expect(content).to include("Category: Test Category")
+ end
+ end
+
+ context "when topic has tags" do
+ fab!(:tag1) { Fabricate(:tag, name: "tag1") }
+ fab!(:tag2) { Fabricate(:tag, name: "tag2") }
+
+ it "includes the tag names in the message" do
+ topic.tags = [tag1, tag2]
+
+ messages = topic_summary.as_llm_messages(contents)
+ content = messages.first[:content]
+
+ expect(content).to include("Tags: tag1, tag2")
+ end
+ end
+
+ context "when topic has no category or tags" do
+ it "doesn't include category or tags in the message" do
+ topic.category = nil
+ topic.tags = []
+
+ messages = topic_summary.as_llm_messages(contents)
+ content = messages.first[:content]
+
+ expect(content).not_to include("Category:")
+ expect(content).not_to include("Tags:")
+ end
+ end
+ end
end