Skip to content

Commit

Permalink
Merge pull request #116 from merefield/use_api_name
Browse files Browse the repository at this point in the history
IMPROVE: use Chat Completions name attribute to simplify code
  • Loading branch information
merefield authored Aug 20, 2024
2 parents efff3b3 + 761e0ad commit bc6851a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
2 changes: 0 additions & 2 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ en:
private: You are a helpful assistant. You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with. The current date and time is %{current_date_time}. When referring to users by name, include an @ symbol directly in front of their username. Only respond to the last question, using the prior information as context, if appropriate.
illegal_urls: "Your last response contained at least one URL which was not valid, please remove it and try again."
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:
Expand Down
4 changes: 2 additions & 2 deletions lib/discourse_chatbot/message/message_prompt_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.create_prompt(opts)
messages += message_collection.reverse.map do |cm|
username = ::User.find(cm.user_id).username
role = (cm.user_id == bot_user_id ? "assistant" : "user")
text = (cm.user_id == bot_user_id ? "#{cm.message}" : I18n.t("chatbot.prompt.post", username: username, raw: cm.message))
text = cm.message
content = []

if SiteSetting.chatbot_support_vision == "directly"
Expand All @@ -26,7 +26,7 @@ def self.create_prompt(opts)
else
content = text
end
{ "role": role, "content": content }
{ "role": role, "name": username, "content": content }
end

messages
Expand Down
11 changes: 6 additions & 5 deletions lib/discourse_chatbot/post/post_prompt_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ def self.create_prompt(opts)
category_id = opts[:category_id]
first_post_role = post_collection.first.topic.first_post.user.id == bot_user_id ? "assistant" : "user"

messages = [{ "role": first_post_role, "content": I18n.t("chatbot.prompt.title", topic_title: post_collection.first.topic.title) }]
messages = [{ "role": first_post_role, "name": post_collection.first.topic.first_post.user.username, "content": I18n.t("chatbot.prompt.title", topic_title: post_collection.first.topic.title) }]

messages << { "role": first_post_role, "content": I18n.t("chatbot.prompt.first_post", username: post_collection.first.topic.first_post.user.username, raw: post_collection.first.topic.first_post.raw) }
messages << { "role": first_post_role, "name": post_collection.first.topic.first_post.user.username, "content": post_collection.first.topic.first_post.raw }

if original_post_number == 1 && (Array(SiteSetting.chatbot_auto_respond_categories.split("|")).include? category_id.to_s) &&
!CategoryCustomField.find_by(category_id: category_id, name: "chatbot_auto_response_additional_prompt").blank?
messages << { "role": first_post_role, "content": CategoryCustomField.find_by(category_id: category_id, name: "chatbot_auto_response_additional_prompt").value }
messages << { "role": first_post_role, "name": post_collection.first.topic.first_post.user.username, "content": CategoryCustomField.find_by(category_id: category_id, name: "chatbot_auto_response_additional_prompt").value }
end

messages += post_collection.reverse.map do |p|
post_content = p.raw
post_content.gsub!(/\[quote.*?\](.*?)\[\/quote\]/m, '') if SiteSetting.chatbot_strip_quotes
role = (p.user_id == bot_user_id ? "assistant" : "user")
text = (p.user_id == bot_user_id ? "#{p.raw}" : I18n.t("chatbot.prompt.post", username: p.user.username, raw: post_content))
name = p.user.username
text = post_content
content = []

if SiteSetting.chatbot_support_vision == "directly"
Expand All @@ -35,7 +36,7 @@ def self.create_prompt(opts)
else
content = text
end
{ "role": role, "content": content }
{ "role": role, "name": name, "content": content }
end

messages
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: 0.9.46
# version: 1.0.0
# authors: merefield
# url: https://github.com/merefield/discourse-chatbot

Expand Down

0 comments on commit bc6851a

Please sign in to comment.