class I18n::Tasks::Translators::OpenAiTranslator
Constants
- BATCH_SIZE
-
max allowed texts per request
- DEFAULT_SYSTEM_PROMPT
- JSON_FORMAT_INSTRUCTIONS_SYSTEM_PROMPT
Public Class Methods
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 27 def initialize(*) begin require 'openai' rescue LoadError raise ::I18n::Tasks::CommandError, "Add gem 'ruby-openai' to your Gemfile to use this command" end super end
Calls superclass method
I18n::Tasks::Translators::BaseTranslator::new
Public Instance Methods
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 51 def no_results_error_message I18n.t('i18n_tasks.openai_translate.errors.no_results') end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 43 def options_for_html {} end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 47 def options_for_plain {} end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 36 def options_for_translate_values(from:, to:, **options) options.merge( from: from, to: to ) end
Private Instance Methods
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 61 def api_key @api_key ||= begin key = @i18n_tasks.translation_config[:openai_api_key] fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.openai_translate.errors.no_api_key') if key.blank? key end end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 115 def build_messages(values, from, to) [ { role: 'system', content: format(system_prompt, from: from, to: to) }, { role: 'user', content: "Translate this array: \n\n\n" }, { role: 'user', content: values.to_json } ] end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 70 def model @model ||= @i18n_tasks.translation_config[:openai_model].presence || 'gpt-4o-mini' end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 74 def system_prompt @system_prompt ||= (@i18n_tasks.translation_config[:openai_system_prompt].presence || DEFAULT_SYSTEM_PROMPT) .concat("\n#{JSON_FORMAT_INSTRUCTIONS_SYSTEM_PROMPT}") @system_prompt end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 95 def translate(values, from, to) response = translator.chat( parameters: { model: model, messages: build_messages(values, from, to), temperature: 0.0, response_format: { type: 'json_object' } } ) translations = response.dig('choices', 0, 'message', 'content') error = response['error'] fail "AI error: #{error}" if error.present? # Extract the array from the JSON object response result = JSON.parse(translations) result['translations'].to_json end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 81 def translate_values(list, from:, to:) results = [] list.each_slice(BATCH_SIZE) do |batch| translations = translate(batch, from, to) result = JSON.parse(translations) results << result @progress_bar.progress += result.size end results.flatten end
Source
# File lib/i18n/tasks/translators/openai_translator.rb, line 57 def translator @translator ||= OpenAI::Client.new(access_token: api_key, log_errors: true) end