Class: Legion::Extensions::Llm::Responses::StreamChunk

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/responses/stream_chunk.rb

Overview

Normalized streaming provider response chunk.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content: nil, thinking: nil, metadata: {}, model: nil, tool_calls: nil, tokens: nil, raw: nil) ⇒ StreamChunk

rubocop:disable Metrics/ParameterLists



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 11

def initialize(content: nil, thinking: nil, metadata: {}, model: nil, tool_calls: nil, tokens: nil, raw: nil) # rubocop:disable Metrics/ParameterLists
  extraction = ThinkingExtractor.extract(content, metadata: )

  @content = extraction.content
  @thinking = thinking || extraction.thinking
  @metadata = extraction.
  @internal_metadata = .to_h
  @model = model
  @tool_calls = tool_calls
  @tokens = tokens
  @raw = raw

  freeze
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def content
  @content
end

#internal_metadataObject (readonly)

Returns the value of attribute internal_metadata.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def 
  @internal_metadata
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def model
  @model
end

#rawObject (readonly)

Returns the value of attribute raw.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def raw
  @raw
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def thinking
  @thinking
end

#tokensObject (readonly)

Returns the value of attribute tokens.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def tokens
  @tokens
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



9
10
11
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 9

def tool_calls
  @tool_calls
end

Instance Method Details

#to_hObject



26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 26

def to_h
  {
    content: content,
    metadata: ,
    model: model,
    tool_calls: tool_calls,
    tokens: tokens
  }.compact
end

#to_internal_hObject



36
37
38
# File 'lib/legion/extensions/llm/responses/stream_chunk.rb', line 36

def to_internal_h
  to_h.merge(thinking: thinking, metadata: , raw: raw).compact
end