FEATURE: Automatic Chat Thread titles (#269)

* FEATURE: Automatic Chat Thread titles

* do not gen title for empty threads

* make it default disabled for now
This commit is contained in:
Rafael dos Santos Silva 2023-10-30 11:56:33 -03:00 committed by GitHub
parent b06380d9fa
commit 3c55ea8fc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 1 deletions

View File

@ -176,6 +176,10 @@ discourse_ai:
default: "3|14" # 3: @staff, 14: @trust_level_4
allow_any: false
refresh: true
ai_helper_automatic_chat_thread_title:
default: false
ai_helper_automatic_chat_thread_title_delay:
default: 5
ai_embeddings_enabled:
default: false

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
module DiscourseAi
module AiHelper
class ChatThreadTitler
def initialize(thread)
@thread = thread
end
def thread_content
# Replace me by a proper API call
@thread
.chat_messages
.joins(:user)
.pluck(:username, :message)
.map { |username, message| "#{username}: #{message}" }
end
def suggested_title
input_hash = { text: thread_content }
return nil if input_hash[:text].blank?
llm_prompt =
DiscourseAi::AiHelper::LlmPrompt
.new
.available_prompts(name_filter: "generate_titles")
.first
prompt = CompletionPrompt.find_by(id: llm_prompt[:id])
raise Discourse::InvalidParameters.new(:mode) if !prompt || !prompt.enabled?
response = DiscourseAi::AiHelper::LlmPrompt.new.generate_and_send_prompt(prompt, input_hash)
response.dig(:suggestions).first
end
end
end
end

View File

@ -3,9 +3,11 @@ module DiscourseAi
module AiHelper
class EntryPoint
def load_files
require_relative "chat_thread_titler"
require_relative "jobs/regular/generate_chat_thread_title"
require_relative "llm_prompt"
require_relative "semantic_categorizer"
require_relative "painter"
require_relative "semantic_categorizer"
require_relative "topic_helper"
end
@ -16,6 +18,16 @@ module DiscourseAi
additional_icons = %w[spell-check language]
additional_icons.each { |icon| plugin.register_svg_icon(icon) }
plugin.on(:chat_thread_created) do |thread|
return unless SiteSetting.composer_ai_helper_enabled
return unless SiteSetting.ai_helper_automatic_chat_thread_title
Jobs.enqueue_in(
SiteSetting.ai_helper_automatic_chat_thread_title_delay.minutes,
:generate_chat_thread_title,
thread_id: thread.id,
)
end
end
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
module Jobs
class GenerateChatThreadTitle < ::Jobs::Base
sidekiq_options queue: "low"
def execute(args)
return unless SiteSetting.ai_helper_automatic_chat_thread_title
return if (thread_id = args[:thread_id]).blank?
thread = ::Chat::Thread.find_by_id(thread_id)
return if thread.nil? || thread.title.present?
title = DiscourseAi::AiHelper::ChatThreadTitler.new(thread).suggested_title
return if title.blank?
# TODO use a proper API that will make the new title update live
thread.update!(title: title)
end
end
end