feat: Add LLM feature router (1/6) (#14839)
## Description Adds the foundation for feature-specific LLM model routing so Captain AI features can resolve their effective provider/model from code defaults and account-level overrides. This fixes the provider metadata key in `config/llm.yml`, adds `Llm::FeatureRouter`, and routes existing `CaptainFeaturable` model defaults through the shared resolver. Fixes https://linear.app/chatwoot/issue/CW-7425/test-new-models ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? - `bundle exec rspec spec/lib/llm/models_spec.rb spec/lib/llm/feature_router_spec.rb spec/models/concerns/captain_featurable_spec.rb` - 23 examples, 0 failures - `bundle exec rubocop lib/llm/models.rb lib/llm/feature_router.rb app/models/concerns/captain_featurable.rb spec/lib/llm/models_spec.rb spec/lib/llm/feature_router_spec.rb spec/models/concerns/captain_featurable_spec.rb` - no offenses - `bundle exec ruby -e "require 'yaml'; config = YAML.load_file('config/llm.yml'); abort('missing providers') unless config['providers']; abort('missing models') unless config['models']; abort('missing features') unless config['features']; puts 'llm.yml ok'"` - `git diff --check` ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
This commit is contained in:
29
lib/llm/feature_router.rb
Normal file
29
lib/llm/feature_router.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module Llm::FeatureRouter
|
||||
class UnknownFeatureError < StandardError; end
|
||||
|
||||
class << self
|
||||
def resolve(feature:, account: nil)
|
||||
feature_key = feature.to_s
|
||||
raise UnknownFeatureError, "Unknown LLM feature: #{feature_key}" unless Llm::Models.feature?(feature_key)
|
||||
|
||||
model = account_model_override(account, feature_key)
|
||||
source = model.present? ? :account_override : :default
|
||||
model ||= Llm::Models.default_model_for(feature_key)
|
||||
|
||||
{
|
||||
feature: feature_key,
|
||||
provider: Llm::Models.provider_for(model),
|
||||
model: model,
|
||||
source: source
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def account_model_override(account, feature_key)
|
||||
model = account&.captain_models&.[](feature_key).presence
|
||||
return unless model
|
||||
return model if Llm::Models.valid_model_for?(feature_key, model)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,30 +2,42 @@ 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 providers = CONFIG.fetch('providers')
|
||||
def models = CONFIG.fetch('models')
|
||||
def features = CONFIG.fetch('features')
|
||||
def feature_keys = features.keys
|
||||
|
||||
def feature?(feature)
|
||||
features.key?(feature.to_s)
|
||||
end
|
||||
|
||||
def default_model_for(feature)
|
||||
CONFIG.dig('features', feature.to_s, 'default')
|
||||
features.dig(feature.to_s, 'default')
|
||||
end
|
||||
|
||||
def models_for(feature)
|
||||
CONFIG.dig('features', feature.to_s, 'models') || []
|
||||
features.dig(feature.to_s, 'models') || []
|
||||
end
|
||||
|
||||
def valid_model_for?(feature, model_name)
|
||||
models_for(feature).include?(model_name.to_s)
|
||||
end
|
||||
|
||||
def model_config(model_name)
|
||||
models[model_name.to_s]
|
||||
end
|
||||
|
||||
def provider_for(model_name)
|
||||
model_config(model_name)&.dig('provider')
|
||||
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]
|
||||
models: models_for(feature_key).map do |model_name|
|
||||
model = model_config(model_name)
|
||||
{
|
||||
id: model_name,
|
||||
display_name: model['display_name'],
|
||||
|
||||
Reference in New Issue
Block a user