Class: OmniAI::Chat::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat/stream.rb

Overview

A stream is used to process a series of chunks of data. It converts the following into a combined payload:

{ "id":"...", "choices": [{ "index": 0,"delta": { "role" :"assistant", "content":"" } }] }
{ "id":"...", "choices": [{ "index": 0,"delta": { "content" :"A" } }] }
{ "id":"...", "choices": [{ "index": 0,"delta": { "content" :"B" } }] }
...

Every

Instance Method Summary collapse

Constructor Details

#initialize(chunks:, logger: nil) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • logger (OmniAI::Client) (defaults to: nil)
  • chunks (Enumerable<String>)


16
17
18
19
# File 'lib/omniai/chat/stream.rb', line 16

def initialize(chunks:, logger: nil)
  @chunks = chunks
  @logger = logger
end

Instance Method Details

#stream! {|delta| ... } ⇒ Hash

Yields:

  • (delta)

Yield Parameters:

Returns:

  • (Hash)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omniai/chat/stream.rb', line 25

def stream!(&block)
  @data = { "choices" => [] }

  @chunks.map do |chunk|
    parser.feed(chunk) do |type, data, id|
      process!(type, data, id, &block)
    end
  end

  @data
end