module RubyLLM::Streaming
Handles streaming responses from AI providers.
Public Instance Methods
Source
# File lib/ruby_llm/streaming.rb, line 35 def handle_stream(&block) build_on_data_handler do |data| block.call(build_chunk(data)) if data.is_a?(Hash) end end
# File lib/ruby_llm/streaming.rb, line 12 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