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, user_agent: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ollama/client.rb', line 13

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

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#outputObject

Returns the value of attribute output.



28
29
30
# File 'lib/ollama/client.rb', line 28

def output
  @output
end

Instance Method Details

#commandsObject



60
61
62
# File 'lib/ollama/client.rb', line 60

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

#helpObject



65
66
67
# File 'lib/ollama/client.rb', line 65

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

#inspectObject Also known as: to_s



104
105
106
# File 'lib/ollama/client.rb', line 104

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

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



69
70
71
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
# File 'lib/ollama/client.rb', line 69

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)


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

def ssl_verify_peer?
  !!@ssl_verify_peer
end