Class: Async::Ollama::Wrapper

Inherits:
REST::Wrapper::Generic
  • Object
show all
Defined in:
lib/async/ollama/wrapper.rb

Overview

Wraps HTTP requests and responses for the Ollama API, handling content negotiation and parsing.

Direct Known Subclasses

ChatWrapper

Constant Summary collapse

APPLICATION_JSON =
"application/json"
APPLICATION_JSON_STREAM =
"application/x-ndjson"

Instance Method Summary collapse

Instance Method Details

#parser_for(response) ⇒ Object

Selects the appropriate parser for the HTTP response.



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/async/ollama/wrapper.rb', line 138

def parser_for(response)
	content_type = response.headers["content-type"]
	media_type = content_type.split(";").first
	
	case media_type
	when APPLICATION_JSON
		return Async::REST::Wrapper::JSON::Parser
	when APPLICATION_JSON_STREAM
		return StreamingResponseParser
	end
end

#prepare_request(request, payload) ⇒ Object

Prepares the HTTP request with appropriate headers and body.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/async/ollama/wrapper.rb', line 122

def prepare_request(request, payload)
	request.headers.add("accept", APPLICATION_JSON)
	request.headers.add("accept", APPLICATION_JSON_STREAM)
	
	if payload
		request.headers["content-type"] = APPLICATION_JSON
		
		request.body = ::Protocol::HTTP::Body::Buffered.new([
			::JSON.dump(payload)
		])
	end
end