Class: Legion::TTY::Components::ModelPicker
- Inherits:
-
Object
- Object
- Legion::TTY::Components::ModelPicker
- Defined in:
- lib/legion/tty/components/model_picker.rb
Instance Method Summary collapse
-
#available_models ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
-
#initialize(current_provider: nil, current_model: nil) ⇒ ModelPicker
constructor
A new instance of ModelPicker.
- #select_with_prompt(output: $stdout) ⇒ Object
Constructor Details
#initialize(current_provider: nil, current_model: nil) ⇒ ModelPicker
Returns a new instance of ModelPicker.
7 8 9 10 |
# File 'lib/legion/tty/components/model_picker.rb', line 7 def initialize(current_provider: nil, current_model: nil) @current_provider = current_provider @current_model = current_model end |
Instance Method Details
#available_models ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/legion/tty/components/model_picker.rb', line 12 def available_models # rubocop:disable Metrics/CyclomaticComplexity return [] unless defined?(Legion::LLM) && Legion::LLM.respond_to?(:settings) providers = Legion::LLM.settings[:providers] return [] unless providers.is_a?(Hash) models = [] providers.each do |name, config| next unless config.is_a?(Hash) && config[:enabled] model = config[:default_model] || name.to_s current = (name.to_s == @current_provider.to_s) models << { provider: name.to_s, model: model, current: current } end models end |
#select_with_prompt(output: $stdout) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/legion/tty/components/model_picker.rb', line 29 def select_with_prompt(output: $stdout) models = available_models return nil if models.empty? require 'tty-prompt' prompt = ::TTY::Prompt.new(output: output) choices = models.map do |m| indicator = m[:current] ? ' (current)' : '' { name: "#{m[:provider]} / #{m[:model]}#{indicator}", value: m } end prompt.select('Select model:', choices, per_page: 10) rescue ::TTY::Reader::InputInterrupt, Interrupt nil end |