class Ollama::Client
def request(method:, path:, handler:, body: nil, stream: nil)
def request(method:, path:, handler:, body: nil, stream: nil) url = @base_url + path responses = Enumerator.new do |yielder| if stream response_block = -> chunk, remaining_bytes, total_bytes do response_line = parse_json(chunk) response_line and yielder.yield response_line end response = excon(url).send(method, headers:, body:, response_block:) else response = excon(url).send(method, headers:, body:) end case response.status when 200 response.body.each_line do |l| response_line = parse_json(l) response_line and yielder.yield response_line end when 404 raise Ollama::Errors::NotFoundError, "#{response.status} #{response.body.inspect}" else raise Ollama::Errors::Error, "#{response.status} #{response.body.inspect}" end end responses.each { |response| handler.call(response) } self rescue Excon::Errors::SocketError => e raise Ollama::Errors::SocketError, "Caught #{e.class} #{e.message.inspect} for #{url.to_s.inspect}" rescue Excon::Errors::Timeout => e raise Ollama::Errors::TimeoutError, "Caught #{e.class} #{e.message.inspect} for #{url.to_s.inspect}" rescue Excon::Error => e raise Ollama::Errors::Error, "Caught #{e.class} #{e.message.inspect} for #{url.to_s.inspect}" end