Class: LlmHub::Completion::Providers::OpenAI

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

Overview

OpenAI completion provider

Direct Known Subclasses

Deepseek

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

Methods included from LlmHub::Common::AbstractMethods

included

Constructor Details

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

Instance Method Details

#extract_answer(response_body) ⇒ Object



31
32
33
34
35
36
# File 'lib/llm_hub/completion/providers/openai.rb', line 31

def extract_answer(response_body)
  choices = response_body&.dig('choices')
  return nil if choices.nil? || choices.empty?

  choices[0]&.dig('message', 'content')
end

#extract_tokens(response_body) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/llm_hub/completion/providers/openai.rb', line 38

def extract_tokens(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

#headersObject



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

def headers
  {
    '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

#urlObject



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

def url
  COMPLETIONS_URI
end