ollama-ruby 1.4.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.contexts/code_comment.rb +25 -0
  3. data/.contexts/full.rb +43 -0
  4. data/.contexts/info.rb +17 -0
  5. data/.contexts/lib.rb +27 -0
  6. data/.contexts/yard.md +93 -0
  7. data/CHANGES.md +39 -0
  8. data/README.md +74 -23
  9. data/Rakefile +4 -3
  10. data/bin/ollama_cli +41 -9
  11. data/bin/ollama_console +18 -0
  12. data/lib/ollama/client/command.rb +29 -3
  13. data/lib/ollama/client/configuration/config.rb +114 -3
  14. data/lib/ollama/client/doc.rb +18 -0
  15. data/lib/ollama/client.rb +131 -2
  16. data/lib/ollama/commands/chat.rb +96 -1
  17. data/lib/ollama/commands/copy.rb +59 -1
  18. data/lib/ollama/commands/create.rb +112 -1
  19. data/lib/ollama/commands/delete.rb +53 -1
  20. data/lib/ollama/commands/embed.rb +82 -1
  21. data/lib/ollama/commands/embeddings.rb +72 -1
  22. data/lib/ollama/commands/generate.rb +118 -2
  23. data/lib/ollama/commands/ps.rb +55 -0
  24. data/lib/ollama/commands/pull.rb +72 -1
  25. data/lib/ollama/commands/push.rb +65 -1
  26. data/lib/ollama/commands/show.rb +64 -1
  27. data/lib/ollama/commands/tags.rb +50 -0
  28. data/lib/ollama/commands/version.rb +50 -1
  29. data/lib/ollama/dto.rb +98 -1
  30. data/lib/ollama/errors.rb +50 -0
  31. data/lib/ollama/handlers/collector.rb +34 -0
  32. data/lib/ollama/handlers/concern.rb +60 -2
  33. data/lib/ollama/handlers/dump_json.rb +20 -0
  34. data/lib/ollama/handlers/dump_yaml.rb +22 -0
  35. data/lib/ollama/handlers/markdown.rb +28 -0
  36. data/lib/ollama/handlers/nop.rb +20 -0
  37. data/lib/ollama/handlers/print.rb +27 -0
  38. data/lib/ollama/handlers/progress.rb +38 -0
  39. data/lib/ollama/handlers/say.rb +66 -0
  40. data/lib/ollama/handlers/single.rb +35 -0
  41. data/lib/ollama/handlers.rb +9 -0
  42. data/lib/ollama/image.rb +67 -0
  43. data/lib/ollama/json_loader.rb +17 -0
  44. data/lib/ollama/message.rb +46 -1
  45. data/lib/ollama/options.rb +27 -2
  46. data/lib/ollama/response.rb +17 -0
  47. data/lib/ollama/tool/function/parameters/property.rb +41 -1
  48. data/lib/ollama/tool/function/parameters.rb +40 -1
  49. data/lib/ollama/tool/function.rb +44 -1
  50. data/lib/ollama/tool.rb +37 -1
  51. data/lib/ollama/version.rb +1 -1
  52. data/lib/ollama.rb +26 -0
  53. data/ollama-ruby.gemspec +7 -6
  54. data/spec/ollama/client/doc_spec.rb +1 -1
  55. data/spec/ollama/client_spec.rb +19 -1
  56. data/spec/ollama/commands/chat_spec.rb +1 -1
  57. data/spec/ollama/commands/copy_spec.rb +1 -1
  58. data/spec/ollama/commands/create_spec.rb +1 -1
  59. data/spec/ollama/commands/delete_spec.rb +1 -1
  60. data/spec/ollama/commands/embed_spec.rb +1 -1
  61. data/spec/ollama/commands/embeddings_spec.rb +1 -1
  62. data/spec/ollama/commands/generate_spec.rb +1 -1
  63. data/spec/ollama/commands/ps_spec.rb +1 -1
  64. data/spec/ollama/commands/pull_spec.rb +1 -1
  65. data/spec/ollama/commands/push_spec.rb +1 -1
  66. data/spec/ollama/commands/show_spec.rb +1 -1
  67. data/spec/ollama/commands/tags_spec.rb +1 -1
  68. data/spec/ollama/commands/version_spec.rb +1 -1
  69. data/spec/ollama/handlers/collector_spec.rb +1 -1
  70. data/spec/ollama/handlers/dump_json_spec.rb +1 -1
  71. data/spec/ollama/handlers/dump_yaml_spec.rb +1 -1
  72. data/spec/ollama/handlers/markdown_spec.rb +1 -1
  73. data/spec/ollama/handlers/nop_spec.rb +2 -2
  74. data/spec/ollama/handlers/print_spec.rb +1 -1
  75. data/spec/ollama/handlers/progress_spec.rb +1 -1
  76. data/spec/ollama/handlers/say_spec.rb +1 -1
  77. data/spec/ollama/handlers/single_spec.rb +1 -1
  78. data/spec/ollama/image_spec.rb +1 -1
  79. data/spec/ollama/message_spec.rb +1 -1
  80. data/spec/ollama/options_spec.rb +1 -1
  81. data/spec/ollama/tool_spec.rb +1 -1
  82. data/spec/spec_helper.rb +2 -6
  83. metadata +25 -5
