Class: Ollama::Commands::Create

Inherits:
Object
  • Object
show all
Includes:
DTO
Defined in:
lib/ollama/commands/create.rb

Overview

A command class that represents the create API endpoint for Ollama.

This class is used to interact with the Ollama API’s create endpoint, which creates a new model based on a modelfile or existing model. It inherits from the base command structure and provides the necessary functionality to execute model creation requests.

Examples:

Creating a new model from an existing model

create = ollama.create(model: 'llama3.1-wopr', from: 'llama3.1', system: 'You are WOPR from WarGames')

Creating a model with files and parameters

create = ollama.create(
  model: 'my-model',
  from: 'llama3.1',
  files: { 'modelfile' => 'FROM llama3.1\nPARAMETER temperature 0.7' },
  parameters: Ollama::Options.new(temperature: 0.7, num_ctx: 8192)
)

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:, from: nil, files: nil, adapters: nil, template: nil, license: nil, system: nil, parameters: nil, messages: nil, stream: true, quantize: nil) ⇒ Create

The initialize method sets up a new instance with streaming enabled by default.

This method is responsible for initializing a new object instance and configuring it with parameters required for model creation. It sets up the model name, source model (if any), files, adapters, template, license, system prompt, parameters, messages, and streaming behavior.

Parameters:

  • model (String)

    the name of the new model to be created

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

    the base model to create from (e.g., ‘llama3.1’)

  • files (Hash, nil) (defaults to: nil)

    file contents for the modelfile and other files

  • adapters (Hash, nil) (defaults to: nil)

    adapter files to use for quantization

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

    the template to use for the model

  • license (String, Array<String>, nil) (defaults to: nil)

    the license(s) for the model

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

    the system prompt to use for the model

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

    configuration parameters for the model

  • messages (Array<Ollama::Message>, nil) (defaults to: nil)

    initial conversation messages

  • stream (TrueClass, FalseClass) (defaults to: true)

    whether to enable streaming for the operation, defaults to true

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

    quantization method to use (e.g., ‘Q4_0’)



50
51
52
53
54
# File 'lib/ollama/commands/create.rb', line 50

def initialize(model:, from: nil, files: nil, adapters: nil, template: nil, license: nil, system: nil, parameters: nil, messages: nil, stream: true, quantize: nil)
  @model, @from, @files, @adapters, @license, @system, @parameters, @messages, @stream, @quantize =
    model, from, as_hash(files), as_hash(adapters), as_array(license), system,
    as_hash(parameters), as_array_of_hashes(messages), stream, quantize
end

Instance Attribute Details

#adaptersHash? (readonly)

The adapters attribute reader returns the adapter files associated with the object.

Returns:

  • (Hash, nil)

    adapter files to use for quantization



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

def adapters
  @adapters
end

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



116
117
118
# File 'lib/ollama/commands/create.rb', line 116

def client=(value)
  @client = value
end

#filesHash? (readonly)

The files attribute reader returns the file contents associated with the object.

Returns:

  • (Hash, nil)

    file contents for the modelfile and other files



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

def files
  @files
end

#fromString? (readonly)

The from attribute reader returns the base model name associated with the object.

Returns:

  • (String, nil)

    the base model to create from (e.g., ‘llama3.1’)



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

def from
  @from
end

#licenseString, ... (readonly)

The license attribute reader returns the license(s) associated with the object.

Returns:

  • (String, Array<String>, nil)

    the license(s) for the model



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

def license
  @license
end

#messagesArray<Ollama::Message>? (readonly)

The messages attribute reader returns the initial conversation messages associated with the object.

Returns:



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

def messages
  @messages
end

#modelString (readonly)

The model attribute reader returns the model name associated with the object.

Returns:

  • (String)

    the name of the new model to be created



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

def model
  @model
end

#parametersOllama::Options? (readonly)

The parameters attribute reader returns the model configuration parameters associated with the object.

Returns:



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

def parameters
  @parameters
end

#quantizeString? (readonly)

The quantize attribute reader returns the quantization method associated with the object.

Returns:

  • (String, nil)

    quantization method to use (e.g., ‘Q4_0’)



106
107
108
# File 'lib/ollama/commands/create.rb', line 106

def quantize
  @quantize
end

#streamTrueClass, FalseClass (readonly)

The stream attribute reader returns the streaming behavior setting associated with the object.

Returns:

  • (TrueClass, FalseClass)

    the streaming behavior flag, indicating whether streaming is enabled for the command execution (defaults to true for create commands)



101
102
103
# File 'lib/ollama/commands/create.rb', line 101

def stream
  @stream
end

#systemString? (readonly)

The system attribute reader returns the system prompt associated with the object.

Returns:

  • (String, nil)

    the system prompt to use for the model



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

def system
  @system
end

Class Method Details

.pathString

The path method returns the API endpoint path for create requests.

This class method provides the specific URL path used to interact with the Ollama API’s create endpoint. It is utilized internally by the command structure to determine the correct API route for creating new models.

Returns:

  • (String)

    the API endpoint path ‘/api/create’ for create requests



28
29
30
# File 'lib/ollama/commands/create.rb', line 28

def self.path
  '/api/create'
end

Instance Method Details

#perform(handler) ⇒ self

The perform method executes a command request using the specified handler.

This method initiates a POST request to the Ollama API’s create endpoint, utilizing the client instance to send the request and process responses through the provided handler. It handles both streaming and non-streaming scenarios based on the command’s configuration.

responses

Parameters:

  • handler (Ollama::Handler)

    the handler object responsible for processing API

Returns:

  • (self)

    returns the current instance after initiating the request



129
130
131
# File 'lib/ollama/commands/create.rb', line 129

def perform(handler)
  @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
end