Class: RubyLLM::StreamAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/stream_accumulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamAccumulator

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

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/ruby_llm/stream_accumulator.rb', line 5

def content
  @content
end

#model_idObject (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_callsObject (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_messageObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_llm/stream_accumulator.rb', line 29

def to_message
  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