Class: Async::Ollama::Wrapper::StreamingResponseParser

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

Instance Method Summary collapse

Constructor Details

#initializeStreamingResponseParser

Returns a new instance of StreamingResponseParser.



34
35
36
37
38
39
40
41
42
# File 'lib/async/ollama/wrapper.rb', line 34

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

Instance Method Details

#eachObject



70
71
72
73
74
75
76
77
78
# File 'lib/async/ollama/wrapper.rb', line 70

def each
	super do |line|
		token = line.delete(:response)
		@response << token
		@value.merge!(line)
		
		yield token
	end
end

#joinObject



80
81
82
83
84
# File 'lib/async/ollama/wrapper.rb', line 80

def join
	self.each{}
	
	return @value
end

#readObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/async/ollama/wrapper.rb', line 44

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