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::StreamParser

Parameters:



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

def initialize(stream)
  @body = {}
  @stream = stream
  @emits = {tools: {}}
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.



33
34
35
# File 'lib/llm/providers/openai/stream_parser.rb', line 33

def free
  @emits.clear
end

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

Parameters:

  • chunk (Hash)

Returns:

  • (LLM::OpenAI::Chunk)


26
27
28
# File 'lib/llm/providers/openai/stream_parser.rb', line 26

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