mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-12 17:13:29 +00:00
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:
parent
b06380d9fa
commit
3c55ea8fc0
@ -176,6 +176,10 @@ discourse_ai:
|
|||||||
default: "3|14" # 3: @staff, 14: @trust_level_4
|
default: "3|14" # 3: @staff, 14: @trust_level_4
|
||||||
allow_any: false
|
allow_any: false
|
||||||
refresh: true
|
refresh: true
|
||||||
|
ai_helper_automatic_chat_thread_title:
|
||||||
|
default: false
|
||||||
|
ai_helper_automatic_chat_thread_title_delay:
|
||||||
|
default: 5
|
||||||
|
|
||||||
ai_embeddings_enabled:
|
ai_embeddings_enabled:
|
||||||
default: false
|
default: false
|
||||||
|
37
lib/modules/ai_helper/chat_thread_titler.rb
Normal file
37
lib/modules/ai_helper/chat_thread_titler.rb
Normal 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
|
@ -3,9 +3,11 @@ module DiscourseAi
|
|||||||
module AiHelper
|
module AiHelper
|
||||||
class EntryPoint
|
class EntryPoint
|
||||||
def load_files
|
def load_files
|
||||||
|
require_relative "chat_thread_titler"
|
||||||
|
require_relative "jobs/regular/generate_chat_thread_title"
|
||||||
require_relative "llm_prompt"
|
require_relative "llm_prompt"
|
||||||
require_relative "semantic_categorizer"
|
|
||||||
require_relative "painter"
|
require_relative "painter"
|
||||||
|
require_relative "semantic_categorizer"
|
||||||
require_relative "topic_helper"
|
require_relative "topic_helper"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -16,6 +18,16 @@ module DiscourseAi
|
|||||||
|
|
||||||
additional_icons = %w[spell-check language]
|
additional_icons = %w[spell-check language]
|
||||||
additional_icons.each { |icon| plugin.register_svg_icon(icon) }
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user