Class: Async::Ollama::StreamingParser

Inherits:
Protocol::HTTP::Body::Wrapper
  • Object
show all
Defined in:
lib/async/ollama/wrapper.rb

Overview

Parses streaming HTTP responses for Ollama, buffering and extracting JSON lines.

Instance Method Summary collapse

Constructor Details

#initializeStreamingParser

Returns a new instance of StreamingParser.



19
20
21
22
23
24
25
26
# File 'lib/async/ollama/wrapper.rb', line 19

def initialize(...)
	super
	
	@buffer = String.new.b
	@offset = 0
	
	@value = {}
end

Instance Method Details

#joinObject

Joins the stream, reading all objects and returning the final value.



58
59
60
61
62
# File 'lib/async/ollama/wrapper.rb', line 58

def join
	self.each{}
	
	return @value
end

#readObject

Reads the next JSON object from the stream.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/async/ollama/wrapper.rb', line 30

def read
	return if @buffer.nil?
	
	while true
		if index = @buffer.index("\n", @offset)
			line = @buffer.byteslice(@offset, index - @offset)
			@buffer = @buffer.byteslice(index + 1, @buffer.bytesize - index - 1)
			@offset = 0
			
			return ::JSON.parse(line, symbolize_names: true)
		end
		
		if chunk = super
			@buffer << chunk
		else
			return nil if @buffer.empty?
			
			line = @buffer
			@buffer = nil
			@offset = 0
			
			return ::JSON.parse(line, symbolize_names: true)
		end
	end
end