2023-10-30 10:56:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiHelper
|
|
|
|
class ChatThreadTitler
|
|
|
|
def initialize(thread)
|
|
|
|
@thread = thread
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggested_title
|
2023-11-27 07:33:31 -05:00
|
|
|
return nil if thread_content.blank?
|
2023-10-30 10:56:33 -04:00
|
|
|
|
2023-11-27 11:03:24 -05:00
|
|
|
prompt = CompletionPrompt.enabled_by_name("generate_titles")
|
2023-11-27 07:33:31 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:mode) if !prompt
|
2023-10-30 10:56:33 -04:00
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
response =
|
2023-11-27 11:03:24 -05:00
|
|
|
DiscourseAi::AiHelper::Assistant.new.generate_and_send_prompt(
|
|
|
|
prompt,
|
|
|
|
thread_content,
|
|
|
|
thread.original_message_user,
|
|
|
|
)
|
2023-11-27 07:33:31 -05:00
|
|
|
response.dig(:suggestions)&.first
|
2023-10-30 10:56:33 -04:00
|
|
|
end
|
2023-11-27 11:03:24 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :thread
|
|
|
|
|
|
|
|
def thread_content
|
|
|
|
# Replace me by a proper API call
|
|
|
|
thread
|
|
|
|
.chat_messages
|
|
|
|
.joins(:user)
|
|
|
|
.pluck(:username, :message)
|
|
|
|
.map { |username, message| "#{username}: #{message}" }
|
|
|
|
.join("\n")
|
|
|
|
end
|
2023-10-30 10:56:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|