Class: Ollama::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/request.rb

Overview

Immutable request object representing an API call. Contains all information needed to execute a request without knowing about HTTP or JSON.

Constant Summary collapse

ENDPOINTS =

Endpoint types: :chat, :generate, :embeddings, :show_model, :list_models, :version, :create_model, :delete_model, :copy_model, :pull, :push, :list_running, :blob

%i[chat generate embeddings show_model list_models version
create_model delete_model copy_model pull push list_running blob].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, model: nil, messages: nil, prompt: nil, images: nil, tools: nil, schema: nil, options: nil, stream: nil, keep_alive: nil, think: nil, format: nil, metadata: {}, raw_options: nil) ⇒ Request

rubocop:disable Metrics/ParameterLists



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama/request.rb', line 19

def initialize(endpoint:, model: nil, messages: nil, prompt: nil, images: nil,
               tools: nil, schema: nil, options: nil, stream: nil, keep_alive: nil,
               think: nil, format: nil, metadata: {}, raw_options: nil)
  @endpoint = endpoint.to_sym
  @model = model
  @messages = messages
  @prompt = prompt
  @images = images
  @tools = tools
  @schema = schema
  @options = options
  @stream = stream
  @keep_alive = keep_alive
  @think = think
  @format = format
  @metadata = .freeze
  @raw_options = raw_options
  freeze
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/ollama/request.rb', line 10

def endpoint
  @endpoint
end

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/ollama/request.rb', line 10

def format
  @format
end

#imagesObject (readonly)

Returns the value of attribute images.



10
11
12
# File 'lib/ollama/request.rb', line 10

def images
  @images
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



10
11
12
# File 'lib/ollama/request.rb', line 10

def keep_alive
  @keep_alive
end

#messagesObject (readonly)

Returns the value of attribute messages.



10
11
12
# File 'lib/ollama/request.rb', line 10

def messages
  @messages
end

#metadataObject (readonly)

Returns the value of attribute metadata.



10
11
12
# File 'lib/ollama/request.rb', line 10

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/ollama/request.rb', line 10

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/ollama/request.rb', line 10

def options
  @options
end

#promptObject (readonly)

Returns the value of attribute prompt.



10
11
12
# File 'lib/ollama/request.rb', line 10

def prompt
  @prompt
end

#raw_optionsObject (readonly)

Returns the value of attribute raw_options.



10
11
12
# File 'lib/ollama/request.rb', line 10

def raw_options
  @raw_options
end

#schemaObject (readonly)

Returns the value of attribute schema.



10
11
12
# File 'lib/ollama/request.rb', line 10

def schema
  @schema
end

#streamObject (readonly)

Returns the value of attribute stream.



10
11
12
# File 'lib/ollama/request.rb', line 10

def stream
  @stream
end

#thinkObject (readonly)

Returns the value of attribute think.



10
11
12
# File 'lib/ollama/request.rb', line 10

def think
  @think
end

#toolsObject (readonly)

Returns the value of attribute tools.



10
11
12
# File 'lib/ollama/request.rb', line 10

def tools
  @tools
end

Class Method Details

.blob(digest:, **_) ⇒ Object



105
106
107
# File 'lib/ollama/request.rb', line 105

def self.blob(digest:, **_)
  new(endpoint: :blob, raw_options: { digest: digest })
end

.build(endpoint, **params) ⇒ Object

Build a request from endpoint-specific parameters



41
42
43
# File 'lib/ollama/request.rb', line 41

def self.build(endpoint, **params)
  new(endpoint: endpoint, **params)
end

.chat(model:, messages:, tools: nil, schema: nil, options: nil, stream: nil, keep_alive: nil, think: nil, format: nil) ⇒ Object

Factory methods for common endpoints rubocop:disable Metrics/ParameterLists



47
48
49
50
51
# File 'lib/ollama/request.rb', line 47

def self.chat(model:, messages:, tools: nil, schema: nil, options: nil, stream: nil,
              keep_alive: nil, think: nil, format: nil, **)
  new(endpoint: :chat, model: model, messages: messages, tools: tools, schema: schema,
      options: options, stream: stream, keep_alive: keep_alive, think: think, format: format)
end

.copy_model(source:, destination:, **_) ⇒ Object



89
90
91
# File 'lib/ollama/request.rb', line 89

def self.copy_model(source:, destination:, **_)
  new(endpoint: :copy_model, raw_options: { source: source, destination: destination })
end

.create_model(model:, from: nil, modelfile: nil, path: nil, system: nil, template: nil, license: nil, parameters: nil, messages: nil, quantize: nil, stream: false) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/ollama/request.rb', line 76

