2023-10-02 17:55:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
if defined?(DiscourseAutomation)
|
2024-03-05 10:48:28 -05:00
|
|
|
DiscourseAutomation::Scriptable.add("llm_triage") do
|
2023-10-02 17:55:30 -04:00
|
|
|
version 1
|
|
|
|
run_in_background
|
|
|
|
|
|
|
|
placeholder :post
|
|
|
|
|
|
|
|
triggerables %i[post_created_edited]
|
|
|
|
|
2024-08-21 10:41:55 -04:00
|
|
|
field :system_prompt, component: :message, required: false
|
2023-10-02 17:55:30 -04:00
|
|
|
field :search_for_text, component: :text, required: true
|
2023-12-18 20:04:15 -05:00
|
|
|
field :model,
|
|
|
|
component: :choices,
|
|
|
|
required: true,
|
|
|
|
extra: {
|
2024-06-20 18:07:17 -04:00
|
|
|
content: DiscourseAi::Automation.available_models,
|
2023-12-18 20:04:15 -05:00
|
|
|
}
|
2023-10-02 17:55:30 -04:00
|
|
|
field :category, component: :category
|
|
|
|
field :tags, component: :tags
|
|
|
|
field :hide_topic, component: :boolean
|
2024-02-28 20:33:28 -05:00
|
|
|
field :flag_post, component: :boolean
|
2024-10-04 02:11:30 -04:00
|
|
|
field :flag_type,
|
|
|
|
component: :choices,
|
|
|
|
required: false,
|
|
|
|
extra: {
|
|
|
|
content: DiscourseAi::Automation.flag_types,
|
|
|
|
},
|
|
|
|
default: "review"
|
2023-10-02 17:55:30 -04:00
|
|
|
field :canned_reply, component: :message
|
|
|
|
field :canned_reply_user, component: :user
|
|
|
|
|
2024-03-22 00:34:18 -04:00
|
|
|
script do |context, fields|
|
2023-10-02 17:55:30 -04:00
|
|
|
post = context["post"]
|
2024-03-22 00:34:18 -04:00
|
|
|
canned_reply = fields.dig("canned_reply", "value")
|
|
|
|
canned_reply_user = fields.dig("canned_reply_user", "value")
|
|
|
|
|
|
|
|
# nothing to do if we already replied
|
|
|
|
next if post.user.username == canned_reply_user
|
|
|
|
next if post.raw.strip == canned_reply.to_s.strip
|
|
|
|
|
2023-10-02 17:55:30 -04:00
|
|
|
system_prompt = fields["system_prompt"]["value"]
|
|
|
|
search_for_text = fields["search_for_text"]["value"]
|
|
|
|
model = fields["model"]["value"]
|
|
|
|
|
|
|
|
category_id = fields.dig("category", "value")
|
|
|
|
tags = fields.dig("tags", "value")
|
|
|
|
hide_topic = fields.dig("hide_topic", "value")
|
2024-02-28 20:33:28 -05:00
|
|
|
flag_post = fields.dig("flag_post", "value")
|
2024-10-04 02:11:30 -04:00
|
|
|
flag_type = fields.dig("flag_type", "value")
|
2023-10-02 17:55:30 -04:00
|
|
|
|
|
|
|
begin
|
2024-05-16 02:49:44 -04:00
|
|
|
RateLimiter.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
"llm_triage_#{post.id}",
|
|
|
|
SiteSetting.ai_automation_max_triage_per_post_per_minute,
|
|
|
|
1.minute,
|
|
|
|
).performed!
|
|
|
|
|
|
|
|
RateLimiter.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
"llm_triage",
|
|
|
|
SiteSetting.ai_automation_max_triage_per_minute,
|
|
|
|
1.minute,
|
|
|
|
).performed!
|
|
|
|
|
2023-12-18 20:04:15 -05:00
|
|
|
DiscourseAi::Automation::LlmTriage.handle(
|
2023-10-02 17:55:30 -04:00
|
|
|
post: post,
|
|
|
|
model: model,
|
|
|
|
search_for_text: search_for_text,
|
|
|
|
system_prompt: system_prompt,
|
|
|
|
category_id: category_id,
|
|
|
|
tags: tags,
|
|
|
|
canned_reply: canned_reply,
|
|
|
|
canned_reply_user: canned_reply_user,
|
|
|
|
hide_topic: hide_topic,
|
2024-02-28 20:33:28 -05:00
|
|
|
flag_post: flag_post,
|
2024-10-04 02:11:30 -04:00
|
|
|
flag_type: flag_type.to_s.to_sym,
|
2024-08-14 14:54:09 -04:00
|
|
|
automation: self.automation,
|
2023-10-02 17:55:30 -04:00
|
|
|
)
|
|
|
|
rescue => e
|
2024-05-16 02:49:44 -04:00
|
|
|
Discourse.warn_exception(e, message: "llm_triage: skipped triage on post #{post.id}")
|
2023-10-02 17:55:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|