Module: ActiveAgent::Providers::Ollama::Embedding::Transforms

Defined in:
lib/active_agent/providers/ollama/embedding/transforms.rb

Overview

Provides transformation methods for normalizing embedding parameters to Ollama API format with OpenAI gem compatibility

Class Method Summary collapse

Class Method Details

.cleanup_serialized_request(openai_hash, ollama_params, defaults) ⇒ Hash

Cleans up serialized request for API submission

Merges OpenAI-compatible params with Ollama-specific params.

Parameters:

  • openai_hash (Hash)

    serialized OpenAI-compatible request

  • ollama_params (Hash)

    Ollama-specific parameters

  • defaults (Hash)

    default values to remove

Returns:

  • (Hash)

    cleaned and merged request hash



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_agent/providers/ollama/embedding/transforms.rb', line 109

def cleanup_serialized_request(openai_hash, ollama_params, defaults)
  # Remove nil values
  cleaned = openai_hash.compact

  # Serialize input (convert single-element array to string)
  if cleaned[:input]
    cleaned[:input] = serialize_input(cleaned[:input])
  end

  # Merge Ollama-specific params, skip defaults
  ollama_params.each do |key, value|
    next if value.nil?
    next if value.respond_to?(:empty?) && value.empty?
    next if defaults.key?(key) && defaults[key] == value

    # Serialize options object if present
    cleaned[key] = if value.respond_to?(:serialize)
      value.serialize
    else
      value
    end
  end

  cleaned
end

.gem_to_hash(obj) ⇒ Hash, Object

Converts gem objects to hash representation

Parameters:

  • obj (Object)

    gem object or primitive value

Returns:

  • (Hash, Object)

    hash if object supports JSON serialization



19
20
21
22
23
24
25
# File 'lib/active_agent/providers/ollama/embedding/transforms.rb', line 19

def gem_to_hash(obj)
  if obj.respond_to?(:to_json)
    JSON.parse(obj.to_json, symbolize_names: true)
  else
    obj
  end
end

.normalize_input(input) ⇒ Array<String>

Normalizes input parameter to Ollama format

Ollama only accepts strings or arrays of strings (no token arrays). Converts single string to array internally for consistency.

Parameters:

  • input (String, Array<String>)

Returns:

  • (Array<String>)

    normalized input as array of strings

Raises:

  • (ArgumentError)

    if input contains non-string values



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_agent/providers/ollama/embedding/transforms.rb', line 62

def normalize_input(input)
  case input
  when String
    [ input.presence ].compact
  when Array
    # Validate all elements are strings
    input.each_with_index do |item, index|
      unless item.is_a?(String)
        raise ArgumentError, "Ollama embedding input must contain only strings, got #{item.class} at index #{index}"
      end
      if item.empty?
        raise ArgumentError, "Ollama embedding input cannot contain empty strings at index #{index}"
      end
    end
    input.compact
  when nil
    nil
  else
    raise ArgumentError, "Cannot normalize #{input.class} to Ollama input (expected String or Array)"
  end
end

.normalize_params(params) ⇒ Array<Hash, Hash>

Normalizes all embedding request parameters

Ollama-specific parameters (options, keep_alive, truncate) are extracted and returned separately from OpenAI-compatible parameters.

Parameters:

  • params (Hash)

    raw request parameters

Returns:

  • (Array<Hash, Hash>)

    tuple of [openai_params, ollama_params]



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_agent/providers/ollama/embedding/transforms.rb', line 34

def normalize_params(params)
  params = params.dup

  # Extract Ollama-specific parameters
  ollama_params = {}
  ollama_params[:options] = params.delete(:options) if params.key?(:options)
  ollama_params[:keep_alive] = params.delete(:keep_alive) if params.key?(:keep_alive)
  ollama_params[:truncate] = params.delete(:truncate) if params.key?(:truncate)

  # Extract options attributes that can be at top level
  extract_option_attributes(params, ollama_params)

  # Normalize input - Ollama only accepts strings, not token arrays
  if params[:input]
    params[:input] = normalize_input(params[:input])
  end

  [ params, ollama_params ]
end

.serialize_input(input) ⇒ String, ...

Serializes input for API submission

Returns single string if array has only one element, otherwise array.

Parameters:

  • input (Array<String>, nil)

Returns:

  • (String, Array<String>, nil)


90
91
92
93
94
95
96
97
98
99
# File 'lib/active_agent/providers/ollama/embedding/transforms.rb', line 90

def serialize_input(input)
  return nil if input.nil?

  # Return single string if array has only one element
  if input.is_a?(Array) && input.length == 1
    input.first
  else
    input
  end
end