Class: BetterTranslate::Providers::ChatGPTProvider
- Inherits:
-
BaseHttpProvider
- Object
- BaseHttpProvider
- BetterTranslate::Providers::ChatGPTProvider
- Defined in:
- lib/better_translate/providers/chatgpt_provider.rb
Overview
OpenAI ChatGPT translation provider
Uses GPT-5-nano model with temperature=1.0 for natural translations.
Constant Summary collapse
- API_URL =
OpenAI API endpoint
"https://api.openai.com/v1/chat/completions"- MODEL =
Model to use for translations
"gpt-5-nano"- TEMPERATURE =
Temperature setting for creativity
1.0
Instance Attribute Summary
Attributes inherited from BaseHttpProvider
#cache, #config, #rate_limiter
Instance Method Summary collapse
-
#build_messages(text, target_lang_name) ⇒ Array<Hash>
private
private
Build messages array for ChatGPT API.
-
#build_system_message(target_lang_name) ⇒ String
private
private
Build system message with optional context.
-
#extract_translation(response) ⇒ String
private
private
Extract translation from API response.
-
#make_chat_completion_request(messages) ⇒ Faraday::Response
private
private
Make chat completion request to OpenAI 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_messages(text, target_lang_name) ⇒ Array<Hash> (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 messages array for ChatGPT API
81 82 83 84 85 86 87 88 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 81 def (text, target_lang_name) = (target_lang_name) [ { role: "system", content: }, { role: "user", content: text } ] end |
#build_system_message(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 system message with optional context
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 96 def (target_lang_name) = "You are a professional translator. Translate the following text to #{target_lang_name}. " \ "Return ONLY the translated text, without any explanations or additional text. " \ "Words like VARIABLE_0, VARIABLE_1, etc. are placeholders and must be kept unchanged in the translation." if config.translation_context && !config.translation_context.empty? += "\n\nContext: #{config.translation_context}" end 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
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 136 def extract_translation(response) parsed = JSON.parse(response.body) translation = parsed.dig("choices", 0, "message", "content") raise TranslationError, "No translation in response" if translation.nil? || translation.empty? translation.strip rescue JSON::ParserError => e raise TranslationError.new( "Failed to parse ChatGPT response", context: { error: e., body: response.body } ) end |
#make_chat_completion_request(messages) ⇒ 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 chat completion request to OpenAI API
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 114 def make_chat_completion_request() body = { model: MODEL, messages: , temperature: TEMPERATURE } headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{config.openai_key}" } make_request(:post, API_URL, body: body, headers: headers) end |
#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>
Translate multiple texts in a batch
68 69 70 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 68 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 39 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 = (text, target_lang_name) response = make_chat_completion_request() extract_translation(response) end rescue ApiError => e raise TranslationError.new( "Failed to translate text with ChatGPT: #{e.}", context: { text: text, target_lang: target_lang_code, original_error: e } ) end |