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

This PR passed the category and tags in the request to the LLM for topic
summary, to be leveraged by prompts.
This commit is contained in:
Mark VanLandingham
2025-07-25 10:35:05 -05:00
committed by GitHub
parent e01053cf68
commit 4e4f18b8b5
2 changed files with 59 additions and 0 deletions
@@ -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>
@@ -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