A platform for community discussion. Free, open, simple.
FIX: Ensure regeneration always uses the correct llm (#36585)
Sam521f805fb690
diff --git a/Gemfile.lock b/Gemfile.lockindex ea1f268a829..ef4cd913cb9 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -143,7 +143,7 @@ GEMdiscourse-seed-fu (2.3.12)activerecord (>= 3.1)activesupport (>= 3.1)- discourse_ai-tokenizers (0.3.1)+ discourse_ai-tokenizers (0.3.2)activesupport (>= 6.0)tiktoken_ruby (~> 0.0.11.1)tokenizers (~> 0.5.4)@@ -961,7 +961,7 @@ CHECKSUMSdiscourse-emojis (1.0.44) sha256=63425a32379fb471d58f5eac9a1e931fc5a94efb7d327f71f1580af4dfe4edb3discourse-fonts (0.0.19) sha256=78d4ddd671615908303a675427039d8d787c935e6deae184c6e143c18c6e0033discourse-seed-fu (2.3.12) sha256=4f61d95c11ed54609046cd04eb3a51b531c5fa863fa86d1bea7d74264e5c75e4- discourse_ai-tokenizers (0.3.1) sha256=a7752f2f7b4b29fea148e2c893b287a1478c4dc8828f684c09e51d5a33bbc373+ discourse_ai-tokenizers (0.3.2) sha256=eba14edf6ac94c5ebe68866bd6eaf1a119bc8019e150e5c4c767b8907e907b21discourse_dev_assets (0.0.5) sha256=a09a801a210aa3f7247dd382dfd73b389d4bd02be86be675eca2d451f9898285docile (1.4.1) sha256=96159be799bfa73cdb721b840e9802126e4e03dfc26863db73647204c727f21edrb (2.2.3) sha256=0b00d6fdb50995fe4a45dea13663493c841112e4068656854646f418fda13373diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rbindex 1584adcdd6e..2bf7a199c66 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb@@ -54,14 +54,18 @@ module DiscourseAiguardian.ensure_can_see!(prompt_post)persona_id = retry_persona_id(post, prompt_post)+ llm_model_id = post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD]- Jobs.enqueue(- :create_ai_reply,+ args = {post_id: prompt_post.id,bot_user_id: post.user_id,persona_id: persona_id,reply_post_id: post.id,- )+ }++ args[:llm_model_id] = llm_model_id.to_i if !llm_model_id.to_i.zero?++ Jobs.enqueue(:create_ai_reply, args)render json: success_jsonenddiff --git a/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb b/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rbindex c505e32a9cf..98924363d9d 100644--- a/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb+++ b/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb@@ -11,12 +11,14 @@ module Jobsreturn if args[:reply_post_id].present? && reply_post.nil?return if reply_post && reply_post.topic_id != post.topic_idpersona_id = args[:persona_id]+ llm_model_id = args[:llm_model_id]beginpersona = DiscourseAi::Personas::Persona.find_by(user: post.user, id: persona_id)raise DiscourseAi::Personas::Bot::BOT_NOT_FOUND if persona.nil?- bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona.new)+ llm_model = LlmModel.find_by(id: llm_model_id.to_i) if !llm_model_id.to_i.zero?+ bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona.new, model: llm_model)DiscourseAi::AiBot::Playground.new(bot).reply_to(post,diff --git a/plugins/discourse-ai/lib/ai_bot.rb b/plugins/discourse-ai/lib/ai_bot.rbindex 562c2aa3690..6e745e058a5 100644--- a/plugins/discourse-ai/lib/ai_bot.rb+++ b/plugins/discourse-ai/lib/ai_bot.rb@@ -5,6 +5,7 @@ module DiscourseAiUSER_AGENT = "Discourse AI Bot 1.0 (https://www.discourse.org)"TOPIC_AI_BOT_PM_FIELD = "is_ai_bot_pm"POST_AI_LLM_NAME_FIELD = "ai_llm_name"+ POST_AI_LLM_MODEL_ID_FIELD = "ai_llm_model_id"POST_AI_PERSONA_ID_FIELD = "ai_persona_id"endenddiff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex 9a56b59444b..f81aec146de 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -515,6 +515,7 @@ module DiscourseAiskip_guardian: true,custom_fields: {DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,+ DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => bot.llm.llm_model.id,DiscourseAi::AiBot::POST_AI_PERSONA_ID_FIELD => bot.persona.id,},)@@ -524,6 +525,10 @@ module DiscourseAi.llm.llm_model.display_name+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD] = bot+ .llm+ .llm_model+ .idreply_post.custom_fields[DiscourseAi::AiBot::POST_AI_PERSONA_ID_FIELD] = bot.persona.idreply_post.save_custom_fields@@ -607,6 +612,7 @@ module DiscourseAiskip_guardian: true,custom_fields: {DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,+ DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => bot.llm.llm_model.id,DiscourseAi::AiBot::POST_AI_PERSONA_ID_FIELD => bot.persona.id,},)diff --git a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rbindex 4a9421fca77..9148ab85f18 100644--- a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb@@ -180,6 +180,78 @@ RSpec.describe DiscourseAi::AiBot::BotController doexpect(response.status).to eq(404)end++ it "allows retrying if LLM model has a negative id (seeded)" do+ seeded_llm_model =+ Fabricate(+ :llm_model,+ id: -9999,+ user: bot_user,+ enabled_chat_bot: true,+ name: "second-model",+ )++ bot = DiscourseAi::Personas::Bot.as(bot_user, persona: persona, model: seeded_llm_model)+ DiscourseAi::Completions::Llm.with_prepared_responses(["first try"], llm: seeded_llm_model) do+ DiscourseAi::AiBot::Playground.new(bot).reply_to(prompt_post)+ end++ reply = pm_topic.reload.posts.last+ original_llm_id = reply.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD]++ expect(original_llm_id.to_i).to eq(-9999)++ retry_text = "retry with original model"++ DiscourseAi::Completions::Llm.with_prepared_responses([retry_text], llm: seeded_llm_model) do+ post "/discourse-ai/ai-bot/post/#{reply.id}/retry"++ job_args = Jobs::CreateAiReply.jobs.last["args"].first+ expect(job_args["llm_model_id"]).to eq(-9999)++ Jobs::CreateAiReply.new.execute(job_args.symbolize_keys)+ end++ expect(response.status).to eq(200)+ expect(reply.reload.raw).to eq(retry_text)+ end++ it "uses the original LLM model when retrying even if persona default changed" do+ second_bot_user = Fabricate(:user)+ second_llm_model =+ Fabricate(:llm_model, user: second_bot_user, enabled_chat_bot: true, name: "second-model")++ pm_topic.topic_allowed_users.find_or_create_by!(user: second_bot_user)++ original_llm_name = reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD]+ original_llm_id = reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD]++ expect(original_llm_name).to be_present+ expect(original_llm_id.to_i).to eq(llm_model.id)++ ai_persona.update!(default_llm_id: second_llm_model.id)+ AiPersona.persona_cache.flush!++ retry_text = "retry with original model"++ DiscourseAi::Completions::Llm.with_prepared_responses([retry_text], llm: llm_model) do+ post "/discourse-ai/ai-bot/post/#{reply_post.id}/retry"++ job_args = Jobs::CreateAiReply.jobs.last["args"].first+ expect(job_args["llm_model_id"]).to eq(llm_model.id)++ Jobs::CreateAiReply.new.execute(job_args.symbolize_keys)+ end++ expect(response.status).to eq(200)+ expect(reply_post.reload.raw).to eq(retry_text)+ expect(reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD]).to eq(+ original_llm_name,+ )+ expect(reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD].to_i).to eq(+ original_llm_id.to_i,+ )+ endenddescribe "#show_bot_username" do