@@ -1,25 +1,136 @@
1
1
  require 'ollama/json_loader'
2
2
 
3
+ # A module that provides configuration management capabilities for the Ollama
4
+ # client.
5
+ #
6
+ # This module extends the client with functionality to manage and apply
7
+ # configuration settings such as base URL, timeouts, and output streams. It
8
+ # allows for flexible initialization of client instances using either direct
9
+ # parameters or pre-defined configuration objects, supporting both programmatic
10
+ # and file-based configuration approaches.
11
+ #
12
+ # @example Configuring a client with a hash of attributes
13
+ # config = Ollama::Client::Config[base_url: 'http://localhost:11434']
14
+ # client = Ollama::Client.configure_with(config)
15
+ #
16
+ # @example Loading configuration from a JSON file
17
+ # config = Ollama::Client::Config.load_from_json('path/to/config.json')
18
+ # client = Ollama::Client.configure_with(config)
3
19
  module Ollama::Client::Configuration
20
+
21
+
22
+ # A class that encapsulates configuration settings for Ollama clients.
23
+ #
24
+ # This class provides a structured way to define and manage various
25
+ # configuration options that can be used when initializing Ollama client
26
+ # instances. It includes properties for setting the base URL, output stream,
27
+ # and timeout values.
28
+ #
29
+ # @example Creating a configuration object
30
+ # config = Ollama::Client::Config[
31
+ # base_url: 'http://localhost:11434',
32
+ # output: $stdout,
33
+ # connect_timeout: 15,
34
+ # read_timeout: 300
35
+ # ]
36
+ #
37
+ # @example Loading configuration from a JSON file
38
+ # config = Ollama::Client::Config.load_from_json('path/to/config.json')
4
39
  class Config
5
40
  extend Ollama::JSONLoader
6
41
 
42
+ # The initialize method sets up a new configuration instance with the
43
+ # specified attributes.
44
+ #
45
+ # This method is responsible for initializing a new
46
+ # Ollama::Client::Configuration::Config instance by processing various
47
+ # configuration options. It iterates through the provided attributes and
48
+ # assigns them to corresponding setter methods, then ensures that the
49
+ # output stream is set to $stdout if no output was specified.
50
+ #
51
+ # @param attributes [ Hash ] a hash containing the configuration attributes to be set
52
+ #
53
+ # @return [ Ollama::Client::Configuration::Config ] returns the initialized configuration instance
7
54
  def initialize(**attributes)
8
55
  attributes.each { |k, v| send("#{k}=", v) }
9
56
  self.output ||= $stdout
10
57
  end
