2024-01-04 08:44:07 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
class Playground
|
2024-02-15 00:37:59 -05:00
|
|
|
attr_reader :bot
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
# An abstraction to manage the bot and topic interactions.
|
|
|
|
# The bot will take care of completions while this class updates the topic title
|
|
|
|
# and stream replies.
|
|
|
|
|
|
|
|
REQUIRE_TITLE_UPDATE = "discourse-ai-title-update"
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
def self.schedule_reply(post)
|
|
|
|
bot_ids = DiscourseAi::AiBot::EntryPoint::BOT_USER_IDS
|
|
|
|
|
|
|
|
return if bot_ids.include?(post.user_id)
|
|
|
|
if AiPersona.mentionables.any? { |mentionable| mentionable[:user_id] == post.user_id }
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
bot_user = nil
|
|
|
|
mentioned = nil
|
|
|
|
|
|
|
|
if post.topic.private_message?
|
|
|
|
bot_user = post.topic.topic_allowed_users.where(user_id: bot_ids).first&.user
|
|
|
|
end
|
|
|
|
|
|
|
|
if AiPersona.mentionables.length > 0
|
|
|
|
mentions = post.mentions.map(&:downcase)
|
|
|
|
mentioned =
|
|
|
|
AiPersona.mentionables.find do |mentionable|
|
|
|
|
mentions.include?(mentionable[:username]) &&
|
|
|
|
(post.user.group_ids & mentionable[:allowed_group_ids]).present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# PM always takes precedence
|
|
|
|
if mentioned && !bot_user
|
|
|
|
model_without_provider = mentioned[:default_llm].split(":").last
|
|
|
|
user_id =
|
|
|
|
DiscourseAi::AiBot::EntryPoint.map_bot_model_to_user_id(model_without_provider)
|
|
|
|
|
|
|
|
if !user_id
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Model #{mentioned[:default_llm]} not found for persona #{mentioned[:username]}",
|
|
|
|
)
|
|
|
|
if Rails.env.development? || Rails.env.test?
|
|
|
|
raise "Model #{mentioned[:default_llm]} not found for persona #{mentioned[:username]}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
bot_user = User.find_by(id: user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if bot_user
|
|
|
|
persona_id = mentioned&.dig(:id) || post.topic.custom_fields["ai_persona_id"]
|
|
|
|
persona = nil
|
|
|
|
|
|
|
|
if persona_id
|
|
|
|
persona =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(user: post.user, id: persona_id.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !persona && persona_name = post.topic.custom_fields["ai_persona"]
|
|
|
|
persona =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(user: post.user, name: persona_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
persona ||= DiscourseAi::AiBot::Personas::General
|
|
|
|
|
|
|
|
bot = DiscourseAi::AiBot::Bot.as(bot_user, persona: persona.new)
|
|
|
|
new(bot).update_playground_with(post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def initialize(bot)
|
|
|
|
@bot = bot
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_playground_with(post)
|
2024-02-15 00:37:59 -05:00
|
|
|
if can_attach?(post)
|
|
|
|
schedule_playground_titling(post)
|
|
|
|
schedule_bot_reply(post)
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_context(post)
|
|
|
|
# Pay attention to the `post_number <= ?` here.
|
|
|
|
# We want to inject the last post as context because they are translated differently.
|
2024-02-15 00:37:59 -05:00
|
|
|
|
|
|
|
# also setting default to 40, allowing huge contexts costs lots of tokens
|
|
|
|
max_posts = 40
|
|
|
|
if bot.persona.class.respond_to?(:max_context_posts)
|
|
|
|
max_posts = bot.persona.class.max_context_posts || 40
|
|
|
|
end
|
|
|
|
|
|
|
|
post_types = [Post.types[:regular]]
|
|
|
|
post_types << Post.types[:whisper] if post.post_type == Post.types[:whisper]
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
context =
|
|
|
|
post
|
|
|
|
.topic
|
|
|
|
.posts
|
|
|
|
.includes(:user)
|
|
|
|
.joins("LEFT JOIN post_custom_prompts ON post_custom_prompts.post_id = posts.id")
|
|
|
|
.where("post_number <= ?", post.post_number)
|
|
|
|
.order("post_number desc")
|
2024-02-15 00:37:59 -05:00
|
|
|
.where("post_type in (?)", post_types)
|
|
|
|
.limit(max_posts)
|
2024-01-04 08:44:07 -05:00
|
|
|
.pluck(:raw, :username, "post_custom_prompts.custom_prompt")
|
|
|
|
|
|
|
|
result = []
|
2024-01-12 12:36:44 -05:00
|
|
|
first = true
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
context.reverse_each do |raw, username, custom_prompt|
|
2024-01-04 08:44:07 -05:00
|
|
|
custom_prompt_translation =
|
|
|
|
Proc.new do |message|
|
|
|
|
# We can't keep backwards-compatibility for stored functions.
|
|
|
|
# Tool syntax requires a tool_call_id which we don't have.
|
|
|
|
if message[2] != "function"
|
|
|
|
custom_context = {
|
|
|
|
content: message[0],
|
2024-01-12 12:36:44 -05:00
|
|
|
type: message[2].present? ? message[2].to_sym : :model,
|
2024-01-04 08:44:07 -05:00
|
|
|
}
|
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
custom_context[:id] = message[1] if custom_context[:type] != :model
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
result << custom_context
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if custom_prompt.present?
|
2024-01-12 12:36:44 -05:00
|
|
|
if first
|
|
|
|
custom_prompt.each(&custom_prompt_translation)
|
|
|
|
first = false
|
|
|
|
else
|
|
|
|
custom_prompt.first(2).each(&custom_prompt_translation)
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
else
|
|
|
|
context = {
|
|
|
|
content: raw,
|
2024-01-12 12:36:44 -05:00
|
|
|
type: (available_bot_usernames.include?(username) ? :model : :user),
|
2024-01-04 08:44:07 -05:00
|
|
|
}
|
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
context[:id] = username if context[:type] == :user
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
result << context
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def title_playground(post)
|
|
|
|
context = conversation_context(post)
|
|
|
|
|
|
|
|
bot
|
|
|
|
.get_updated_title(context, post.user)
|
|
|
|
.tap do |new_title|
|
|
|
|
PostRevisor.new(post.topic.first_post, post.topic).revise!(
|
|
|
|
bot.bot_user,
|
|
|
|
title: new_title.sub(/\A"/, "").sub(/"\Z/, ""),
|
|
|
|
)
|
|
|
|
post.topic.custom_fields.delete(DiscourseAi::AiBot::EntryPoint::REQUIRE_TITLE_UPDATE)
|
|
|
|
post.topic.save_custom_fields
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to(post)
|
|
|
|
reply = +""
|
|
|
|
start = Time.now
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
post_type =
|
|
|
|
post.post_type == Post.types[:whisper] ? Post.types[:whisper] : Post.types[:regular]
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
context = {
|
|
|
|
site_url: Discourse.base_url,
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
site_description: SiteSetting.site_description,
|
|
|
|
time: Time.zone.now,
|
|
|
|
participants: post.topic.allowed_users.map(&:username).join(", "),
|
|
|
|
conversation_context: conversation_context(post),
|
|
|
|
user: post.user,
|
|
|
|
}
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
reply_user = bot.bot_user
|
|
|
|
if bot.persona.class.respond_to?(:user_id)
|
|
|
|
reply_user = User.find_by(id: bot.persona.class.user_id) || reply_user
|
|
|
|
end
|
|
|
|
|
|
|
|
stream_reply = post.topic.private_message?
|
|
|
|
|
|
|
|
# we need to ensure persona user is allowed to reply to the pm
|
|
|
|
if post.topic.private_message?
|
|
|
|
if !post.topic.topic_allowed_users.exists?(user_id: reply_user.id)
|
|
|
|
post.topic.topic_allowed_users.create!(user_id: reply_user.id)
|
|
|
|
end
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
if stream_reply
|
|
|
|
reply_post =
|
|
|
|
PostCreator.create!(
|
|
|
|
reply_user,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
raw: "",
|
|
|
|
skip_validations: true,
|
|
|
|
skip_jobs: true,
|
|
|
|
post_type: post_type,
|
|
|
|
)
|
2024-01-09 07:20:28 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
publish_update(reply_post, { raw: reply_post.cooked })
|
|
|
|
|
|
|
|
redis_stream_key = "gpt_cancel:#{reply_post.id}"
|
|
|
|
Discourse.redis.setex(redis_stream_key, 60, 1)
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
new_custom_prompts =
|
|
|
|
bot.reply(context) do |partial, cancel, placeholder|
|
|
|
|
reply << partial
|
|
|
|
raw = reply.dup
|
|
|
|
raw << "\n\n" << placeholder if placeholder.present?
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
if stream_reply && !Discourse.redis.get(redis_stream_key)
|
2024-01-04 08:44:07 -05:00
|
|
|
cancel&.call
|
|
|
|
reply_post.update!(raw: reply, cooked: PrettyText.cook(reply))
|
|
|
|
end
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
if stream_reply
|
|
|
|
# Minor hack to skip the delay during tests.
|
|
|
|
if placeholder.blank?
|
|
|
|
next if (Time.now - start < 0.5) && !Rails.env.test?
|
|
|
|
start = Time.now
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
Discourse.redis.expire(redis_stream_key, 60)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
publish_update(reply_post, { raw: raw })
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return if reply.blank?
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
if stream_reply
|
|
|
|
# land the final message prior to saving so we don't clash
|
|
|
|
reply_post.cooked = PrettyText.cook(reply)
|
|
|
|
publish_final_update(reply_post)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
reply_post.revise(
|
|
|
|
bot.bot_user,
|
|
|
|
{ raw: reply },
|
|
|
|
skip_validations: true,
|
|
|
|
skip_revision: true,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
reply_post =
|
|
|
|
PostCreator.create!(
|
|
|
|
reply_user,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
raw: reply,
|
|
|
|
skip_validations: true,
|
|
|
|
post_type: post_type,
|
|
|
|
)
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-05 13:21:14 -05:00
|
|
|
# not need to add a custom prompt for a single reply
|
|
|
|
if new_custom_prompts.length > 1
|
|
|
|
reply_post.post_custom_prompt ||= reply_post.build_post_custom_prompt(custom_prompt: [])
|
|
|
|
prompt = reply_post.post_custom_prompt.custom_prompt || []
|
2024-01-04 08:44:07 -05:00
|
|
|
prompt.concat(new_custom_prompts)
|
2024-01-05 13:21:14 -05:00
|
|
|
reply_post.post_custom_prompt.update!(custom_prompt: prompt)
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
2024-01-05 13:21:14 -05:00
|
|
|
|
|
|
|
reply_post
|
2024-01-15 02:51:14 -05:00
|
|
|
ensure
|
2024-02-15 00:37:59 -05:00
|
|
|
publish_final_update(reply_post) if stream_reply
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-01-15 02:51:14 -05:00
|
|
|
def publish_final_update(reply_post)
|
|
|
|
return if @published_final_update
|
|
|
|
if reply_post
|
|
|
|
publish_update(reply_post, { cooked: reply_post.cooked, done: true })
|
|
|
|
# we subscribe at position -2 so we will always get this message
|
|
|
|
# moving all cooked on every page load is wasteful ... this means
|
|
|
|
# we have a benign message at the end, 2 is set to ensure last message
|
|
|
|
# is delivered
|
|
|
|
publish_update(reply_post, { noop: true })
|
|
|
|
@published_final_update = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def can_attach?(post)
|
|
|
|
return false if bot.bot_user.nil?
|
2024-02-15 00:37:59 -05:00
|
|
|
return false if post.topic.private_message? && post.post_type != Post.types[:regular]
|
2024-01-04 08:44:07 -05:00
|
|
|
return false if (SiteSetting.ai_bot_allowed_groups_map & post.user.group_ids).blank?
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
def schedule_playground_titling(post)
|
|
|
|
if post.post_number == 1 && post.topic.private_message?
|
2024-01-04 08:44:07 -05:00
|
|
|
post.topic.custom_fields[REQUIRE_TITLE_UPDATE] = true
|
|
|
|
post.topic.save_custom_fields
|
2024-02-15 00:37:59 -05:00
|
|
|
|
|
|
|
::Jobs.enqueue_in(
|
|
|
|
5.minutes,
|
|
|
|
:update_ai_bot_pm_title,
|
|
|
|
post_id: post.id,
|
|
|
|
bot_user_id: bot.bot_user.id,
|
|
|
|
)
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
2024-02-15 00:37:59 -05:00
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
def schedule_bot_reply(post)
|
|
|
|
persona_id =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.system_personas[bot.persona.class] ||
|
|
|
|
bot.persona.class.id
|
|
|
|
::Jobs.enqueue(
|
|
|
|
:create_ai_reply,
|
2024-01-04 08:44:07 -05:00
|
|
|
post_id: post.id,
|
2024-02-15 00:37:59 -05:00
|
|
|
bot_user_id: bot.bot_user.id,
|
|
|
|
persona_id: persona_id,
|
2024-01-04 08:44:07 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def context(topic)
|
|
|
|
{
|
|
|
|
site_url: Discourse.base_url,
|
|
|
|
site_title: SiteSetting.title,
|
|
|
|
site_description: SiteSetting.site_description,
|
|
|
|
time: Time.zone.now,
|
|
|
|
participants: topic.allowed_users.map(&:username).join(", "),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_update(bot_reply_post, payload)
|
2024-01-15 02:51:14 -05:00
|
|
|
payload = { post_id: bot_reply_post.id, post_number: bot_reply_post.post_number }.merge(
|
|
|
|
payload,
|
|
|
|
)
|
2024-01-04 08:44:07 -05:00
|
|
|
MessageBus.publish(
|
|
|
|
"discourse-ai/ai-bot/topic/#{bot_reply_post.topic_id}",
|
2024-01-15 02:51:14 -05:00
|
|
|
payload,
|
2024-01-04 08:44:07 -05:00
|
|
|
user_ids: bot_reply_post.topic.allowed_user_ids,
|
2024-01-15 02:51:14 -05:00
|
|
|
max_backlog_size: 2,
|
|
|
|
max_backlog_age: 60,
|
2024-01-04 08:44:07 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_bot_usernames
|
|
|
|
@bot_usernames ||= DiscourseAi::AiBot::EntryPoint::BOTS.map(&:second)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|