mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-12-20 06:33:32 +00:00
35 lines
762 B
Ruby
35 lines
762 B
Ruby
|
|
# frozen_string_literal: true
|
||
|
|
|
||
|
|
module DiscourseAi
|
||
|
|
module Configuration
|
||
|
|
class SimpleLlmValidator
|
||
|
|
def initialize(opts = {})
|
||
|
|
@opts = opts
|
||
|
|
end
|
||
|
|
|
||
|
|
def valid_value?(val)
|
||
|
|
return true if val == ""
|
||
|
|
|
||
|
|
run_test(val).tap { |result| @unreachable = result }
|
||
|
|
rescue StandardError => e
|
||
|
|
raise e if Rails.env.test?
|
||
|
|
@unreachable = true
|
||
|
|
true
|
||
|
|
end
|
||
|
|
|
||
|
|
def run_test(val)
|
||
|
|
DiscourseAi::Completions::Llm
|
||
|
|
.proxy(val)
|
||
|
|
.generate("How much is 1 + 1?", user: nil, feature_name: "llm_validator")
|
||
|
|
.present?
|
||
|
|
end
|
||
|
|
|
||
|
|
def error_message
|
||
|
|
return unless @unreachable
|
||
|
|
|
||
|
|
I18n.t("discourse_ai.llm.configuration.model_unreachable")
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|