Class: Ollama::Commands::Generate
- Inherits:
-
Object
- Object
- Ollama::Commands::Generate
- Includes:
- DTO
- Defined in:
- lib/ollama/commands/generate.rb
Overview
A command class that represents the generate API endpoint for Ollama.
This class is used to interact with the Ollama API’s generate endpoint, which generates text completions using a specified model. It inherits from the base command structure and provides the necessary functionality to execute generation requests for text completion tasks.
Instance Attribute Summary collapse
-
#client ⇒ Object
writeonly
The client attribute writer allows setting the client instance associated with the object.
-
#context ⇒ Array<Integer>?
readonly
The context attribute reader returns the context vector for continuation.
-
#format ⇒ String?
readonly
The format attribute reader returns the format specification for the response.
-
#images ⇒ Array<Ollama::Image>?
readonly
The images attribute reader returns image objects associated with the generate command.
-
#keep_alive ⇒ String?
readonly
The keep_alive attribute reader returns the duration to keep the model loaded in memory.
-
#model ⇒ String
readonly
The model attribute reader returns the model name associated with the generate command.
-
#options ⇒ Ollama::Options?
readonly
The options attribute reader returns configuration parameters for the model.
-
#prompt ⇒ String
readonly
The prompt attribute reader returns the text prompt used for generation.
-
#raw ⇒ Boolean?
readonly
The raw attribute reader returns whether raw output without formatting should be returned.
-
#stream ⇒ Boolean?
readonly
The stream attribute reader returns whether responses will be streamed.
-
#suffix ⇒ String?
readonly
The suffix attribute reader returns any suffix that was appended to the generated text.
-
#system ⇒ String?
readonly
The system attribute reader returns the system message that sets context for generation.
-
#template ⇒ String?
readonly
The template attribute reader returns the template used for formatting the prompt.
-
#think ⇒ Boolean, ...
readonly
The think attribute reader returns whether thinking mode is enabled for generation.
Class Method Summary collapse
-
.path ⇒ String
The path method returns the API endpoint path for generate requests.
Instance Method Summary collapse
-
#initialize(model:, prompt:, suffix: nil, images: nil, format: nil, options: nil, system: nil, template: nil, context: nil, stream: nil, raw: nil, keep_alive: nil, think: nil) ⇒ Generate
constructor
The initialize method sets up a new instance with default streaming behavior.
-
#perform(handler) ⇒ Object
The perform method executes the generate command using the specified handler.
Methods included from DTO
#==, #as_array, #as_array_of_hashes, #as_hash, #as_json, #empty?, #to_json
Constructor Details
#initialize(model:, prompt:, suffix: nil, images: nil, format: nil, options: nil, system: nil, template: nil, context: nil, stream: nil, raw: nil, keep_alive: nil, think: nil) ⇒ Generate
The initialize method sets up a new instance with default streaming behavior.
This method is responsible for initializing a Generate command object with all the necessary parameters for text generation. It handles optional parameters and ensures proper data types (e.g., converting images to arrays).
47 48 49 50 |
# File 'lib/ollama/commands/generate.rb', line 47 def initialize(model:, prompt:, suffix: nil, images: nil, format: nil, options: nil, system: nil, template: nil, context: nil, stream: nil, raw: nil, keep_alive: nil, think: nil) @model, @prompt, @suffix, @images, @format, @options, @system, @template, @context, @stream, @raw, @keep_alive, @think = model, prompt, suffix, (Array(images) if images), format, , system, template, context, stream, raw, keep_alive, think end |
Instance Attribute Details
#client=(value) ⇒ Object (writeonly)
The client attribute writer allows setting the client instance associated with the object.
This method assigns the client that will be used to perform requests and handle responses for this command. It is typically called internally when a command is executed through a client instance.
125 126 127 |
# File 'lib/ollama/commands/generate.rb', line 125 def client=(value) @client = value end |
#context ⇒ Array<Integer>? (readonly)
The context attribute reader returns the context vector for continuation.
95 96 97 |
# File 'lib/ollama/commands/generate.rb', line 95 def context @context end |
#format ⇒ String? (readonly)
The format attribute reader returns the format specification for the response.
75 76 77 |
# File 'lib/ollama/commands/generate.rb', line 75 def format @format end |
#images ⇒ Array<Ollama::Image>? (readonly)
The images attribute reader returns image objects associated with the generate command.
70 71 72 |
# File 'lib/ollama/commands/generate.rb', line 70 def images @images end |
#keep_alive ⇒ String? (readonly)
The keep_alive attribute reader returns the duration to keep the model loaded in memory.
110 111 112 |
# File 'lib/ollama/commands/generate.rb', line 110 def keep_alive @keep_alive end |
#model ⇒ String (readonly)
The model attribute reader returns the model name associated with the generate command.
55 56 57 |
# File 'lib/ollama/commands/generate.rb', line 55 def model @model end |
#options ⇒ Ollama::Options? (readonly)
The options attribute reader returns configuration parameters for the model.
80 81 82 |
# File 'lib/ollama/commands/generate.rb', line 80 def @options end |
#prompt ⇒ String (readonly)
The prompt attribute reader returns the text prompt used for generation.
60 61 62 |
# File 'lib/ollama/commands/generate.rb', line 60 def prompt @prompt end |
#raw ⇒ Boolean? (readonly)
The raw attribute reader returns whether raw output without formatting should be returned.
105 106 107 |
# File 'lib/ollama/commands/generate.rb', line 105 def raw @raw end |
#stream ⇒ Boolean? (readonly)
The stream attribute reader returns whether responses will be streamed.
100 101 102 |
# File 'lib/ollama/commands/generate.rb', line 100 def stream @stream end |
#suffix ⇒ String? (readonly)
The suffix attribute reader returns any suffix that was appended to the generated text.
65 66 67 |
# File 'lib/ollama/commands/generate.rb', line 65 def suffix @suffix end |
#system ⇒ String? (readonly)
The system attribute reader returns the system message that sets context for generation.
85 86 87 |
# File 'lib/ollama/commands/generate.rb', line 85 def system @system end |
#template ⇒ String? (readonly)
The template attribute reader returns the template used for formatting the prompt.
90 91 92 |
# File 'lib/ollama/commands/generate.rb', line 90 def template @template end |
#think ⇒ Boolean, ... (readonly)
The think attribute reader returns whether thinking mode is enabled for generation.
115 116 117 |
# File 'lib/ollama/commands/generate.rb', line 115 def think @think end |
Class Method Details
.path ⇒ String
The path method returns the API endpoint path for generate requests.
This class method provides the specific URL path used to interact with the Ollama API’s generate endpoint. It is utilized internally by the command structure to determine the correct API route for text generation operations.
23 24 25 |
# File 'lib/ollama/commands/generate.rb', line 23 def self.path '/api/generate' end |
Instance Method Details
#perform(handler) ⇒ Object
The perform method executes the generate command using the specified handler.
This method sends a POST request to the Ollama API’s generate endpoint with the command parameters serialized as JSON. It delegates to the client’s request method for actual HTTP communication.
134 135 136 |
# File 'lib/ollama/commands/generate.rb', line 134 def perform(handler) @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:) end |