Module: RubyLLM::Streaming

Included in:
Provider
Defined in:
lib/ruby_llm/streaming.rb

Overview

Handles streaming responses from AI providers.

Class Method Summary collapse

Class Method Details

.handle_stream(&block) ⇒ Object



31
32
33
34
35
# File 'lib/ruby_llm/streaming.rb', line 31

def handle_stream(&block)
  to_json_stream do |data|
    block.call(build_chunk(data)) if data
  end
end

.stream_response(connection, payload, additional_headers = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_llm/streaming.rb', line 8

def stream_response(connection, payload, additional_headers = {}, &block)
  accumulator = StreamAccumulator.new

  response = connection.post stream_url, payload do |req|
    req.headers = additional_headers.merge(req.headers) unless additional_headers.empty?
    if faraday_1?
      req.options[:on_data] = handle_stream do |chunk|
        accumulator.add chunk
        block.call chunk
      end
    else
      req.options.on_data = handle_stream do |chunk|
        accumulator.add chunk
        block.call chunk
      end
    end
  end

  message = accumulator.to_message(response)
  RubyLLM.logger.debug "Stream completed: #{message.content}"
  message
end