Class: OmniAI::Chat::Stream

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

Overview

A stream given when streaming.

Instance Method Summary collapse

Constructor Details

#initialize(response:) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • response (HTTP::Response)
  • block (Proc)


9
10
11
12
# File 'lib/omniai/chat/stream.rb', line 9

def initialize(response:)
  @response = response
  @parser = EventStreamParser::Parser.new
end

Instance Method Details

#stream! {|OmniAI::Chat::Chunk| ... } ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/omniai/chat/stream.rb', line 15

def stream!
  @response.body.each do |chunk|
    @parser.feed(chunk) do |_, data|
      next if data.eql?('[DONE]')

      yield(Chunk.new(data: JSON.parse(data)))
    end
  end
end