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, String, nil) (defaults to: nil)

    whether to enable thinking mode for generation. Can be “high”, “medium”, “low” instead of true



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



125
126
127
# File 'lib/ollama/commands/generate.rb', line 125

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



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

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



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

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



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

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



110
111
112
# File 'lib/ollama/commands/generate.rb', line 110

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



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

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



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

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



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

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



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

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)



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

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



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

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



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

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



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

def template
  @template
end

#thinkBoolean, ... (readonly)

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

Returns:

  • (Boolean, String, nil)

    whether to enable thinking mode for generation



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

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

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