From 1ae2eaca38d3053969c1b422a5c6b098830b17dd Mon Sep 17 00:00:00 2001 From: nvh0412 Date: Mon, 14 Oct 2024 23:21:39 +1100 Subject: [PATCH] FEATURE: Add a new setting to disable the automatic topic title feature of the AI bot Add a new setting with the default set to true to maintain backward compatibility with the current behavior. We can utilize this setting to prevent our Playground from automatically generating the title right after it finishes the reply_to method. --- config/locales/server.en.yml | 1 + config/settings.yml | 2 + lib/ai_bot/playground.rb | 12 +++- spec/lib/modules/ai_bot/playground_spec.rb | 64 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 3f564bcb4..44f90783c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -87,6 +87,7 @@ en: ai_bot_enabled: "Enable the AI Bot module." ai_bot_enable_chat_warning: "Display a warning when PM chat is initiated. Can be overriden by editing the translation string: discourse_ai.ai_bot.pm_warning" + ai_bot_automatic_topic_title: "Automatically set the topic titles based on post contents." ai_bot_allowed_groups: "When the GPT Bot has access to the PM, it will reply to members of these groups." ai_bot_debugging_allowed_groups: "Allow these groups to see a debug button on posts which displays the raw AI request and response" ai_bot_public_sharing_allowed_groups: "Allow these groups to share AI personal messages with the public via a unique publicly available link. Note: if your site requires login, shares will also require login." diff --git a/config/settings.yml b/config/settings.yml index 981105305..27b186b44 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -393,6 +393,8 @@ discourse_ai: ai_bot_enable_chat_warning: default: false client: true + ai_bot_automatic_topic_title: + default: true ai_bot_debugging_allowed_groups: type: group_list list_type: compact diff --git a/lib/ai_bot/playground.rb b/lib/ai_bot/playground.rb index fb41b4f57..257051bd5 100644 --- a/lib/ai_bot/playground.rb +++ b/lib/ai_bot/playground.rb @@ -487,9 +487,7 @@ def reply_to(post) reply_post ensure publish_final_update(reply_post) if stream_reply - if reply_post && post.post_number == 1 && post.topic.private_message? - title_playground(reply_post) - end + title_playground(reply_post) if reply_post && can_automatically_update_title?(post) end def available_bot_usernames @@ -503,6 +501,14 @@ def available_bot_user_ids private + def can_automatically_update_title?(post) + return false if !SiteSetting.ai_bot_automatic_topic_title + return false if !post.topic.private_message? + return false if post.post_number != 1 + + true + end + def available_bot_users @available_bots ||= User.joins("INNER JOIN llm_models llm ON llm.user_id = users.id").where(active: true) diff --git a/spec/lib/modules/ai_bot/playground_spec.rb b/spec/lib/modules/ai_bot/playground_spec.rb index 47f529a47..d98f1002d 100644 --- a/spec/lib/modules/ai_bot/playground_spec.rb +++ b/spec/lib/modules/ai_bot/playground_spec.rb @@ -805,6 +805,70 @@ expect(custom_prompt.last.last).to eq(bot_user.username) end + context "with title_playground" do + let(:playground) { DiscourseAi::AiBot::Playground.new(bot) } + + before { allow(playground).to receive(:title_playground) } + + context "when the ai_bot_automatic_topic_title config is disabled" do + before { SiteSetting.ai_bot_automatic_topic_title = false } + + it "does not update the title" do + response = "This is a suggested title" + + DiscourseAi::Completions::Llm.with_prepared_responses([response]) do + playground.reply_to(first_post) + end + + expect(playground).not_to have_received(:title_playground) + end + end + + context "when the message in the topic is not the private message" do + before { SiteSetting.ai_bot_automatic_topic_title = true } + + it "does not update the title" do + allow(first_post.topic).to receive(:private_message?).and_return(false) + + response = "This is a suggested title" + + DiscourseAi::Completions::Llm.with_prepared_responses([response]) do + playground.reply_to(first_post) + end + + expect(playground).not_to have_received(:title_playground) + end + end + + context "when the post is not the first one in the topic" do + before { SiteSetting.ai_bot_automatic_topic_title = true } + + it "does not update the title" do + response = "This is a suggested title" + + DiscourseAi::Completions::Llm.with_prepared_responses([response]) do + playground.reply_to(second_post) + end + + expect(playground).not_to have_received(:title_playground) + end + end + + context "when the AI bot can automatically update the title" do + before { SiteSetting.ai_bot_automatic_topic_title = true } + + it "updates the title using bot suggestions" do + response = "This is a suggested title" + + DiscourseAi::Completions::Llm.with_prepared_responses([response]) do + playground.reply_to(first_post) + end + + expect(playground).to have_received(:title_playground) + end + end + end + context "with Dall E bot" do before { SiteSetting.ai_openai_api_key = "123" }