From 36622d5a23ef850d2c0703089848e8aafd54d8ff Mon Sep 17 00:00:00 2001 From: merefield Date: Wed, 19 Jun 2024 23:39:24 +0100 Subject: [PATCH] FEATURE: rename title of PMs based on discussion --- config/locales/server.en.yml | 3 ++ config/settings.yml | 3 ++ .../post/post_reply_creator.rb | 29 +++++++++++++++++++ lib/discourse_chatbot/reply_creator.rb | 1 + 4 files changed, 36 insertions(+) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index f44527d..33d1fe8 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -28,6 +28,7 @@ en: chatbot_max_look_behind: "Maximum number of Posts or Chat Messages bot will consider as prompt for completion, the more the more impressive may be its response, but the more costly the interaction will be." chatbot_strip_quotes: "Determine if quotes are stripped and not sent within prompts (doesn't affect OP)" chatbot_permitted_in_private_messages: "Allow Chatbot to interact in Private Messages" + chatbot_private_message_auto_title: "Automatically create a Topic title from the prior messages in a Private Message" chatbot_include_inner_thoughts_in_private_messages: "(FUNCTION DEBUG) Get Chatbot to post its background thinking on Private Messages (RAG only)" chatbot_permitted_in_chat: "Allow Chatbot to interact in Chat" chatbot_can_trigger_from_whisper: "Allow Chatbot to be triggered from a whisper (get creative!)" @@ -102,6 +103,8 @@ en: title: "The subject of this conversation is %{topic_title}" first_post: "The first thing someone said was %{username} who said %{raw}" post: "%{username} said %{raw}" + private_message: + title_creation: "Create a short Topic title from summarising the prior messages" function: calculator: description: | diff --git a/config/settings.yml b/config/settings.yml index b6d8970..0c6a12f 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -5,6 +5,9 @@ plugins: chatbot_permitted_in_private_messages: default: true client: true + chatbot_private_message_auto_title: + client: false + default: true chatbot_permitted_in_chat: default: true client: true diff --git a/lib/discourse_chatbot/post/post_reply_creator.rb b/lib/discourse_chatbot/post/post_reply_creator.rb index 7c17ae3..fd46b34 100644 --- a/lib/discourse_chatbot/post/post_reply_creator.rb +++ b/lib/discourse_chatbot/post/post_reply_creator.rb @@ -37,6 +37,35 @@ def create new_post = PostCreator.create!(@author, default_opts) + if @is_private_msg && SiteSetting.chatbot_private_message_auto_title && new_post.topic.posts_count < 10 + prior_messages = PostPromptUtils.create_prompt(@options) + + client = OpenAI::Client.new + + model_name = + case @options[:trust_level] + when TRUST_LEVELS[0], TRUST_LEVELS[1], TRUST_LEVELS[2] + SiteSetting.send("chatbot_open_ai_model_custom_" + @options[:trust_level] + "_trust") ? + SiteSetting.send("chatbot_open_ai_model_custom_name_" + @options[:trust_level] + "_trust") : + SiteSetting.send("chatbot_open_ai_model_" + @options[:trust_level] + "_trust") + else + SiteSetting.chatbot_open_ai_model_custom_low_trust ? SiteSetting.chatbot_open_ai_model_custom_name_low_trust : SiteSetting.chatbot_open_ai_model_low_trust + end + + res = client.chat( + parameters: { + model: model_name, + messages: prior_messages << { role: "user", content: I18n.t("chatbot.prompt.private_message.title_creation") } + } + ) + + if !res["error"].present? + topic = ::Topic.find(@topic_or_channel_id) + topic.title = res["choices"][0]["message"]["content"] + topic.save! + end + end + is_private_msg = new_post.topic.private_message? begin diff --git a/lib/discourse_chatbot/reply_creator.rb b/lib/discourse_chatbot/reply_creator.rb index 4226ff6..f23130b 100644 --- a/lib/discourse_chatbot/reply_creator.rb +++ b/lib/discourse_chatbot/reply_creator.rb @@ -3,6 +3,7 @@ module ::DiscourseChatbot class ReplyCreator def initialize(options = {}) + @options = options @author = ::User.find_by(id: options[:bot_user_id]) @guardian = Guardian.new(@author) @reply_to = options[:reply_to_message_or_post_id]