Class: Ollama::Client::ChatStreamProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/client/chat_stream_processor.rb

Overview

Consumes an NDJSON /api/chat stream, aggregates message fields, and dispatches hooks. Line buffering matches Ollama::GenerateStreamHandler so both NDJSON parsers stay parallel.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hooks, provider: nil) ⇒ ChatStreamProcessor

Returns a new instance of ChatStreamProcessor.

Parameters:



19
20
21
22
# File 'lib/ollama/client/chat_stream_processor.rb', line 19

def initialize(hooks, provider: nil)
  @hooks = hooks
  @provider = provider
end

Class Method Details

.call(res, hooks, provider: nil) ⇒ Hash

Convenience class method to process a stream.

Parameters:

Returns:

  • (Hash)

    the aggregated result



13
14
15
# File 'lib/ollama/client/chat_stream_processor.rb', line 13

def self.call(res, hooks, provider: nil)
  new(hooks, provider: provider).call(res)
end

Instance Method Details

#call(res) ⇒ Hash

Returns the aggregated result.

Parameters:

  • res (Net::HTTPResponse)

Returns:

  • (Hash)

    the aggregated result



26
27
28
29
30
31
32
33
# File 'lib/ollama/client/chat_stream_processor.rb', line 26

def call(res)
  reset_accumulators!

  buffer = +""
  res.read_body { |chunk| drain_chunk(buffer, chunk) }

  build_result
end