11
58
 
59
+ # The [] method creates a new instance of the class using a hash of
60
+ # attributes.
61
+ #
62
+ # This class method provides a convenient way to instantiate an object by
63
+ # passing a hash containing the desired attribute values. It converts the
64
+ # hash keys to symbols and forwards them as keyword arguments to the
65
+ # constructor.
66
+ #
67
+ # @param value [ Hash ] a hash containing the attribute names and their values
68
+ #
69
+ # @return [ self ] a new instance of the class initialized with the provided
70
+ # attributes
12
71
  def self.[](value)
13
72
  new(**value.to_h)
14
73
  end
15
74
 
16
- attr_accessor :base_url, :output, :connect_timeout, :read_timeout,
17
- :write_timeout, :debug, :user_agent
75
+ # The base_url attribute accessor allows reading and setting the base URL
76
+ # of the Ollama API endpoint.
77
+ #
78
+ # @attr [ URI ] the new base URL to be set for API requests
79
+ attr_accessor :base_url
80
+
81
+ # The output attribute accessor allows reading and setting the output stream
82
+ # used for handling responses and messages.
83
+ #
84
+ # @attr [ IO ] the new output stream to be set for response handling
85
+ attr_accessor :output
86
+
87
+ # The connect_timeout attribute accessor allows reading and setting the
88
+ # connection timeout value.
89
+ #
90
+ # @attr [ Integer, nil ] the new connection timeout value to be set
91
+ attr_accessor :connect_timeout
92
+
93
+ # The read_timeout attribute accessor allows reading and setting the read
94
+ # timeout value.
95
+ #
96
+ # @attr [ Integer, nil ] the new read timeout value to be set
97
+ attr_accessor :read_timeout
98
+
99
+ # The write_timeout attribute accessor allows reading and setting the write
100
+ # timeout value.
101
+ #
102
+ # @attr [ Integer, nil ] the new write timeout value to be set
103
+ attr_accessor :write_timeout
104
+
105
+ # The debug attribute accessor allows reading and setting the debug flag.
106
+ #
107
+ # @attr [ Boolean, nil ] the new debug flag value to be set
108
+ attr_accessor :debug
109
+
110
+ # The user_agent attribute accessor allows reading and setting the user
111
+ # agent string used for making requests to the Ollama API.
112
+ #
113
+ # @attr [ String, nil ] the new user agent string to be set for API requests
114
+ attr_accessor :user_agent
18
115
  end
19
116
 
20
117
  extend Tins::Concern
21
118
 
22
- module ClassMethods
119
+ class_methods do
120
+ # The configure_with method initializes a new client instance using the
121
+ # provided configuration object.
122
+
123
+ # This method takes a configuration object and uses its attributes to set
124
+ # up a new Ollama::Client instance. It extracts individual configuration
125
+ # parameters from the input object and passes them to the client's
126
+ # constructor, allowing for flexible initialization based on pre-defined
127
+ # settings.
128
+
129
+ # @param config [ Ollama::Client::Configuration::Config ] the configuration
130
+ # object containing client settings
131
+ #
132
+ # @return [ Ollama::Client ] a new client instance configured with the
133
+ # provided settings
23
134
  def configure_with(config)
24
135
  new(
25
136
  base_url: config.base_url,
@@ -1,5 +1,15 @@
1
1
  require 'term/ansicolor'
2
2
 
3
+ # A class that generates documentation links for Ollama API commands.
4
+ #
5
+ # This class is responsible for creating human-readable documentation
6
+ # references for various Ollama API endpoints. It maps command names to their
7
+ # corresponding documentation URLs, providing easy access to API documentation
8
+ # for developers working with the Ollama client.
9
+ #
10
+ # @example Generating a documentation link for a command
11
+ # doc = Ollama::Client::Doc.new(:generate)
12
+ # puts doc.to_s # => hyperlink to generate command documentation
3
13
  class Ollama::Client::Doc
4
14
  include Term::ANSIColor
5
15
 
@@ -22,6 +32,14 @@ class Ollama::Client::Doc
22
32
  )[name]
