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.
- 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 +39 -0
- data/README.md +74 -23
- data/Rakefile +4 -3
- data/bin/ollama_cli +41 -9
- 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 +7 -6
- 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 +25 -5
data/lib/ollama/commands/pull.rb
CHANGED
@@ -1,18 +1,89 @@
|
|
1
|
+
# The command method creates a command method for the Ollama client
|
2
|
+
#
|
3
|
+
# Defines a new command method that corresponds to an Ollama API endpoint. The
|
4
|
+
# command method can be invoked with parameters and an optional handler to
|
5
|
+
# process responses. It determines which handler to use based on whether the
|
6
|
+
# command supports streaming and the presence of an explicit handler.
|
7
|
+
#
|
8
|
+
# @param name [ Symbol ] the name of the command to define
|
9
|
+
# @param default_handler [ Class ] the default handler class to use when no explicit handler is provided
|
10
|
+
# @param stream_handler [ Class, nil ] the handler class to use for streaming responses, if applicable
|
11
|
+
#
|
12
|
+
# @note Create Command `name`, if `stream` was true, set `stream_handler`
|
13
|
+
# as default, otherwise `default_handler`.
|
14
|
+
#
|
15
|
+
# @return [ self ] returns the receiver after defining the command method
|
1
16
|
class Ollama::Commands::Pull
|
2
17
|
include Ollama::DTO
|
3
18
|
|
19
|
+
# The path method returns the API endpoint path for pull requests.
|
20
|
+
#
|
21
|
+
# This class method provides the specific URL path used to interact with the
|
22
|
+
# Ollama API's pull endpoint. It is utilized internally by the command
|
23
|
+
# structure to determine the correct API route for downloading models from a
|
24
|
+
# remote registry.
|
25
|
+
#
|
26
|
+
# @return [ String ] the API endpoint path '/api/pull' for pull requests
|
4
27
|
def self.path
|
5
28
|
'/api/pull'
|
6
29
|
end
|
7
30
|
|
31
|
+
# The initialize method sets up a new instance with streaming enabled by default.
|
32
|
+
#
|
33
|
+
# This method is responsible for initializing a new object instance and
|
34
|
+
# configuring it with a default setting that enables streaming behavior.
|
35
|
+
# It is typically called during the object creation process to establish
|
36
|
+
# the initial state of the instance.
|
37
|
+
#
|
38
|
+
# @param model [ String ] the name of the model to be pushed
|
39
|
+
# @param insecure [ TrueClass, FalseClass, nil ] whether to allow insecure
|
40
|
+
# connections, or nil to use default
|
41
|
+
# @param stream [ TrueClass, FalseClass ] whether to enable streaming for
|
42
|
+
# the operation, defaults to true
|
8
43
|
def initialize(model:, insecure: nil, stream: true)
|
9
44
|
@model, @insecure, @stream = model, insecure, stream
|
10
45
|
end
|
11
46
|
|
12
|
-
|
47
|
+
# The model attribute reader returns the model name associated with the object.
|
48
|
+
#
|
49
|
+
# @return [ String ] the name of the model used by the command instance
|
50
|
+
attr_reader :model
|
13
51
|
|
52
|
+
# The insecure attribute reader returns the insecure connection setting
|
53
|
+
# associated with the object.
|
54
|
+
#
|
55
|
+
# @return [ TrueClass, FalseClass, nil ] the insecure flag indicating whether
|
56
|
+
# insecure connections are allowed, or nil if not set
|
57
|
+
attr_reader :insecure
|
58
|
+
|
59
|
+
# The stream attribute reader returns the streaming behavior setting
|
60
|
+
# associated with the object.
|
61
|
+
#
|
62
|
+
# @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
|
63
|
+
# whether streaming is enabled for the command execution
|
64
|
+
attr_reader :stream
|
65
|
+
|
66
|
+
# The client attribute writer allows setting the client instance associated
|
67
|
+
# with the object.
|
68
|
+
#
|
69
|
+
# This method assigns the client that will be used to perform requests and
|
70
|
+
# handle responses for this command. It is typically called internally when a
|
71
|
+
# command is executed through a client instance.
|
72
|
+
#
|
73
|
+
# @attr_writer [ Ollama::Client ] the assigned client instance
|
14
74
|
attr_writer :client
|
15
75
|
|
76
|
+
# The perform method executes a command request using the specified handler.
|
77
|
+
#
|
78
|
+
# This method initiates a request to the Ollama API endpoint associated with
|
79
|
+
# the command, utilizing the client instance to send the request and process
|
80
|
+
# responses through the provided handler. It handles both streaming and
|
81
|
+
# non-streaming scenarios based on the command's configuration.
|
82
|
+
#
|
83
|
+
# @param handler [ Ollama::Handler ] the handler object responsible for processing API
|
84
|
+
# responses
|
85
|
+
#
|
86
|
+
# @return [ self ] returns the current instance after initiating the request
|
16
87
|
def perform(handler)
|
17
88
|
@client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
|
18
89
|
end
|
data/lib/ollama/commands/push.rb
CHANGED
@@ -1,18 +1,82 @@
|
|
1
|
+
# A command class that represents the push API endpoint for Ollama.
|
2
|
+
#
|
3
|
+
# This class is used to interact with the Ollama API's push endpoint, which
|
4
|
+
# uploads a model to a remote registry. It inherits from the base command
|
5
|
+
# structure and provides the necessary functionality to execute push requests
|
6
|
+
# for model deployment.
|
7
|
+
#
|
8
|
+
# @example Pushing a model to a remote registry
|
9
|
+
# push = ollama.push(model: 'user/llama3.1')
|
1
10
|
class Ollama::Commands::Push
|
2
11
|
include Ollama::DTO
|
3
12
|
|
13
|
+
# The path method returns the API endpoint path for push requests.
|
14
|
+
#
|
15
|
+
# This class method provides the specific URL path used to interact with the
|
16
|
+
# Ollama API's push endpoint. It is utilized internally by the command
|
17
|
+
# structure to determine the correct API route for uploading models to a
|
18
|
+
# remote registry.
|
19
|
+
#
|
20
|
+
# @return [ String ] the API endpoint path '/api/push' for push requests
|
4
21
|
def self.path
|
5
22
|
'/api/push'
|
6
23
|
end
|
7
24
|
|
25
|
+
# The initialize method sets up a new instance with streaming enabled by default.
|
26
|
+
#
|
27
|
+
# This method is responsible for initializing a new object instance and
|
28
|
+
# configuring it with a default setting that enables streaming behavior.
|
29
|
+
# It is typically called during the object creation process to establish
|
30
|
+
# the initial state of the instance.
|
31
|
+
#
|
32
|
+
# @param model [ String ] the name of the model to be pushed
|
33
|
+
# @param insecure [ TrueClass, FalseClass, nil ] whether to allow insecure
|
34
|
+
# connections, or nil to use default
|
35
|
+
# @param stream [ TrueClass, FalseClass ] whether to enable streaming for
|
36
|
+
# the operation, defaults to true
|
8
37
|
def initialize(model:, insecure: nil, stream: true)
|
9
38
|
@model, @insecure, @stream = model, insecure, stream
|
10
39
|
end
|
11
40
|
|
12
|
-
|
41
|
+
# The model attribute reader returns the model name associated with the object.
|
42
|
+
#
|
43
|
+
# @return [ String ] the name of the model to be pushed
|
44
|
+
attr_reader :model
|
13
45
|
|
46
|
+
# The insecure attribute reader returns the insecure connection setting
|
47
|
+
# associated with the object.
|
48
|
+
#
|
49
|
+
# @return [ TrueClass, FalseClass, nil ] the insecure flag indicating whether
|
50
|
+
# insecure connections are allowed, or nil if not set
|
51
|
+
attr_reader :insecure
|
52
|
+
|
53
|
+
# The stream attribute reader returns the streaming behavior setting
|
54
|
+
# associated with the object.
|
55
|
+
#
|
56
|
+
# @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
|
57
|
+
# whether streaming is enabled for the command execution
|
58
|
+
attr_reader :stream
|
59
|
+
|
60
|
+
# The client attribute writer allows setting the client instance associated
|
61
|
+
# with the object.
|
62
|
+
#
|
63
|
+
# This method assigns the client that will be used to perform requests and
|
64
|
+
# handle responses for this command. It is typically called internally when a
|
65
|
+
# command is executed through a client instance.
|
66
|
+
#
|
67
|
+
# @attr_writer [ Ollama::Client ] the assigned client instance
|
14
68
|
attr_writer :client
|
15
69
|
|
70
|
+
# The perform method executes a command request using the specified handler.
|
71
|
+
#
|
72
|
+
# This method initiates a request to the Ollama API endpoint associated with
|
73
|
+
# the command, utilizing the client instance to send the request and process
|
74
|
+
# responses through the provided handler. It handles both streaming and
|
75
|
+
# non-streaming scenarios based on the command's configuration.
|
76
|
+
#
|
77
|
+
# @param handler [ Ollama::Handler ] the handler object responsible for processing API responses
|
78
|
+
#
|
79
|
+
# @return [ self ] returns the current instance after initiating the request
|
16
80
|
def perform(handler)
|
17
81
|
@client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
|
18
82
|
end
|
data/lib/ollama/commands/show.rb
CHANGED
@@ -1,19 +1,82 @@
|
|
1
|
+
# A command class that represents the show API endpoint for Ollama.
|
2
|
+
#
|
3
|
+
# This class is used to interact with the Ollama API's show endpoint, which
|
4
|
+
# retrieves detailed information about a specific model. It inherits from the
|
5
|
+
# base command structure and provides the necessary functionality to execute
|
6
|
+
# show requests for model details.
|
7
|
+
#
|
8
|
+
# @example Retrieving detailed information about a model
|
9
|
+
# show = ollama.show(model: 'llama3.1')
|
10
|
+
# show.model_info # => hash containing model details
|
1
11
|
class Ollama::Commands::Show
|
2
12
|
include Ollama::DTO
|
3
13
|
|
14
|
+
# The path method returns the API endpoint path for show requests.
|
15
|
+
#
|
16
|
+
# This class method provides the specific URL path used to interact with the
|
17
|
+
# Ollama API's show endpoint. It is utilized internally by the command
|
18
|
+
# structure to determine the correct API route for retrieving detailed
|
19
|
+
# information about a specific model.
|
20
|
+
#
|
21
|
+
# @return [ String ] the API endpoint path '/api/show' for show requests
|
4
22
|
def self.path
|
5
23
|
'/api/show'
|
6
24
|
end
|
7
25
|
|
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
|
+
#
|
33
|
+
# @param model [ String ] the name of the model to be used @param verbose [
|
34
|
+
# TrueClass, FalseClass, nil ] whether to enable verbose output, or nil to
|
35
|
+
# use default
|
8
36
|
def initialize(model:, verbose: nil)
|
9
37
|
@model, @verbose = model, verbose
|
10
38
|
@stream = false
|
11
39
|
end
|
12
40
|
|
13
|
-
|
41
|
+
# The model attribute reader returns the model name associated with the object.
|
42
|
+
#
|
43
|
+
# @return [ String ] the name of the model used by the command instance
|
44
|
+
attr_reader :model
|
14
45
|
|
46
|
+
# The verbose attribute reader returns the verbose setting associated with
|
47
|
+
# the object.
|
48
|
+
#
|
49
|
+
# @return [ TrueClass, FalseClass, nil ] the verbose flag indicating whether
|
50
|
+
# verbose output is enabled, or nil if not set
|
51
|
+
attr_reader :verbose
|
52
|
+
|
53
|
+
# The stream attribute reader returns the streaming behavior setting
|
54
|
+
# associated with the object.
|
55
|
+
#
|
56
|
+
# @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
|
57
|
+
# whether streaming is enabled for the command execution
|
58
|
+
attr_reader :stream
|
59
|
+
|
60
|
+
# The client attribute writer allows setting the client instance associated
|
61
|
+
# with the object.
|
62
|
+
#
|
63
|
+
# This method assigns the client that will be used to perform requests and
|
64
|
+
# handle responses for this command. It is typically called internally when a
|
65
|
+
# command is executed through a client instance.
|
66
|
+
#
|
67
|
+
# @attr_writer [ Ollama::Client ] the assigned client instance
|
15
68
|
attr_writer :client
|
16
69
|
|
70
|
+
# The perform method executes a command request using the specified handler.
|
71
|
+
#
|
72
|
+
# This method initiates a request to the Ollama API endpoint associated with
|
73
|
+
# the command, utilizing the client instance to send the request and process
|
74
|
+
# responses through the provided handler. It handles both streaming and
|
75
|
+
# non-streaming scenarios based on the command's configuration.
|
76
|
+
#
|
77
|
+
# @param handler [ Ollama::Handler ] the handler object responsible for processing API responses
|
78
|
+
#
|
79
|
+
# @return [ self ] returns the current instance after initiating the request
|
17
80
|
def perform(handler)
|
18
81
|
@client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
|
19
82
|
end
|
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
|