Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 1cbaed1

Browse files
committed
shorten tokens to 5 so scan is faster
1 parent a288b8d commit 1cbaed1

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

lib/ai_moderation/spam_scanner.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def self.test_post(post, custom_instructions: nil)
130130
llm.generate(
131131
prompt,
132132
temperature: 0.1,
133-
max_tokens: 100,
133+
max_tokens: 5,
134134
user: Discourse.system_user,
135135
feature_name: "spam_detection_test",
136136
feature_context: {
@@ -158,9 +158,27 @@ def self.test_post(post, custom_instructions: nil)
158158
log << "LLM: #{settings.llm_model.name}\n\n"
159159
log << "System Prompt: #{build_system_prompt(custom_instructions)}\n\n"
160160
log << "Context: #{context}\n\n"
161-
log << "Result: #{result}"
161+
log << "Result: #{result}\n\n"
162162

163163
is_spam = check_if_spam(result)
164+
165+
prompt.push(type: :model, content: result)
166+
prompt.push(type: :user, content: "Explain your reasoning")
167+
168+
reasoning =
169+
llm.generate(
170+
prompt,
171+
temperature: 0.1,
172+
max_tokens: 100,
173+
user: Discourse.system_user,
174+
feature_name: "spam_detection_test",
175+
feature_context: {
176+
post_id: post.id,
177+
},
178+
)&.strip
179+
180+
log << "Reasoning: #{reasoning}"
181+
164182
{ is_spam: is_spam, log: log }
165183
end
166184

@@ -191,7 +209,7 @@ def self.perform_scan(post)
191209
llm.generate(
192210
prompt,
193211
temperature: 0.1,
194-
max_tokens: 100,
212+
max_tokens: 5,
195213
user: Discourse.system_user,
196214
feature_name: "spam_detection",
197215
feature_context: {
@@ -214,8 +232,12 @@ def self.perform_scan(post)
214232
handle_spam(post, log) if is_spam
215233
end
216234
rescue StandardError => e
217-
raise e if Rails.env.test?
218-
Discourse.warn_exception(e, message: "Error in SpamScanner for post #{post.id}")
235+
# we need retries otherwise stuff will not be handled
236+
Discourse.warn_exception(
237+
e,
238+
message: "Discourse AI: Error in SpamScanner for post #{post.id}",
239+
)
240+
raise e
219241
end
220242
end
221243

spec/lib/modules/ai_moderation/spam_scanner_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,32 @@
159159

160160
before { Jobs.run_immediately! }
161161

162+
it "Can correctly run tests" do
163+
prompts = nil
164+
result =
165+
DiscourseAi::Completions::Llm.with_prepared_responses(
166+
["spam", "the reason is just because"],
167+
) do |_, _, _prompts|
168+
prompts = _prompts
169+
described_class.test_post(post, custom_instructions: "123")
170+
end
171+
172+
expect(prompts.length).to eq(2)
173+
expect(result[:is_spam]).to eq(true)
174+
expect(result[:log]).to include("123")
175+
expect(result[:log]).to include("just because")
176+
177+
result =
178+
DiscourseAi::Completions::Llm.with_prepared_responses(
179+
["not_spam", "the reason is just because"],
180+
) do |_, _, _prompts|
181+
prompts = _prompts
182+
described_class.test_post(post, custom_instructions: "123")
183+
end
184+
185+
expect(result[:is_spam]).to eq(false)
186+
end
187+
162188
it "Correctly handles spam scanning" do
163189
expect(described_class.flagging_user.id).not_to eq(Discourse.system_user.id)
164190

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
before { sign_in(admin) }
9898

9999
it "can scan using post url" do
100-
DiscourseAi::Completions::Llm.with_prepared_responses(["spam"]) do
100+
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "just because"]) do
101101
post "/admin/plugins/discourse-ai/ai-spam/test.json", params: { post_url: spam_post2.url }
102102
end
103103

@@ -108,7 +108,7 @@
108108
end
109109

110110
it "can scan using post id" do
111-
DiscourseAi::Completions::Llm.with_prepared_responses(["spam"]) do
111+
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "because apples"]) do
112112
post "/admin/plugins/discourse-ai/ai-spam/test.json",
113113
params: {
114114
post_url: spam_post.id.to_s,
@@ -133,7 +133,7 @@
133133

134134
AiSpamLog.create!(post: spam_post, llm_model: llm_model, is_spam: true, created_at: 1.day.ago)
135135

136-
DiscourseAi::Completions::Llm.with_prepared_responses(["spam"]) do
136+
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "because banana"]) do
137137
post "/admin/plugins/discourse-ai/ai-spam/test.json",
138138
params: {
139139
post_url: spam_post.url,
@@ -148,6 +148,7 @@
148148
expect(parsed["log"]).to include(spam_post.raw)
149149
expect(parsed["is_spam"]).to eq(true)
150150
expect(parsed["log"]).to include("Scan History:")
151+
expect(parsed["log"]).to include("banana")
151152
end
152153
end
153154

0 commit comments

Comments
 (0)