class RubyLLM::StreamAccumulator
Assembles streaming responses from LLMs into complete messages.
Attributes
Public Class Methods
Source
# File lib/ruby_llm/stream_accumulator.rb, line 11 def initialize @content = +'' @thinking_text = +'' @thinking_signature = nil @tool_calls = {} @input_tokens = nil @output_tokens = nil @cached_tokens = nil @cache_creation_tokens = nil @thinking_tokens = nil @inside_think_tag = false @pending_think_tag = +'' @latest_tool_call_id = nil @tool_call_ids_by_index = {} end
Public Instance Methods
Source
# File lib/ruby_llm/stream_accumulator.rb, line 27 def add(chunk) RubyLLM.logger.debug { chunk.inspect } if RubyLLM.config.log_stream_debug @model_id ||= chunk.model_id handle_chunk_content(chunk) append_thinking_from_chunk(chunk) count_tokens chunk RubyLLM.logger.debug { inspect } if RubyLLM.config.log_stream_debug end
Source
# File lib/ruby_llm/stream_accumulator.rb, line 37 def to_message(response) Message.new( role: :assistant, content: content.empty? ? nil : content, thinking: Thinking.build( text: @thinking_text.empty? ? nil : @thinking_text, signature: @thinking_signature ), tokens: Tokens.build( input: @input_tokens, output: @output_tokens, cached: @cached_tokens, cache_creation: @cache_creation_tokens, thinking: @thinking_tokens ), model_id: model_id, tool_calls: tool_calls_from_stream, raw: response ) end