23
33
  end
24
34
 
35
+ # The to_s method converts the documentation object to a string representation.
36
+ #
37
+ # This method generates a human-readable string that includes a hyperlink to the
38
+ # corresponding Ollama API documentation for the command, if a URL is available.
39
+ # The resulting string can be used for display purposes or logging.
40
+ #
41
+ # @return [ String ] a string representation containing the formatted documentation link
42
+ # or an empty string if no URL is defined for the command
25
43
  def to_s
26
44
  (hyperlink(@url) { @name } if @url).to_s
27
45
  end
data/lib/ollama/client.rb CHANGED
@@ -1,3 +1,16 @@
1
+ # A class that serves as the main entry point for interacting with the Ollama API.
2
+ #
3
+ # The Client class provides methods to communicate with an Ollama server, handling
4
+ # various API endpoints such as chat, generate, create, and model management commands.
5
+ # It manages configuration settings like base URL, timeouts, and output streams,
6
+ # and supports different response handlers for processing API results.
7
+ #
8
+ # @example Initializing a client with a base URL
9
+ # client = Ollama::Client.new(base_url: 'http://localhost:11434')
10
+ #
11
+ # @example Configuring a client using a configuration object
12
+ # config = Ollama::Client::Config[base_url: 'http://localhost:11434']
13
+ # client = Ollama::Client.configure_with(config)
1
14
  class Ollama::Client
2
15
  end
3
16
  require 'ollama/client/doc'
@@ -12,6 +25,22 @@ class Ollama::Client
12
25
 
13
26
  annotate :doc
14
27
 
28
+ # The initialize method sets up a new client instance with the specified configuration parameters.
29
+ #
30
+ # This method is responsible for initializing a new Ollama::Client instance by processing
31
+ # various configuration options including the base URL, output stream, timeouts, and debug settings.
32
+ # It handles default values for the base URL by falling back to an environment variable,
33
+ # validates that the base URL is a valid HTTP or HTTPS URI, and extracts SSL verification
34
+ # settings from query parameters. The method also sets up instance variables for all
35
+ # configuration options, making them available for use in subsequent client operations.
36
+ #
37
+ # @param base_url [ String, nil ] the base URL of the Ollama API endpoint, defaults to nil
38
+ # @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
39
+ # @param connect_timeout [ Integer, nil ] the connection timeout value in seconds, defaults to nil
40
+ # @param read_timeout [ Integer, nil ] the read timeout value in seconds, defaults to nil
41
+ # @param write_timeout [ Integer, nil ] the write timeout value in seconds, defaults to nil
42
+ # @param debug [ Boolean, nil ] the debug flag indicating whether debug output is enabled, defaults to nil
43
+ # @param user_agent [ String, nil ] the user agent string to be used for API requests, defaults to nil
15
44
  def initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil, user_agent: nil)
16
45
  base_url.nil? and base_url = ENV.fetch('OLLAMA_URL') do
17
46
  raise ArgumentError,
@@ -27,14 +56,39 @@ class Ollama::Client
27
56
  base_url, output, connect_timeout, read_timeout, write_timeout, debug, user_agent
28
57
  end
29
58
 
59
+ # The output attribute accessor allows reading and setting the output stream
60
+ # used for handling responses and messages.
61
+ #
62
+ # @attr [ IO ] the output stream, typically $stdout, to which responses and
63
+ # messages are written
30
64
  attr_accessor :output
31
65
 
66
+ # The base_url attribute reader returns the base URL used for making requests to the Ollama API.
67
+ #
68
+ # @return [ URI ] the base URL configured for API requests
32
69
  attr_reader :base_url
33
70
 
