From a08d168740b897c5a6f0d72ad15d2a886ac77961 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Wed, 28 Aug 2024 15:57:58 -0300 Subject: [PATCH] FEATURE: Initial support for seeded LLMs (#756) --- .../discourse_ai/admin/ai_llms_controller.rb | 8 + app/models/llm_model.rb | 4 + app/serializers/llm_model_serializer.rb | 12 + .../components/ai-llm-editor-form.gjs | 245 ++++++++++-------- config/locales/client.en.yml | 4 +- config/locales/server.en.yml | 2 + config/settings.yml | 25 ++ lib/automation.rb | 8 +- lib/configuration/llm_validator.rb | 19 ++ 9 files changed, 210 insertions(+), 117 deletions(-) diff --git a/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/app/controllers/discourse_ai/admin/ai_llms_controller.rb index ad6ff6b4..243b0a2b 100644 --- a/app/controllers/discourse_ai/admin/ai_llms_controller.rb +++ b/app/controllers/discourse_ai/admin/ai_llms_controller.rb @@ -45,6 +45,10 @@ module DiscourseAi def update llm_model = LlmModel.find(params[:id]) + if llm_model.seeded? + return render_json_error(I18n.t("discourse_ai.llm.cannot_edit_builtin"), status: 403) + end + if llm_model.update(ai_llm_params(updating: llm_model)) llm_model.toggle_companion_user render json: LlmModelSerializer.new(llm_model) @@ -56,6 +60,10 @@ module DiscourseAi def destroy llm_model = LlmModel.find(params[:id]) + if llm_model.seeded? + return render_json_error(I18n.t("discourse_ai.llm.cannot_delete_builtin"), status: 403) + end + in_use_by = DiscourseAi::Configuration::LlmValidator.new.modules_using(llm_model) if !in_use_by.empty? diff --git a/app/models/llm_model.rb b/app/models/llm_model.rb index c1c7ef7a..c411fb14 100644 --- a/app/models/llm_model.rb +++ b/app/models/llm_model.rb @@ -89,6 +89,10 @@ class LlmModel < ActiveRecord::Base provider_params&.dig(key) end + def seeded? + id < 0 + end + private def required_provider_params diff --git a/app/serializers/llm_model_serializer.rb b/app/serializers/llm_model_serializer.rb index ca852409..d515ca62 100644 --- a/app/serializers/llm_model_serializer.rb +++ b/app/serializers/llm_model_serializer.rb @@ -21,4 +21,16 @@ class LlmModelSerializer < ApplicationSerializer def used_by DiscourseAi::Configuration::LlmValidator.new.modules_using(object) end + + def api_key + object.seeded? ? "********" : object.api_key + end + + def url + object.seeded? ? "********" : object.url + end + + def provider + object.seeded? ? "CDCK" : object.provider + end end diff --git a/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/assets/javascripts/discourse/components/ai-llm-editor-form.gjs index 028662c4..1281481c 100644 --- a/assets/javascripts/discourse/components/ai-llm-editor-form.gjs +++ b/assets/javascripts/discourse/components/ai-llm-editor-form.gjs @@ -62,6 +62,10 @@ export default class AiLlmEditorForm extends Component { return this.args.model.used_by?.join(", "); } + get seeded() { + return this.args.model.id < 0; + } + get inUseWarning() { return I18n.t("discourse_ai.llms.in_use_warning", { settings: this.modulesUsingModel, @@ -170,13 +174,19 @@ export default class AiLlmEditorForm extends Component { }