Module: OllamaChat::MessageOutput

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

Overview

A module that provides output functionality for chat messages.

This module encapsulates methods for piping assistant responses to command standard input and writing assistant responses to files. It handles the mechanics of sending output to external processes or saving content to disk while providing appropriate error handling and user feedback.

Examples:

Piping a response to a command

chat.pipe('cat > output.txt')

Writing a response to a file

chat.output('response.txt')

Instance Method Summary collapse

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.

Parameters:

  • filename (String)

    the path to the file where the output should be written

Returns:

  • (Chat)

    returns self on success, nil on failure

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ollama_chat/message_output.rb', line 53

def output(filename)
  if message = @messages.last and message.role == 'assistant'
    begin
      write_file_unless_exist(filename, message)
      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

Parameters:

  • cmd (String)

    the command to which the output should be piped

Returns:

  • (OllamaChat::Chat)

    returns self

  • (nil)

    returns nil if the command is not provided or if there is



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ollama_chat/message_output.rb', line 22

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

#write_file_unless_exist(filename, message) ⇒ TrueClass? (private)

The write_file_unless_exist method creates a new file with the specified message content, but only if a file with that name does not already exist.

Parameters:

  • filename (String)

    the path of the file to be created

  • message (Ollama::Message)

    the message object containing the content to write

Returns:

  • (TrueClass)

    if the file was successfully created

  • (nil)

    if the file already exists and was not created



77
78
79
80
81
82
83
84
85
86
# File 'lib/ollama_chat/message_output.rb', line 77

def write_file_unless_exist(filename, message)
  if File.exist?(filename)
    STDERR.puts "File #{filename.inspect} already exists. Choose another filename."
    return
  end
  File.open(filename, ?w) do |output|
    output.write(message.content)
  end
  true
end