ollama-ruby 1.5.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 +22 -0
  8. data/README.md +54 -0
  9. data/Rakefile +3 -2
  10. data/bin/ollama_cli +31 -4
  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 +6 -5
  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 +24 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f22498af2b68b25f11d486872f46a42ad16b94244d3f0a9cb7df9e436056894
4
- data.tar.gz: aa154d720f3df668020bdf39d38c7964e7081cabcfa4daec761ebb3809bfa57f
3
+ metadata.gz: 4a174884d564679a1043d7a7efe61bdcdb9924e692e75918ca59d11c7af02509
4
+ data.tar.gz: fce0f1c512683a77fe1c074a9ea6f839432652de6e5ba2e8ae5b368f6c4febd9
5
5
  SHA512:
6
- metadata.gz: a32fb5d1bd7dd225c8917c8e43b43f9983eb85c971fbe01f4993f9b0131761d467a93658d2cf50ca5db1d44da6ea20ad6885aded8a5747ea259ab3d4c261c6b2
7
- data.tar.gz: 067ca2f5c9c7de901732aae837dc151e023751a935952e4c0533dd50124610c367fc6149e16c096724077bb0c8f9f0781028f7dd108a32dfd4cdf1b533af9040
6
+ metadata.gz: 1d9d8c43b1bc35626c97b6974d80894890e4b76dbbcc021ded7db22eed54d2bf0e82cb163a0fde53bfddda5ff8ecd34773929f29bc49efbc8b34b0823e376045
7
+ data.tar.gz: 0fc66f6d378a9cc0e31bb130d5cb8fa0f6b847b8cb30328bf3b0713483272d5eae6edcf5bf139a2d565e27de9b89722176806e9656e45242f8102c24c804ad9d
@@ -0,0 +1,25 @@
1
+ context do
2
+ namespace "lib" do
3
+ Dir['lib/**/*.rb'].each do |filename|
4
+ file filename, tags: 'lib'
5
+ end
6
+ end
7
+
8
+ namespace "spec" do
9
+ Dir['spec/**/*.rb'].each do |filename|
10
+ file filename, tags: 'spec'
11
+ end
12
+ end
13
+
14
+ file 'README.md', tags: 'documentation'
15
+
16
+ file '.contexts/yard.md', tags: [ 'yard', 'cheatsheet' ]
17
+
18
+ meta guidelins: <<~EOT
19
+ # Guidelines for creating YARD documentation
20
+
21
+ - Look into the file, with tags yard and cheatsheet for how comment ruby
22
+ constructs.
23
+ - In comments above initialize methods never omit @return
24
+ EOT
25
+ end
data/.contexts/full.rb ADDED
@@ -0,0 +1,43 @@
1
+ context do
2
+ variable project_name: Pathname.pwd.basename
3
+
4
+ variable project_version: File.read('VERSION').chomp
5
+
6
+ variable branch: `git rev-parse --abbrev-ref HEAD`.chomp
7
+
8
+ namespace "structure" do
9
+ command "tree", tags: %w[ project_structure ]
10
+ end
11
+
12
+ namespace "bin" do
13
+ Dir['bin/**/*'].each do |filename|
14
+ file filename, tags: 'bin'
15
+ end
16
+ end
17
+
18
+ namespace "lib" do
19
+ Dir['lib/**/*.rb'].each do |filename|
20
+ file filename, tags: 'lib'
21
+ end
22
+ end
23
+
24
+ namespace "spec" do
25
+ Dir['spec/**/*.rb'].each do |filename|
26
+ file filename, tags: 'spec'
27
+ end
28
+ end
29
+
30
+ namespace "gems" do
31
+ file Dir['*.gemspec'].first
32
+ file 'Gemfile'
33
+ file 'Gemfile.lock'
34
+ end
35
+
36
+ file 'Rakefile', tags: 'gem_hadar'
37
+
38
+ file 'README.md', tags: 'documentation'
39
+
40
+ meta ruby: RUBY_DESCRIPTION
41
+
42
+ meta code_coverage: json('coverage/coverage_context.json')
43
+ end
data/.contexts/info.rb ADDED
@@ -0,0 +1,17 @@
1
+ context do
2
+ variable project_name: Pathname.pwd.basename
3
+
4
+ variable project_version: File.read('VERSION').chomp
5
+
6
+ variable branch: `git rev-parse --abbrev-ref HEAD`.chomp
7
+
8
+ namespace "structure" do
9
+ command "tree", tags: %w[ project_structure ]
10
+ end
11
+
12
+ file 'Rakefile', tags: 'gem_hadar'
13
+
14
+ file 'README.md', tags: 'documentation'
15
+
16
+ meta ruby: RUBY_DESCRIPTION
17
+ end
data/.contexts/lib.rb ADDED
@@ -0,0 +1,27 @@
1
+ context do
2
+ variable branch: `git rev-parse --abbrev-ref HEAD`.chomp
3
+
4
+ namespace "structure" do
5
+ command "tree", tags: %w[ project_structure ]
6
+ end
7
+
8
+ namespace "lib" do
9
+ Dir['lib/**/*.rb'].each do |filename|
10
+ file filename, tags: 'lib'
11
+ end
12
+ end
13
+
14
+ namespace "gems" do
15
+ file Dir['*.gemspec'].first
16
+ file 'Gemfile'
17
+ file 'Gemfile.lock'
18
+ end
19
+
20
+ file 'Rakefile', tags: 'gem_hadar'
21
+
22
+ file 'README.md', tags: 'documentation'
23
+
24
+ meta ruby: RUBY_DESCRIPTION
25
+
26
+ meta code_coverage: json('coverage/coverage_context.json')
27
+ end
data/.contexts/yard.md ADDED
@@ -0,0 +1,93 @@
1
+ # YARD CHEATSHEET http://yardoc.org
2
+
3
+ ## May 2020 - updated fork: https://gist.github.com/phansch/db18a595d2f5f1ef16646af72fe1fb0e
4
+
5
+ cribbed from http://pastebin.com/xgzeAmBn
6
+
7
+ Templates to remind you of the options and formatting for the different types of objects you might
8
+ want to document using YARD.
9
+
10
+ ## Modules
11
+
12
+ # Namespace for classes and modules that handle serving documentation over HTTP
13
+ # @since 0.6.0
14
+
15
+ ## Classes
16
+
17
+ # Abstract base class for CLI utilities. Provides some helper methods for
18
+ # the option parser
19
+ #
20
+ # @author Full Name
21
+ # @abstract
22
+ # @since 0.6.0
23
+ # @attr [Types] attribute_name a full description of the attribute
24
+ # @attr_reader [Types] name description of a readonly attribute
25
+ # @attr_writer [Types] name description of writeonly attribute
26
+ # @deprecated Describe the reason or provide alt. references here
27
+
28
+ ## Methods
29
+
30
+ # An alias to {Parser::SourceParser}'s parsing method
31
+ #
32
+ # @author Donovan Bray
33
+ #
34
+ # @see http://example.com Description of URL
35
+ # @see SomeOtherClass#method
36
+ #
37
+ # @deprecated Use {#my_new_method} instead of this method because
38
+ # it uses a library that is no longer supported in Ruby 1.9.
39
+ # The new method accepts the same parameters.
40
+ #
41
+ # @abstract
42
+ # @private
43
+ #
44
+ # @param opts [Hash] the options to create a message with.
45
+ # @option opts [String] :subject The subject
46
+ # @option opts [String] :from ('nobody') From address
47
+ # @option opts [String] :to Recipient email
48
+ # @option opts [String] :body ('') The email's body
49
+ #
50
+ # @param (see User#initialize)
51
+ # @param [OptionParser] opts the option parser object
52
+ # @param [Array<String>] args the arguments passed from input. This
53
+ # array will be modified.
54
+ # @param [Array<String, Symbol>] list the list of strings and symbols.
55
+ #
56
+ # The options parsed out of the commandline.
57
+ # Default options are:
58
+ # :format => :dot
59
+ #
60
+ # @example Reverse a string
61
+ # "mystring.reverse" #=> "gnirtsym"
62
+ #
63
+ # @example Parse a glob of files
64
+ # YARD.parse('lib/**/*.rb')
65
+ #
66
+ # @raise [ExceptionClass] description
67
+ #
68
+ # @return [optional, types, ...] description
69
+ # @return [true] always returns true
70
+ # @return [void]
71
+ # @return [String, nil] the contents of our object or nil
72
+ # if the object has not been filled with data.
73
+ #
74
+ # We don't care about the "type" here:
75
+ # @return the object
76
+ #
77
+ # @return [String, #read] a string or object that responds to #read
78
+ # @return description here with no types
79
+
80
+ ## Anywhere
81
+
82
+ # @todo Add support for Jabberwocky service
83
+ # There is an open source Jabberwocky library available
84
+ # at http://somesite.com that can be integrated easily
85
+ # into the project.
86
+
87
+ ## Blocks
88
+
89
+ # for block {|a, b, c| ... }
90
+ # @yield [a, b, c] Description of block
91
+ #
92
+ # @yieldparam [optional, types, ...] argname description
93
+ # @yieldreturn [optional, types, ...] description
data/CHANGES.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # Changes
2
2
 
