Module: OllamaChat::MessageOutput

Included in:
Chat
Defined in:
lib/ollama_chat/message_output.rb

Instance Method Summary collapse

Instance Method Details

#output(filename) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ollama_chat/message_output.rb', line 24

def output(filename)
  if message = @messages.last and message.role == 'assistant'
    begin
      write_file_unless_exist(filename)
      STDOUT.puts "Last response was written to #{filename.inspect}."
      self
    rescue => e
      STDERR.puts "Writing to #{filename.inspect}, caused #{e.class}: #{e}."
    end
  else
    STDERR.puts "No response available to write to #{filename.inspect}."
  end
end

#pipe(cmd) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ollama_chat/message_output.rb', line 2

def pipe(cmd)
  cmd.present? or return
  if message = @messages.last and message.role == 'assistant'
    begin
      IO.popen(cmd, ?w) do |output|
        output.write(message.content)
      end
      exit_code = $?&.exitstatus
      if exit_code == 0
        STDOUT.puts "Last response was piped to #{cmd.inspect}."
      else
        STDERR.puts "Executing #{cmd.inspect}, failed with exit code #{exit_code}."
      end
      self
    rescue => e
      STDERR.puts "Executing #{cmd.inspect}, caused #{e.class}: #{e}."
    end
  else
    STDERR.puts "No response available to output to pipe command #{cmd.inspect}."
  end
end