Class: Durable::Llm::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/durable/llm/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/durable/llm/cli.rb', line 9

def self.exit_on_failure?
  true
end

Instance Method Details

#chatObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/durable/llm/cli.rb', line 57

def chat
  model = options[:model] || 'gpt-3.5-turbo'
  provider_class = Durable::Llm::Providers.model_id_to_provider(model)

  raise "no provider found for model '#{model}'" if provider_class.nil? || provider_class.name.nil?

  provider_name = provider_class.name.split('::').last.downcase.to_sym
  client = Durable::Llm::Client.new(provider_name)

  messages = []
  messages << { role: 'system', content: options[:system] } if options[:system]

  cli = HighLine.new

  cli.say("Chatting with #{model}")
  cli.say("Type 'exit' or 'quit' to exit")
  cli.say("Type '!multi' to enter multiple lines, then '!end' to finish")

  loop do
    input = cli.ask('> ')
    break if %w[exit quit].include?(input.downcase)

    if input == '!multi'
      input = cli.ask("Enter multiple lines. Type '!end' to finish:") do |q|
        q.gather = '!end'
      end
    end

    messages << { role: 'user', content: input }
    params = {
      model: model,
      messages: messages
    }
    params.merge!(options[:option]) if options[:option]

    response = client.completion(params)
    cli.say(response.choices.first.to_s)
    messages << { role: 'assistant', content: response.choices.first.to_s }
  end
end

#modelsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/durable/llm/cli.rb', line 100

def models
  cli = HighLine.new
  cli.say('Available models:')

  Durable::Llm::Providers.providers.each do |provider_name|
    provider_class = Durable::Llm::Providers.const_get(provider_name.to_s.capitalize)
    provider_models = provider_class.models

    cli.say("#{provider_name.to_s.capitalize}:")
    provider_models.each do |model|
      cli.say("  #{model}")
    end
  end
end

#prompt(*prompt) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/durable/llm/cli.rb', line 21

def prompt(*prompt)
  model = options[:model] || 'gpt-3.5-turbo'
  provider_class = Durable::Llm::Providers.model_id_to_provider(model)

  raise "no provider found for model '#{model}'" if provider_class.nil?

  provider_name = provider_class.name.split('::').last.downcase.to_sym
  client = Durable::Llm::Client.new(provider_name)

  messages = []
  messages << { role: 'system', content: options[:system] } if options[:system]
  messages << { role: 'user', content: prompt.join(' ') }

  params = {
    model: model,
    messages: messages
  }
  params.merge!(options[:option]) if options[:option]

  if options[:no_stream] || !client.stream?
    response = client.completion(params)
    puts response.choices.first
  else
    client.stream(params) do |chunk|
      print chunk
      $stdout.flush
    end
  end
end