Class: RubyLLM::StreamAccumulator
- Inherits:
-
Object
- Object
- RubyLLM::StreamAccumulator
- Defined in:
- lib/ruby_llm/stream_accumulator.rb
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.
7 8 9 10 11 12 13 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 7 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.
5 6 7 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 5 def content @content end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
5 6 7 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 5 def model_id @model_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
5 6 7 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 5 def tool_calls @tool_calls end |
Instance Method Details
#add(chunk) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 15 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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby_llm/stream_accumulator.rb', line 29 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 |