2023-10-02 17:55:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
if defined?(DiscourseAutomation)
|
|
|
|
DiscourseAutomation::Scriptable::LLM_TRIAGE = "llm_triage"
|
|
|
|
|
|
|
|
DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scriptable::LLM_TRIAGE) do
|
|
|
|
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(
|
|
|
|
"discourse_automation.scriptables.#{DiscourseAutomation::Scriptable::LLM_TRIAGE}.system_prompt_missing_post_placeholder",
|
|
|
|
)
|
|
|
|
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
|
|
|
|
field :canned_reply, component: :message
|
|
|
|
field :canned_reply_user, component: :user
|
|
|
|
|
|
|
|
script do |context, fields, automation|
|
|
|
|
post = context["post"]
|
|
|
|
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")
|
|
|
|
canned_reply = fields.dig("canned_reply", "value")
|
|
|
|
canned_reply_user = fields.dig("canned_reply_user", "value")
|
|
|
|
|
|
|
|
if post.raw.strip == canned_reply.to_s.strip
|
|
|
|
# nothing to do if we already replied
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "llm_triage: failed to run inference")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|