Class: OmniAI::OpenAI::Chat::Stream

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

Overview

For each chunk yield the text delta. Parse the final content into a response.

Instance Method Summary collapse

Instance Method Details

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

Yields:

  • (delta)

Yield Parameters:

  • delta (OmniAI::Chat::Delta)

Returns:

  • (Hash)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/omniai/openai/chat/stream.rb', line 12

def stream!(&block)
  response = {}

  @chunks.each do |chunk|
    parser.feed(chunk) do |type, data, _id|
      case type
      when /response\.(.*)_text\.delta/
        block.call(OmniAI::Chat::Delta.new(text: JSON.parse(data)["delta"]))
      when "response.completed"
        response = JSON.parse(data)["response"]
      end
    end
  end

  response
end