2023-10-02 17:55:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
return if !defined?(DiscourseAutomation)
|
|
|
|
|
2023-12-18 20:04:15 -05:00
|
|
|
describe DiscourseAi::Automation::LlmTriage do
|
2024-03-22 00:34:18 -04:00
|
|
|
fab!(:category)
|
|
|
|
fab!(:reply_user) { Fabricate(:user) }
|
2023-10-02 17:55:30 -04:00
|
|
|
|
|
|
|
let(:automation) { Fabricate(:automation, script: "llm_triage", enabled: true) }
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
fab!(:llm_model)
|
|
|
|
|
2023-10-02 17:55:30 -04:00
|
|
|
def add_automation_field(name, value, type: "text")
|
|
|
|
automation.fields.create!(
|
|
|
|
component: type,
|
|
|
|
name: name,
|
|
|
|
metadata: {
|
|
|
|
value: value,
|
|
|
|
},
|
|
|
|
target: "script",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-03-22 00:34:18 -04:00
|
|
|
before do
|
2023-10-02 17:55:30 -04:00
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
add_automation_field("system_prompt", "hello %%POST%%")
|
|
|
|
add_automation_field("search_for_text", "bad")
|
2024-07-30 12:44:57 -04:00
|
|
|
add_automation_field("model", "custom:#{llm_model.id}")
|
2023-10-02 17:55:30 -04:00
|
|
|
add_automation_field("category", category.id, type: "category")
|
|
|
|
add_automation_field("tags", %w[aaa bbb], type: "tags")
|
|
|
|
add_automation_field("hide_topic", true, type: "boolean")
|
2024-02-28 20:33:28 -05:00
|
|
|
add_automation_field("flag_post", true, type: "boolean")
|
2023-10-02 17:55:30 -04:00
|
|
|
add_automation_field("canned_reply", "Yo this is a reply")
|
2024-03-22 00:34:18 -04:00
|
|
|
add_automation_field("canned_reply_user", reply_user.username, type: "user")
|
2024-10-23 01:49:56 -04:00
|
|
|
add_automation_field("max_post_tokens", 100)
|
2024-03-22 00:34:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can trigger via automation" do
|
2024-10-23 01:49:56 -04:00
|
|
|
post = Fabricate(:post, raw: "hello " * 5000)
|
2023-10-02 17:55:30 -04:00
|
|
|
|
2024-10-23 01:49:56 -04:00
|
|
|
body = {
|
|
|
|
model: "gpt-3.5-turbo-0301",
|
|
|
|
usage: {
|
|
|
|
prompt_tokens: 337,
|
|
|
|
completion_tokens: 162,
|
|
|
|
total_tokens: 499,
|
|
|
|
},
|
|
|
|
choices: [
|
|
|
|
{ message: { role: "assistant", content: "bad" }, finish_reason: "stop", index: 0 },
|
|
|
|
],
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, "https://api.openai.com/v1/chat/completions").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: body,
|
|
|
|
)
|
|
|
|
|
|
|
|
automation.running_in_background!
|
|
|
|
automation.trigger!({ "post" => post })
|
2023-10-02 17:55:30 -04:00
|
|
|
|
|
|
|
topic = post.topic.reload
|
|
|
|
expect(topic.category_id).to eq(category.id)
|
|
|
|
expect(topic.tags.pluck(:name)).to contain_exactly("aaa", "bbb")
|
|
|
|
expect(topic.visible).to eq(false)
|
|
|
|
reply = topic.posts.order(:post_number).last
|
|
|
|
expect(reply.raw).to eq("Yo this is a reply")
|
2024-03-22 00:34:18 -04:00
|
|
|
expect(reply.user.id).to eq(reply_user.id)
|
2024-10-23 01:49:56 -04:00
|
|
|
|
|
|
|
ai_log = AiApiAuditLog.order("id desc").first
|
|
|
|
expect(ai_log.feature_name).to eq("llm_triage")
|
|
|
|
expect(ai_log.feature_context).to eq(
|
|
|
|
{ "automation_id" => automation.id, "automation_name" => automation.name },
|
|
|
|
)
|
|
|
|
|
|
|
|
count = ai_log.raw_request_payload.scan("hello").size
|
|
|
|
# we could use the exact count here but it can get fragile
|
|
|
|
# as we change tokenizers, this will give us reasonable confidence
|
|
|
|
expect(count).to be <= (100)
|
|
|
|
expect(count).to be > (50)
|
2024-03-22 00:34:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not reply to the canned_reply_user" do
|
|
|
|
post = Fabricate(:post, user: reply_user)
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do
|
|
|
|
automation.running_in_background!
|
|
|
|
automation.trigger!({ "post" => post })
|
|
|
|
end
|
|
|
|
|
|
|
|
last_post = post.topic.reload.posts.order(:post_number).last
|
|
|
|
expect(last_post.raw).to eq post.raw
|
2023-10-02 17:55:30 -04:00
|
|
|
end
|
|
|
|
end
|