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) ⇒ void
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).
46 47 48 49 |
# File 'lib/ollama/commands/generate.rb', line 46 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.
124 125 126 |
# File 'lib/ollama/commands/generate.rb', line 124 def client=(value) @client = value end |
#context ⇒ Array<Integer>? (readonly)
The context attribute reader returns the context vector for continuation.
94 95 96 |
# File 'lib/ollama/commands/generate.rb', line 94 def context @context end |
#format ⇒ String? (readonly)
The format attribute reader returns the format specification for the response.
74 75 76 |
# File 'lib/ollama/commands/generate.rb', line 74 def format @format end |
#images ⇒ Array<Ollama::Image>? (readonly)
The images attribute reader returns image objects associated with the generate command.
69 70 71 |
# File 'lib/ollama/commands/generate.rb', line 69 def images @images end |
#keep_alive ⇒ String? (readonly)
The keep_alive attribute reader returns the duration to keep the model loaded in memory.
109 110 111 |
# File 'lib/ollama/commands/generate.rb', line 109 def keep_alive @keep_alive end |
#model ⇒ String (readonly)
The model attribute reader returns the model name associated with the generate command.
54 55 56 |
# File 'lib/ollama/commands/generate.rb', line 54 def model @model end |
#options ⇒ Ollama::Options? (readonly)
The options attribute reader returns configuration parameters for the model.
79 80 81 |
# File 'lib/ollama/commands/generate.rb', line 79 def @options end |
#prompt ⇒ String (readonly)
The prompt attribute reader returns the text prompt used for generation.
59 60 61 |
# File 'lib/ollama/commands/generate.rb', line 59 def prompt @prompt end |
#raw ⇒ Boolean? (readonly)
The raw attribute reader returns whether raw output without formatting should be returned.
104 105 106 |
# File 'lib/ollama/commands/generate.rb', line 104 def raw @raw end |
#stream ⇒ Boolean? (readonly)
The stream attribute reader returns whether responses will be streamed.
99 100 101 |
# File 'lib/ollama/commands/generate.rb', line 99 def stream @stream end |
#suffix ⇒ String? (readonly)
The suffix attribute reader returns any suffix that was appended to the generated text.
64 65 66 |
# File 'lib/ollama/commands/generate.rb', line 64 def suffix @suffix end |
#system ⇒ String? (readonly)
The system attribute reader returns the system message that sets context for generation.
84 85 86 |
# File 'lib/ollama/commands/generate.rb', line 84 def system @system end |
#template ⇒ String? (readonly)
The template attribute reader returns the template used for formatting the prompt.
89 90 91 |
# File 'lib/ollama/commands/generate.rb', line 89 def template @template end |
#think ⇒ Boolean? (readonly)
The think attribute reader returns whether thinking mode is enabled for generation.
114 115 116 |
# File 'lib/ollama/commands/generate.rb', line 114 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) ⇒ void
This method returns an undefined value.
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 |