Class: Legion::TTY::Components::ModelPicker
- Inherits:
-
Object
- Object
- Legion::TTY::Components::ModelPicker
- Includes:
- Logging::Helper
- Defined in:
- lib/legion/tty/components/model_picker.rb
Instance Method Summary collapse
- #available_models ⇒ Object
-
#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.
11 12 13 14 |
# File 'lib/legion/tty/components/model_picker.rb', line 11 def initialize(current_provider: nil, current_model: nil) @current_provider = current_provider @current_model = current_model end |
Instance Method Details
#available_models ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/legion/tty/components/model_picker.rb', line 16 def available_models 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/legion/tty/components/model_picker.rb', line 33 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 => e log.debug { "model picker cancelled: #{e.}" } nil end |