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
@@ -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
|
@@ -1,10 +1,23 @@
|
|
1
1
|
require 'infobar'
|
2
2
|
require 'tins/unit'
|
3
3
|
|
4
|
+
# A handler that displays progress information for streaming operations.
|
5
|
+
#
|
6
|
+
# This class is designed to provide visual feedback during long-running
|
7
|
+
# operations such as model creation, pulling, or pushing. It uses a progress
|
8
|
+
# bar to show the current status and estimated time remaining, making it easier
|
9
|
+
# to monitor the progress of these operations in terminal environments.
|
10
|
+
#
|
11
|
+
# @example Displaying progress for a model creation operation
|
12
|
+
# ollama.create(model: 'my-model', stream: true, &Progress)
|
4
13
|
class Ollama::Handlers::Progress
|
5
14
|
include Ollama::Handlers::Concern
|
6
15
|
include Term::ANSIColor
|
7
16
|
|
17
|
+
# The initialize method sets up a new handler instance with the specified
|
18
|
+
# output destination and initializes internal state for progress tracking.
|
19
|
+
#
|
20
|
+
# @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
|
8
21
|
def initialize(output: $stdout)
|
9
22
|
super
|
10
23
|
@current = 0
|
@@ -12,6 +25,20 @@ class Ollama::Handlers::Progress
|
|
12
25
|
@last_status = nil
|
13
26
|
end
|
14
27
|
|
28
|
+
# The call method processes a response by updating progress information and
|
29
|
+
# displaying status updates.
|
30
|
+
#
|
31
|
+
# This method handles the display of progress information for streaming
|
32
|
+
# operations by updating the progress bar with current completion status,
|
33
|
+
# handling status changes, and displaying any error messages that occur
|
34
|
+
# during the operation. It manages internal state to track progress and
|
35
|
+
# ensures proper formatting of output.
|
36
|
+
#
|
37
|
+
# @param response [ Ollama::Response ] the response object containing
|
38
|
+
# progress information
|
39
|
+
#
|
40
|
+
# @return [ self ] returns the handler instance itself after processing the
|
41
|
+
# response
|
15
42
|
def call(response)
|
16
43
|
infobar.display.output = @output
|
17
44
|
if status = response.status
|
@@ -40,6 +67,17 @@ class Ollama::Handlers::Progress
|
|
40
67
|
|
41
68
|
private
|
42
69
|
|
70
|
+
# The message method formats progress information into a descriptive string.
|
71
|
+
#
|
72
|
+
# This method takes current and total values and creates a formatted progress
|
73
|
+
# message that includes the current value, total value, time elapsed,
|
74
|
+
# estimated time remaining, and the current rate of progress.
|
75
|
+
#
|
76
|
+
# @param current [ Integer ] the current progress value
|
77
|
+
# @param total [ Integer ] the total progress value
|
78
|
+
#
|
79
|
+
# @return [ String ] a formatted progress message containing current status,
|
80
|
+
# time information, and estimated completion details
|
43
81
|
def message(current, total)
|
44
82
|
progress = '%s/%s' % [ current, total ].map {
|
45
83
|
Tins::Unit.format(_1, format: '%.2f %U')
|
data/lib/ollama/handlers/say.rb
CHANGED
@@ -1,8 +1,29 @@
|
|
1
1
|
require 'shellwords'
|
2
2
|
|
3
|
+
# A handler that uses the system's say command to speak response content.
|
4
|
+
#
|
5
|
+
# The Say handler is designed to convert text responses from Ollama API
|
6
|
+
# commands into audible speech using the operating system's native
|
7
|
+
# text-to-speech capabilities. It supports customization of voice and
|
8
|
+
# interactive modes, making it suitable for applications where audio feedback
|
9
|
+
# is preferred over visual display.
|
10
|
+
#
|
11
|
+
# @example Using the Say handler with a custom voice
|
12
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Say.new(voice: 'Alex'))
|
13
|
+
#
|
14
|
+
# @example Using the Say handler in interactive mode
|
15
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Say.new(interactive: true))
|
3
16
|
class Ollama::Handlers::Say
|
4
17
|
include Ollama::Handlers::Concern
|
5
18
|
|
19
|
+
# The initialize method sets up a new handler instance with the specified
|
20
|
+
# output destination and configures voice and interactive settings for
|
21
|
+
# text-to-speech functionality.
|
22
|
+
#
|
23
|
+
# @param output [ IO, nil ] the output stream to be used for handling responses, defaults to nil
|
24
|
+
# @param voice [ String ] the voice to be used for speech synthesis, defaults to 'Samantha'
|
25
|
+
# @param interactive [ TrueClass, FalseClass, String, nil ] the interactive
|
26
|
+
# mode setting for speech synthesis, defaults to nil
|
6
27
|
def initialize(output: nil, voice: 'Samantha', interactive: nil)
|
7
28
|
@voice = voice
|
8
29
|
@interactive = interactive
|
@@ -13,10 +34,28 @@ class Ollama::Handlers::Say
|
|
13
34
|
end
|
14
35
|
end
|
15
36
|
|
37
|
+
# The voice attribute reader returns the voice associated with the object.
|
38
|
+
#
|
39
|
+
# @return [ String ] the voice value stored in the instance variable
|
16
40
|
attr_reader :voice
|
17
41
|
|
42
|
+
# The interactive attribute reader returns the interactive mode setting
|
43
|
+
# associated with the object.
|
44
|
+
#
|
45
|
+
# @return [ TrueClass, FalseClass, String, nil ] the interactive mode value
|
46
|
+
# stored in the instance variable
|
18
47
|
attr_reader :interactive
|
19
48
|
|
49
|
+
# The call method processes a response by printing its content to the output stream.
|
50
|
+
#
|
51
|
+
# This method handles the display of response content by extracting text from the response object
|
52
|
+
# and writing it to the configured output stream. It manages the output stream state, reopening it
|
53
|
+
# if necessary when it has been closed, and ensures proper handling of streaming responses by
|
54
|
+
# closing the output stream when the response indicates completion.
|
55
|
+
#
|
56
|
+
# @param response [ Ollama::Response ] the response object containing content to be printed
|
57
|
+
#
|
58
|
+
# @return [ self ] returns the handler instance itself after processing the response
|
20
59
|
def call(response)
|
21
60
|
if @output.closed?
|
22
61
|
wait_output_pid
|
@@ -32,18 +71,45 @@ class Ollama::Handlers::Say
|
|
32
71
|
|
33
72
|
private
|
34
73
|
|
74
|
+
# The open_output method creates a new IO object for handling speech output.
|
75
|
+
#
|
76
|
+
# This method initializes a pipe to the system's say command, configuring it
|
77
|
+
# with the specified voice and interactive settings. It returns an IO object
|
78
|
+
# that can be used to write text content which will be converted to speech by
|
79
|
+
# the operating system.
|
80
|
+
#
|
81
|
+
# @return [ IO ] an IO object connected to the say command for text-to-speech conversion
|
35
82
|
def open_output
|
36
83
|
io = IO.popen(Shellwords.join(command(voice:, interactive:)), 'w')
|
37
84
|
io.sync = true
|
38
85
|
io
|
39
86
|
end
|
40
87
|
|
88
|
+
# The wait_output_pid method waits for the output process to complete.
|
89
|
+
#
|
90
|
+
# This method checks if there is an active output process ID and waits for it
|
91
|
+
# to finish execution. It uses non-blocking wait to avoid hanging the main
|
92
|
+
# thread.
|
93
|
+
# If the process has already terminated, it handles the Errno::ECHILD
|
94
|
+
# exception gracefully without raising an error.
|
41
95
|
def wait_output_pid
|
42
96
|
@output_pid or return
|
43
97
|
Process.wait(@output_pid, Process::WNOHANG | Process::WUNTRACED)
|
44
98
|
rescue Errno::ECHILD
|
45
99
|
end
|
46
100
|
|
101
|
+
# The command method constructs a say command array with specified voice and
|
102
|
+
# interactive options.
|
103
|
+
#
|
104
|
+
# This method builds an array representing a system command for the 'say'
|
105
|
+
# utility, incorporating the provided voice and interactive settings to
|
106
|
+
# configure text-to-speech behavior.
|
107
|
+
#
|
108
|
+
# @param voice [ String ] the voice to be used for speech synthesis
|
109
|
+
# @param interactive [ TrueClass, FalseClass, String, nil ] the interactive
|
110
|
+
# mode setting for speech synthesis
|
111
|
+
#
|
112
|
+
# @return [ Array<String> ] an array containing the command and its arguments
|
47
113
|
def command(voice:, interactive:)
|
48
114
|
command = [ 'say' ]
|
49
115
|
voice and command.concat([ '-v', voice ])
|
@@ -1,16 +1,51 @@
|
|
1
|
+
# A handler that collects responses and returns either a single response or an
|
2
|
+
# array of responses.
|
3
|
+
#
|
4
|
+
# The Single handler is designed to accumulate responses during command
|
5
|
+
# execution and provides a result that is either the single response when only
|
6
|
+
# one is present, or the complete array of responses when multiple are
|
7
|
+
# collected.
|
8
|
+
#
|
9
|
+
# @example Using the Single handler to collect a single response
|
10
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Single)
|
1
11
|
class Ollama::Handlers::Single
|
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 operations.
|
40
|
+
#
|
41
|
+
# This method provides access to the accumulated results after a command has
|
42
|
+
# been executed with a handler that collects responses. It returns either
|
43
|
+
# a single response when only one result is present, or the complete array
|
44
|
+
# of responses when multiple results are collected.
|
45
|
+
#
|
46
|
+
# @return [ Ollama::Response, Array<Ollama::Response>, nil ] the collected response data,
|
47
|
+
# which may be a single response, an array of responses, or nil
|
48
|
+
# if no responses were collected
|
14
49
|
def result
|
15
50
|
@array.size <= 1 ? @array.first : @array
|
16
51
|
end
|
data/lib/ollama/handlers.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# A module that groups together various handler classes used to process
|
2
|
+
# responses from the Ollama API.
|
3
|
+
#
|
4
|
+
# Handlers are responsible for defining how API responses should be processed, displayed, or stored.
|
5
|
+
# They implement a common interface that allows them to be passed as arguments to client commands,
|
6
|
+
# providing flexibility in how response data is handled.
|
7
|
+
#
|
8
|
+
# @example Using a handler with a client command
|
9
|
+
# ollama.generate(model: 'llama3.1', prompt: 'Hello World', &Print)
|
1
10
|
module Ollama::Handlers
|
2
11
|
end
|
3
12
|
|
data/lib/ollama/image.rb
CHANGED
@@ -1,30 +1,87 @@
|
|
1
1
|
require 'base64'
|
2
2
|
|
3
|
+
# Ollama::Image
|
4
|
+
#
|
5
|
+
# Represents an image object that can be used in messages sent to the Ollama API.
|
6
|
+
# The Image class provides methods to create image objects from various sources
|
7
|
+
# including base64 encoded strings, file paths, IO objects, and raw string data.
|
8
|
+
#
|
9
|
+
# @example Creating an image from a file path
|
10
|
+
# image = Ollama::Image.for_filename('path/to/image.jpg')
|
11
|
+
#
|
12
|
+
# @example Creating an image from a base64 string
|
13
|
+
# image = Ollama::Image.for_base64('base64encodedstring', path: 'image.jpg')
|
14
|
+
#
|
15
|
+
# @example Creating an image from a string
|
16
|
+
# image = Ollama::Image.for_string('raw image data')
|
17
|
+
#
|
18
|
+
# @example Creating an image from an IO object
|
19
|
+
# File.open('path/to/image.jpg', 'rb') do |io|
|
20
|
+
# image = Ollama::Image.for_io(io, path: 'image.jpg')
|
21
|
+
# end
|
3
22
|
class Ollama::Image
|
23
|
+
# Initializes a new Image object with the provided data.
|
24
|
+
#
|
25
|
+
# @param data [ String ] the image data to store in the object
|
4
26
|
def initialize(data)
|
5
27
|
@data = data
|
6
28
|
end
|
7
29
|
|
30
|
+
# The path attribute stores the file path associated with the image object.
|
31
|
+
#
|
32
|
+
# @return [ String, nil ] the path to the image file, or nil if not set
|
8
33
|
attr_accessor :path
|
9
34
|
|
35
|
+
# The data attribute reader returns the image data stored in the object.
|
36
|
+
#
|
37
|
+
# @return [ String ] the raw image data contained within the image object
|
10
38
|
attr_reader :data
|
11
39
|
|
12
40
|
class << self
|
41
|
+
# Creates a new Image object from base64 encoded data.
|
42
|
+
#
|
43
|
+
# @param data [ String ] the base64 encoded image data
|
44
|
+
# @param path [ String, nil ] the optional file path associated with the image
|
45
|
+
#
|
46
|
+
# @return [ Ollama::Image ] a new Image instance initialized with the
|
47
|
+
# provided data and path
|
13
48
|
def for_base64(data, path: nil)
|
14
49
|
obj = new(data)
|
15
50
|
obj.path = path
|
16
51
|
obj
|
17
52
|
end
|
18
53
|
|
54
|
+
# Creates a new Image object from a string by encoding it to base64.
|
55
|
+
#
|
56
|
+
# @param string [ String ] the raw string data to be converted into an image
|
57
|
+
# @param path [ String, nil ] the optional file path associated with the image
|
58
|
+
#
|
59
|
+
# @return [ Ollama::Image ] a new Image instance initialized with the
|
60
|
+
# encoded string data and optional path
|
19
61
|
def for_string(string, path: nil)
|
20
62
|
for_base64(Base64.encode64(string), path:)
|
21
63
|
end
|
22
64
|
|
65
|
+
# Creates a new Image object from an IO object by reading its contents and
|
66
|
+
# optionally setting a path.
|
67
|
+
#
|
68
|
+
# @param io [ IO ] the IO object to read image data from
|
69
|
+
# @param path [ String, nil ] the optional file path associated with the image
|
70
|
+
#
|
71
|
+
# @return [ Ollama::Image ] a new Image instance initialized with the IO
|
72
|
+
# object's data and optional path
|
23
73
|
def for_io(io, path: nil)
|
24
74
|
path ||= io.path
|
25
75
|
for_string(io.read, path:)
|
26
76
|
end
|
27
77
|
|
78
|
+
# Creates a new Image object from a file path by opening the file in binary
|
79
|
+
# mode and passing it to the for_io method.
|
80
|
+
#
|
81
|
+
# @param path [ String ] the file system path to the image file
|
82
|
+
#
|
83
|
+
# @return [ Ollama::Image ] a new Image instance initialized with the
|
84
|
+
# contents of the file at the specified path
|
28
85
|
def for_filename(path)
|
29
86
|
File.open(path, 'rb') { |io| for_io(io, path:) }
|
30
87
|
end
|
@@ -32,10 +89,20 @@ class Ollama::Image
|
|
32
89
|
private :new
|
33
90
|
end
|
34
91
|
|
92
|
+
# The == method compares two Image objects for equality based on their data
|
93
|
+
# contents.
|
94
|
+
#
|
95
|
+
# @param other [ Ollama::Image ] the other Image object to compare against
|
96
|
+
#
|
97
|
+
# @return [ TrueClass, FalseClass ] true if both Image objects have identical
|
98
|
+
# data, false otherwise
|
35
99
|
def ==(other)
|
36
100
|
@data == other.data
|
37
101
|
end
|
38
102
|
|
103
|
+
# The to_s method returns the raw image data stored in the Image object.
|
104
|
+
#
|
105
|
+
# @return [ String ] the raw image data contained within the image object
|
39
106
|
def to_s
|
40
107
|
@data
|
41
108
|
end
|
data/lib/ollama/json_loader.rb
CHANGED
@@ -1,4 +1,21 @@
|
|
1
|
+
# A module that provides functionality for loading configuration data from JSON
|
2
|
+
# files.
|
3
|
+
#
|
4
|
+
# This module extends classes with a method to load configuration attributes
|
5
|
+
# from a JSON file, making it easy to initialize objects with settings stored
|
6
|
+
# in external files.
|
7
|
+
|
8
|
+
# @example Loading configuration from a JSON file
|
9
|
+
# config = MyConfigClass.load_from_json('path/to/config.json')
|
1
10
|
module Ollama::JSONLoader
|
11
|
+
# The load_from_json method loads configuration data from a JSON file.
|
12
|
+
#
|
13
|
+
# This method reads the specified JSON file and uses its contents to
|
14
|
+
# initialize a new instance of the class by passing the parsed data
|
15
|
+
# as keyword arguments to the constructor.
|
16
|
+
#
|
17
|
+
# @param path [ String ] the filesystem path to the JSON configuration file
|
18
|
+
# @return [ self ] a new instance of the class initialized with the JSON data
|
2
19
|
def load_from_json(path)
|
3
20
|
json = File.read(path)
|
4
21
|
new(**config_hash = JSON.parse(json, symbolize_names: true))
|
data/lib/ollama/message.rb
CHANGED
@@ -1,8 +1,53 @@
|
|
1
|
+
# Ollama::Message
|
2
|
+
#
|
3
|
+
# Represents a message object used in communication with the Ollama API.
|
4
|
+
# This class encapsulates the essential components of a message, including
|
5
|
+
# the role of the sender, the content of the message, optional thinking
|
6
|
+
# content, associated images, and tool calls.
|
7
|
+
#
|
8
|
+
# @example Creating a basic message
|
9
|
+
# message = Ollama::Message.new(
|
10
|
+
# role: 'user',
|
11
|
+
# content: 'Hello, world!'
|
12
|
+
# )
|
13
|
+
#
|
14
|
+
# @example Creating a message with additional attributes
|
15
|
+
# image = Ollama::Image.for_filename('path/to/image.jpg')
|
16
|
+
# message = Ollama::Message.new(
|
17
|
+
# role: 'user',
|
18
|
+
# content: 'Look at this image',
|
19
|
+
# images: [image]
|
20
|
+
# )
|
1
21
|
class Ollama::Message
|
2
22
|
include Ollama::DTO
|
3
23
|
|
4
|
-
|
24
|
+
# The role attribute reader returns the role associated with the message.
|
25
|
+
#
|
26
|
+
# @return [ String ] the role of the message sender, such as 'user' or 'assistant'
|
27
|
+
attr_reader :role
|
5
28
|
|
29
|
+
# The content attribute reader returns the textual content of the message.
|
30
|
+
#
|
31
|
+
# @return [ String ] the content of the message
|
32
|
+
attr_reader :content
|
33
|
+
|
34
|
+
# The thinking attribute reader returns the thinking content associated with the message.
|
35
|
+
#
|
36
|
+
# @return [ String, nil ] the thinking content of the message, or nil if not set
|
37
|
+
attr_reader :thinking
|
38
|
+
|
39
|
+
# The images attribute reader returns the image objects associated with the message.
|
40
|
+
#
|
41
|
+
# @return [ Array<Ollama::Image>, nil ] an array of image objects, or nil if no images are associated with the message
|
42
|
+
attr_reader :images
|
43
|
+
|
44
|
+
# The initialize method sets up a new Message instance with the specified attributes.
|
45
|
+
#
|
46
|
+
# @param role [ String ] the role of the message sender, such as 'user' or 'assistant'
|
47
|
+
# @param content [ String ] the textual content of the message
|
48
|
+
# @param thinking [ String, nil ] optional thinking content for the message
|
49
|
+
# @param images [ Ollama::Image, Array<Ollama::Image>, nil ] optional image objects associated with the message
|
50
|
+
# @param tool_calls [ Hash, Array<Hash>, nil ] optional tool calls made in the message
|
6
51
|
def initialize(role:, content:, thinking: nil, images: nil, tool_calls: nil, **)
|
7
52
|
@role, @content, @thinking, @images, @tool_calls =
|
8
53
|
role, content, thinking, (Array(images) if images),
|