Skip to content

Commit

Permalink
IMPROVE: greeting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
merefield committed Oct 30, 2024
1 parent af34f40 commit 016d5d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
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

0 comments on commit 016d5d1

Please sign in to comment.