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
@@ -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),
|
data/lib/ollama/options.rb
CHANGED
@@ -1,7 +1,21 @@
|
|
1
1
|
require 'ollama/json_loader'
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# A class that encapsulates configuration options for Ollama models.
|
4
|
+
#
|
5
|
+
# This class provides a structured way to define and manage various parameters
|
6
|
+
# that can be passed to Ollama models during generation or chat operations. It
|
7
|
+
# includes type validation to ensure that option values conform to expected
|
8
|
+
# data types, making it easier to work with model configurations
|
9
|
+
# programmatically.
|
10
|
+
#
|
11
|
+
# @example Creating an Options object with specific settings
|
12
|
+
# options = Ollama::Options.new(
|
13
|
+
# temperature: 0.7,
|
14
|
+
# num_ctx: 8192,
|
15
|
+
# top_p: 0.9
|
16
|
+
# )
|
17
|
+
#
|
18
|
+
# [Options are explained in the parameters for the modelfile.](https://github.com/ollama/ollama/blob/main/docs/modelfile.md#parameter)
|
5
19
|
class Ollama::Options
|
6
20
|
include Ollama::DTO
|
7
21
|
extend Ollama::JSONLoader
|
@@ -69,6 +83,17 @@ class Ollama::Options
|
|
69
83
|
end
|
70
84
|
}
|
71
85
|
|
86
|
+
# The [] method creates a new instance of the class using a hash of attributes.
|
87
|
+
#
|
88
|
+
# This class method provides a convenient way to instantiate an object by
|
89
|
+
# passing a hash containing the desired attribute values. It converts the
|
90
|
+
# hash keys to symbols and forwards them as keyword arguments to the
|
91
|
+
# constructor.
|
92
|
+
#
|
93
|
+
# @param value [ Hash ] a hash containing the attribute names and their values
|
94
|
+
#
|
95
|
+
# @return [ self ] a new instance of the class initialized with the provided
|
96
|
+
# attributes
|
72
97
|
def self.[](value)
|
73
98
|
new(**value.to_h)
|
74
99
|
end
|
data/lib/ollama/response.rb
CHANGED
@@ -1,4 +1,21 @@
|
|
1
|
+
# A subclass of JSON::GenericObject that represents responses from the Ollama API.
|
2
|
+
#
|
3
|
+
# This class serves as a specialized response object that extends
|
4
|
+
# JSON::GenericObject to provide structured access to API response data. It
|
5
|
+
# maintains the ability to convert to JSON format while preserving the response
|
6
|
+
# data in a hash-like structure.
|
7
|
+
#
|
8
|
+
# @example Accessing response data
|
9
|
+
# response = Ollama::Response.new(key: 'value')
|
10
|
+
# response[:key] # => 'value'
|
1
11
|
class Ollama::Response < JSON::GenericObject
|
12
|
+
# The as_json method converts the object's attributes into a JSON-compatible hash.
|
13
|
+
#
|
14
|
+
# This method gathers all defined attributes of the object and constructs a
|
15
|
+
# hash representation, excluding any nil values or empty collections.
|
16
|
+
#
|
17
|
+
# @note This removes "json_class" attribute from hash for responses.
|
18
|
+
# @return [ Hash ] a hash containing the object's non-nil and non-empty attributes
|
2
19
|
def as_json(*)
|
3
20
|
to_hash
|
4
21
|
end
|
@@ -1,8 +1,48 @@
|
|
1
|
+
# A class that represents a single property within the parameters specification
|
2
|
+
# for a tool function.
|
3
|
+
#
|
4
|
+
# This class encapsulates the definition of an individual parameter, including
|
5
|
+
# its data type, descriptive text, and optional enumeration values that define
|
6
|
+
# valid inputs.
|
7
|
+
#
|
8
|
+
# @example Creating a property with type and description
|
9
|
+
# property = Ollama::Tool::Function::Parameters::Property.new(
|
10
|
+
# type: 'string',
|
11
|
+
# description: 'The location to get weather for'
|
12
|
+
# )
|
13
|
+
#
|
14
|
+
# @example Creating a property with an enumeration of valid values
|
15
|
+
# property = Ollama::Tool::Function::Parameters::Property.new(
|
16
|
+
# type: 'string',
|
17
|
+
# description: 'Temperature unit',
|
18
|
+
# enum: %w[celsius fahrenheit]
|
19
|
+
# )
|
1
20
|
class Ollama::Tool::Function::Parameters::Property
|
2
21
|
include Ollama::DTO
|
3
22
|
|
4
|
-
|
23
|
+
# The type attribute reader returns the type associated with the object.
|
24
|
+
#
|
25
|
+
# @return [ String ] the type value stored in the instance variable
|
26
|
+
attr_reader :type
|
5
27
|
|
28
|
+
# The description attribute reader returns the description associated with
|
29
|
+
# the object.
|
30
|
+
#
|
31
|
+
# @return [ String ] the description value stored in the instance variable
|
32
|
+
attr_reader :description
|
33
|
+
|
34
|
+
# The enum attribute reader returns the enumeration values associated with the object.
|
35
|
+
#
|
36
|
+
# @return [ Array<String>, nil ] an array of valid string values that the
|
37
|
+
# property can take, or nil if not set
|
38
|
+
attr_reader :enum
|
39
|
+
|
40
|
+
# The initialize method sets up a new Property instance with the specified
|
41
|
+
# attributes.
|
42
|
+
#
|
43
|
+
# @param type [ String ] the data type of the property
|
44
|
+
# @param description [ String ] a detailed explanation of what the property represents
|
45
|
+
# @param enum [ Array<String>, nil ] an optional array of valid values that the property can take
|
6
46
|
def initialize(type:, description:, enum: nil)
|
7
47
|
@type, @description, @enum = type, description, Array(enum)
|
8
48
|
end
|
@@ -1,8 +1,47 @@
|
|
1
|
+
# A class that represents the parameters specification for a tool function in
|
2
|
+
# Ollama API interactions.
|
3
|
+
#
|
4
|
+
# This class encapsulates the structure required for defining the parameters
|
5
|
+
# that a function tool accepts. It includes the type of parameter object, the
|
6
|
+
# properties of each parameter, and which parameters are required.
|
7
|
+
#
|
8
|
+
# @example Creating a parameters specification for a tool function
|
9
|
+
# property = Ollama::Tool::Function::Parameters::Property.new(
|
10
|
+
# type: 'string',
|
11
|
+
# description: 'The location to get weather for'
|
12
|
+
# )
|
13
|
+
# parameters = Ollama::Tool::Function::Parameters.new(
|
14
|
+
# type: 'object',
|
15
|
+
# properties: { location: property },
|
16
|
+
# required: %w[location]
|
17
|
+
# )
|
1
18
|
class Ollama::Tool::Function::Parameters
|
2
19
|
include Ollama::DTO
|
3
20
|
|
4
|
-
|
21
|
+
# The type attribute reader returns the type associated with the object.
|
22
|
+
#
|
23
|
+
# @return [ String ] the type value stored in the instance variable
|
24
|
+
attr_reader :type
|
5
25
|
|
26
|
+
# The properties attribute reader returns the properties associated with the
|
27
|
+
# object.
|
28
|
+
#
|
29
|
+
# @return [ Hash ] the properties hash, or nil if not set
|
30
|
+
attr_reader :properties
|
31
|
+
|
32
|
+
# The required attribute reader returns the required parameter values
|
33
|
+
# associated with the object.
|
34
|
+
#
|
35
|
+
# @return [ Array<String>, nil ] an array of required parameter names, or nil
|
36
|
+
# if not set
|
37
|
+
attr_reader :required
|
38
|
+
|
39
|
+
# The initialize method sets up a new Parameters instance with the specified
|
40
|
+
# attributes.
|
41
|
+
#
|
42
|
+
# @param type [ String ] the type of parameter object
|
43
|
+
# @param properties [ Hash ] the properties of each parameter
|
44
|
+
# @param required [ Array<String> ] the names of required parameters
|
6
45
|
def initialize(type:, properties:, required:)
|
7
46
|
@type, @properties, @required =
|
8
47
|
type, Hash(properties).transform_values(&:to_hash), Array(required)
|
data/lib/ollama/tool/function.rb
CHANGED
@@ -1,8 +1,51 @@
|
|
1
|
+
# A class that represents a function definition for tool usage in Ollama API
|
2
|
+
# interactions.
|
3
|
+
#
|
4
|
+
# This class encapsulates the structure required for defining functions that
|
5
|
+
# can be passed to models to enable function calling capabilities. It includes
|
6
|
+
# the function's name, description, parameters specification, and list of
|
7
|
+
# required parameters.
|
8
|
+
#
|
9
|
+
# @example Creating a function definition for a tool
|
10
|
+
# function = Ollama::Tool::Function.new(
|
11
|
+
# name: 'get_current_weather',
|
12
|
+
# description: 'Get the current weather for a location',
|
13
|
+
# parameters: parameters,
|
14
|
+
# required: %w[location]
|
15
|
+
# )
|
1
16
|
class Ollama::Tool::Function
|
2
17
|
include Ollama::DTO
|
3
18
|
|
4
|
-
|
19
|
+
# The name attribute reader returns the name associated with the object.
|
20
|
+
#
|
21
|
+
# @return [ String ] the name value stored in the instance variable
|
22
|
+
attr_reader :name
|
5
23
|
|
24
|
+
# The description attribute reader returns the description associated with
|
25
|
+
# the object.
|
26
|
+
#
|
27
|
+
# @return [ String ] the description value stored in the instance variable
|
28
|
+
attr_reader :description
|
29
|
+
|
30
|
+
# The parameters attribute reader returns the parameters associated with the
|
31
|
+
# object.
|
32
|
+
#
|
33
|
+
# @return [ Hash, nil ] the parameters hash, or nil if not set
|
34
|
+
attr_reader :parameters
|
35
|
+
|
36
|
+
# The required attribute reader returns the required parameter values
|
37
|
+
# associated with the object.
|
38
|
+
#
|
39
|
+
# @return [ Array<String>, nil ] an array of required parameter names, or nil if not set
|
40
|
+
attr_reader :required
|
41
|
+
|
42
|
+
# The initialize method sets up a new Tool::Function instance with the
|
43
|
+
# specified attributes.
|
44
|
+
#
|
45
|
+
# @param name [ String ] the name of the function
|
46
|
+
# @param description [ String ] a brief description of what the function does
|
47
|
+
# @param parameters [ Hash, nil ] optional parameters specification for the function
|
48
|
+
# @param required [ Array<String>, nil ] optional array of required parameter names
|
6
49
|
def initialize(name:, description:, parameters: nil, required: nil)
|
7
50
|
@name, @description, @parameters, @required =
|
8
51
|
name, description, (Hash(parameters) if parameters),
|
data/lib/ollama/tool.rb
CHANGED
@@ -1,8 +1,44 @@
|
|
1
|
+
# Represents a tool definition used in interactions with the Ollama API.
|
2
|
+
# This class encapsulates the structure required for defining tools that can be
|
3
|
+
# passed to models to enable function calling capabilities. It includes the type
|
4
|
+
# of tool and the associated function definition, which specifies the tool's name,
|
5
|
+
# description, parameters, and required fields.
|
6
|
+
#
|
7
|
+
# @example Creating a tool with a function definition
|
8
|
+
# tool = Ollama::Tool.new(
|
9
|
+
# type: 'function',
|
10
|
+
# function: Ollama::Tool::Function.new(
|
11
|
+
# name: 'get_current_weather',
|
12
|
+
# description: 'Get the current weather for a location',
|
13
|
+
# parameters: Ollama::Tool::Function::Parameters.new(
|
14
|
+
# type: 'object',
|
15
|
+
# properties: { location: property },
|
16
|
+
# required: %w[location]
|
17
|
+
# )
|
18
|
+
# )
|
19
|
+
# )
|
1
20
|
class Ollama::Tool
|
2
21
|
include Ollama::DTO
|
3
22
|
|
4
|
-
|
23
|
+
# The type attribute reader returns the type associated with the tool.
|
24
|
+
#
|
25
|
+
# @return [ String ] the type of tool, typically 'function' for function
|
26
|
+
# calling capabilities
|
27
|
+
attr_reader :type
|
5
28
|
|
29
|
+
# The function attribute reader returns the function definition associated
|
30
|
+
# with the tool.
|
31
|
+
#
|
32
|
+
# @return [ Hash ] the function definition as a hash, containing details such
|
33
|
+
# as the function's name, description, parameters, and required fields
|
34
|
+
attr_reader :function
|
35
|
+
|
36
|
+
# The initialize method sets up a new Tool instance with the specified type
|
37
|
+
# and function.
|
38
|
+
#
|
39
|
+
# @param type [ String ] the type of tool being created
|
40
|
+
# @param function [ Ollama::Tool::Function ] the function definition
|
41
|
+
# associated with the tool
|
6
42
|
def initialize(type:, function:)
|
7
43
|
@type, @function = type, function.to_hash
|
8
44
|
end
|