Class: LlmHub::Completion::Providers::OpenAI
- Inherits:
-
Base
- Object
- Base
- LlmHub::Completion::Providers::OpenAI
show all
- Defined in:
- lib/llm_hub/completion/providers/openai.rb
Overview
OpenAI completion provider
Constant Summary
collapse
- COMPLETIONS_URI =
'https://api.openai.com/v1/chat/completions'
Instance Attribute Summary
Attributes inherited from Base
#api_key
Instance Method Summary
collapse
Methods inherited from Base
#initialize
included
Instance Method Details
31
32
33
34
35
36
|
# File 'lib/llm_hub/completion/providers/openai.rb', line 31
def (response_body)
choices = response_body&.dig('choices')
return nil if choices.nil? || choices.empty?
choices[0]&.dig('message', 'content')
end
|
38
39
40
41
42
43
44
45
|
# File 'lib/llm_hub/completion/providers/openai.rb', line 38
def (response_body)
usage = response_body&.dig('usage')
{
total_tokens: usage&.dig('total_tokens'),
prompt_tokens: usage&.dig('prompt_tokens'),
completion_tokens: usage&.dig('completion_tokens')
}
end
|
14
15
16
17
18
19
|
# File 'lib/llm_hub/completion/providers/openai.rb', line 14
def
{
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@api_key}"
}
end
|
#request_body(system_prompt, content, model_name, option_params) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/llm_hub/completion/providers/openai.rb', line 21
def request_body(system_prompt, content, model_name, option_params)
base_params = {
model: model_name,
n: 1,
temperature: 0.2,
messages: build_messages(system_prompt, content)
}
base_params.merge(option_params)
end
|
#url ⇒ Object
10
11
12
|
# File 'lib/llm_hub/completion/providers/openai.rb', line 10
def url
COMPLETIONS_URI
end
|