mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-08 18:29:32 +00:00
* DEV: AI bot migration to the Llm pattern. We added tool and conversation context support to the Llm service in discourse-ai#366, meaning we met all the conditions to migrate this module. This PR migrates to the new pattern, meaning adding a new bot now requires minimal effort as long as the service supports it. On top of this, we introduce the concept of a "Playground" to separate the PM-specific bits from the completion, allowing us to use the bot in other contexts like chat in the future. Commands are called tools, and we simplified all the placeholder logic to perform updates in a single place, making the flow more one-wayish. * Followup fixes based on testing * Cleanup unused inference code * FIX: text-based tools could be in the middle of a sentence * GPT-4-turbo support * Use new LLM API
38 lines
1.2 KiB
Ruby
38 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::Jobs
|
|
class CreateAiReply < ::Jobs::Base
|
|
sidekiq_options retry: false
|
|
|
|
def execute(args)
|
|
return unless bot_user = User.find_by(id: args[:bot_user_id])
|
|
return unless post = Post.includes(:topic).find_by(id: args[:post_id])
|
|
|
|
begin
|
|
persona = nil
|
|
if persona_id = post.topic.custom_fields["ai_persona_id"]
|
|
persona =
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(user: post.user, id: persona_id.to_i)
|
|
raise DiscourseAi::AiBot::Bot::BOT_NOT_FOUND if persona.nil?
|
|
end
|
|
|
|
if !persona && persona_name = post.topic.custom_fields["ai_persona"]
|
|
persona =
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(user: post.user, name: persona_name)
|
|
raise DiscourseAi::AiBot::Bot::BOT_NOT_FOUND if persona.nil?
|
|
end
|
|
|
|
persona ||= DiscourseAi::AiBot::Personas::General
|
|
|
|
bot = DiscourseAi::AiBot::Bot.as(bot_user, persona: persona.new)
|
|
|
|
DiscourseAi::AiBot::Playground.new(bot).reply_to(post)
|
|
rescue DiscourseAi::AiBot::Bot::BOT_NOT_FOUND
|
|
Rails.logger.warn(
|
|
"Bot not found for post #{post.id} - perhaps persona was deleted or bot was disabled",
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|