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: {
|
|
|
|
content: DiscourseAi::Automation::AVAILABLE_MODELS,
|
|
|
|
}
|
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"]
|
|
|
|
|
2023-10-04 18:00:45 -04:00
|
|
|
if !%w[gpt-4 gpt-3-5-turbo claude-2].include?(model)
|
2023-10-02 17:55:30 -04:00
|
|
|
Rails.logger.warn("llm_triage: model #{model} is not supported")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
Discourse.warn_exception(e, message: "llm_triage: failed to run inference")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|