def self.create_model(model:, from: nil, modelfile: nil, path: nil, system: nil,
                      template: nil, license: nil, parameters: nil, messages: nil,
                      quantize: nil, stream: false, **)
  new(endpoint: :create_model, model: model,
      raw_options: { from: from, modelfile: modelfile, path: path, system: system,
                     template: template, license: license, parameters: parameters,
                     messages: messages, quantize: quantize, stream: stream })
end

.delete_model(model:, **_) ⇒ Object



85
86
87
# File 'lib/ollama/request.rb', line 85

def self.delete_model(model:, **_)
  new(endpoint: :delete_model, model: model)
end

.embeddings(model:, input:, options: nil, keep_alive: nil) ⇒ Object



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

def self.embeddings(model:, input:, options: nil, keep_alive: nil, **)
  new(endpoint: :embeddings, model: model, prompt: input, options: options, keep_alive: keep_alive)
end

.generate(model:, prompt:, schema: nil, options: nil, stream: nil, keep_alive: nil, think: nil, images: nil, system: nil, suffix: nil, raw: nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/ollama/request.rb', line 53

def self.generate(model:, prompt:, schema: nil, options: nil, stream: nil, keep_alive: nil,
                  think: nil, images: nil, system: nil, suffix: nil, raw: nil, **)
  new(endpoint: :generate, model: model, prompt: prompt, schema: schema, options: options,
      stream: stream, keep_alive: keep_alive, think: think, images: images,
      raw_options: { system: system, suffix: suffix, raw: raw })
end

.list_models(**_) ⇒ Object



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

def self.list_models(**_)
  new(endpoint: :list_models)
end

.list_running(**_) ⇒ Object



101
102
103
# File 'lib/ollama/request.rb', line 101

def self.list_running(**_)
  new(endpoint: :list_running)
end

.pull(model:, insecure: false, stream: false, **_) ⇒ Object



93
94
95
# File 'lib/ollama/request.rb', line 93

def self.pull(model:, insecure: false, stream: false, **_)
  new(endpoint: :pull, model: model, raw_options: { insecure: insecure, stream: stream })
end

.push(model:, insecure: false, stream: false, **_) ⇒ Object



97
98
99
# File 'lib/ollama/request.rb', line 97

def self.push(model:, insecure: false, stream: false, **_)
  new(endpoint: :push, model: model, raw_options: { insecure: insecure, stream: stream })
end

.show_model(model:, verbose: nil) ⇒ Object



64
65
66
# File 'lib/ollama/request.rb', line 64

def self.show_model(model:, verbose: nil, **)
  new(endpoint: :show_model, model: model, raw_options: { verbose: verbose })
end

.version(**_) ⇒ Object



72
73
74
# File 'lib/ollama/request.rb', line 72

def self.version(**_)
  new(endpoint: :version)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Equality based on all attributes



158
159
160
161
162
# File 'lib/ollama/request.rb', line 158

def ==(other)
  return false unless other.is_a?(Request)

  to_h == other.to_h
end

#base_pathObject Also known as: path

Get the base path for the endpoint



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ollama/request.rb', line 117

def base_path
  case @endpoint
  when :chat then "/api/chat"
  when :generate then "/api/generate"
  when :embeddings then "/api/embed"
  when :show_model then "/api/show"
  when :list_models then "/api/tags"
  when :version then "/api/version"
  when :create_model then "/api/create"
  when :delete_model then "/api/delete"
  when :copy_model then "/api/copy"
  when :pull then "/api/pull"
  when :push then "/api/push"
  when :list_running then "/api/ps"
  when :blob then "/api/blobs"
  else "/api/#{@endpoint}"
  end
end

#hashObject



165
166
167
# File 'lib/ollama/request.rb', line 165

def hash
  to_h.hash
end

#streaming?Boolean

Check if endpoint supports streaming

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/ollama/request.rb', line 111

def streaming?
  hooks = @options.is_a?(Hash) ? @options[:hooks] : nil
  (hooks && !hooks.empty?) || @stream == true
end

#to_hObject

Convert to a hash for serialization



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ollama/request.rb', line 138

def to_h
  {
    endpoint: @endpoint,
    model: @model,
    messages: @messages,
    prompt: @prompt,
    images: @images,
    tools: @tools,
    schema: @schema,
    options: @options,
    stream: @stream,
    keep_alive: @keep_alive,
    think: @think,
    format: @format,
    metadata: @metadata,
    raw_options: @raw_options
  }.compact
end