2024-01-29 14:04:25 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Configuration
|
|
|
|
class LlmValidator
|
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
|
|
|
if val == ""
|
2024-08-30 16:16:11 -04:00
|
|
|
@parent_module_name = modules_and_choose_llm_settings.invert[@opts[:name]]
|
2024-06-19 17:01:35 -04:00
|
|
|
|
2024-08-30 16:16:11 -04:00
|
|
|
@parent_enabled = SiteSetting.public_send(@parent_module_name)
|
2024-01-29 14:04:25 -05:00
|
|
|
return !@parent_enabled
|
|
|
|
end
|
|
|
|
|
2024-08-28 14:57:58 -04:00
|
|
|
allowed_seeded_model?(val)
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
run_test(val).tap { |result| @unreachable = result }
|
|
|
|
rescue StandardError => e
|
|
|
|
raise e if Rails.env.test?
|
2024-06-19 17:01:35 -04:00
|
|
|
@unreachable = true
|
|
|
|
false
|
|
|
|
end
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def run_test(val)
|
2024-06-19 17:01:35 -04:00
|
|
|
DiscourseAi::Completions::Llm
|
2024-07-30 12:44:57 -04:00
|
|
|
.proxy(val)
|
2024-06-19 17:01:35 -04:00
|
|
|
.generate("How much is 1 + 1?", user: nil, feature_name: "llm_validator")
|
|
|
|
.present?
|
|
|
|
end
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
def modules_using(llm_model)
|
|
|
|
choose_llm_settings = modules_and_choose_llm_settings.values
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
choose_llm_settings.select { |s| SiteSetting.public_send(s) == "custom:#{llm_model.id}" }
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
if @parent_enabled
|
|
|
|
return(
|
|
|
|
I18n.t(
|
|
|
|
"discourse_ai.llm.configuration.disable_module_first",
|
2024-08-30 16:16:11 -04:00
|
|
|
setting: @parent_module_name,
|
2024-01-29 14:04:25 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-08-28 14:57:58 -04:00
|
|
|
if @invalid_seeded_model
|
|
|
|
return I18n.t("discourse_ai.llm.configuration.invalid_seeded_model")
|
|
|
|
end
|
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
return unless @unreachable
|
2024-01-29 14:04:25 -05:00
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
I18n.t("discourse_ai.llm.configuration.model_unreachable")
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
def choose_llm_setting_for(module_enabler_setting)
|
|
|
|
modules_and_choose_llm_settings[module_enabler_setting]
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
2024-06-19 17:01:35 -04:00
|
|
|
def modules_and_choose_llm_settings
|
|
|
|
{
|
|
|
|
ai_embeddings_semantic_search_enabled: :ai_embeddings_semantic_search_hyde_model,
|
2024-08-12 18:40:23 -04:00
|
|
|
ai_helper_enabled: :ai_helper_model,
|
2024-07-03 20:48:18 -04:00
|
|
|
ai_summarization_enabled: :ai_summarization_model,
|
2024-06-19 17:01:35 -04:00
|
|
|
}
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
2024-08-28 14:57:58 -04:00
|
|
|
|
|
|
|
def allowed_seeded_model?(val)
|
|
|
|
id = val.split(":").last
|
|
|
|
return true if id.to_i > 0
|
|
|
|
|
|
|
|
setting = @opts[:name]
|
|
|
|
allowed_list = SiteSetting.public_send("#{setting}_allowed_seeded_models")
|
|
|
|
|
|
|
|
if allowed_list.split("|").exclude?(id)
|
|
|
|
@invalid_seeded_model = true
|
|
|
|
raise Discourse::InvalidParameters.new
|
|
|
|
end
|
|
|
|
end
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|