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.
- checksums.yaml +4 -4
- data/.contexts/code_comment.rb +25 -0
- data/.contexts/full.rb +43 -0
- data/.contexts/info.rb +17 -0
- data/.contexts/lib.rb +27 -0
- data/.contexts/yard.md +93 -0
- data/CHANGES.md +22 -0
- data/README.md +54 -0
- data/Rakefile +3 -2
- data/bin/ollama_cli +31 -4
- data/bin/ollama_console +18 -0
- data/lib/ollama/client/command.rb +29 -3
- data/lib/ollama/client/configuration/config.rb +114 -3
- data/lib/ollama/client/doc.rb +18 -0
- data/lib/ollama/client.rb +131 -2
- data/lib/ollama/commands/chat.rb +96 -1
- data/lib/ollama/commands/copy.rb +59 -1
- data/lib/ollama/commands/create.rb +112 -1
- data/lib/ollama/commands/delete.rb +53 -1
- data/lib/ollama/commands/embed.rb +82 -1
- data/lib/ollama/commands/embeddings.rb +72 -1
- data/lib/ollama/commands/generate.rb +118 -2
- data/lib/ollama/commands/ps.rb +55 -0
- data/lib/ollama/commands/pull.rb +72 -1
- data/lib/ollama/commands/push.rb +65 -1
- data/lib/ollama/commands/show.rb +64 -1
- data/lib/ollama/commands/tags.rb +50 -0
- data/lib/ollama/commands/version.rb +50 -1
- data/lib/ollama/dto.rb +98 -1
- data/lib/ollama/errors.rb +50 -0
- data/lib/ollama/handlers/collector.rb +34 -0
- data/lib/ollama/handlers/concern.rb +60 -2
- data/lib/ollama/handlers/dump_json.rb +20 -0
- data/lib/ollama/handlers/dump_yaml.rb +22 -0
- data/lib/ollama/handlers/markdown.rb +28 -0
- data/lib/ollama/handlers/nop.rb +20 -0
- data/lib/ollama/handlers/print.rb +27 -0
- data/lib/ollama/handlers/progress.rb +38 -0
- data/lib/ollama/handlers/say.rb +66 -0
- data/lib/ollama/handlers/single.rb +35 -0
- data/lib/ollama/handlers.rb +9 -0
- data/lib/ollama/image.rb +67 -0
- data/lib/ollama/json_loader.rb +17 -0
- data/lib/ollama/message.rb +46 -1
- data/lib/ollama/options.rb +27 -2
- data/lib/ollama/response.rb +17 -0
- data/lib/ollama/tool/function/parameters/property.rb +41 -1
- data/lib/ollama/tool/function/parameters.rb +40 -1
- data/lib/ollama/tool/function.rb +44 -1
- data/lib/ollama/tool.rb +37 -1
- data/lib/ollama/version.rb +1 -1
- data/lib/ollama.rb +26 -0
- data/ollama-ruby.gemspec +6 -5
- data/spec/ollama/client/doc_spec.rb +1 -1
- data/spec/ollama/client_spec.rb +19 -1
- data/spec/ollama/commands/chat_spec.rb +1 -1
- data/spec/ollama/commands/copy_spec.rb +1 -1
- data/spec/ollama/commands/create_spec.rb +1 -1
- data/spec/ollama/commands/delete_spec.rb +1 -1
- data/spec/ollama/commands/embed_spec.rb +1 -1
- data/spec/ollama/commands/embeddings_spec.rb +1 -1
- data/spec/ollama/commands/generate_spec.rb +1 -1
- data/spec/ollama/commands/ps_spec.rb +1 -1
- data/spec/ollama/commands/pull_spec.rb +1 -1
- data/spec/ollama/commands/push_spec.rb +1 -1
- data/spec/ollama/commands/show_spec.rb +1 -1
- data/spec/ollama/commands/tags_spec.rb +1 -1
- data/spec/ollama/commands/version_spec.rb +1 -1
- data/spec/ollama/handlers/collector_spec.rb +1 -1
- data/spec/ollama/handlers/dump_json_spec.rb +1 -1
- data/spec/ollama/handlers/dump_yaml_spec.rb +1 -1
- data/spec/ollama/handlers/markdown_spec.rb +1 -1
- data/spec/ollama/handlers/nop_spec.rb +2 -2
- data/spec/ollama/handlers/print_spec.rb +1 -1
- data/spec/ollama/handlers/progress_spec.rb +1 -1
- data/spec/ollama/handlers/say_spec.rb +1 -1
- data/spec/ollama/handlers/single_spec.rb +1 -1
- data/spec/ollama/image_spec.rb +1 -1
- data/spec/ollama/message_spec.rb +1 -1
- data/spec/ollama/options_spec.rb +1 -1
- data/spec/ollama/tool_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -6
- metadata +24 -5
data/lib/ollama/commands/tags.rb
CHANGED
@@ -1,18 +1,68 @@
|
|
1
|
+
# A command class that represents the tags API endpoint for Ollama.
|
2
|
+
#
|
3
|
+
# This class is used to interact with the Ollama API's tags endpoint, which
|
4
|
+
# retrieves information about locally available models. It inherits from the
|
5
|
+
# base command structure and provides the necessary functionality to execute
|
6
|
+
# tag listing requests.
|
7
|
+
#
|
8
|
+
# @example Retrieving a list of local models
|
9
|
+
# tags = ollama.tags
|
10
|
+
# tags.models # => array of model information hashes
|
1
11
|
class Ollama::Commands::Tags
|
12
|
+
|
13
|
+
# The path method returns the API endpoint path for tag listing requests.
|
14
|
+
#
|
15
|
+
# This class method provides the specific URL path used to interact with the
|
16
|
+
# Ollama API's tags endpoint. It is utilized internally by the command
|
17
|
+
# structure to determine the correct API route for retrieving information
|
18
|
+
# about locally available models.
|
19
|
+
#
|
20
|
+
# @return [ String ] the API endpoint path '/api/tags' for tag listing requests
|
2
21
|
def self.path
|
3
22
|
'/api/tags'
|
4
23
|
end
|
5
24
|
|
25
|
+
# The initialize method sets up a new instance with streaming disabled.
|
26
|
+
#
|
27
|
+
# This method is responsible for initializing a new object instance and
|
28
|
+
# configuring it with a default setting that disables streaming behavior. It
|
29
|
+
# is typically called during the object creation process to establish the
|
30
|
+
# initial state of the instance.
|
31
|
+
#
|
32
|
+
# @param parameters [ Hash ] a hash containing initialization parameters
|
6
33
|
def initialize(**parameters)
|
7
34
|
parameters.empty? or raise ArgumentError,
|
8
35
|
"Invalid parameters: #{parameters.keys * ' '}"
|
9
36
|
@stream = false
|
10
37
|
end
|
11
38
|
|
39
|
+
# The stream attribute reader returns the streaming behavior setting
|
40
|
+
# associated with the object.
|
41
|
+
#
|
42
|
+
# @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
|
43
|
+
# whether streaming is enabled for the command execution
|
12
44
|
attr_reader :stream
|
13
45
|
|
46
|
+
# The client attribute writer allows setting the client instance associated
|
47
|
+
# with the object.
|
48
|
+
#
|
49
|
+
# This method assigns the client that will be used to perform requests and
|
50
|
+
# handle responses for this command. It is typically called internally when a
|
51
|
+
# command is executed through a client instance.
|
52
|
+
#
|
53
|
+
# @attr_writer [ Ollama::Client ] the assigned client instance
|
14
54
|
attr_writer :client
|
15
55
|
|
56
|
+
# The perform method executes a command request using the specified handler.
|
57
|
+
#
|
58
|
+
# This method initiates a request to the Ollama API endpoint associated with
|
59
|
+
# the command, utilizing the client instance to send the request and process
|
60
|
+
# responses through the provided handler. It handles both streaming and
|
61
|
+
# non-streaming scenarios based on the command's configuration.
|
62
|
+
#
|
63
|
+
# @param handler [ Ollama::Handlers ] the handler object responsible for processing API responses
|
64
|
+
#
|
65
|
+
# @return [ self ] returns the current instance after initiating the request
|
16
66
|
def perform(handler)
|
17
67
|
@client.request(method: :get, path: self.class.path, stream:, handler:)
|
18
68
|
end
|
@@ -1,16 +1,65 @@
|
|
1
|
+
|
2
|
+
# A command class that represents the version API endpoint for Ollama.
|
3
|
+
#
|
4
|
+
# This class is used to interact with the Ollama API's version endpoint,
|
5
|
+
# which retrieves information about the Ollama server's version. It inherits
|
6
|
+
# from the base command structure and provides the necessary functionality
|
7
|
+
# to execute version requests.
|
8
|
+
#
|
9
|
+
# @example Retrieving the Ollama server version
|
10
|
+
# version = ollama.version
|
11
|
+
# puts version[:version] # => "0.1.0" or similar version string
|
1
12
|
class Ollama::Commands::Version
|
13
|
+
# The path method returns the API endpoint path for version requests.
|
14
|
+
#
|
15
|
+
# This class method provides the specific URL path used to interact with the
|
16
|
+
# Ollama API's version endpoint. It is utilized internally by the command
|
17
|
+
# structure to determine the correct API route for version-related
|
18
|
+
# operations.
|
19
|
+
#
|
20
|
+
# @return [ String ] the API endpoint path '/api/version' for version
|
21
|
+
# requests
|
2
22
|
def self.path
|
3
23
|
'/api/version'
|
4
24
|
end
|
5
25
|
|
6
|
-
|
26
|
+
# The initialize method sets up a new instance with streaming disabled.
|
27
|
+
#
|
28
|
+
# This method is responsible for initializing a new object instance and
|
29
|
+
# configuring it with a default setting that disables streaming behavior.
|
30
|
+
# It is typically called during the object creation process to establish
|
31
|
+
# the initial state of the instance.
|
32
|
+
def initialize
|
7
33
|
@stream = false
|
8
34
|
end
|
9
35
|
|
36
|
+
# The stream attribute reader returns the streaming behavior setting
|
37
|
+
# associated with the object.
|
38
|
+
#
|
39
|
+
# @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
|
40
|
+
# whether streaming is enabled for the command execution
|
10
41
|
attr_reader :stream
|
11
42
|
|
43
|
+
# The client attribute writer allows setting the client instance associated
|
44
|
+
# with the object.
|
45
|
+
#
|
46
|
+
# This method assigns the client that will be used to perform requests and
|
47
|
+
# handle responses for this command. It is typically called internally when a
|
48
|
+
# command is executed through a client instance.
|
49
|
+
#
|
50
|
+
# @attr_writer [ Ollama::Client ] the assigned client instance
|
12
51
|
attr_writer :client
|
13
52
|
|
53
|
+
# The perform method executes a command request using the specified handler.
|
54
|
+
#
|
55
|
+
# This method initiates a request to the Ollama API endpoint associated with
|
56
|
+
# the command, utilizing the client instance to send the request and process
|
57
|
+
# responses through the provided handler. It handles both streaming and
|
58
|
+
# non-streaming scenarios based on the command's configuration.
|
59
|
+
#
|
60
|
+
# @param handler [ Ollama::Handler ] the handler object responsible for processing API responses
|
61
|
+
#
|
62
|
+
# @return [ self ] returns the current instance after initiating the request
|
14
63
|
def perform(handler)
|
15
64
|
@client.request(method: :get, path: self.class.path, stream:, handler:)
|
16
65
|
end
|
data/lib/ollama/dto.rb
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
# A module that provides a foundation for data transfer objects (DTOs) within
|
2
|
+
# the Ollama library.
|
3
|
+
#
|
4
|
+
# The DTO module includes common functionality for converting objects to and
|
5
|
+
# from JSON, handling attribute management, and providing utility methods for
|
6
|
+
# processing arrays and hashes. It serves as a base class for various command
|
7
|
+
# and data structures used in communicating with the Ollama API.
|
8
|
+
#
|
9
|
+
# @example Using DTO functionality in a command class
|
10
|
+
# class MyCommand
|
11
|
+
# include Ollama::DTO
|
12
|
+
# attr_reader :name, :value
|
13
|
+
# def initialize(name:, value:)
|
14
|
+
# @name, @value = name, value
|
15
|
+
# end
|
16
|
+
# end
|
1
17
|
module Ollama::DTO
|
2
18
|
extend Tins::Concern
|
3
19
|
|
@@ -5,19 +21,49 @@ module Ollama::DTO
|
|
5
21
|
self.attributes = Set.new
|
6
22
|
end
|
7
23
|
|
8
|
-
|
24
|
+
class_methods do
|
25
|
+
# The attributes accessor reads and writes the attributes instance
|
26
|
+
# variable.
|
27
|
+
#
|
28
|
+
# @return [ Set ] the set of attributes stored in the instance variable
|
9
29
|
attr_accessor :attributes
|
10
30
|
|
31
|
+
# The from_hash method creates a new instance of the class by converting a
|
32
|
+
# hash into keyword arguments.
|
33
|
+
#
|
34
|
+
# This method is typically used to instantiate objects from JSON data or
|
35
|
+
# other hash-based sources, transforming the hash keys to symbols and
|
36
|
+
# passing them as keyword arguments to the constructor.
|
37
|
+
#
|
38
|
+
# @param hash [ Hash ] a hash containing the attributes for the new instance
|
39
|
+
#
|
40
|
+
# @return [ self ] a new instance of the class initialized with the hash data
|
11
41
|
def from_hash(hash)
|
12
42
|
new(**hash.transform_keys(&:to_sym))
|
13
43
|
end
|
14
44
|
|
45
|
+
# The attr_reader method extends the functionality of the standard
|
46
|
+
# attr_reader by also registering the declared attributes in the class's
|
47
|
+
# attributes set.
|
48
|
+
#
|
49
|
+
# @param names [ Array<Symbol> ] one or more attribute names to be declared
|
50
|
+
# as readable and registered
|
15
51
|
def attr_reader(*names)
|
16
52
|
super
|
17
53
|
attributes.merge(names.map(&:to_sym))
|
18
54
|
end
|
19
55
|
end
|
20
56
|
|
57
|
+
# The as_array_of_hashes method converts an object into an array of hashes.
|
58
|
+
#
|
59
|
+
# If the object responds to to_hash, it wraps the result in an array.
|
60
|
+
# If the object responds to to_ary, it maps each element to a hash and
|
61
|
+
# returns the resulting array.
|
62
|
+
#
|
63
|
+
# @param obj [ Object ] the object to be converted
|
64
|
+
#
|
65
|
+
# @return [ Array<Hash>, nil ] an array of hashes if the conversion was
|
66
|
+
# possible, or nil otherwise
|
21
67
|
def as_array_of_hashes(obj)
|
22
68
|
if obj.respond_to?(:to_hash)
|
23
69
|
[ obj.to_hash ]
|
@@ -26,10 +72,29 @@ module Ollama::DTO
|
|
26
72
|
end
|
27
73
|
end
|
28
74
|
|
75
|
+
# The as_hash method converts an object to a hash representation.
|
76
|
+
#
|
77
|
+
# If the object responds to to_hash, it returns the result of that method call.
|
78
|
+
# If the object does not respond to to_hash, it returns nil.
|
79
|
+
#
|
80
|
+
# @param obj [ Object ] the object to be converted to a hash
|
81
|
+
#
|
82
|
+
# @return [ Hash, nil ] the hash representation of the object or nil if the
|
83
|
+
# object does not respond to to_hash
|
29
84
|
def as_hash(obj)
|
30
85
|
obj&.to_hash
|
31
86
|
end
|
32
87
|
|
88
|
+
# The as_array method converts an object into an array representation.
|
89
|
+
#
|
90
|
+
# If the object is nil, it returns nil.
|
91
|
+
# If the object responds to to_ary, it calls to_ary and returns the result.
|
92
|
+
# Otherwise, it wraps the object in an array and returns it.
|
93
|
+
#
|
94
|
+
# @param obj [ Object ] the object to be converted to an array
|
95
|
+
#
|
96
|
+
# @return [ Array, nil ] an array containing the object or its elements, or
|
97
|
+
# nil if the input is nil
|
33
98
|
def as_array(obj)
|
34
99
|
if obj.nil?
|
35
100
|
obj
|
@@ -40,21 +105,53 @@ module Ollama::DTO
|
|
40
105
|
end
|
41
106
|
end
|
42
107
|
|
108
|
+
# The as_json method converts the object's attributes into a JSON-compatible
|
109
|
+
# hash.
|
110
|
+
#
|
111
|
+
# This method gathers all defined attributes of the object and constructs a
|
112
|
+
# hash representation, excluding any nil values or empty collections.
|
113
|
+
#
|
114
|
+
# @return [ Hash ] a hash containing the object's non-nil and non-empty attributes
|
43
115
|
def as_json(*)
|
44
116
|
self.class.attributes.each_with_object({}) { |a, h| h[a] = send(a) }.
|
45
117
|
reject { _2.nil? || _2.ask_and_send(:size) == 0 }
|
46
118
|
end
|
47
119
|
|
120
|
+
# The == method compares two objects for equality based on their JSON representation.
|
121
|
+
#
|
122
|
+
# This method checks if the JSON representation of the current object is
|
123
|
+
# equal to the JSON representation of another object.
|
124
|
+
#
|
125
|
+
# @param other [ Object ] the object to compare against
|
126
|
+
#
|
127
|
+
# @return [ TrueClass, FalseClass ] true if both objects have identical JSON
|
128
|
+
# representations, false otherwise
|
48
129
|
def ==(other)
|
49
130
|
as_json == other.as_json
|
50
131
|
end
|
51
132
|
|
52
133
|
alias to_hash as_json
|
53
134
|
|
135
|
+
# The empty? method checks whether the object has any attributes defined.
|
136
|
+
#
|
137
|
+
# This method determines if the object contains no attributes by checking
|
138
|
+
# if its hash representation is empty. It is typically used to verify
|
139
|
+
# if an object, such as a DTO, has been initialized with any values.
|
140
|
+
#
|
141
|
+
# @return [ TrueClass, FalseClass ] true if the object has no attributes,
|
142
|
+
# false otherwise
|
54
143
|
def empty?
|
55
144
|
to_hash.empty?
|
56
145
|
end
|
57
146
|
|
147
|
+
# The to_json method converts the object's JSON representation into a JSON
|
148
|
+
# string format.
|
149
|
+
#
|
150
|
+
# This method utilizes the object's existing as_json representation and
|
151
|
+
# applies the standard JSON serialization to produce a formatted JSON string
|
152
|
+
# output.
|
153
|
+
#
|
154
|
+
# @return [ String ] a JSON string representation of the object
|
58
155
|
def to_json(*)
|
59
156
|
as_json.to_json(*)
|
60
157
|
end
|
data/lib/ollama/errors.rb
CHANGED
@@ -1,14 +1,64 @@
|
|
1
1
|
module Ollama
|
2
|
+
|
3
|
+
# A module that groups together various error classes used by the Ollama client.
|
4
|
+
#
|
5
|
+
# This module serves as a namespace for custom exception types that are raised
|
6
|
+
# when errors occur during interactions with the Ollama API, providing more
|
7
|
+
# specific information about the nature of the problem.
|
2
8
|
module Errors
|
9
|
+
# The base error class for Ollama-related exceptions.
|
10
|
+
#
|
11
|
+
# This class serves as the parent class for all custom exceptions raised by the Ollama library.
|
12
|
+
# It provides a common foundation for error handling within the gem, allowing developers to
|
13
|
+
# rescue specific Ollama errors or the general error type when interacting with the Ollama API.
|
3
14
|
class Error < StandardError
|
4
15
|
end
|
5
16
|
|
17
|
+
# Ollama error class for handling cases where a requested resource is not
|
18
|
+
# found.
|
19
|
+
#
|
20
|
+
# This exception is raised when the Ollama API returns a 404 status code,
|
21
|
+
# indicating that the requested model or resource could not be located.
|
22
|
+
#
|
23
|
+
# @example Handling a not found error
|
24
|
+
# begin
|
25
|
+
# ollama.show(model: 'nonexistent-model')
|
26
|
+
# rescue Ollama::Errors::NotFoundError
|
27
|
+
# puts "Model was not found"
|
28
|
+
# end
|
6
29
|
class NotFoundError < Error
|
7
30
|
end
|
8
31
|
|
32
|
+
# Ollama error class for handling timeout errors when communicating with
|
33
|
+
# the Ollama API.
|
34
|
+
#
|
35
|
+
# This exception is raised when a request to the Ollama API times out
|
36
|
+
# during connection, reading, or writing operations. It inherits from
|
37
|
+
# Ollama::Errors::Error and provides specific handling for timeout-related
|
38
|
+
# issues.
|
39
|
+
#
|
40
|
+
# @example Handling a timeout error
|
41
|
+
# begin
|
42
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World')
|
43
|
+
# rescue Ollama::Errors::TimeoutError
|
44
|
+
# puts "Request timed out, consider increasing timeouts"
|
45
|
+
# end
|
9
46
|
class TimeoutError < Error
|
10
47
|
end
|
11
48
|
|
49
|
+
# Ollama error class for handling socket errors when communicating with the Ollama API.
|
50
|
+
#
|
51
|
+
# This exception is raised when a socket-level error occurs while
|
52
|
+
# attempting to connect to or communicate with the Ollama API.
|
53
|
+
# It inherits from Ollama::Errors::Error and provides specific handling for
|
54
|
+
# network-related issues that prevent successful API communication.
|
55
|
+
#
|
56
|
+
# @example Handling a socket error
|
57
|
+
# begin
|
58
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World')
|
59
|
+
# rescue Ollama::Errors::SocketError
|
60
|
+
# puts "Network connection failed"
|
61
|
+
# end
|
12
62
|
class SocketError < Error
|
13
63
|
end
|
14
64
|
end
|
@@ -1,16 +1,50 @@
|
|
1
|
+
# A handler that collects responses into an array and returns the array as the result.
|
2
|
+
#
|
3
|
+
# The Collector handler is designed to accumulate all responses during command execution
|
4
|
+
# and provide access to the complete collection of responses through its result method.
|
5
|
+
# It is typically used when multiple responses are expected and need to be processed
|
6
|
+
# together rather than individually.
|
7
|
+
#
|
8
|
+
# @example Using the Collector handler to gather all responses
|
9
|
+
# responses = ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Collector)
|
10
|
+
# # responses will contain an array of all response objects received
|
1
11
|
class Ollama::Handlers::Collector
|
2
12
|
include Ollama::Handlers::Concern
|
3
13
|
|
14
|
+
# The initialize method sets up a new handler instance with the specified
|
15
|
+
# output destination and initializes an empty array for collecting responses.
|
16
|
+
#
|
17
|
+
# @param output [ IO ] the output stream to be used for handling responses,
|
18
|
+
# defaults to $stdout
|
4
19
|
def initialize(output: $stdout)
|
5
20
|
super
|
6
21
|
@array = []
|
7
22
|
end
|
8
23
|
|
24
|
+
# The call method processes a response by appending it to an internal array.
|
25
|
+
#
|
26
|
+
# This method is responsible for handling individual responses during command
|
27
|
+
# execution by storing them in an internal array for later retrieval. It
|
28
|
+
# supports method chaining by returning the handler instance itself after
|
29
|
+
# processing.
|
30
|
+
#
|
31
|
+
# @param response [ Ollama::Response ] the response object to be processed and stored
|
32
|
+
#
|
33
|
+
# @return [ self ] returns the handler instance itself after processing the response
|
9
34
|
def call(response)
|
10
35
|
@array << response
|
11
36
|
self
|
12
37
|
end
|
13
38
|
|
39
|
+
# The result method returns the collected response data from handler
|
40
|
+
# operations.
|
41
|
+
#
|
42
|
+
# This method provides access to the accumulated results after a command has
|
43
|
+
# been executed with a handler that collects responses. It returns the
|
44
|
+
# internal array containing all responses that were processed by the handler.
|
45
|
+
#
|
46
|
+
# @return [ Array<Ollama::Response>, nil ] the array of collected response objects,
|
47
|
+
# or nil if no responses were collected
|
14
48
|
def result
|
15
49
|
@array
|
16
50
|
end
|
@@ -1,29 +1,87 @@
|
|
1
1
|
require 'tins/concern'
|
2
2
|
require 'tins/implement'
|
3
3
|
|
4
|
+
# A module that defines the common interface and behavior for all response
|
5
|
+
# handlers used with Ollama client commands.
|
6
|
+
#
|
7
|
+
# Handlers are responsible for processing responses from the Ollama API
|
8
|
+
# according to specific logic, such as printing output, collecting results, or
|
9
|
+
# displaying progress. This module establishes the foundational structure that
|
10
|
+
# all handler classes must implement to be compatible with the client's command
|
11
|
+
# execution flow.
|
4
12
|
module Ollama::Handlers::Concern
|
5
13
|
extend Tins::Concern
|
6
14
|
extend Tins::Implement
|
7
15
|
|
16
|
+
# The initialize method sets up a new handler instance with the specified
|
17
|
+
# output destination.
|
18
|
+
#
|
19
|
+
# @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
|
8
20
|
def initialize(output: $stdout)
|
9
21
|
@output = output
|
10
22
|
end
|
11
23
|
|
24
|
+
# The output attribute reader returns the output stream used by the client.
|
25
|
+
#
|
26
|
+
# @return [ IO ] the output stream, typically $stdout, to which responses and
|
27
|
+
# messages are written
|
12
28
|
attr_reader :output
|
13
29
|
|
30
|
+
# The result method returns the collected response data from handler operations.
|
31
|
+
#
|
32
|
+
# This method provides access to the accumulated results after a command has
|
33
|
+
# been executed with a handler that collects responses, such as Collector or
|
34
|
+
# Single handlers.
|
35
|
+
#
|
36
|
+
# @return [ Ollama::Response, Array<Ollama::Response>, nil ] the result of the handler operation,
|
37
|
+
# which may be a single response, an array of responses, or nil
|
38
|
+
# depending on the handler type and the command execution
|
14
39
|
attr_reader :result
|
15
40
|
|
16
|
-
implement :call
|
17
41
|
|
42
|
+
# The implement :call, :subclass line enforces that any class including this
|
43
|
+
# concern must implement a `call` instance method. This creates a contract
|
44
|
+
# that ensures all handler implementations will have the required interface
|
45
|
+
# for processing Ollama API responses. The :subclass parameter indicates this
|
46
|
+
# validation occurs at the subclass level rather than the module level,
|
47
|
+
# meaning concrete classes inheriting from this concern must provide their
|
48
|
+
# own implementation of the call method. This is a form of compile-time
|
49
|
+
# interface checking that helps prevent runtime errors when handlers are
|
50
|
+
# expected to be callable with response objects.
|
51
|
+
implement :call, :subclass
|
52
|
+
|
53
|
+
# The to_proc method converts the handler instance into a proc object.
|
54
|
+
#
|
55
|
+
# This method returns a lambda that takes a response parameter and calls the
|
56
|
+
# handler's call method with that response, enabling the handler to be used
|
57
|
+
# in contexts where a proc is expected.
|
58
|
+
#
|
59
|
+
# @return [ Proc ] a proc that wraps the handler's call method for response
|
60
|
+
# processing
|
18
61
|
def to_proc
|
19
62
|
-> response { call(response) }
|
20
63
|
end
|
21
64
|
|
22
|
-
|
65
|
+
class_methods do
|
66
|
+
# The call method invokes the handler's call method with the provided
|
67
|
+
# response.
|
68
|
+
#
|
69
|
+
# @param response [ Ollama::Response ] the response object to be processed by the
|
70
|
+
# handler
|
71
|
+
#
|
72
|
+
# @return [ self ] returns the handler instance itself after processing the
|
73
|
+
# response
|
23
74
|
def call(response)
|
24
75
|
new.call(response)
|
25
76
|
end
|
26
77
|
|
78
|
+
# The to_proc method converts the handler class into a proc object.
|
79
|
+
#
|
80
|
+
# This method creates a new instance of the handler and returns its to_proc
|
81
|
+
# representation, enabling the handler class to be used in contexts where a
|
82
|
+
# proc is expected.
|
83
|
+
#
|
84
|
+
# @return [ Proc ] a proc that wraps the handler's call method for response processing
|
27
85
|
def to_proc
|
28
86
|
new.to_proc
|
29
87
|
end
|
@@ -1,6 +1,26 @@
|
|
1
|
+
# A handler that outputs JSON representations of responses to the specified
|
2
|
+
# output stream.
|
3
|
+
#
|
4
|
+
# This class is designed to serialize and display API responses in JSON format,
|
5
|
+
# making it easy to inspect the raw data returned by Ollama commands. It
|
6
|
+
# implements the standard handler interface and can be used with any command
|
7
|
+
# that supports response processing.
|
8
|
+
#
|
9
|
+
# @example Using the DumpJSON handler to output response data as JSON
|
10
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &DumpJSON)
|
1
11
|
class Ollama::Handlers::DumpJSON
|
2
12
|
include Ollama::Handlers::Concern
|
3
13
|
|
14
|
+
# The call method processes a response by outputting its JSON representation.
|
15
|
+
#
|
16
|
+
# This method takes a response object and serializes it into a formatted JSON
|
17
|
+
# string, which is then written to the specified output stream. It is
|
18
|
+
# designed to provide detailed inspection of API responses in a
|
19
|
+
# human-readable format.
|
20
|
+
#
|
21
|
+
# @param response [ Ollama::Response ] the response object to be serialized and output
|
22
|
+
#
|
23
|
+
# @return [ self ] returns the handler instance itself after processing the response
|
4
24
|
def call(response)
|
5
25
|
@output.puts JSON::pretty_generate(response, allow_nan: true, max_nesting: false)
|
6
26
|
self
|
@@ -1,6 +1,28 @@
|
|
1
|
+
# A handler that outputs YAML representations of responses to the specified
|
2
|
+
# output stream.
|
3
|
+
#
|
4
|
+
# This class is designed to serialize and display API responses in YAML format,
|
5
|
+
# making it easy to inspect the raw data returned by Ollama commands. It
|
6
|
+
# implements the standard handler interface and can be used with any command
|
7
|
+
# that supports response processing.
|
8
|
+
#
|
9
|
+
# @example Using the DumpYAML handler to output response data as YAML
|
10
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &DumpYAML)
|
1
11
|
class Ollama::Handlers::DumpYAML
|
2
12
|
include Ollama::Handlers::Concern
|
3
13
|
|
14
|
+
# The call method processes a response by outputting its YAML representation.
|
15
|
+
#
|
16
|
+
# This method takes a response object and serializes it into YAML format,
|
17
|
+
# writing the result to the configured output stream. It is designed to
|
18
|
+
# handle API responses that need to be displayed or logged in a structured
|
19
|
+
# YAML format for debugging or inspection purposes.
|
20
|
+
#
|
21
|
+
# @param response [ Ollama::Response ] the response object to be serialized
|
22
|
+
# and output
|
23
|
+
#
|
24
|
+
# @return [ self ] returns the handler instance itself after processing the
|
25
|
+
# response
|
4
26
|
def call(response)
|
5
27
|
@output.puts Psych.dump(response)
|
6
28
|
self
|
@@ -1,10 +1,25 @@
|
|
1
1
|
require 'term/ansicolor'
|
2
2
|
require 'kramdown/ansi'
|
3
3
|
|
4
|
+
# A handler that processes responses by rendering them as ANSI-markdown output.
|
5
|
+
#
|
6
|
+
# This class is designed to display streaming or non-streaming responses in a
|
7
|
+
# formatted markdown style using ANSI escape codes for terminal rendering.
|
8
|
+
# It supports both continuous and single-message display modes, making it
|
9
|
+
# suitable for interactive terminal applications where styled text output is
|
10
|
+
# desired.
|
11
|
+
#
|
12
|
+
# @example Displaying a response as markdown
|
13
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Markdown)
|
4
14
|
class Ollama::Handlers::Markdown
|
5
15
|
include Ollama::Handlers::Concern
|
6
16
|
include Term::ANSIColor
|
7
17
|
|
18
|
+
# The initialize method sets up a new handler instance with the specified
|
19
|
+
# output destination and streaming behavior.
|
20
|
+
#
|
21
|
+
# @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
|
22
|
+
# @param stream [ TrueClass, FalseClass ] whether to enable streaming mode, defaults to true
|
8
23
|
def initialize(output: $stdout, stream: true)
|
9
24
|
super(output:)
|
10
25
|
@stream = stream
|
@@ -12,6 +27,19 @@ class Ollama::Handlers::Markdown
|
|
12
27
|
@content = ''
|
13
28
|
end
|
14
29
|
|
30
|
+
# The call method processes a response by rendering its content as
|
31
|
+
# ANSI-markdown.
|
32
|
+
#
|
33
|
+
# This method handles the display of response content in a formatted markdown
|
34
|
+
# style using ANSI escape codes for terminal rendering. It supports both
|
35
|
+
# streaming and non-streaming modes, allowing for continuous updates or
|
36
|
+
# single-message display.
|
37
|
+
#
|
38
|
+
# @param response [ Ollama::Response ] the response object containing content
|
39
|
+
# to be rendered
|
40
|
+
#
|
41
|
+
# @return [ self ] returns the handler instance itself after processing the
|
42
|
+
# response
|
15
43
|
def call(response)
|
16
44
|
if content = response.response || response.message&.content
|
17
45
|
if @stream
|
data/lib/ollama/handlers/nop.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
|
+
# A handler that performs no operation on responses.
|
2
|
+
#
|
3
|
+
# The NOP (No Operation) handler is used when it's necessary to pass a handler
|
4
|
+
# object to a command without actually processing or displaying the responses.
|
5
|
+
# It implements the required interface for response handling but takes no
|
6
|
+
# action when called, making it useful for scenarios where a handler is
|
7
|
+
# required by the API but no specific processing is desired.
|
8
|
+
#
|
9
|
+
# @example Using the NOP handler
|
10
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &NOP)
|
1
11
|
class Ollama::Handlers::NOP
|
2
12
|
include Ollama::Handlers::Concern
|
3
13
|
|
14
|
+
# The call method processes a response and returns the handler instance.
|
15
|
+
#
|
16
|
+
# This method is intended to be overridden by concrete handler
|
17
|
+
# implementations to define specific behavior for handling API responses. It
|
18
|
+
# serves as the core interface for response processing within the handler
|
19
|
+
# pattern.
|
20
|
+
#
|
21
|
+
# @param response [ Ollama::Response ] the response object to be processed by the handler
|
22
|
+
#
|
23
|
+
# @return [ self ] returns the handler instance itself after processing the response
|
4
24
|
def call(response)
|
5
25
|
self
|
6
26
|
end
|
@@ -1,11 +1,38 @@
|
|
1
|
+
# A handler that prints response content to the output stream.
|
2
|
+
#
|
3
|
+
# The Print handler is designed to output text responses from Ollama API
|
4
|
+
# commands to a specified output stream. It extracts content from responses and
|
5
|
+
# displays it directly, making it useful for interactive terminal applications
|
6
|
+
# where immediate feedback is desired.
|
7
|
+
#
|
8
|
+
# @example Using the Print handler with a chat command
|
9
|
+
# ollama.chat(model: 'llama3.1', messages:, &Print)
|
10
|
+
#
|
11
|
+
# @example Using the Print handler with a generate command
|
12
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Print)
|
1
13
|
class Ollama::Handlers::Print
|
2
14
|
include Ollama::Handlers::Concern
|
3
15
|
|
16
|
+
# The initialize method sets up a new handler instance with the specified
|
17
|
+
# output destination and enables synchronous writing to the output stream.
|
18
|
+
#
|
19
|
+
# @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
|
4
20
|
def initialize(output: $stdout)
|
5
21
|
super
|
6
22
|
@output.sync = true
|
7
23
|
end
|
8
24
|
|
25
|
+
# The call method processes a response by printing its content to the output
|
26
|
+
# stream.
|
27
|
+
#
|
28
|
+
# This method extracts content from the response object and prints it to the
|
29
|
+
# configured output stream. If the response indicates completion, it adds a
|
30
|
+
# newline character after printing. The method returns the handler instance
|
31
|
+
# itself to allow for method chaining.
|
32
|
+
#
|
33
|
+
# @param response [ Ollama::Response ] the response object to be processed
|
34
|
+
#
|
35
|
+
# @return [ self ] returns the handler instance after processing the response
|
9
36
|
def call(response)
|
10
37
|
if content = response.response || response.message&.content
|
11
38
|
@output.print content
|