Class: Ollama::Commands::Embed

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

Overview

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

This class is used to interact with the Ollama API’s embed endpoint, which generates embeddings for text input using a specified model. It inherits from the base command structure and provides the necessary functionality to execute embedding requests for generating vector representations of text.

Examples:

Generating embeddings for a single text

embed = ollama.embed(model: 'all-minilm', input: 'Why is the sky blue?')

Generating embeddings for multiple texts

embed = ollama.embed(model: 'all-minilm', input: ['Why is the sky blue?', 'Why is the grass green?'])

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:, input:, options: nil, truncate: nil, keep_alive: nil, dimensions: nil) ⇒ Embed

The initialize method sets up a new instance with streaming disabled.

This method is responsible for initializing a new object instance and configuring it with parameters required for embedding operations. It sets up the model, input text(s), and optional parameters while explicitly disabling streaming since embedding operations are typically non-streaming.

Parameters:

  • model (String)

    the name of the model to use for generating embeddings

  • input (String, Array<String>)

    the text input(s) to generate embeddings for

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

    optional configuration parameters for the model

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

    whether to truncate the input if it exceeds context length

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

    duration to keep the model loaded in memory

  • dimensions (Integer, nil) (defaults to: nil)

    truncates the output embedding to the specified dimension.



40
41
42
43
44
# File 'lib/ollama/commands/embed.rb', line 40

def initialize(model:, input:, options: nil, truncate: nil, keep_alive: nil, dimensions: nil)
  @model, @input, @options, @truncate, @keep_alive, @dimensions =
    model, input, options, truncate, keep_alive, dimensions
  @stream = false
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.



92
93
94
# File 'lib/ollama/commands/embed.rb', line 92

def client=(value)
  @client = value
end

#dimensionsInteger? (readonly)

The dimensions attribute reader returns the dimensions associated with the object.

Returns:

  • (Integer, nil)

    the dimensions value stored in the instance variable



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

def dimensions
  @dimensions
end

#inputString+ (readonly)

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

Returns:

  • (String, Array<String>)

    the text input(s) to generate embeddings for



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

def input
  @input
end

#keep_aliveString? (readonly)

The keep_alive attribute reader returns the keep-alive duration associated with the object.

Returns:

  • (String, nil)

    duration to keep the model loaded in memory



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

def keep_alive
  @keep_alive
end

#modelString (readonly)

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

Returns:

  • (String)

    the name of the model used by the command instance



49
50
51
# File 'lib/ollama/commands/embed.rb', line 49

def model
  @model
end

#optionsOllama::Options? (readonly)

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

Returns:

  • (Ollama::Options, nil)

    optional configuration parameters for the model



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

def options
  @options
end

#streamFalseClass (readonly)

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

Returns:

  • (FalseClass)

    the streaming behavior flag, indicating whether streaming is enabled for the command execution (always false for embed commands)



82
83
84
# File 'lib/ollama/commands/embed.rb', line 82

def stream
  @stream
end

#truncateBoolean? (readonly)

The truncate attribute reader returns the truncate setting associated with the object.

Returns:

  • (Boolean, nil)

    whether to truncate the input if it exceeds context length



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

def truncate
  @truncate
end

Class Method Details

.pathString

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

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

Returns:

  • (String)

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



23
24
25
# File 'lib/ollama/commands/embed.rb', line 23

def self.path
  '/api/embed'
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 embed endpoint, utilizing the client instance to send the request and process responses through the provided handler. It handles non-streaming scenarios since embedding commands do not support streaming.

responses

Parameters:

  • handler (Ollama::Handler)

    the handler object responsible for processing API

Returns:

  • (self)

    returns the current instance after initiating the request



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

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