3
+ ## 2025-08-18 v1.6.0
4
+
5
+ - Added **context_spook** gem as development dependency for documentation management
6
+ and introduced new context files for project structure documentation in
7
+ `.contexts/` directory
8
+ - Modified `bin/ollama_cli` to use `named_placeholders` and
9
+ `named_placeholders_interpolate` methods for prompt variable interpolation
10
+ - Added default value handling for missing prompt variables in `bin/ollama_cli`
11
+ - Removed default value `{ ?M => '{}' }` for the `?M` option in command line parser
12
+ - Displayed Ollama server version in bold and base URL with hyperlink formatting
13
+ - Used `Term::ANSIColor` for styled output in connection status message
14
+ - Updated `gem_hadar` development dependency to **2.0**
15
+ - Replaced manual SimpleCov configuration with `GemHadar::SimpleCov.start`
16
+ - Enhanced `parse_json` method in `Ollama::Client` to handle
17
+ `JSON::ParserError` exceptions gracefully with warnings
18
+ - Updated `Ollama::Client` spec to test error handling behavior for invalid
19
+ JSON input
20
+ - Replaced `RSpec.describe` with `describe` for cleaner test syntax
21
+ - Added `ollama_browse` utility documentation to README.md
22
+ - Improved nil comparison syntax in tests
23
+ - Reset `OLLAMA_URL` environment variable after client spec tests
24
+
3
25
  ## 2025-07-21 v1.5.0
