Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Re-introduce quick test validation when selecting embeddings model (#34524)

Roman Rizzi41e87ba91f44
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 744e2bf6e5d..f8eab535a00 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -610,6 +610,7 @@ en:
disable_embeddings: "You have to disable 'ai embeddings enabled' first."
invalid_config: "You selected a invalid option."
choose_model: "Set 'ai embeddings selected model' first."
+ model_test_failed: "We failed to generate a test embedding with this model. Check it's correctly configured."
llm_models:
missing_provider_param: "%{param} can't be blank"
diff --git a/plugins/discourse-ai/lib/configuration/embedding_defs_validator.rb b/plugins/discourse-ai/lib/configuration/embedding_defs_validator.rb
index 6662da33220..7a6b67b426a 100644
--- a/plugins/discourse-ai/lib/configuration/embedding_defs_validator.rb
+++ b/plugins/discourse-ai/lib/configuration/embedding_defs_validator.rb
@@ -8,11 +8,36 @@ module DiscourseAi
end
def valid_value?(val)
- val.present? || !SiteSetting.ai_embeddings_enabled
+ if val.blank?
+ if SiteSetting.ai_embeddings_enabled
+ @disable_embeddings = true
+ return false
+ else
+ return true
+ end
+ end
+
+ embedding_def = EmbeddingDefinition.find_by(id: val)
+ if embedding_def.blank?
+ @missing_record = true
+ return false
+ end
+
+ return true if Rails.env.test? && @opts[:run_check_in_tests].blank?
+
+ DiscourseAi::Embeddings::Vector.new(embedding_def).vector_from("this is a test")
+ true
+ rescue Net::HTTPBadResponse => e
+ false
end
def error_message
- I18n.t("discourse_ai.embeddings.configuration.disable_embeddings")
+ if @disable_embeddings
+ return I18n.t("discourse_ai.embeddings.configuration.disable_embeddings")
+ end
+ return I18n.t("discourse_ai.embeddings.configuration.invalid_config") if @missing_record
+
+ I18n.t("discourse_ai.embeddings.configuration.model_test_failed")
end
end
end
diff --git a/plugins/discourse-ai/lib/configuration/embeddings_model_validator.rb b/plugins/discourse-ai/lib/configuration/embeddings_model_validator.rb
deleted file mode 100644
index 489f2656bd3..00000000000
--- a/plugins/discourse-ai/lib/configuration/embeddings_model_validator.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseAi
- module Configuration
- class EmbeddingsModelValidator
- def initialize(opts = {})
- @opts = opts
- end
-
- def valid_value?(val)
- return true if Rails.env.test?
-
- representation =
- DiscourseAi::Embeddings::VectorRepresentations::Base.find_representation(val)
-
- return false if representation.nil?
-
- if !representation.correctly_configured?
- @representation = representation
- return false
- end
-
- if !can_generate_embeddings?(val)
- @unreachable = true
- return false
- end
-
- true
- end
-
- def error_message
- return(I18n.t("discourse_ai.embeddings.configuration.model_unreachable")) if @unreachable
-
- @representation&.configuration_hint
- end
-
- def can_generate_embeddings?(val)
- vdef = DiscourseAi::Embeddings::VectorRepresentations::Base.find_representation(val).new
- DiscourseAi::Embeddings::Vector.new(vdef).vector_from("this is a test").present?
- end
- end
- end
-end
diff --git a/plugins/discourse-ai/spec/configuration/embedding_defs_validator_spec.rb b/plugins/discourse-ai/spec/configuration/embedding_defs_validator_spec.rb
new file mode 100644
index 00000000000..b6a8e7b016f
--- /dev/null
+++ b/plugins/discourse-ai/spec/configuration/embedding_defs_validator_spec.rb
@@ -0,0 +1,65 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Configuration::EmbeddingDefsValidator do
+ before { enable_current_plugin }
+
+ describe "#valid_value?" do
+ fab!(:embedding_definition)
+ let(:validator) { described_class.new({ run_check_in_tests: true }) }
+
+ context "when resetting the value back to blank" do
+ before do
+ WebMock.stub_request(:post, embedding_definition.url).to_return(
+ status: 200,
+ body: [[1]].to_json,
+ )
+
+ SiteSetting.ai_embeddings_selected_model = embedding_definition.id
+ end
+
+ context "with embeddings enabled" do
+ before { SiteSetting.ai_embeddings_enabled = true }
+
+ it "returns false" do
+ expect(validator.valid_value?("")).to eq(false)
+ expect(validator.error_message).to eq(
+ I18n.t("discourse_ai.embeddings.configuration.disable_embeddings"),
+ )
+ end
+ end
+
+ context "with embeddings disabled" do
+ it "returns true" do
+ expect(validator.valid_value?("")).to eq(true)
+ end
+ end
+ end
+
+ context "when selecting a model" do
+ it "returns false if we cannot find a matching record" do
+ expect(validator.valid_value?(-9_999_999_999_999)).to eq(false)
+ expect(validator.error_message).to eq(
+ I18n.t("discourse_ai.embeddings.configuration.invalid_config"),
+ )
+ end
+
+ it "returns false if the test fails" do
+ WebMock.stub_request(:post, embedding_definition.url).to_return(status: 404)
+
+ expect(validator.valid_value?(embedding_definition.id)).to eq(false)
+ expect(validator.error_message).to eq(
+ I18n.t("discourse_ai.embeddings.configuration.model_test_failed"),
+ )
+ end
+
+ it "returns true if test works" do
+ WebMock.stub_request(:post, embedding_definition.url).to_return(
+ status: 200,
+ body: [[1]].to_json,
+ )
+
+ expect(validator.valid_value?(embedding_definition.id)).to eq(true)
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/embeddings/semantic_related_spec.rb b/plugins/discourse-ai/spec/lib/modules/embeddings/semantic_related_spec.rb
index 620d323d4b7..a28af4f7c7b 100644
--- a/plugins/discourse-ai/spec/lib/modules/embeddings/semantic_related_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/embeddings/semantic_related_spec.rb
@@ -25,14 +25,6 @@ describe DiscourseAi::Embeddings::SemanticRelated do
describe "#related_topic_ids_for" do
it "returns empty array if AI embeddings are disabled" do
SiteSetting.ai_embeddings_enabled = false
- SiteSetting.ai_embeddings_selected_model = 1234
-
- expect(semantic_related.related_topic_ids_for(normal_topic_1)).to eq([])
- end
-
- it "returns empty array if AI embeddings model is invalid" do
- SiteSetting.ai_embeddings_enabled = true
- SiteSetting.ai_embeddings_selected_model = 1234
expect(semantic_related.related_topic_ids_for(normal_topic_1)).to eq([])
end