Class: RubyLLM::StreamAccumulator
- Inherits:
-
Object
- Object
- RubyLLM::StreamAccumulator
- Defined in:
- lib/ruby_llm/stream_accumulator.rb
Overview
Assembles streaming responses from LLMs into complete messages. Handles the complexities of accumulating content and tool calls from partial chunks while tracking token usage.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Instance Method Summary collapse
- #add(chunk) ⇒ Object
-
#initialize ⇒ StreamAccumulator
constructor
A new instance of StreamAccumulator.
- #to_message ⇒ Object
Constructor Details
#initialize ⇒ StreamAccumulator
Returns a new instance of StreamAccumulator.
10 11 12 13 14 15 16 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 10 def initialize @content = String.new @tool_calls = {} @input_tokens = 0 @output_tokens = 0 @latest_tool_call_id = nil end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 8 def content @content end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
8 9 10 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 8 def model_id @model_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
8 9 10 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 8 def tool_calls @tool_calls end |
Instance Method Details
#add(chunk) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 18 def add(chunk) RubyLLM.logger.debug chunk.inspect @model_id ||= chunk.model_id if chunk.tool_call? accumulate_tool_calls chunk.tool_calls else @content << (chunk.content || '') end count_tokens chunk RubyLLM.logger.debug inspect end |
#to_message ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 32 def Message.new( role: :assistant, content: content, model_id: model_id, tool_calls: tool_calls_from_stream, input_tokens: @input_tokens.positive? ? @input_tokens : nil, output_tokens: @output_tokens.positive? ? @output_tokens : nil ) end |