4
26
 
5
27
  * Update `ollama_cli` script to handle client configuration via JSON and
data/README.md CHANGED
@@ -402,6 +402,60 @@ The `-S` option enables streaming for generation. This allows the model to
402
402
  generate text in chunks, rather than waiting for the entire response to be
403
403
  generated.
404
404
 
405
+ ### `ollama_browse`
406
+
407
+ The `ollama_browse` executable is a utility for exploring model tags and their
408
+ associated metadata directly from your command line. It allows you to quickly
409
+ view details such as file sizes, context windows, and digests for different
410
+ versions of a model hosted on the Ollama platform.
411
+
412
+ #### Usage
413
+
414
+ ```bash
415
+ Usage: ollama_browse MODEL
416
+
417
+ MODEL is the Ollama model name (e.g., 'deepseek-v3')
418
+ ```
419
+
420
+ To use `ollama_browse`, specify the model you wish to explore:
421
+
422
+ ```bash
423
+ ollama_browse deepseek-v3
424
+ ```
425
+
426
+ #### Output
427
+
428
+ The script will display information about different versions of the specified
429
+ model, including:
430
+
431
+ - **Digest**: A unique identifier for the model version.
432
+ - **File Size**: The size of the model file.
433
+ - **Context Window**: The maximum context length supported by the model.
434
+ - **Tags**: Different tags associated with the model version.
435
+
436
+ Example output for `deepseek-v3`:
437
+
438
+ ```bash
439
+ Model: https://ollama.com/library/deepseek-v3
440
+ 5da0e2d4a9e0 404GB 4K
441
+ · deepseek-v3:latest
442
+ · deepseek-v3:671blatest
443
+ · deepseek-v3:671b-q4_K_M
444
+ 96061c74c1a5 713GB 4K
445
+ · deepseek-v3:671b-q8_0
446
+ 7770bf5a5ed8 1.3TB 4K
447
+ · deepseek-v3:671b-fp16
448
+ ```
449
+
450
+ This output shows:
451
+ - Different versions of the `deepseek-v3` model.
452
+ - Each version's digest, file size, and context window.
453
+ - Associated tags for each version, which you can click to view more details on
454
+ the Ollama website.
455
+
456
+ **Note:** Tags are grouped by their corresponding digests, allowing users to
457
+ easily identify equivalent versions of a model.
458
+
405
459
  ### ollama\_chat
