Class: LlmHub::Embedding::Providers::OpenAI
- Inherits:
-
Base
- Object
- Base
- LlmHub::Embedding::Providers::OpenAI
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
included
Instance Method Details
29
30
31
32
33
34
|
# File 'lib/llm_hub/embedding/providers/openai.rb', line 29
def (response_body)
data_array = response_body&.dig('data')
return nil if data_array.nil? || data_array.empty?
data_array[0]&.dig('embedding')
end
|
36
37
38
39
40
41
42
|
# File 'lib/llm_hub/embedding/providers/openai.rb', line 36
def (response_body)
usage = response_body&.dig('usage')
{
total_tokens: usage&.dig('total_tokens'),
prompt_tokens: usage&.dig('prompt_tokens')
}
end
|
14
15
16
17
18
19
|
# File 'lib/llm_hub/embedding/providers/openai.rb', line 14
def
{
'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
|
#url ⇒ Object
10
11
12
|
# File 'lib/llm_hub/embedding/providers/openai.rb', line 10
def url
EMBEDDINGS_URI
end
|