Class: TranslationAPI::Provider::OpenAI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_api/provider/openai/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



7
8
9
# File 'lib/translation_api/provider/openai/response.rb', line 7

def initialize(response)
  @response = response
end

Instance Method Details

#dig_used_tokens(type:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/translation_api/provider/openai/response.rb', line 15

def dig_used_tokens(type:)
  case type
  when :input
    @response.dig("usage", "prompt_tokens")
  when :output
    @response.dig("usage", "completion_tokens")
  else
    raise ArgumentError, "Invalid token type: #{type}"
  end
end

#translated_textObject



11
12
13
# File 'lib/translation_api/provider/openai/response.rb', line 11

def translated_text
  @response.dig("choices", 0, "message", "content")
end