406
460
 
407
461
  This is a chat client that allows you to connect to an Ollama server and engage
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ GemHadar do
16
16
  '.yardoc', 'doc', 'tags', 'errors.lst', 'cscope.out', 'coverage', 'tmp',
17
17
  'yard'
18
18
  package_ignore '.all_images.yml', '.tool-versions', '.gitignore', 'VERSION',
19
- '.rspec', *Dir.glob('.github/**/*', File::FNM_DOTMATCH)
19
+ '.rspec', '.github', '.contexts'
20
20
  readme 'README.md'
21
21
 
22
22
  executables << 'ollama_console' << 'ollama_update' << 'ollama_cli' << 'ollama_browse'
@@ -26,7 +26,7 @@ GemHadar do
26
26
  dependency 'excon', '~> 1.0'
27
27
  dependency 'infobar', '~> 0.8'
28
28
  dependency 'json', '~> 2.0'
29
- dependency 'tins', '~> 1.34'
29
+ dependency 'tins', '~> 1.41'
30
30
  dependency 'term-ansicolor', '~> 1.11'
31
31
  dependency 'kramdown-ansi', '~> 0.0', '>= 0.0.1'
32
32
  dependency 'ostruct', '~> 0.0'
@@ -36,6 +36,7 @@ GemHadar do
36
36
  development_dependency 'webmock'
37
37
  development_dependency 'debug'
38
38
  development_dependency 'simplecov'
39
+ development_dependency 'context_spook'
39
40
 
40
41
  licenses << 'MIT'
41
42
 
data/bin/ollama_cli CHANGED
@@ -9,17 +9,35 @@ require 'json'
9
9
  require 'tmpdir'
10
10
 
11
11
  module Ollama::Handlers
12
+ # A handler that starts a chat session and prints the initial response.
13
+ #
14
+ # @example
15
+ # chat(model: 'llama3.1', messages: message, &ChatStart)
12
16
  class ChatStart
13
17
  include Ollama::Handlers::Concern
14
18
 
19
+ # The initialize method sets up a new Markdown handler instance with the
20
+ # specified output.
21
+ #
22
+ # @param output [IO] the output stream to write markdown content to,
23
+ # defaults to $stdout
15
24
  def initialize(output: $stdout)
16
25
  super
17
26
  @output.sync = true
18
27
  @content = ''
19
28
  end
20
29
 
30
+ # The content attribute reader returns the textual content of the message.
31
+ #
32
+ # @return [ String ] the content of the message
21
33
  attr_reader :content
22
34
 
35
+ # The call method processes a response by appending its content to an
36
+ # internal buffer and output stream. It also appends a newline and flushes
37
+ # the output when the response indicates completion.
38
+ #
39
+ # @param response [Object] the response object to process
40
+ # @return [self] returns itself to allow for method chaining
23
41
  def call(response)
24
42
  if content = response.response
25
43
  @content << content
@@ -62,7 +80,11 @@ def get_file_argument(path_or_content, default: nil)
62
80
  end
63
81
  end
64
82
 
65
- # Outputs usage information for the `ollama_cli`.
83
+ # The usage method displays the command-line usage information for the
84
+ # ollama_cli executable.
85
+ #
86
+ # This method prints out a formatted help message that describes all available
87
+ # options and their expected arguments for using the ollama_cli tool.
66
88
  def usage
67
89
  puts <<~EOT
68
90
  Usage: #{File.basename($0)} [OPTIONS]
@@ -84,7 +106,7 @@ def usage
84
106
  exit 0
85
107
  end
86
108
 
87
- opts = go 'u:m:M:s:p:P:H:c:STh', defaults: { ?H => 'ChatStart', ?M => '{}' }
109
+ opts = go 'u:m:M:s:p:P:H:c:STh', defaults: { ?H => 'ChatStart' }
88
110
 
89
111
  opts[?h] and usage
90
112
 
@@ -103,13 +125,18 @@ prompt = get_file_argument(opts[?p], default: ENV['OLLAMA_PROMPT'])
103
125
  if prompt.nil?
104
126
  prompt = STDIN.read
105
127
  else
