class Ollama::Client

client = Ollama::Client.configure_with(config)
config = Ollama::Client::Config[base_url: ‘localhost:11434’]
@example Configuring a client using a configuration object
client = Ollama::Client.new(base_url: ‘localhost:11434’)
@example Initializing a client with a base URL
and supports different response handlers for processing API results.
It manages configuration settings like base URL, timeouts, and output streams,
various API endpoints such as chat, generate, create, and model management commands.
The Client class provides methods to communicate with an Ollama server, handling
A class that serves as the main entry point for interacting with the Ollama API.

def self.user_agent

Returns:
  • (String) - a formatted user agent string in the format "Ollama::Client/1.2.3"
def self.user_agent
  '%s/%s' % [ self, Ollama::VERSION ]
end

def commands

Returns:
  • (Array) - an array of command names sorted alphabetically
def commands
  doc_annotations.sort_by(&:first).transpose.last
end

def excon(url)

Returns:
  • (Excon) - a new Excon client instance configured with the specified parameters

Parameters:
  • url (String) -- the URL to be used for the Excon client
def excon(url)
  params = {
    connect_timeout: @connect_timeout,
    read_timeout:    @read_timeout,
    write_timeout:   @write_timeout,
    ssl_verify_peer: @ssl_verify_peer,
    debug:           @debug,
  }.compact
  Excon.new(url, params)
end

def headers

Returns:
  • (Hash) - a hash containing the HTTP headers with keys 'User-Agent' and 'Content-Type'
def headers
  {
    'User-Agent'   => @user_agent || self.class.user_agent,
    'Content-Type' => 'application/json; charset=utf-8',
  }
end

def help

are available for execution through the client interface.
It is typically used to provide users with information about which commands
and outputs them as a comma-separated string to the configured output stream.
This method retrieves the sorted list of documented commands from the client

The help method displays a list of available commands to the output stream.
def help
  @output.puts "Commands: %s" % commands.join(?,)
end

def initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil, user_agent: nil)

Parameters:
  • user_agent (String, nil) -- the user agent string to be used for API requests, defaults to nil
  • debug (Boolean, nil) -- the debug flag indicating whether debug output is enabled, defaults to nil
  • write_timeout (Integer, nil) -- the write timeout value in seconds, defaults to nil
  • read_timeout (Integer, nil) -- the read timeout value in seconds, defaults to nil
  • connect_timeout (Integer, nil) -- the connection timeout value in seconds, defaults to nil
  • output (IO) -- the output stream to be used for handling responses, defaults to $stdout
  • base_url (String, nil) -- the base URL of the Ollama API endpoint, defaults to nil
def initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil, user_agent: nil)
  base_url.nil? and base_url = ENV.fetch('OLLAMA_URL') do
    raise ArgumentError,
      'missing :base_url parameter or OLLAMA_URL environment variable'
  end
  base_url.is_a? URI or base_url = URI.parse(base_url)
  base_url.is_a?(URI::HTTP) || base_url.is_a?(URI::HTTPS) or
    raise ArgumentError, "require #{base_url.inspect} to be http/https-URI"
  @ssl_verify_peer = base_url.query.to_s.split(?&).inject({}) { |h, l|
    h.merge Hash[*l.split(?=)]
  }['ssl_verify_peer'] != 'false'
  @base_url, @output, @connect_timeout, @read_timeout, @write_timeout, @debug, @user_agent =
    base_url, output, connect_timeout, read_timeout, write_timeout, debug, user_agent
end

def inspect

Returns:
  • (String) - a string representation in the format "#"
def inspect
  "#<#{self.class}@#{@base_url}>"
end

def parse_json(string)

Returns:
  • (Ollama::Response, nil) - the parsed JSON object or nil if parsing fails

Parameters:
  • string (String) -- the JSON string to be parsed
def parse_json(string)
  JSON.parse(string, object_class: Ollama::Response)
rescue JSON::ParserError => e
  warn "Caught #{e.class}: #{e}"
  return
end

def request(method:, path:, handler:, body: nil, stream: nil)

Returns:
  • (Ollama::Client) - returns the client instance itself after initiating the request

Parameters:
  • stream (TrueClass, FalseClass, nil) -- whether to enable streaming for the operation
  • body (String, nil) -- the request body content, if applicable
  • handler (Ollama::Handler) -- the handler object responsible for processing API responses
  • path (String) -- the API endpoint path to request
  • method (Symbol) -- the HTTP method to use for the request (:get, :post, :delete)
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

def ssl_verify_peer?

Returns:
  • (TrueClass, FalseClass) - true if SSL peer verification is enabled,
def ssl_verify_peer?
  !!@ssl_verify_peer
end