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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7010b648d380dea2bb5fcfd89597af4abbb37e064905dce4e393162d9896ef5
4
- data.tar.gz: ea8df217db7e0ddd70df548820c3abc73c7efa233af56ccde171dbaa087a9a92
3
+ metadata.gz: 4a174884d564679a1043d7a7efe61bdcdb9924e692e75918ca59d11c7af02509
4
+ data.tar.gz: fce0f1c512683a77fe1c074a9ea6f839432652de6e5ba2e8ae5b368f6c4febd9
5
5
  SHA512:
6
- metadata.gz: 4ac32ac4fd527aa94f11b50fe72c0d22a4d326368d2d7655d090a4af9e3fea466f478574c2c0ce2d70ef1394e7f3da8709ee5f76fdbf9672004d91850d45245d
7
- data.tar.gz: cfe22940814d91e3755cfc0d83b662db4ff505ac7b00980915eeaa51f63ff3b06a8a240cd926dbcf798d27c558c20c768a6f19e99119497e220c1e8fd1fd30f9
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,44 @@
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
+
25
+ ## 2025-07-21 v1.5.0
26
+
27
+ * Update `ollama_cli` script to handle client configuration via JSON and
28
+ clarify argument types:
29
+ * Added support for `-c ` option to specify client configuration as JSON
30
+ * Updated documentation to clarify which arguments expect JSON input
31
+ * Replaced direct client initialization with `configure_with` method for
32
+ better maintainability
33
+ * Update documentation for `ollama_cli` script:
34
+ * Reorganized usage instructions for clarity
35
+ * Added descriptions for new options: `-c ` and `-H `
36
+ * Clarified which arguments expect JSON input:
37
+ - `-M OPTIONS`: model options in JSON format
38
+ - `-s SYSTEM` and `-p PROMPT`: plain text inputs
39
+ * Improved formatting for better readability
40
+ * Add `ollama_browse` to Rakefile executable tasks
41
+
3
42
  ## 2025-07-17 v1.4.0
4
43
 
5
44
  * **New CLI Tool**: Added `bin/ollama_browse` for exploring model tags and
data/README.md CHANGED
@@ -350,26 +350,22 @@ To use `ollama_cli`, simply run it from the command line and follow the usage
350
350
  instructions:
351
351
 
