Class: LLM::OpenAI::StreamParser

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/openai/stream_parser.rb

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ LLM::OpenAI::Chunk



16
17
18
19
20
21
22
23
24
# File 'lib/llm/providers/openai/stream_parser.rb', line 16

def initialize(stream)
  @body = {}
  @stream = stream
  @emits = {tools: {}}
  @can_emit_content = stream.respond_to?(:on_content)
  @can_emit_reasoning_content = stream.respond_to?(:on_reasoning_content)
  @can_emit_tool_call = stream.respond_to?(:on_tool_call)
  @can_push_content = stream.respond_to?(:<<)
end

Instance Attribute Details

#bodyHash (readonly)

Returns the fully constructed response body

Returns:

  • (Hash)


12
13
14
# File 'lib/llm/providers/openai/stream_parser.rb', line 12

def body
  @body
end

Instance Method Details

#freevoid

This method returns an undefined value.

Frees internal parser state used during streaming.



36
37
38
# File 'lib/llm/providers/openai/stream_parser.rb', line 36

def free
  @emits.clear
end

#parse!(chunk) ⇒ LLM::OpenAI::Chunk

Parameters:

  • chunk (Hash)

Returns:

  • (LLM::OpenAI::Chunk)


29
30
31
# File 'lib/llm/providers/openai/stream_parser.rb', line 29

def parse!(chunk)
  tap { merge!(chunk) }
end