Class: Mj::ChatGpt::Commands::AskCommandHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mj/chatgpt/commands/ask_command_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ AskCommandHandler

Returns a new instance of AskCommandHandler.



10
11
12
# File 'lib/mj/chatgpt/commands/ask_command_handler.rb', line 10

def initialize(client:)
  @client = client
end

Instance Method Details

#handle(command) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/mj/chatgpt/commands/ask_command_handler.rb', line 14

def handle(command)
  request = Request.from_config_file(command.options.fetch("config_file"))
  @client.chat(**request.asking(command.question).to_h)
rescue Faraday::Error => exception
  {
    error: exception.message,
    body: exception.response&.dig(:body, "error") || exception.response
  }
end