Class: Ollama::Handlers::Say

Inherits:
Object
  • Object
show all
Includes:
Concern
Defined in:
lib/ollama/handlers/say.rb

Overview

A handler that uses the system’s say command to speak response content.

The Say handler is designed to convert text responses from Ollama API commands into audible speech using the operating system’s native text-to-speech capabilities. It supports customization of voice and interactive modes, making it suitable for applications where audio feedback is preferred over visual display.

Examples:

Using the Say handler with a custom voice

ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Say.new(voice: 'Alex'))

Using the Say handler in interactive mode

ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Say.new(interactive: true))

Instance Attribute Summary collapse

Attributes included from Concern

#output, #result

Instance Method Summary collapse

Methods included from Concern

#to_proc

Constructor Details

#initialize(output: nil, voice: 'Samantha', interactive: nil) ⇒ Say

The initialize method sets up a new handler instance with the specified output destination and configures voice and interactive settings for text-to-speech functionality.

mode setting for speech synthesis, defaults to nil

Parameters:

  • output (IO, nil) (defaults to: nil)

    the output stream to be used for handling responses, defaults to nil

  • voice (String) (defaults to: 'Samantha')

    the voice to be used for speech synthesis, defaults to ‘Samantha’

  • interactive (TrueClass, FalseClass, String, nil) (defaults to: nil)

    the interactive



27
28
29
30
31
32
33
34
35
# File 'lib/ollama/handlers/say.rb', line 27

def initialize(output: nil, voice: 'Samantha', interactive: nil)
  @voice       = voice
  @interactive = interactive
  super(output:)
  unless output
    @output = open_output
    @output_pid = @output.pid
  end
end

Instance Attribute Details

#interactiveTrueClass, ... (readonly)

The interactive attribute reader returns the interactive mode setting associated with the object.

stored in the instance variable

Returns:

  • (TrueClass, FalseClass, String, nil)

    the interactive mode value



47
48
49
# File 'lib/ollama/handlers/say.rb', line 47

def interactive
  @interactive
end

#voiceString (readonly)

The voice attribute reader returns the voice associated with the object.

Returns:

  • (String)

    the voice value stored in the instance variable



40
41
42
# File 'lib/ollama/handlers/say.rb', line 40

def voice
  @voice
end

Instance Method Details

#call(response) ⇒ self

The call method processes a response by printing its content to the output stream.

This method handles the display of response content by extracting text from the response object and writing it to the configured output stream. It manages the output stream state, reopening it if necessary when it has been closed, and ensures proper handling of streaming responses by closing the output stream when the response indicates completion.

Parameters:

  • response (Ollama::Response)

    the response object containing content to be printed

Returns:

  • (self)

    returns the handler instance itself after processing the response



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ollama/handlers/say.rb', line 59

def call(response)
  if @output.closed?
    wait_output_pid
    @output     = open_output
    @output_pid = @output.pid
  end
  if content = response.response || response.message&.content
    @output.print content
  end
  response.done and @output.close
  self
end

#command(voice:, interactive:) ⇒ Array<String> (private)

The command method constructs a say command array with specified voice and interactive options.

This method builds an array representing a system command for the ‘say’ utility, incorporating the provided voice and interactive settings to configure text-to-speech behavior.

mode setting for speech synthesis

Parameters:

  • voice (String)

    the voice to be used for speech synthesis

  • interactive (TrueClass, FalseClass, String, nil)

    the interactive

Returns:

  • (Array<String>)

    an array containing the command and its arguments



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ollama/handlers/say.rb', line 113

def command(voice:, interactive:)
  command = [ 'say' ]
  voice and command.concat([ '-v', voice ])
  case interactive
  when true
    command << '-i'
  when String
    command << '--interactive=%s' % interactive
  end
  command
end

#open_outputIO (private)

The open_output method creates a new IO object for handling speech output.

This method initializes a pipe to the system’s say command, configuring it with the specified voice and interactive settings. It returns an IO object that can be used to write text content which will be converted to speech by the operating system.

Returns:

  • (IO)

    an IO object connected to the say command for text-to-speech conversion



82
83
84
85
86
# File 'lib/ollama/handlers/say.rb', line 82

def open_output
  io = IO.popen(Shellwords.join(command(voice:, interactive:)), 'w')
  io.sync = true
  io
end

#wait_output_pidObject (private)

The wait_output_pid method waits for the output process to complete.

This method checks if there is an active output process ID and waits for it to finish execution. It uses non-blocking wait to avoid hanging the main thread. If the process has already terminated, it handles the Errno::ECHILD exception gracefully without raising an error.



95
96
97
98
99
# File 'lib/ollama/handlers/say.rb', line 95

def wait_output_pid
  @output_pid or return
  Process.wait(@output_pid, Process::WNOHANG | Process::WUNTRACED)
rescue Errno::ECHILD
end