Class: Ollama::Commands::Generate

Inherits:
Object
  • Object
show all
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.

Examples:

Generating a text completion

generate = ollama.generate(model: 'llama3.1', prompt: 'Why is the sky blue?')

Generating with streaming enabled

generate = ollama.generate(model: 'llama3.1', prompt: 'Why is the sky blue?', stream: true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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).

Parameters:

  • model (String)

    the name of the model to use for generation

  • prompt (String)

    the text prompt to generate completions for

  • suffix (String, nil) (defaults to: nil)

    optional suffix to append to the generated text

  • images (Ollama::Image, Array<Ollama::Image>, nil) (defaults to: nil)

    optional image(s) to include in the request

  • format (String, nil) (defaults to: nil)

    optional format specification for the response

  • options (Ollama::Options, nil) (defaults to: nil)

    optional configuration parameters for the model

  • system (String, nil) (defaults to: nil)

    optional system message to set context for generation

  • template (String, nil) (defaults to: nil)

    optional template to use for formatting the prompt

  • context (Array<Integer>, nil) (defaults to: nil)

    optional context vector for continuation

  • stream (Boolean, nil) (defaults to: nil)

    whether to stream responses (default: false)

  • raw (Boolean, nil) (defaults to: nil)

    whether to return raw output without formatting

  • keep_alive (String, nil) (defaults to: nil)

    duration to keep the model loaded in memory

  • think (Boolean, nil) (defaults to: nil)

    whether to enable thinking mode for generation



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, options, 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

#contextArray<Integer>? (readonly)

The context attribute reader returns the context vector for continuation.

Returns:

  • (Array<Integer>, nil)

    optional context vector for continuation



94
95
96
# File 'lib/ollama/commands/generate.rb', line 94

def context
  @context
end

#formatString? (readonly)

The format attribute reader returns the format specification for the response.

Returns:

  • (String, nil)

    optional format specification for the response



74
75
76
# File 'lib/ollama/commands/generate.rb', line 74

def format
  @format
end

#imagesArray<Ollama::Image>? (readonly)

The images attribute reader returns image objects associated with the generate command.

Returns:

  • (Array<Ollama::Image>, nil)

    array of image objects, or nil if none provided



69
70
71
# File 'lib/ollama/commands/generate.rb', line 69

def images
  @images
end

#keep_aliveString? (readonly)

The keep_alive attribute reader returns the duration to keep the model loaded in memory.

Returns:

  • (String, nil)

    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

#modelString (readonly)

The model attribute reader returns the model name associated with the generate command.

Returns:

  • (String)

    the name of the model used for generation



54
55
56
# File 'lib/ollama/commands/generate.rb', line 54

def model
  @model
end

#optionsOllama::Options? (readonly)

The options attribute reader returns configuration parameters for the model.

Returns:

  • (Ollama::Options, nil)

    optional configuration parameters for the model



79
80
81
# File 'lib/ollama/commands/generate.rb', line 79

def options
  @options
end

#promptString (readonly)

The prompt attribute reader returns the text prompt used for generation.

Returns:

  • (String)

    the text prompt to generate completions for



59
60
61
# File 'lib/ollama/commands/generate.rb', line 59

def prompt
  @prompt
end

#rawBoolean? (readonly)

The raw attribute reader returns whether raw output without formatting should be returned.

Returns:

  • (Boolean, nil)

    whether to return raw output without formatting



104
105
106
# File 'lib/ollama/commands/generate.rb', line 104

def raw
  @raw
end

#streamBoolean? (readonly)

The stream attribute reader returns whether responses will be streamed.

Returns:

  • (Boolean, nil)

    whether to stream responses (default: false)



99
100
101
# File 'lib/ollama/commands/generate.rb', line 99

def stream
  @stream
end

#suffixString? (readonly)

The suffix attribute reader returns any suffix that was appended to the generated text.

Returns:

  • (String, nil)

    optional suffix to append to the generated text



64
65
66
# File 'lib/ollama/commands/generate.rb', line 64

def suffix
  @suffix
end

#systemString? (readonly)

The system attribute reader returns the system message that sets context for generation.

Returns:

  • (String, nil)

    optional system message to set context for generation



84
85
86
# File 'lib/ollama/commands/generate.rb', line 84

def system
  @system
end

#templateString? (readonly)

The template attribute reader returns the template used for formatting the prompt.

Returns:

  • (String, nil)

    optional template to use for formatting the prompt



89
90
91
# File 'lib/ollama/commands/generate.rb', line 89

def template
  @template
end

#thinkBoolean? (readonly)

The think attribute reader returns whether thinking mode is enabled for generation.

Returns:

  • (Boolean, nil)

    whether to enable thinking mode for generation



114
115
116
# File 'lib/ollama/commands/generate.rb', line 114

def think
  @think
end

Class Method Details

.pathString

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.

Returns:

  • (String)

    the API endpoint path '/api/generate' for generate requests



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.

Parameters:

  • handler (Ollama::Handler)

    the handler to process responses from the API



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