2024-01-29 16:04:25 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Configuration
|
|
|
|
class LlmValidator
|
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
|
|
|
if val == ""
|
2025-07-16 14:51:32 -07:00
|
|
|
if @opts[:name] == :ai_default_llm_model
|
|
|
|
@parent_module_names = []
|
2024-06-19 18:01:35 -03:00
|
|
|
|
2025-07-16 14:51:32 -07:00
|
|
|
enabled_settings.each do |setting_name|
|
|
|
|
if SiteSetting.public_send(setting_name) == true
|
|
|
|
@parent_module_names << setting_name
|
|
|
|
@parent_enabled = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return !@parent_enabled
|
|
|
|
end
|
2024-01-29 16:04:25 -03:00
|
|
|
end
|
|
|
|
|
2024-07-30 13:44:57 -03:00
|
|
|
run_test(val).tap { |result| @unreachable = result }
|
|
|
|
rescue StandardError => e
|
|
|
|
raise e if Rails.env.test?
|
2025-02-01 06:01:15 +09:00
|
|
|
@unreachable = true
|
2024-12-21 04:52:11 +09:00
|
|
|
true
|
2024-06-19 18:01:35 -03:00
|
|
|
end
|
2024-01-29 16:04:25 -03:00
|
|
|
|
2024-07-30 13:44:57 -03:00
|
|
|
def run_test(val)
|
2024-06-19 18:01:35 -03:00
|
|
|
DiscourseAi::Completions::Llm
|
2024-07-30 13:44:57 -03:00
|
|
|
.proxy(val)
|
2024-06-19 18:01:35 -03:00
|
|
|
.generate("How much is 1 + 1?", user: nil, feature_name: "llm_validator")
|
|
|
|
.present?
|
|
|
|
end
|
2024-01-29 16:04:25 -03:00
|
|
|
|
2024-06-19 18:01:35 -03:00
|
|
|
def modules_using(llm_model)
|
2025-07-16 10:56:18 -07:00
|
|
|
in_use_llms = AiPersona.where.not(default_llm_id: nil).pluck(:default_llm_id)
|
|
|
|
default_llm = SiteSetting.ai_default_llm_model.presence&.to_i
|
2024-01-29 16:04:25 -03:00
|
|
|
|
2025-07-16 10:56:18 -07:00
|
|
|
combined_llms = (in_use_llms + [default_llm]).compact.uniq
|
|
|
|
combined_llms
|
2024-01-29 16:04:25 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2025-07-16 14:51:32 -07:00
|
|
|
if @parent_enabled && @parent_module_names.present?
|
2024-01-29 16:04:25 -03:00
|
|
|
return(
|
|
|
|
I18n.t(
|
2025-07-16 14:51:32 -07:00
|
|
|
"discourse_ai.llm.configuration.disable_modules_first",
|
|
|
|
settings: @parent_module_names.join(", "),
|
2024-01-29 16:04:25 -03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-06-19 18:01:35 -03:00
|
|
|
return unless @unreachable
|
2024-01-29 16:04:25 -03:00
|
|
|
|
2024-06-19 18:01:35 -03:00
|
|
|
I18n.t("discourse_ai.llm.configuration.model_unreachable")
|
2024-01-29 16:04:25 -03:00
|
|
|
end
|
|
|
|
|
2025-07-16 14:51:32 -07:00
|
|
|
def enabled_settings
|
|
|
|
%i[
|
|
|
|
ai_embeddings_semantic_search_enabled
|
|
|
|
ai_helper_enabled
|
|
|
|
ai_summarization_enabled
|
|
|
|
ai_translation_enabled
|
|
|
|
]
|
2024-01-29 16:04:25 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|