Class: BetterTranslate::Providers::GeminiProvider
- Inherits:
-
BaseHttpProvider
- Object
- BaseHttpProvider
- BetterTranslate::Providers::GeminiProvider
- Defined in:
- lib/better_translate/providers/gemini_provider.rb
Overview
Google Gemini translation provider
Uses gemini-2.5-flash-lite model for fast, high-quality translations.
Constant Summary collapse
- API_URL =
Google Gemini API endpoint
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent"- MODEL =
Model to use for translations
"gemini-2.5-flash-lite"
Instance Attribute Summary
Attributes inherited from BaseHttpProvider
#cache, #config, #rate_limiter
Instance Method Summary collapse
-
#build_prompt(text, target_lang_name) ⇒ String
private
private
Build prompt for Gemini API.
-
#extract_translation(response) ⇒ String
private
private
Extract translation from API response.
-
#make_generation_request(prompt) ⇒ Faraday::Response
private
private
Make generation request to Gemini API.
-
#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>
Translate multiple texts in a batch.
-
#translate_text(text, target_lang_code, target_lang_name) ⇒ String
Translate a single text.
Methods inherited from BaseHttpProvider
#build_cache_key, #calculate_backoff, #handle_response, #http_client, #initialize, #log_retry, #make_request, #with_cache
Constructor Details
This class inherits a constructor from BetterTranslate::Providers::BaseHttpProvider
Instance Method Details
#build_prompt(text, target_lang_name) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build prompt for Gemini API
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/better_translate/providers/gemini_provider.rb', line 78 def build_prompt(text, target_lang_name) base_prompt = "Translate the following text to #{target_lang_name}. " \ "Return ONLY the translated text, without any explanations. " \ "Words like VARIABLE_0, VARIABLE_1, etc. are placeholders and must be kept unchanged in the translation.\n\n" \ "Text: #{text}" if config.translation_context && !config.translation_context.empty? base_prompt = "Context: #{config.translation_context}\n\n#{base_prompt}" end base_prompt end |
#extract_translation(response) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract translation from API response
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/better_translate/providers/gemini_provider.rb', line 124 def extract_translation(response) parsed = JSON.parse(response.body) translation = parsed.dig("candidates", 0, "content", "parts", 0, "text") raise TranslationError, "No translation in response" if translation.nil? || translation.empty? translation.strip rescue JSON::ParserError => e raise TranslationError.new( "Failed to parse Gemini response", context: { error: e., body: response.body } ) end |
#make_generation_request(prompt) ⇒ Faraday::Response (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Make generation request to Gemini API
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/better_translate/providers/gemini_provider.rb', line 97 def make_generation_request(prompt) url = "#{API_URL}?key=#{config.google_gemini_key}" body = { contents: [ { parts: [ { text: prompt } ] } ] } headers = { "Content-Type" => "application/json" } make_request(:post, url, body: body, headers: headers) end |
#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>
Translate multiple texts in a batch
65 66 67 |
# File 'lib/better_translate/providers/gemini_provider.rb', line 65 def translate_batch(texts, target_lang_code, target_lang_name) texts.map { |text| translate_text(text, target_lang_code, target_lang_name) } end |
#translate_text(text, target_lang_code, target_lang_name) ⇒ String
Translate a single text
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/better_translate/providers/gemini_provider.rb', line 36 def translate_text(text, target_lang_code, target_lang_name) Validator.validate_text!(text) Validator.validate_language_code!(target_lang_code) cache_key = build_cache_key(text, target_lang_code) with_cache(cache_key) do prompt = build_prompt(text, target_lang_name) response = make_generation_request(prompt) extract_translation(response) end rescue ApiError => e raise TranslationError.new( "Failed to translate text with Gemini: #{e.}", context: { text: text, target_lang: target_lang_code, original_error: e } ) end |