feat: abstract models

This commit is contained in:
Shivam Mishra
2025-12-23 19:38:01 +05:30
parent 4eed216560
commit ae3687364d
6 changed files with 106 additions and 141 deletions
-81
View File
@@ -1,81 +0,0 @@
# frozen_string_literal: true
module Llm::ConfigService
CONFIG_PATH = Rails.root.join('config/llm.yml').freeze
class << self
def config
@config ||= load_config
end
def reload!
@config = load_config
end
%w[providers models features].each do |key|
define_method(key) do
config[key] || {}
end
end
def models_for_feature(feature_key)
features.dig(feature_key.to_s, 'models') || []
end
def default_model_for_feature(feature_key)
features.dig(feature_key.to_s, 'default')
end
def model_info(model_name)
models[model_name.to_s]
end
def provider_info(provider_name)
providers[provider_name.to_s]
end
def feature_keys
features.keys
end
def valid_model_for_feature?(feature_key, model_name)
allowed_models = models_for_feature(feature_key)
allowed_models.include?(model_name.to_s)
end
def feature_config(feature_key)
feature = features[feature_key.to_s]
return nil unless feature
{
models: feature['models'].filter_map do |model_name|
model = models[model_name]
next nil unless model
{
id: model_name,
display_name: model['display_name'],
provider: model['provider'],
coming_soon: model['coming_soon'],
credit_multiplier: model['credit_multiplier']
}
end,
default: feature['default']
}
end
def all_features_config
features.keys.index_with do |feature_key|
feature_config(feature_key)
end
end
private
def load_config
return {} unless File.exist?(CONFIG_PATH)
YAML.load_file(CONFIG_PATH) || {}
end
end
end
+43
View File
@@ -0,0 +1,43 @@
# frozen_string_literal: true
module Llm::Models
CONFIG = YAML.load_file(Rails.root.join('config/llm.yml')).freeze
class << self
def providers = CONFIG['providers']
def models = CONFIG['models']
def features = CONFIG['features']
def feature_keys = CONFIG['features'].keys
def default_model_for(feature)
CONFIG.dig('features', feature.to_s, 'default')
end
def models_for(feature)
CONFIG.dig('features', feature.to_s, 'models') || []
end
def valid_model_for?(feature, model_name)
models_for(feature).include?(model_name.to_s)
end
def feature_config(feature_key)
feature = features[feature_key.to_s]
return nil unless feature
{
models: feature['models'].map do |model_name|
model = models[model_name]
{
id: model_name,
display_name: model['display_name'],
provider: model['provider'],
coming_soon: model['coming_soon'],
credit_multiplier: model['credit_multiplier']
}
end,
default: feature['default']
}
end
end
end