Skip to content

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

FIX: LLM config must be valid before running a test (#34806)

Roman Rizzidf34b9ab718c
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
index 702fcddac40..51ee5957c15 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb
@@ -138,11 +138,15 @@ module DiscourseAi
def test
RateLimiter.new(current_user, "llm_test_#{current_user.id}", 3, 1.minute).performed!
- llm_model = LlmModel.new(ai_llm_params)
-
- DiscourseAi::Configuration::LlmValidator.new.run_test(llm_model)
+ # We don't care about the display_name attr for testing.
+ llm_model = LlmModel.new(ai_llm_params.merge(display_name: "LLM test"))
- render json: { success: true }
+ if llm_model.valid?
+ DiscourseAi::Configuration::LlmValidator.new.run_test(llm_model)
+ render json: { success: true }
+ else
+ render json: { success: false, validation_errors: llm_model.errors.full_messages }
+ end
rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed => e
render json: { success: false, error: e.message }
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
index 5657b49274a..f7941f12a97 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
@@ -26,6 +26,7 @@ export default class AiLlmEditorForm extends Component {
@tracked testRunning = false;
@tracked testResult = null;
@tracked testError = null;
+ @tracked testValidationErrors = null;
@cached
get formData() {
@@ -100,7 +101,11 @@ export default class AiLlmEditorForm extends Component {
}
get testErrorMessage() {
- return i18n("discourse_ai.llms.tests.failure", { error: this.testError });
+ if (this.testValidationErrors?.length > 0) {
+ return i18n("discourse_ai.llms.tests.invalid_config");
+ } else {
+ return i18n("discourse_ai.llms.tests.failure", { error: this.testError });
+ }
}
get displayTestResult() {
@@ -230,8 +235,10 @@ export default class AiLlmEditorForm extends Component {
if (this.testResult) {
this.testError = null;
+ this.testValidationErrors = null;
} else {
this.testError = configTestResult.error;
+ this.testValidationErrors = configTestResult.validation_errors;
}
} catch (e) {
popupAjaxError(e);
@@ -622,6 +629,11 @@ export default class AiLlmEditorForm extends Component {
<div class="ai-llm-editor-tests__failure">
{{icon "xmark"}}
{{this.testErrorMessage}}
+ <ul>
+ {{#each this.testValidationErrors as |error|}}
+ <li>{{error}}</li>
+ {{/each}}
+ </ul>
</div>
{{/if}}
</ConditionalLoadingSpinner>
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index f199db23aad..239137e7c7a 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -694,6 +694,7 @@ en:
title: "Run test"
running: "Running test..."
success: "Success!"
+ invalid_config: "Fix the following errors in your configuration first:"
failure: "Trying to contact the model returned this error: %{error}"
hints:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index a7e6d70b2f7..a1a05955a5d 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -1,4 +1,10 @@
en:
+ activerecord:
+ attributes:
+ llm_model:
+ name: "Model ID"
+ max_prompt_tokens: "Context window"
+
discourse_automation:
ai:
flag_types:
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
index b94c381ffc9..1fb2c526098 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb
@@ -486,6 +486,21 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do
end
end
end
+
+ context "when config is invalid" do
+ it "returns a success false with the validation error" do
+ get "/admin/plugins/discourse-ai/ai-llms/test.json",
+ params: {
+ ai_llm: test_attrs.except(:max_prompt_tokens),
+ }
+
+ expect(response).to be_successful
+ expect(response.parsed_body["success"]).to eq(false)
+ expect(response.parsed_body["validation_errors"]).to contain_exactly(
+ "Context window is not a number",
+ )
+ end
+ end
end
describe "DELETE #destroy" do