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