Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPROVE: refine greeting logic #126

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/controllers/discourse_chatbot/chatbot_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion lib/discourse_chatbot/bots/open_ai_bot_rag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,13 +72,27 @@ 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 = []
UserField.where(editable: true).order(:id).each do |user_field|
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
Expand Down Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down