71
+ # The ssl_verify_peer? method checks whether SSL peer verification is enabled.
72
+ #
73
+ # This method returns a boolean value indicating if the client should verify
74
+ # the SSL certificate of the Ollama server during communication. It converts
75
+ # the internal SSL verification flag to a boolean value for easy checking.
76
+ #
77
+ # @return [ TrueClass, FalseClass ] true if SSL peer verification is enabled,
78
+ # false otherwise
34
79
  def ssl_verify_peer?
35
80
  !!@ssl_verify_peer
36
81
  end
37
82
 
83
+ # Defines a command method with its associated command class and handlers.
84
+ #
85
+ # This is an example of Ruby's metaprogramming capabilities where we dynamically
86
+ # create methods that delegate to specific command classes. The client supports
87
+ # many commands including chat, generate, tags, show, create, copy, delete,
88
+ # pull, push, embed, embeddings, ps, and version.
89
+ #
90
+ # @example Generated command method
91
+ # client.chat(model: 'llama3.1', messages: [{role: 'user', content: 'Hello'}])
38
92
  command(:chat, default_handler: Single, stream_handler: Collector)
39
93
 
40
94
  command(:generate, default_handler: Single, stream_handler: Collector)
@@ -61,15 +115,45 @@ class Ollama::Client
61
115
 
62
116
  command(:version, default_handler: Single)
63
117
 
118
+ # The commands method retrieves and sorts the documented commands available
119
+ # in the client.
120
+ #
121
+ # This method extracts all command annotations from the class, sorts them by
122
+ # their names, and returns an array containing only the command names in
123
+ # alphabetical order.
124
+ #
125
+ # @return [ Array<String> ] an array of command names sorted alphabetically
64
126
  def commands
65
127
  doc_annotations.sort_by(&:first).transpose.last
66
128
  end
67
129
 
68
130
  doc Doc.new(:help)
131
+ # The help method displays a list of available commands to the output stream.
132
+ #
133
+ # This method retrieves the sorted list of documented commands from the client
134
+ # and outputs them as a comma-separated string to the configured output stream.
135
+ # It is typically used to provide users with information about which commands
136
+ # are available for execution through the client interface.
69
137
  def help
70
138
  @output.puts "Commands: %s" % commands.join(?,)
71
139
  end
72
140
 
141
+ # The request method sends an HTTP request to the Ollama API and processes
142
+ # responses through a handler.
143
+ #
144
+ # This method constructs an HTTP request to the specified API endpoint,
145
+ # handling both streaming and non-streaming responses. It manages different
146
+ # HTTP status codes, including success (200), not found (404), and other
147
+ # error cases. The method also includes comprehensive error handling for
148
+ # network-related issues such as socket errors and timeouts.
149
+ #
150
+ # @param method [ Symbol ] the HTTP method to use for the request (:get, :post, :delete)
151
+ # @param path [ String ] the API endpoint path to request
152
+ # @param handler [ Ollama::Handler ] the handler object responsible for processing API responses
153
+ # @param body [ String, nil ] the request body content, if applicable
154
+ # @param stream [ TrueClass, FalseClass, nil ] whether to enable streaming for the operation
155
+ #
156
+ # @return [ Ollama::Client ] returns the client instance itself after initiating the request
73
157
  def request(method:, path:, handler:, body: nil, stream: nil)
74
158
  url = @base_url + path
75
159
  responses = Enumerator.new do |yielder|
@@ -105,14 +189,28 @@ class Ollama::Client
105
189
  raise Ollama::Errors::Error, "Caught #{e.class} #{e.message.inspect} for #{url.to_s.inspect}"
106
190
  end
107
191
 
192
+ # The inspect method returns a string representation of the client instance.
193
+ #
194
+ # This method provides a human-readable description of the client object,
195
+ # including its class name and the base URL it is configured to use.
196
+ #
197
+ # @return [ String ] a string representation in the format "#<Ollama::Client@http://localhost:11434>"
108
198
  def inspect
109
- "#<#{self.class}@#{@base_url.to_s}>"
199
+ "#<#{self.class}@#{@base_url}>"
110
200
  end
