Class: OmniAI::Embed::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/embed/response.rb

Overview

The response returned by the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, context: nil) ⇒ Response

Returns a new instance of Response.

Parameters:



12
13
14
15
# File 'lib/omniai/embed/response.rb', line 12

def initialize(data:, context: nil)
  @data = data
  @context = context
end

Instance Attribute Details

#dataHash

Returns:

  • (Hash)


8
9
10
# File 'lib/omniai/embed/response.rb', line 8

def data
  @data
end

Instance Method Details

#embedding(index: 0) ⇒ Array<Float>

Parameters:

  • index (Integer) (defaults to: 0)

    optional

Returns:

  • (Array<Float>)


41
42
43
# File 'lib/omniai/embed/response.rb', line 41

def embedding(index: 0)
  embeddings[index]
end

#embeddingsArray<Array<Float>>

Returns:

  • (Array<Array<Float>>)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omniai/embed/response.rb', line 46

def embeddings
  @embeddings ||= begin
    deserializer = @context&.deserializer(:embeddings)

    if deserializer
      deserializer.call(@data, context: @context)
    else
      @data['data'].map { |embedding| embedding['embedding'] }
    end
  end
end

#inspectString

Returns:

  • (String)


18
19
20
# File 'lib/omniai/embed/response.rb', line 18

def inspect
  "#<#{self.class.name}>"
end

#usageUsage

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omniai/embed/response.rb', line 23

def usage
  @usage ||= begin
    deserializer = @context&.deserializer(:usage)

    if deserializer
      deserializer.call(@data, context: @context)
    else
      prompt_tokens = @data.dig('usage', 'prompt_tokens')
      total_tokens = @data.dig('usage', 'total_tokens')

      Usage.new(prompt_tokens:, total_tokens:)
    end
  end
end