106
- vars = prompt.scan(/%\{([^}]+)\}/).inject([], &:concat).uniq.map(&:to_sym)
128
+ vars = prompt.named_placeholders
107
129
  stdin = (STDIN.read if vars.include?(:stdin)).to_s
108
130
  values = opts[?P].to_a.inject({ stdin: }) { |h, pair|
109
131
  n, v = pair.split(?=, 2)
110
132
  h.merge(n.to_sym => v)
111
133
  }
112
- prompt = prompt % values
134
+ default = -> v {
135
+ msg = "prompt var #{v.inspect} not set"
136
+ STDERR.puts msg
137
+ "[#{msg}]"
138
+ }
139
+ prompt = prompt.named_placeholders_interpolate(values, default:)
113
140
  end
114
141
 
115
142
  if ENV['DEBUG'].to_i == 1
data/bin/ollama_console CHANGED
@@ -5,10 +5,24 @@ include Ollama
5
5
  require 'irb'
6
6
  require 'irb/history'
7
7
 
8
+ # The base_url method returns the Ollama API base URL.
9
+ #
10
+ # It first checks for the OLLAMA_URL environment variable.
11
+ # If not set, it defaults to 'http://' followed by the OLLAMA_HOST environment variable.
12
+ #
13
+ # @return [ String ] the base URL for the Ollama API
8
14
  def base_url
9
15
  ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
10
16
  end
11
17
 
18
+ # The ollama method provides access to a configured Ollama client instance.
19
+ #
20
+ # This method initializes and returns a singleton instance of the Ollama client,
21
+ # using the base URL specified in the base_url parameter. If an instance already
22
+ # exists, it returns the cached version instead of creating a new one.
23
+ #
24
+ # @param base_url [ String ] the base URL for the Ollama API endpoint
25
+ # @return [ Ollama::Client ] a configured Ollama client instance
12
26
  def ollama
13
27
  $ollama ||= Client.new(base_url:)
14
28
  end
@@ -21,5 +35,9 @@ if io = IRB.conf[:MAIN_CONTEXT].io and io.support_history_saving?
21
35
  io.load_history
22
36
  at_exit { io.save_history }
23
37
  end
38
+ puts "Now connected to ollama server, version %s, at %s" % [
39
+ Term::ANSIColor.bold { ollama.version.version },
40
+ Term::ANSIColor.hyperlink(ollama.base_url) { ollama.base_url },
41
+ ]
24
42
  ollama.help
25
43
  IRB.irb nil, ollama
@@ -1,9 +1,35 @@
1
+ # A module that provides command definition capabilities for the Ollama client.
2
+ #
3
+ # This module extends the client with the ability to define and execute API
4
+ # commands by creating method definitions that correspond to Ollama API
5
+ # endpoints. It handles the creation of command methods with appropriate
6
+ # default and streaming handlers, and manages the execution flow for these
7
+ # commands.
8
+ #
9
+ # @example Defining a custom command
10
+ # class MyClient
11
+ # include Ollama::Client::Command
12
+ # command(:custom, default_handler: Single, stream_handler: Collector)
13
+ # end
1
14
  module Ollama::Client::Command
2
15
  extend Tins::Concern
3
16
 
4
- module ClassMethods
5
- # Create Command `name`, if `stream` was true, set `stream_handler` as
6
- # default, otherwise `default_handler`.
17
+ class_methods do
18
+ # Creates a command method for the Ollama client
19
+ #
20
+ # Defines a new command method that corresponds to an Ollama API endpoint.
21
+ # The command method can be invoked with parameters and an optional handler
22
+ # to process responses. It determines which handler to use based on whether
23
+ # the command supports streaming and the presence of an explicit handler.
24
+ #
25
+ # @param name [ Symbol ] the name of the command to define
26
+ # @param default_handler [ Class ] the default handler class to use when no explicit handler is provided
27
+ # @param stream_handler [ Class, nil ] the handler class to use for streaming responses, if applicable
28
+ #
29
+ # @note Create Command `name`, if `stream` was true, set `stream_handler`
30
+ # as default, otherwise `default_handler`.
31
+ #
32
+ # @return [ self ] returns the receiver after defining the command method
7
33
  def command(name, default_handler:, stream_handler: nil)
8
34
  klass = Ollama::Commands.const_get(name.to_s.camelize)
9
35
  doc Ollama::Client::Doc.new(name)
@@ -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