Class: Jekyll::ClientSearch::OllamaEmbeddingAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/client_search/ollama_embedding_adapter.rb

Overview

Generates embeddings via a local Ollama server using the ollama-ruby gem. The gem is required lazily so that users who do not enable embeddings never need to install it.

Configuration:

embedding:
enabled: true
model: all-minilm          # or nomic-embed-text, bge-m3, etc.
base_url: http://localhost:11434

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, base_url: "http://localhost:11434", connect_timeout: 5, read_timeout: 120) ⇒ OllamaEmbeddingAdapter

Returns a new instance of OllamaEmbeddingAdapter.



17
18
19
20
21
22
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 17

def initialize(model:, base_url: "http://localhost:11434", connect_timeout: 5, read_timeout: 120)
  @model = model
  @base_url = base_url
  @connect_timeout = connect_timeout
  @read_timeout = read_timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



15
16
17
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 15

def base_url
  @base_url
end

#connect_timeoutObject (readonly)

Returns the value of attribute connect_timeout.



15
16
17
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 15

def connect_timeout
  @connect_timeout
end

#modelObject (readonly)

Returns the value of attribute model.



15
16
17
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 15

def model
  @model
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



15
16
17
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 15

def read_timeout
  @read_timeout
end

Instance Method Details

#embed(text) ⇒ Object

Returns a float vector for the given text. Raises a clear error if the ollama-ruby gem is not installed or the server is unreachable.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll/client_search/ollama_embedding_adapter.rb', line 26

def embed(text)
  embedding = client.embed(model: @model, input: text).embeddings&.first
  return embedding if valid_embedding?(embedding)

  Jekyll.logger.warn "ClientSearch:", "embedding response was empty or invalid"
  nil
rescue LoadError, NameError
  raise Jekyll::Errors::FatalException,
        "Add gem \"ollama-ruby\" to your Gemfile to use embedding features"
rescue StandardError => e
  Jekyll.logger.warn "ClientSearch:", "embedding failed for text: #{e.message}"
  nil
end