Skip to content

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

FEATURE: Give Ask AI the accepted answers of solved topics (#45161)

Rafael dos Santos Silva8978e42766bb
diff --git a/plugins/discourse-ai/lib/agents/ask_ai_synthesis.rb b/plugins/discourse-ai/lib/agents/ask_ai_synthesis.rb
index a042e519c59..ba0b156ed08 100644
--- a/plugins/discourse-ai/lib/agents/ask_ai_synthesis.rb
+++ b/plugins/discourse-ai/lib/agents/ask_ai_synthesis.rb
@@ -48,6 +48,7 @@ module DiscourseAi
- Return related_count sources when that many candidates are useful.
- Do not include an unrelated source merely to reach the limit.
- Prefer authoritative guides and direct answers over discussions that only repeat the question.
+ - A solved candidate's accepted_answer passage is the answer its community accepted. Prefer it as evidence for how to fix or do something.
- Topic authors, staff, and documentation categories are useful signals. Treat likes as a weak signal only.
- Consider freshness only for time-sensitive questions.
diff --git a/plugins/discourse-ai/lib/discoveries/retrieval.rb b/plugins/discourse-ai/lib/discoveries/retrieval.rb
index 3ab7b4dd389..08d5ae00ccf 100644
--- a/plugins/discourse-ai/lib/discoveries/retrieval.rb
+++ b/plugins/discourse-ai/lib/discoveries/retrieval.rb
@@ -353,6 +353,7 @@ module DiscourseAi
.where(id: candidates.pluck("post_id").compact)
.includes(:user, topic: [{ category: :parent_category }, :tags])
.index_by(&:id)
+ accepted_answers = accepted_answers_by_topic_id(posts.values.map(&:topic_id))
hidden_tags = DiscourseTagging.hidden_tag_names if SiteSetting.tagging_enabled
candidates.filter_map do |candidate|
@@ -370,17 +371,60 @@ module DiscourseAi
excerpt: plain_text(candidate["excerpt"]),
).stringify_keys
- candidate.merge(
- search_metadata,
- "category_id" => topic.category_id,
- "username" => post.user&.username,
- "name" => SiteSetting.enable_names? ? post.user&.name : nil,
- "avatar_template" => post.user&.avatar_template,
- "author_is_staff" => post.user&.staff? || false,
- "is_topic_op" => post.post_number == 1,
- "topic_bumped_at" => topic.bumped_at&.iso8601,
+ candidate =
+ candidate.merge(
+ search_metadata,
+ "category_id" => topic.category_id,
+ "username" => post.user&.username,
+ "name" => SiteSetting.enable_names? ? post.user&.name : nil,
+ "avatar_template" => post.user&.avatar_template,
+ "author_is_staff" => post.user&.staff? || false,
+ "is_topic_op" => post.post_number == 1,
+ "topic_bumped_at" => topic.bumped_at&.iso8601,
+ )
+ answers = accepted_answers[topic.id]
+ answers.present? ? with_accepted_answers(candidate, answers) : candidate
+ end
+ end
+
+ def accepted_answers_by_topic_id(topic_ids)
+ return {} if !defined?(DiscourseSolved) || !SiteSetting.solved_enabled || topic_ids.empty?
+
+ guardian = Guardian.new(@user)
+ Post
+ .where(
+ id:
+ DiscourseSolved::TopicAnswer
+ .joins(:solved_topic)
+ .where(discourse_solved_solved_topics: { topic_id: topic_ids })
+ .select(:answer_post_id),
+ post_type: Post.types[:regular],
+ hidden: false,
+ )
+ .includes(:topic)
+ .select { |post| guardian.can_see_post?(post) }
+ .group_by(&:topic_id)
+ end
+
+ # Retrieval mostly matches the question in a topic's first post, so an
+ # accepted answer would rarely reach the model unless it is added.
+ def with_accepted_answers(candidate, answers)
+ answer_ids = answers.map(&:id)
+ passages =
+ Array(candidate["passages"]).map do |passage|
+ next passage if answer_ids.exclude?(passage["post_id"])
+
+ passage.merge("accepted_answer" => true)
+ end
+ answers.each do |answer|
+ next if passages.any? { |passage| passage["post_id"] == answer.id }
+
+ passages << passage_from_source(source_from_post(answer, excerpt: answer.cooked)).merge(
+ "accepted_answer" => true,
)
end
+
+ candidate.merge("solved" => true, "passages" => passages)
end
def plain_text(value)
diff --git a/plugins/discourse-ai/lib/discoveries/synthesis.rb b/plugins/discourse-ai/lib/discoveries/synthesis.rb
index 2f7dd46cdc4..1505efb3b39 100644
--- a/plugins/discourse-ai/lib/discoveries/synthesis.rb
+++ b/plugins/discourse-ai/lib/discoveries/synthesis.rb
@@ -192,9 +192,10 @@ module DiscourseAi
input["url"] = slugless_url(candidate["url"]) if candidate["url"]
input["created"] = day(candidate["created"]) if candidate["created"]
input["author_is_staff"] = true if candidate["author_is_staff"]
+ input["solved"] = true if candidate["solved"]
if candidate["passages"].present?
input["passages"] = candidate["passages"].map do |passage|
- passage.slice("post_number", "excerpt").compact
+ passage.slice("post_number", "excerpt", "accepted_answer").compact
end
else
input["excerpt"] = candidate["excerpt"] if candidate["excerpt"]
diff --git a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
index 9f0411ac3b6..e6d15e66cd3 100644
--- a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb
@@ -380,6 +380,67 @@ describe DiscourseAi::Discoveries::Retrieval do
end
end
+ if defined?(DiscourseSolved)
+ describe "#call with solved topics" do
+ fab!(:answer) { Fabricate(:post, topic: topic_1, raw: "Restart the robot to fix it") }
+
+ before do
+ SiteSetting.solved_enabled = true
+ Fabricate(:solved_topic, topic: topic_1, answer_post: answer)
+ end
+
+ def call(lexical)
+ described_class.new(
+ user:,
+ lexical_retriever: ->(_query) { lexical },
+ semantic_retriever: ->(_query) { [] },
+ ).call("robot")
+ end
+
+ it "adds the accepted answer as a passage of a solved topic" do
+ candidates = call([source(post_1), source(post_2)]).candidates
+
+ expect(candidates.first).to include("solved" => true)
+ expect(candidates.first.fetch("passages")).to contain_exactly(
+ include("post_id" => post_1.id),
+ include(
+ "post_id" => answer.id,
+ "post_number" => answer.post_number,
+ "excerpt" => "Restart the robot to fix it",
+ "accepted_answer" => true,
+ ),
+ )
+ expect(candidates.second).not_to include("solved")
+ end
+
+ it "marks an accepted answer that retrieval matched instead of repeating it" do
+ candidates = call([source(answer)]).candidates
+
+ expect(candidates.first.fetch("passages")).to contain_exactly(
+ include("post_id" => answer.id, "accepted_answer" => true),
+ )
+ end
+
+ it "leaves out an accepted answer the user cannot see" do
+ answer.update!(hidden: true)
+
+ candidates = call([source(post_1)]).candidates
+
+ expect(candidates.first).not_to include("solved")
+ expect(candidates.first.fetch("passages").pluck("post_id")).to eq([post_1.id])
+ end
+
+ it "leaves out accepted answers when solved is disabled" do
+ SiteSetting.solved_enabled = false
+
+ candidates = call([source(post_1)]).candidates
+
+ expect(candidates.first).not_to include("solved")
+ expect(candidates.first.fetch("passages").pluck("post_id")).to eq([post_1.id])
+ end
+ end
+ end
+
describe "#call with external sources" do
def external_source(name: "Help center", &search)
Struct
diff --git a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
index 10f53bb31f9..ba2078ccb19 100644
--- a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
@@ -77,6 +77,35 @@ describe DiscourseAi::Discoveries::Synthesis do
)
end
+ it "flags a solved candidate and its accepted answer passage" do
+ candidate = {
+ "source_ref" => "source_1",
+ "title" => "Robot stuck",
+ "url" => "/t/robot-stuck/1/1",
+ "solved" => true,
+ "passages" => [
+ { "post_number" => 1, "excerpt" => "My robot is stuck." },
+ { "post_number" => 4, "excerpt" => "Restart it.", "accepted_answer" => true },
+ ],
+ }
+
+ supplied_candidates =
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ answerable: false, source_refs: [], title: "", answer: "" }],
+ ) do |_, _, prompts|
+ synthesis.call(query: "robot stuck", candidates: [candidate])
+ JSON.parse(prompts.first.messages.last[:content]).fetch("candidates")
+ end
+
+ expect(supplied_candidates.first).to include(
+ "solved" => true,
+ "passages" => [
+ { "post_number" => 1, "excerpt" => "My robot is stuck." },
+ { "post_number" => 4, "excerpt" => "Restart it.", "accepted_answer" => true },
+ ],
+ )
+ end
+
it "sends an external candidate's publish date as a day and its link unchanged" do
candidate = {
"source_ref" => "source_1",