352
352
  ```bash
353
- ollama_cli [OPTIONS]
354
- ```
355
-
356
- The available options are:
357
-
358
- * `-u URL`: The Ollama base URL. Can be set as an environment variable
359
- `OLLAMA_URL`.
360
- * `-m MODEL`: The Ollama model to chat with. Defaults to `llama3.1` if not
361
- specified.
362
- * `-M OPTIONS`: The Ollama model options to use. Can be set as an environment
363
- variable `OLLAMA_MODEL_OPTIONS`.
364
- * `-s SYSTEM`: The system prompt to use as a file. Can be set as an environment
365
- variable `OLLAMA_SYSTEM`.
366
- * `-p PROMPT`: The user prompt to use as a file. If it contains `%{stdin}`, it
367
- will be substituted with the standard input. If not given, stdin will be used
368
- as the prompt.
369
- * `-P VARIABLE`: Sets a prompt variable, e.g. `foo=bar`. Can be used multiple
370
- times.
371
- * `-H HANDLER`: The handler to use for the response. Defaults to `Print`.
372
- * `-S`: Use streaming for generation.
353
+ Usage: ollama_cli [OPTIONS]
354
+
355
+ -u URL the ollama base url, $OLLAMA_URL
356
+ -c CLIENT the ollama client config (JSON), $OLLAMA_CLIENT
357
+ -m MODEL the ollama model to chat with, $OLLAMA_MODEL
358
+ -M OPTIONS the ollama model options (JSON), $OLLAMA_MODEL_OPTIONS
359
+ -s SYSTEM the system prompt as plain text, $OLLAMA_SYSTEM
360
+ -p PROMPT the user prompt as plain text, $OLLAMA_PROMPT
361
+ if it contains %{stdin} it is substituted by stdin input
362
+ -P VARIABLE sets prompt var %{foo} to "bar" if VARIABLE is foo=bar
363
+ -H HANDLER the handler to use for the response, defaults to ChatStart
364
+ -S use streaming for generation
365
+ -T use thinking for generation
366
+ -h this help
367
+
368
+ ```
373
369
 
374
370
  #### Environment Variables
375
371
 
@@ -377,10 +373,11 @@ The following environment variables can be set to customize the behavior of
377
373
  `ollama_cli`:
378
374
 
379
375
  * `OLLAMA_URL`: The Ollama base URL.
376
+ * `OLLAMA_CLIENT`: The client config (JSON).
380
377
  * `OLLAMA_MODEL`: The Ollama model to chat with.
381
- * `OLLAMA_MODEL_OPTIONS`: The Ollama model options to use.
382
- * `OLLAMA_SYSTEM`: The system prompt to use as a file.
383
- * `OLLAMA_PROMPT`: The user prompt to use as a file.
378
+ * `OLLAMA_MODEL_OPTIONS`: The Ollama model options (JSON).
379
+ * `OLLAMA_SYSTEM`: The system prompt to use as plain text.
380
+ * `OLLAMA_PROMPT`: The user prompt to use as plain text.
384
381
 
385
382
  #### Debug Mode
386
383
 
@@ -405,6 +402,60 @@ The `-S` option enables streaming for generation. This allows the model to
405
402
  generate text in chunks, rather than waiting for the entire response to be
406
403
  generated.
407
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
+
408
459
  ### ollama\_chat
409
460
 
410
461
  This is a chat client that allows you to connect to an Ollama server and engage
data/Rakefile CHANGED
@@ -16,17 +16,17 @@ 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
- executables << 'ollama_console' << 'ollama_update' << 'ollama_cli'
22
+ executables << 'ollama_console' << 'ollama_update' << 'ollama_cli' << 'ollama_browse'
23
23
 
24
24
  required_ruby_version '~> 3.1'
25
25
 
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,16 +80,21 @@ 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]
69
91
 
70
92
  -u URL the ollama base url, $OLLAMA_URL
93
+ -c CLIENT the ollama client config (JSON), $OLLAMA_CLIENT
71
94
  -m MODEL the ollama model to chat with, $OLLAMA_MODEL
72
- -M OPTIONS the ollama model options to use, $OLLAMA_MODEL_OPTIONS
73
- -s SYSTEM the system prompt to use as a file, $OLLAMA_SYSTEM
74
- -p PROMPT the user prompt to use as a file, $OLLAMA_PROMPT
95
+ -M OPTIONS the ollama model options (JSON), $OLLAMA_MODEL_OPTIONS
96
+ -s SYSTEM the system prompt as plain text, $OLLAMA_SYSTEM
97
+ -p PROMPT the user prompt as plain text, $OLLAMA_PROMPT
75
98
  if it contains %{stdin} it is substituted by stdin input
76
99
  -P VARIABLE sets prompt var %{foo} to "bar" if VARIABLE is foo=bar
77
100
  -H HANDLER the handler to use for the response, defaults to ChatStart
@@ -83,11 +106,15 @@ def usage
83
106
  exit 0
84
107
  end
85
108
 
86
- opts = go 'u:m:M:s:p:P:H:STh', defaults: { ?H => 'ChatStart', ?M => '{}' }
109
+ opts = go 'u:m:M:s:p:P:H:c:STh', defaults: { ?H => 'ChatStart' }
87
110
 
88
111
  opts[?h] and usage
89
112
 
90
- base_url = opts[?u] || ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
113
+ base_url = opts[?u] || ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
114
+ client_config = Client::Config[
115
+ { base_url: } |
116
+ JSON(get_file_argument(opts[?c], default: ENV['OLLAMA_CLIENT']).full? || '{}')
117
+ ]
91
118
  model = opts[?m] || ENV.fetch('OLLAMA_MODEL', 'llama3.1')
92
119
  options = Ollama::Options.from_hash(JSON(
93
120
  get_file_argument(opts[?M], default: ENV['OLLAMA_MODEL_OPTIONS'])
@@ -98,13 +125,18 @@ prompt = get_file_argument(opts[?p], default: ENV['OLLAMA_PROMPT'])
98
125
  if prompt.nil?
99
126
  prompt = STDIN.read
100
127
  else
101
- vars = prompt.scan(/%\{([^}]+)\}/).inject([], &:concat).uniq.map(&:to_sym)
128
+ vars = prompt.named_placeholders
102
129
  stdin = (STDIN.read if vars.include?(:stdin)).to_s
103
130
  values = opts[?P].to_a.inject({ stdin: }) { |h, pair|
104
131
  n, v = pair.split(?=, 2)
105
132
  h.merge(n.to_sym => v)
106
133
  }
107
- 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:)
108
140
  end
109
141
 
110
142
  if ENV['DEBUG'].to_i == 1
@@ -127,7 +159,7 @@ handler = case
127
159
  handler
128
160
  end
129
161
 
130
- Client.new(base_url:, read_timeout: 120).generate(
162
+ Client.configure_with(client_config).generate(
131
163
  model:,
132
164
  system:,
133
165
  prompt:,
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)