Class: LlmHub::Embedding::Providers::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/llm_hub/embedding/providers/openai.rb

Overview

OpenAI embedding provider

Constant Summary collapse

EMBEDDINGS_URI =
'https://api.openai.com/v1/embeddings'

Instance Attribute Summary

Attributes inherited from Base

#api_key

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Common::AbstractMethods

included

Constructor Details

This class inherits a constructor from LlmHub::Embedding::Providers::Base

Instance Method Details

#extract_embedding(response_body) ⇒ Object



29
30
31
32
33
34
# File 'lib/llm_hub/embedding/providers/openai.rb', line 29

def extract_embedding(response_body)
  data_array = response_body&.dig('data')
  return nil if data_array.nil? || data_array.empty?

  data_array[0]&.dig('embedding')
end

#extract_tokens(response_body) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/llm_hub/embedding/providers/openai.rb', line 36

def extract_tokens(response_body)
  usage = response_body&.dig('usage')
  {
    total_tokens: usage&.dig('total_tokens'),
    prompt_tokens: usage&.dig('prompt_tokens')
  }
end

#headersObject



14
15
16
17
18
19
# File 'lib/llm_hub/embedding/providers/openai.rb', line 14

def headers
  {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{@api_key}"
  }
end

#request_body(text, model_name, option_params) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/llm_hub/embedding/providers/openai.rb', line 21

def request_body(text, model_name, option_params)
  base_params = {
    model: model_name,
    input: text
  }
  base_params.merge(option_params)
end

#urlObject



10
11
12
# File 'lib/llm_hub/embedding/providers/openai.rb', line 10

def url
  EMBEDDINGS_URI
end