Class: Ollama::Handlers::Collector

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

Overview

A handler that collects responses into an array and returns the array as the result.

The Collector handler is designed to accumulate all responses during command execution and provide access to the complete collection of responses through its result method. It is typically used when multiple responses are expected and need to be processed together rather than individually.

Examples:

Using the Collector handler to gather all responses

responses = ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Collector)
# responses will contain an array of all response objects received

Instance Attribute Summary

Attributes included from Concern

#output

Instance Method Summary collapse

Methods included from Concern

#to_proc

Constructor Details

#initialize(output: $stdout) ⇒ Collector

The initialize method sets up a new handler instance with the specified output destination and initializes an empty array for collecting responses.

defaults to $stdout



19
20
21
22
# File 'lib/ollama/handlers/collector.rb', line 19

def initialize(output: $stdout)
  super
  @array = []
end

Instance Method Details

#call(response) ⇒ self

The call method processes a response by appending it to an internal array.

This method is responsible for handling individual responses during command execution by storing them in an internal array for later retrieval. It supports method chaining by returning the handler instance itself after processing.



34
35
36
37
# File 'lib/ollama/handlers/collector.rb', line 34

def call(response)
  @array << response
  self
end

#resultArray<Ollama::Response>?

The result method returns the collected response data from handler operations.

This method provides access to the accumulated results after a command has been executed with a handler that collects responses. It returns the internal array containing all responses that were processed by the handler.



48
49
50
# File 'lib/ollama/handlers/collector.rb', line 48

def result
  @array
end