111
201
 
112
202
  alias to_s inspect
113
203
 
114
204
  private
115
205
 
206
+ # The headers method constructs and returns a hash of HTTP headers.
207
+ #
208
+ # This method generates a set of standard HTTP headers required for making
209
+ # requests to the Ollama API, including the User-Agent and Content-Type. It
210
+ # uses the instance's configured user agent or falls back to the class-level
211
+ # user agent if none is set.
212
+ #
213
+ # @return [ Hash ] a hash containing the HTTP headers with keys 'User-Agent' and 'Content-Type'
116
214
  def headers
117
215
  {
118
216
  'User-Agent' => @user_agent || self.class.user_agent,
@@ -120,10 +218,29 @@ class Ollama::Client
120
218
  }
121
219
  end
122
220
 
221
+ # The user_agent method generates a formatted user agent string for API requests.
222
+ #
223
+ # This method creates a user agent identifier that combines the class name
224
+ # with the library version, which is used to identify the client making
225
+ # requests to the Ollama API.
226
+ #
227
+ # @return [ String ] a formatted user agent string in the format "Ollama::Client/1.2.3"
123
228
  def self.user_agent
124
229
  '%s/%s' % [ self, Ollama::VERSION ]
125
230
  end
126
231
 
232
+ # The excon method creates and returns a new Excon client instance configured
233
+ # with the receiver's timeout and debugging settings.
234
+ #
235
+ # This method constructs an Excon client object using the provided URL and
236
+ # configures it with connection, read, and write timeouts, SSL verification
237
+ # settings, and debug mode based on the instance variables of the receiver.
238
+ # It compacts the parameters hash to remove any nil values before passing
239
+ # them to Excon.new.
240
+ #
241
+ # @param url [ String ] the URL to be used for the Excon client
242
+ #
243
+ # @return [ Excon ] a new Excon client instance configured with the specified parameters
127
244
  def excon(url)
