Class: LittleGhost::Embeddings::Request

Inherits:
Data
  • Object
show all
Defined in:
lib/little_ghost/embeddings/request.rb,
lib/little_ghost/embeddings/request.rb

Overview

Carries the validated inputs and controls for one embedding operation.

LittleGhost builds this value for LittleGhost.embed and passes it to the selected provider. Provider implementations may receive calls concurrently. They must observe cancellation_token and deadline while performing external work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs:, settings: {}, limits: {}, cancellation_token: Support::CancellationToken.new, deadline: nil) ⇒ Request

Validates configured budgets before copying retained input strings.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/little_ghost/embeddings/request.rb', line 17

def initialize(inputs:, settings: {}, limits: {}, cancellation_token: Support::CancellationToken.new, deadline: nil)
  values = inputs.is_a?(String) ? [inputs] : inputs
  unless values.is_a?(Array) && !values.empty? && values.all? { |value| value.is_a?(String) && !value.empty? }
    raise ArgumentError, "inputs must be a nonempty String or Array of nonempty Strings"
  end
  raise ArgumentError, "settings must be a mapping" unless settings.respond_to?(:to_h)
  raise ArgumentError, "limits must be a mapping" unless limits.respond_to?(:to_h)

  configured_limits = DEFAULT_LIMITS.merge(limits.to_h.transform_keys(&:to_sym))
  configured_limits.transform_values! { |value| Integer(value) }
  unless configured_limits.values.all?(&:positive?)
    raise ArgumentError, "embedding limits must be positive integers"
  end
  if values.length > configured_limits.fetch(:max_inputs)
    raise UnsupportedInputError, "Embedding input count exceeds the configured limit"
  end
  total_bytes = 0
  values.each do |value|
    bytes = value.bytesize
    if bytes > configured_limits.fetch(:max_input_bytes)
      raise UnsupportedInputError, "An embedding input exceeds the configured byte limit"
    end
    total_bytes += bytes
    if total_bytes > configured_limits.fetch(:max_total_bytes)
      raise UnsupportedInputError, "Embedding inputs exceed the configured aggregate byte limit"
    end
  end

  super(
    inputs: values.map { |value| value.dup.freeze }.freeze,
    settings: settings.to_h.transform_keys(&:to_sym).freeze,
    limits: configured_limits.freeze,
    cancellation_token:,
    deadline:
  )
end

Instance Attribute Details

#cancellation_tokenObject (readonly)

Returns the value of attribute cancellation_token

Returns:

  • (Object)

    the current value of cancellation_token



15
16
17
# File 'lib/little_ghost/embeddings/request.rb', line 15

def cancellation_token
  @cancellation_token
end

#deadlineObject (readonly)

Returns the value of attribute deadline

Returns:

  • (Object)

    the current value of deadline



15
16
17
# File 'lib/little_ghost/embeddings/request.rb', line 15

def deadline
  @deadline
end

#inputsObject (readonly)

Returns the value of attribute inputs

Returns:

  • (Object)

    the current value of inputs



15
16
17
# File 'lib/little_ghost/embeddings/request.rb', line 15

def inputs
  @inputs
end

#limitsObject (readonly)

Returns the value of attribute limits

Returns:

  • (Object)

    the current value of limits



15
16
17
# File 'lib/little_ghost/embeddings/request.rb', line 15

def limits
  @limits
end

#settingsObject (readonly)

Returns the value of attribute settings

Returns:

  • (Object)

    the current value of settings



15
16
17
# File 'lib/little_ghost/embeddings/request.rb', line 15

def settings
  @settings
end