Class: Ollama::Client

Inherits:
Object
  • Object
show all
Includes:
Command, Handlers, Tins::Annotate
Defined in:
lib/ollama/client.rb,
lib/ollama/client.rb

Defined Under Namespace

Modules: Command Classes: Doc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ollama/client.rb', line 16

def initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: 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 =
    base_url, output, connect_timeout, read_timeout, write_timeout, debug
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



33
34
35
# File 'lib/ollama/client.rb', line 33

def base_url
  @base_url
end

#outputObject

Returns the value of attribute output.



31
32
33
# File 'lib/ollama/client.rb', line 31

def output
  @output
end

Instance Method Details

#commandsObject



63
64
65
# File 'lib/ollama/client.rb', line 63

def commands
  doc_annotations.sort_by(&:first).transpose.last
end

#helpObject



68
69
70
# File 'lib/ollama/client.rb', line 68

def help
  @output.puts "Commands: %s" % commands.join(?,)
end

#inspectObject Also known as: to_s



107
108
109
# File 'lib/ollama/client.rb', line 107

def inspect
  "#<#{self.class}@#{@base_url.to_s}>"
end

#request(method:, path:, handler:, body: nil, stream: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ollama/client.rb', line 72

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

#ssl_verify_peer?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ollama/client.rb', line 35

def ssl_verify_peer?
  !!@ssl_verify_peer
end