Skip to content

Commit

Permalink
Merge pull request #106 from merefield/pm_auto_title
Browse files Browse the repository at this point in the history
FEATURE: PM Auto Title
  • Loading branch information
merefield authored Jun 20, 2024
2 parents ade8f24 + e435abf commit 9ba1b92
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ en:
chatbot_max_look_behind: "Maximum number of Posts or Chat Messages bot will consider as prompt for completion, the more the more impressive may be its response, but the more costly the interaction will be."
chatbot_strip_quotes: "Determine if quotes are stripped and not sent within prompts (doesn't affect OP)"
chatbot_permitted_in_private_messages: "Allow Chatbot to interact in Private Messages"
chatbot_private_message_auto_title: "Automatically create a Topic title from the prior messages in a Private Message"
chatbot_include_inner_thoughts_in_private_messages: "(FUNCTION DEBUG) Get Chatbot to post its background thinking on Private Messages (RAG only)"
chatbot_permitted_in_chat: "Allow Chatbot to interact in Chat"
chatbot_can_trigger_from_whisper: "Allow Chatbot to be triggered from a whisper (get creative!)"
Expand Down Expand Up @@ -102,6 +103,8 @@ en:
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:
calculator:
description: |
Expand Down
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ plugins:
chatbot_permitted_in_private_messages:
default: true
client: true
chatbot_private_message_auto_title:
client: false
default: true
chatbot_permitted_in_chat:
default: true
client: true
Expand Down
29 changes: 29 additions & 0 deletions lib/discourse_chatbot/post/post_reply_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ def create

new_post = PostCreator.create!(@author, default_opts)

if @is_private_msg && SiteSetting.chatbot_private_message_auto_title && new_post.topic.posts_count < 10
prior_messages = PostPromptUtils.create_prompt(@options)

client = OpenAI::Client.new

model_name =
case @options[:trust_level]
when TRUST_LEVELS[0], TRUST_LEVELS[1], TRUST_LEVELS[2]
SiteSetting.send("chatbot_open_ai_model_custom_" + @options[:trust_level] + "_trust") ?
SiteSetting.send("chatbot_open_ai_model_custom_name_" + @options[:trust_level] + "_trust") :
SiteSetting.send("chatbot_open_ai_model_" + @options[:trust_level] + "_trust")
else
SiteSetting.chatbot_open_ai_model_custom_low_trust ? SiteSetting.chatbot_open_ai_model_custom_name_low_trust : SiteSetting.chatbot_open_ai_model_low_trust
end

res = client.chat(
parameters: {
model: model_name,
messages: prior_messages << { role: "user", content: I18n.t("chatbot.prompt.private_message.title_creation") }
}
)

if !res["error"].present?
topic = ::Topic.find(@topic_or_channel_id)
topic.title = res["choices"][0]["message"]["content"]
topic.save!
end
end

is_private_msg = new_post.topic.private_message?

begin
Expand Down
1 change: 1 addition & 0 deletions lib/discourse_chatbot/reply_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ::DiscourseChatbot
class ReplyCreator

def initialize(options = {})
@options = options
@author = ::User.find_by(id: options[:bot_user_id])
@guardian = Guardian.new(@author)
@reply_to = options[:reply_to_message_or_post_id]
Expand Down

0 comments on commit 9ba1b92

Please sign in to comment.