From b480f13a0ffacd777c56462962482a4a897de498 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Fri, 27 Dec 2024 07:12:29 +0900 Subject: [PATCH] FIX: Prevent LLM enumerator from erroring when spam enabled (#1045) This PR fixes an issue where LLM enumerator would error out when `SiteSetting.ai_spam_detection = true` but there was no `AiModerationSetting.spam` present. Typically, we add an `LlmDependencyValidator` for the setting itself, however, since Spam is unique in that it has it's model set in `AiModerationSetting` instead of a `SiteSetting`, we'll add a simple check here to prevent erroring out. --- lib/configuration/llm_enumerator.rb | 2 +- spec/configuration/llm_enumerator_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 spec/configuration/llm_enumerator_spec.rb diff --git a/lib/configuration/llm_enumerator.rb b/lib/configuration/llm_enumerator.rb index 30a8b6948..1209001d1 100644 --- a/lib/configuration/llm_enumerator.rb +++ b/lib/configuration/llm_enumerator.rb @@ -38,7 +38,7 @@ def self.global_usage rval[model_id] << { type: :ai_embeddings_semantic_search } end - if SiteSetting.ai_spam_detection_enabled + if SiteSetting.ai_spam_detection_enabled && AiModerationSetting.spam.present? model_id = AiModerationSetting.spam[:llm_model_id] rval[model_id] << { type: :ai_spam } end diff --git a/spec/configuration/llm_enumerator_spec.rb b/spec/configuration/llm_enumerator_spec.rb new file mode 100644 index 000000000..771dff357 --- /dev/null +++ b/spec/configuration/llm_enumerator_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +RSpec.describe DiscourseAi::Configuration::LlmEnumerator do + fab!(:fake_model) + + describe "#global_usage" do + before do + SiteSetting.ai_helper_model = "custom:#{fake_model.id}" + SiteSetting.ai_helper_enabled = true + end + + it "returns a hash of Llm models in use globally" do + expect(described_class.global_usage).to eq(fake_model.id => [{ type: :ai_helper }]) + end + + it "doesn't error on spam when spam detection is enabled but moderation setting is missing" do + SiteSetting.ai_spam_detection_enabled = true + expect { described_class.global_usage }.not_to raise_error + end + end +end