Module: OllamaChat::MessageOutput
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/message_output.rb
Instance Method Summary collapse
-
#output(filename) ⇒ Chat
The output method writes the last assistant message to a file.
-
#pipe(cmd) ⇒ OllamaChat::Chat?
The pipe method forwards the last assistant message to a command’s standard input.
Instance Method Details
#output(filename) ⇒ Chat
Note:
If no assistant message is available, an error message is printed to stderr.
The output method writes the last assistant message to a file.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ollama_chat/message_output.rb', line 41 def output(filename) if = @messages.last and .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) ⇒ OllamaChat::Chat?
The pipe method forwards the last assistant message to a command’s standard input.
no assistant message
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ollama_chat/message_output.rb', line 10 def pipe(cmd) cmd.present? or return if = @messages.last and .role == 'assistant' begin IO.popen(cmd, ?w) do |output| output.write(.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 |