Class: TranslationAPI::Provider::Gemini::Response
- Inherits:
-
Object
- Object
- TranslationAPI::Provider::Gemini::Response
- Defined in:
- lib/translation_api/provider/gemini/response.rb
Constant Summary collapse
- REQUEST_FAILED_MESSAGE =
"Request failed with status"
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #dig_used_tokens(type:) ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #translated_text ⇒ Object
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
13 14 15 |
# File 'lib/translation_api/provider/gemini/response.rb', line 13 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/translation_api/provider/gemini/response.rb', line 11 def response @response end |
Instance Method Details
#dig_used_tokens(type:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/translation_api/provider/gemini/response.rb', line 24 def dig_used_tokens(type:) case type when :input body_json.dig("usageMetadata", "promptTokenCount") when :output body_json.dig("usageMetadata", "candidatesTokenCount") else raise ArgumentError, "Invalid token type: #{type}" end end |
#translated_text ⇒ Object
17 18 19 20 21 22 |
# File 'lib/translation_api/provider/gemini/response.rb', line 17 def translated_text = "#{REQUEST_FAILED_MESSAGE} #{@response.status}" raise ArgumentError, unless @response.status == 200 body_json.dig("candidates", 0, "content", "parts", 0, "text") end |