Skip to content

Commit cf45e68

Browse files
authored
FIX: persona triage should be logged to automation (#1326)
We were logging persona triage as "bot" in logs, causing some confusions around real world usage This amends it so we log usage to "automation - AUTOMATION NAME"
1 parent 2a62658 commit cf45e68

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

lib/ai_bot/playground.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ def self.reply_to_post(
190190
add_user_to_pm: false,
191191
stream_reply: false,
192192
auto_set_title: false,
193-
silent_mode: false
193+
silent_mode: false,
194+
feature_name: nil
194195
)
195196
ai_persona = AiPersona.find_by(id: persona_id)
196197
raise Discourse::InvalidParameters.new(:persona_id) if !ai_persona
@@ -210,6 +211,7 @@ def self.reply_to_post(
210211
stream_reply: stream_reply,
211212
auto_set_title: auto_set_title,
212213
silent_mode: silent_mode,
214+
feature_name: feature_name,
213215
)
214216
rescue => e
215217
if Rails.env.test?
@@ -380,6 +382,7 @@ def reply_to(
380382
stream_reply: nil,
381383
auto_set_title: true,
382384
silent_mode: false,
385+
feature_name: nil,
383386
&blk
384387
)
385388
# this is a multithreading issue
@@ -414,6 +417,7 @@ def reply_to(
414417
DiscourseAi::Personas::BotContext.new(
415418
post: post,
416419
custom_instructions: custom_instructions,
420+
feature_name: feature_name,
417421
messages:
418422
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
419423
post,

lib/automation/llm_persona_triage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def self.handle(post:, persona_id:, whisper: false, silent_mode: false, automati
88
persona_id: persona_id,
99
whisper: whisper,
1010
silent_mode: silent_mode,
11+
feature_name: "automation - #{automation&.name}",
1112
)
1213
rescue => e
1314
Discourse.warn_exception(

spec/lib/discourse_automation/llm_persona_triage_spec.rb

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
fab!(:user)
77
fab!(:bot_user) { Fabricate(:user) }
88

9-
fab!(:llm_model) do
10-
Fabricate(:llm_model, provider: "anthropic", name: "claude-3-opus", enabled_chat_bot: true)
11-
end
9+
fab!(:llm_model) { Fabricate(:anthropic_model, name: "claude-3-opus", enabled_chat_bot: true) }
1210

1311
fab!(:ai_persona) do
1412
persona =
@@ -25,7 +23,9 @@
2523
persona
2624
end
2725

28-
let(:automation) { Fabricate(:automation, script: "llm_persona_triage", enabled: true) }
26+
let(:automation) do
27+
Fabricate(:automation, name: "my automation", script: "llm_persona_triage", enabled: true)
28+
end
2929

3030
def add_automation_field(name, value, type: "text")
3131
automation.fields.create!(
@@ -49,12 +49,40 @@ def add_automation_field(name, value, type: "text")
4949
it "can respond to a post using the specified persona" do
5050
post = Fabricate(:post, raw: "This is a test post that needs triage")
5151

52-
response_text = "I've analyzed your post and can help with that."
52+
response_text = "I analyzed your post and can help with that."
5353

54-
DiscourseAi::Completions::Llm.with_prepared_responses([response_text]) do
55-
automation.running_in_background!
56-
automation.trigger!({ "post" => post })
57-
end
54+
body = (<<~STRING).strip
55+
event: message_start
56+
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
57+
58+
event: content_block_start
59+
data: {"type": "content_block_start", "index":0, "content_block": {"type": "text", "text": ""}}
60+
61+
event: ping
62+
data: {"type": "ping"}
63+
64+
event: content_block_delta
65+
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "#{response_text}"}}
66+
67+
event: content_block_stop
68+
data: {"type": "content_block_stop", "index": 0}
69+
70+
event: message_delta
71+
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}
72+
73+
event: message_stop
74+
data: {"type": "message_stop"}
75+
STRING
76+
77+
stub_request(:post, "https://api.anthropic.com/v1/messages").to_return(body: body)
78+
79+
automation.running_in_background!
80+
automation.trigger!({ "post" => post })
81+
82+
log = AiApiAuditLog.last
83+
expect(log).to be_present
84+
expect(log.user_id).to eq(post.user_id)
85+
expect(log.feature_name).to eq("automation - #{automation.name}")
5886

5987
topic = post.topic.reload
6088
last_post = topic.posts.order(:post_number).last

0 commit comments

Comments
 (0)