2023-12-18 20:04:15 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
module DiscourseAi
|
|
|
|
module Automation
|
|
|
|
module LlmTriage
|
|
|
|
def self.handle(
|
|
|
|
post:,
|
|
|
|
model:,
|
|
|
|
search_for_text:,
|
|
|
|
system_prompt:,
|
|
|
|
category_id: nil,
|
|
|
|
tags: nil,
|
|
|
|
canned_reply: nil,
|
|
|
|
canned_reply_user: nil,
|
2024-02-28 20:33:28 -05:00
|
|
|
hide_topic: nil,
|
2024-08-14 14:54:09 -04:00
|
|
|
flag_post: nil,
|
2024-10-04 02:11:30 -04:00
|
|
|
flag_type: nil,
|
2024-10-23 01:49:56 -04:00
|
|
|
automation: nil,
|
|
|
|
max_post_tokens: nil
|
2023-12-18 20:04:15 -05:00
|
|
|
)
|
2024-02-28 20:33:28 -05:00
|
|
|
if category_id.blank? && tags.blank? && canned_reply.blank? && hide_topic.blank? &&
|
|
|
|
flag_post.blank?
|
2023-12-18 20:04:15 -05:00
|
|
|
raise ArgumentError, "llm_triage: no action specified!"
|
|
|
|
end
|
|
|
|
|
2024-10-23 01:49:56 -04:00
|
|
|
llm = DiscourseAi::Completions::Llm.proxy(model)
|
|
|
|
|
2024-08-21 10:41:55 -04:00
|
|
|
s_prompt = system_prompt.to_s.sub("%%POST%%", "") # Backwards-compat. We no longer sub this.
|
|
|
|
prompt = DiscourseAi::Completions::Prompt.new(s_prompt)
|
2023-12-18 20:04:15 -05:00
|
|
|
|
2024-10-23 01:49:56 -04:00
|
|
|
content = "title: #{post.topic.title}\n#{post.raw}"
|
2023-12-18 20:04:15 -05:00
|
|
|
|
2024-10-23 01:49:56 -04:00
|
|
|
content = llm.tokenizer.truncate(content, max_post_tokens) if max_post_tokens.present?
|
|
|
|
|
|
|
|
prompt.push(type: :user, content: content)
|
|
|
|
|
|
|
|
result = nil
|
2023-12-19 15:58:38 -05:00
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
result =
|
|
|
|
llm.generate(
|
2024-08-21 10:41:55 -04:00
|
|
|
prompt,
|
2024-01-04 07:53:47 -05:00
|
|
|
temperature: 0,
|
2024-08-14 14:54:09 -04:00
|
|
|
max_tokens: 700, # ~500 words
|
2024-01-04 07:53:47 -05:00
|
|
|
user: Discourse.system_user,
|
2024-05-13 23:28:46 -04:00
|
|
|
feature_name: "llm_triage",
|
2024-10-23 01:49:56 -04:00
|
|
|
feature_context: {
|
|
|
|
automation_id: automation&.id,
|
|
|
|
automation_name: automation&.name,
|
|
|
|
},
|
2024-08-14 14:54:09 -04:00
|
|
|
)&.strip
|
2023-12-18 20:04:15 -05:00
|
|
|
|
2024-08-22 17:32:42 -04:00
|
|
|
if result.present? && result.downcase.include?(search_for_text.downcase)
|
2023-12-18 20:04:15 -05:00
|
|
|
user = User.find_by_username(canned_reply_user) if canned_reply_user.present?
|
|
|
|
user = user || Discourse.system_user
|
|
|
|
if canned_reply.present?
|
|
|
|
PostCreator.create!(
|
|
|
|
user,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
raw: canned_reply,
|
|
|
|
reply_to_post_number: post.post_number,
|
|
|
|
skip_validations: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
changes = {}
|
|
|
|
changes[:category_id] = category_id if category_id.present?
|
|
|
|
changes[:tags] = tags if SiteSetting.tagging_enabled? && tags.present?
|
|
|
|
|
|
|
|
if changes.present?
|
|
|
|
first_post = post.topic.posts.where(post_number: 1).first
|
|
|
|
changes[:bypass_bump] = true
|
|
|
|
changes[:skip_validations] = true
|
|
|
|
first_post.revise(Discourse.system_user, changes)
|
|
|
|
end
|
|
|
|
|
|
|
|
post.topic.update!(visible: false) if hide_topic
|
2024-02-28 20:33:28 -05:00
|
|
|
|
2024-08-14 14:54:09 -04:00
|
|
|
if flag_post
|
|
|
|
score_reason =
|
|
|
|
I18n
|
|
|
|
.t("discourse_automation.scriptables.llm_triage.flagged_post")
|
|
|
|
.sub("%%LLM_RESPONSE%%", result)
|
|
|
|
.sub("%%AUTOMATION_ID%%", automation&.id.to_s)
|
|
|
|
.sub("%%AUTOMATION_NAME%%", automation&.name.to_s)
|
|
|
|
|
2024-10-04 02:11:30 -04:00
|
|
|
if flag_type == :spam
|
|
|
|
PostActionCreator.new(
|
|
|
|
Discourse.system_user,
|
|
|
|
post,
|
|
|
|
PostActionType.types[:spam],
|
|
|
|
message: score_reason,
|
|
|
|
queue_for_review: true,
|
|
|
|
).perform
|
|
|
|
else
|
|
|
|
reviewable =
|
|
|
|
ReviewablePost.needs_review!(target: post, created_by: Discourse.system_user)
|
|
|
|
|
|
|
|
reviewable.add_score(
|
|
|
|
Discourse.system_user,
|
|
|
|
ReviewableScore.types[:needs_approval],
|
|
|
|
reason: score_reason,
|
|
|
|
force_review: true,
|
|
|
|
)
|
|
|
|
end
|
2024-08-14 14:54:09 -04:00
|
|
|
end
|
2023-12-18 20:04:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|