Skip to content

Commit f789d3e

Browse files
authored
FIX: Triage-flagged posts didn't have a score. (#752)
The score will contain the LLM result, and make sure the flag isn't displayed when a minimum score threshold is present.
1 parent 867cd54 commit f789d3e

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

config/locales/server.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ en:
55
title: Triage posts using AI
66
description: "Triage posts using a large language model"
77
system_prompt_missing_post_placeholder: "System prompt must contain a placeholder for the post: %%POST%%"
8+
flagged_post: |
9+
<div>Response from the model:</div>
10+
<p>%%LLM_RESPONSE%%</p>
11+
<b>Triggered by the <a href="/admin/plugins/discourse-automation/%%AUTOMATION_ID%%">%%AUTOMATION_NAME%%</a> rule.</b>
812
llm_report:
913
title: Periodic report using AI
1014
description: "Periodic report based on a large language model"

discourse_automation/llm_triage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
canned_reply_user: canned_reply_user,
7979
hide_topic: hide_topic,
8080
flag_post: flag_post,
81+
automation: self.automation,
8182
)
8283
rescue => e
8384
Discourse.warn_exception(e, message: "llm_triage: skipped triage on post #{post.id}")

lib/automation/llm_triage.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def self.handle(
1313
canned_reply: nil,
1414
canned_reply_user: nil,
1515
hide_topic: nil,
16-
flag_post: nil
16+
flag_post: nil,
17+
automation: nil
1718
)
1819
if category_id.blank? && tags.blank? && canned_reply.blank? && hide_topic.blank? &&
1920
flag_post.blank?
@@ -38,12 +39,12 @@ def self.handle(
3839
llm.generate(
3940
filled_system_prompt,
4041
temperature: 0,
41-
max_tokens: llm.tokenizer.tokenize(search_for_text).length * 2 + 10,
42+
max_tokens: 700, # ~500 words
4243
user: Discourse.system_user,
4344
feature_name: "llm_triage",
44-
)
45+
)&.strip
4546

46-
if result.present? && result.strip.downcase.include?(search_for_text)
47+
if result.present? && result.downcase.include?(search_for_text)
4748
user = User.find_by_username(canned_reply_user) if canned_reply_user.present?
4849
user = user || Discourse.system_user
4950
if canned_reply.present?
@@ -69,7 +70,24 @@ def self.handle(
6970

7071
post.topic.update!(visible: false) if hide_topic
7172

72-
ReviewablePost.needs_review!(target: post, created_by: Discourse.system_user) if flag_post
73+
if flag_post
74+
reviewable =
75+
ReviewablePost.needs_review!(target: post, created_by: Discourse.system_user)
76+
77+
score_reason =
78+
I18n
79+
.t("discourse_automation.scriptables.llm_triage.flagged_post")
80+
.sub("%%LLM_RESPONSE%%", result)
81+
.sub("%%AUTOMATION_ID%%", automation&.id.to_s)
82+
.sub("%%AUTOMATION_NAME%%", automation&.name.to_s)
83+
84+
reviewable.add_score(
85+
Discourse.system_user,
86+
ReviewableScore.types[:needs_approval],
87+
reason: score_reason,
88+
force_review: true,
89+
)
90+
end
7391
end
7492
end
7593
end

spec/lib/modules/automation/llm_triage_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def triage(**args)
1515
hide_topic: true,
1616
system_prompt: "test %%POST%%",
1717
search_for_text: "bad",
18+
automation: nil,
1819
)
1920
end
2021

@@ -29,6 +30,7 @@ def triage(**args)
2930
hide_topic: true,
3031
system_prompt: "test %%POST%%",
3132
search_for_text: "bad",
33+
automation: nil,
3234
)
3335
end
3436

@@ -45,6 +47,7 @@ def triage(**args)
4547
category_id: category.id,
4648
system_prompt: "test %%POST%%",
4749
search_for_text: "bad",
50+
automation: nil,
4851
)
4952
end
5053

@@ -61,6 +64,7 @@ def triage(**args)
6164
search_for_text: "bad",
6265
canned_reply: "test canned reply 123",
6366
canned_reply_user: user.username,
67+
automation: nil,
6468
)
6569
end
6670

@@ -78,12 +82,14 @@ def triage(**args)
7882
system_prompt: "test %%POST%%",
7983
search_for_text: "bad",
8084
flag_post: true,
85+
automation: nil,
8186
)
8287
end
8388

8489
reviewable = ReviewablePost.last
8590

8691
expect(reviewable.target).to eq(post)
92+
expect(reviewable.reviewable_scores.first.reason).to include("bad")
8793
end
8894

8995
it "can handle garbled output from LLM" do
@@ -94,6 +100,7 @@ def triage(**args)
94100
system_prompt: "test %%POST%%",
95101
search_for_text: "bad",
96102
flag_post: true,
103+
automation: nil,
97104
)
98105
end
99106

0 commit comments

Comments
 (0)