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]
|
|
|
|
|
|
|
|
field :system_prompt,
|
|
|
|
component: :message,
|
|
|
|
required: true,
|
2023-11-03 07:30:09 -04:00
|
|
|
validator: ->(input) do
|
2023-10-02 17:55:30 -04:00
|
|
|
if !input.include?("%%POST%%")
|
|
|
|
I18n.t(
|
2024-03-05 10:48:28 -05:00
|
|
|
"discourse_automation.scriptables.llm_triage.system_prompt_missing_post_placeholder",
|
2023-10-02 17:55:30 -04:00
|
|
|
)
|
|
|
|
end
|
2023-11-03 07:30:09 -04:00
|
|
|
end,
|
2023-10-02 17:55:30 -04:00
|
|
|
accepts_placeholders: true
|
|
|
|
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
|
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")
|
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,
|
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
|