128
245
  params = {
129
246
  connect_timeout: @connect_timeout,
@@ -135,9 +252,21 @@ class Ollama::Client
135
252
  Excon.new(url, params)
136
253
  end
137
254
 
255
+ # The parse_json method attempts to parse a JSON string into a structured
256
+ # object.
257
+ #
258
+ # This method takes a string containing JSON data and converts it into a Ruby
259
+ # object using the JSON.parse method. It specifies Ollama::Response as the
260
+ # object class to ensure that the parsed data is wrapped in the appropriate
261
+ # response structure.
262
+ #
263
+ # @param string [ String ] the JSON string to be parsed
264
+ #
265
+ # @return [ Ollama::Response, nil ] the parsed JSON object or nil if parsing fails
138
266
  def parse_json(string)
139
267
  JSON.parse(string, object_class: Ollama::Response)
140
- rescue JSON::ParserError
268
+ rescue JSON::ParserError => e
269
+ warn "Caught #{e.class}: #{e}"
141
270
  return
142
271
  end
143
272
  end
@@ -1,20 +1,115 @@
1
+ # A command class that represents the chat API endpoint for Ollama.
2
+ #
3
+ # This class is used to interact with the Ollama API's chat endpoint, which
4
+ # generates conversational responses using a specified model. It inherits from
5
+ # the base command structure and provides the necessary functionality to execute
6
+ # chat requests for interactive conversations with language models.
7
+ #
8
+ # @example Initiating a chat conversation
9
+ # messages = [
10
+ # Ollama::Message.new(role: 'user', content: 'Hello, how are you?'),
11
+ # Ollama::Message.new(role: 'assistant', content: 'I am doing well, thank you!')
12
+ # ]
13
+ # chat = ollama.chat(model: 'llama3.1', stream: true, messages:)
1
14
  class Ollama::Commands::Chat
2
15
  include Ollama::DTO
3
16
 
17
+ # The path method returns the API endpoint path for chat requests.
18
+ #
19
+ # This class method provides the specific URL path used to interact with the
20
+ # Ollama API's chat endpoint. It is utilized internally by the command
21
+ # structure to determine the correct API route for conversational interactions.
22
+ #
23
+ # @return [ String ] the API endpoint path '/api/chat' for chat requests
4
24
  def self.path
5
25
  '/api/chat'
6
26
  end
7
27
 
28
+ # The initialize method sets up a new instance with streaming behavior.
29
+ #
30
+ # This method is responsible for initializing a new object instance and
31
+ # configuring it with parameters required for chat interactions. It sets up
32
+ # the model, conversation messages, tools, format, options, streaming behavior,
33
+ # keep-alive duration, and thinking mode.
34
+ #
35
+ # @param model [ String ] the name of the model to use for chat responses
36
+ # @param messages [ Array<Ollama::Message>, Hash, nil ] conversation history with roles and content
37
+ # @param tools [ Array<Ollama::Tool>, Hash, nil ] tools available for function calling
38
+ # @param format [ String, nil ] response format (e.g., 'json')
39
+ # @param options [ Ollama::Options, nil ] configuration parameters for the model
40
+ # @param stream [ TrueClass, FalseClass, nil ] whether to enable streaming for the operation
41
+ # @param keep_alive [ String, nil ] duration to keep the model loaded in memory
42
+ # @param think [ Boolean, nil ] whether to enable thinking mode for reasoning
8
43
  def initialize(model:, messages:, tools: nil, format: nil, options: nil, stream: nil, keep_alive: nil, think: nil)
9
44
  @model, @messages, @tools, @format, @options, @stream, @keep_alive, @think =
10
45
  model, as_array_of_hashes(messages), as_array_of_hashes(tools),
11
46
  format, options, stream, keep_alive, think
12
47
  end
13
48
 
14
- attr_reader :model, :messages, :tools, :format, :options, :stream, :keep_alive, :think
49
+ # The model attribute reader returns the model name associated with the object.
50
+ #
51
+ # @return [ String ] the name of the model to use for chat responses
52
+ attr_reader :model
15
53
 
54
+ # The messages attribute reader returns the conversation history associated with the object.
55
+ #
56
+ # @return [ Array<Ollama::Message>, nil ] conversation history with roles and content
57
+ attr_reader :messages
58
+
59
+ # The tools attribute reader returns the available tools associated with the object.
60
+ #
61
+ # @return [ Array<Ollama::Tool>, nil ] tools available for function calling
62
+ attr_reader :tools
63
+
64
+ # The format attribute reader returns the response format associated with the object.
65
+ #
66
+ # @return [ String, nil ] response format (e.g., 'json')
67
+ attr_reader :format
68
+
69
+ # The options attribute reader returns the model configuration parameters associated with the object.
70
+ #
71
+ # @return [ Ollama::Options, nil ] configuration parameters for the model
72
+ attr_reader :options
73
+
74
+ # The stream attribute reader returns the streaming behavior setting
75
+ # associated with the object.
76
+ #
77
+ # @return [ TrueClass, FalseClass, nil ] the streaming behavior flag, indicating whether
78
+ # streaming is enabled for the command execution (nil by default)
79
+ attr_reader :stream
80
+
81
+ # The keep_alive attribute reader returns the keep-alive duration associated with the object.
82
+ #
83
+ # @return [ String, nil ] duration to keep the model loaded in memory
84
+ attr_reader :keep_alive
85
+
86
+ # The think attribute reader returns the thinking mode setting associated with the object.
87
+ #
88
+ # @return [ Boolean, nil ] whether thinking mode is enabled for reasoning
89
+ attr_reader :think
90
+
91
+ # The client attribute writer allows setting the client instance associated
92
+ # with the object.
93
+ #
94
+ # This method assigns the client that will be used to perform requests and
95
+ # handle responses for this command. It is typically called internally when a
96
+ # command is executed through a client instance.
97
+ #
98
+ # @attr_writer [ Ollama::Client ] the assigned client instance
16
99
  attr_writer :client
17
100
 
101
+
102
+ # The perform method executes a command request using the specified handler.
103
+ #
104
+ # This method initiates a POST request to the Ollama API's chat endpoint,
105
+ # utilizing the client instance to send the request and process responses
106
+ # through the provided handler. It handles both streaming and non-streaming
107
+ # scenarios based on the command's configuration.
108
+ #
109
+ # @param handler [ Ollama::Handler ] the handler object responsible for processing API
110
+ # responses
111
+ #
112
+ # @return [ self ] returns the current instance after initiating the request
18
113
  def perform(handler)
19
114
  @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
20
115
  end
@@ -1,18 +1,76 @@
1
+ # A command class that represents the copy API endpoint for Ollama.
2
+ #
3
+ # This class is used to interact with the Ollama API's copy endpoint, which
4
+ # creates a copy of an existing model with a new name. It inherits from the base
5
+ # command structure and provides the necessary functionality to execute copy
6
+ # requests for model duplication.
7
+ #
8
+ # @example Copying a model to a new name
9
+ # copy = ollama.copy(source: 'llama3.1', destination: 'user/llama3.1')
1
10
  class Ollama::Commands::Copy
2
11
  include Ollama::DTO
3
12
 
13
+ # The path method returns the API endpoint path for copy requests.
14
+ #
15
+ # This class method provides the specific URL path used to interact with the
16
+ # Ollama API's copy endpoint. It is utilized internally by the command
17
+ # structure to determine the correct API route for duplicating models.
18
+ #
19
+ # @return [ String ] the API endpoint path '/api/copy' for copy requests
4
20
  def self.path
5
21
  '/api/copy'
6
22
  end
7
23
 
24
+ # The initialize method sets up a new instance with streaming disabled.
25
+ #
26
+ # This method is responsible for initializing a new object instance and
27
+ # configuring it with the source and destination model names. It explicitly
28
+ # disables streaming since copy operations are typically non-streaming.
29
+ #
30
+ # @param source [ String ] the name of the source model to be copied
31
+ # @param destination [ String ] the name of the new model to be created
8
32
  def initialize(source:, destination:)
9
33
  @source, @destination, @stream = source, destination, false
10
34
  end
11
35
 
12
- attr_reader :source, :destination, :stream
36
+ # The source attribute reader returns the source model name associated with the object.
37
+ #
38
+ # @return [ String ] the name of the source model to be copied
39
+ attr_reader :source
13
40
 
41
+ # The destination attribute reader returns the destination model name associated with the object.
42
+ #
43
+ # @return [ String ] the name of the new model to be created
44
+ attr_reader :destination
45
+
46
+ # The stream attribute reader returns the streaming behavior setting
47
+ # associated with the object.
48
+ #
49
+ # @return [ FalseClass ] the streaming behavior flag, indicating whether
50
+ # streaming is enabled for the command execution (always false for copy commands)
51
+ attr_reader :stream
52
+
53
+ # The client attribute writer allows setting the client instance associated
54
+ # with the object.
55
+ #
56
+ # This method assigns the client that will be used to perform requests and
57
+ # handle responses for this command. It is typically called internally when a
58
+ # command is executed through a client instance.
59
+ #
60
+ # @attr_writer [ Ollama::Client ] the assigned client instance
14
61
  attr_writer :client
15
62
 
63
+ # The perform method executes a command request using the specified handler.
64
+ #
65
+ # This method initiates a POST request to the Ollama API's copy endpoint,
66
+ # utilizing the client instance to send the request and process responses
67
+ # through the provided handler. It handles non-streaming scenarios since
68
+ # copy commands do not support streaming.
69
+ #
70
+ # @param handler [ Ollama::Handler ] the handler object responsible for processing API
71
+ # responses
72
+ #
73
+ # @return [ self ] returns the current instance after initiating the request
16
74
  def perform(handler)
17
75
  @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
18
76
  end