From 016d5d1479e32ca21422e50935060379e0663ba8 Mon Sep 17 00:00:00 2001 From: merefield Date: Wed, 30 Oct 2024 08:24:26 +0000 Subject: [PATCH 1/2] IMPROVE: greeting logic --- .../discourse_chatbot/chatbot_controller.rb | 13 ++++++++----- lib/discourse_chatbot/bots/open_ai_bot_rag.rb | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/controllers/discourse_chatbot/chatbot_controller.rb b/app/controllers/discourse_chatbot/chatbot_controller.rb index 69138a6..b328a8f 100644 --- a/app/controllers/discourse_chatbot/chatbot_controller.rb +++ b/app/controllers/discourse_chatbot/chatbot_controller.rb @@ -20,12 +20,12 @@ def start_bot_convo kick_off_statement = I18n.t("chatbot.quick_access_kick_off.announcement") - if SiteSetting.chatbot_user_fields_collection + trust_level = ::DiscourseChatbot::EventEvaluation.new.trust_level(current_user.id) + opts = { trust_level: trust_level, user_id: current_user.id } - trust_level = ::DiscourseChatbot::EventEvaluation.new.trust_level(current_user.id) - opts = { trust_level: trust_level, user_id: current_user.id } + start_bot = ::DiscourseChatbot::OpenAiBotRag.new(opts, false) - start_bot = ::DiscourseChatbot::OpenAiBotRag.new(opts, false) + if SiteSetting.chatbot_user_fields_collection && start_bot.has_empty_user_fields?(opts) system_message = { "role": "system", "content": I18n.t("chatbot.prompt.system.rag.private", current_date_time: DateTime.current) } assistant_message = { "role": "assistant", "content": I18n.t("chatbot.prompt.quick_access_kick_off.announcement", username: current_user.username) } @@ -76,7 +76,10 @@ def start_bot_convo last_chat = ::Chat::Message.find_by(id: chat_channel.latest_not_deleted_message_id) - if (last_chat && (over_quota && last_chat.message != I18n.t('chatbot.errors.overquota') || !over_quota && last_chat.message != I18n.t("chatbot.quick_access_kick_off.announcement"))) || last_chat.nil? + if (last_chat && + (over_quota && last_chat.message != I18n.t('chatbot.errors.overquota') || + !over_quota && last_chat.message != kick_off_statement)) || + last_chat.nil? Chat::CreateMessage.call( params: { chat_channel_id: chat_channel_id, diff --git a/lib/discourse_chatbot/bots/open_ai_bot_rag.rb b/lib/discourse_chatbot/bots/open_ai_bot_rag.rb index 51684fc..ede421a 100644 --- a/lib/discourse_chatbot/bots/open_ai_bot_rag.rb +++ b/lib/discourse_chatbot/bots/open_ai_bot_rag.rb @@ -43,7 +43,7 @@ def get_response(prompt, opts) if private_discussion system_message = { "role": "system", "content": I18n.t("chatbot.prompt.system.rag.private", current_date_time: DateTime.current) } - if SiteSetting.chatbot_user_fields_collection + if SiteSetting.chatbot_user_fields_collection && has_empty_user_fields?(opts) system_message[:content] += " " + get_system_message_suffix(opts) end else @@ -72,6 +72,18 @@ def get_response(prompt, opts) } end + def has_empty_user_fields?(opts) + UserField.where(editable: true).order(:id).each do |user_field| + user_field_type = user_field.field_type_enum + next unless ["dropdown", "confirm", "text"].include?(user_field_type) + if !::UserCustomField.where(user_id: opts[:user_id], name: "user_field_#{UserField.find_by(name: user_field.name).id}" ).exists? || + ::UserCustomField.where(user_id: opts[:user_id], name: "user_field_#{UserField.find_by(name: user_field.name).id}" ).first.value.blank? + return true + end + end + false + end + def get_system_message_suffix(opts) system_message_suffix = "" system_message_suffix_array = [] @@ -79,6 +91,8 @@ def get_system_message_suffix(opts) user_field_options = [] user_field_id = user_field.id user_field_type = user_field.field_type_enum + next unless ["dropdown", "confirm", "text"].include?(user_field_type) + if user_field_type == "dropdown" UserFieldOption.where(user_field_id: user_field_id).each do |option| user_field_options << option.value @@ -142,6 +156,8 @@ def merge_functions(opts) if opts[:private] && SiteSetting.chatbot_user_fields_collection start_length = functions.length UserField.where(editable: true).order(:id).each do |user_field| + user_field_type = user_field.field_type_enum + next unless ["dropdown", "confirm", "text"].include?(user_field_type) if !::UserCustomField.where(user_id: opts[:user_id], name: "user_field_#{UserField.find_by(name: user_field.name).id}" ).exists? || ::UserCustomField.where(user_id: opts[:user_id], name: "user_field_#{UserField.find_by(name: user_field.name).id}" ).first.value.blank? functions << ::DiscourseChatbot::UserFieldFunction.new(user_field.name, opts[:user_id]) From 28ffcce68550600bcdb283c528503f233269bb47 Mon Sep 17 00:00:00 2001 From: merefield Date: Wed, 30 Oct 2024 08:24:56 +0000 Subject: [PATCH 2/2] bump patch --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 10d2801..ab1b511 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-chatbot # about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages -# version: 1.4.2 +# version: 1.4.3 # authors: merefield # url: https://github.com/merefield/discourse-chatbot