class I18n::Tasks::Translators::WatsonxTranslator
Constants
- BATCH_SIZE
-
max allowed texts per request
- DEFAULT_SYSTEM_PROMPT
Public Instance Methods
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 42 def no_results_error_message I18n.t('i18n_tasks.watsonx_translate.errors.no_results') end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 34 def options_for_html {} end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 38 def options_for_plain {} end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 27 def options_for_translate_values(from:, to:, **options) options.merge( from: from, to: to ) end
Private Instance Methods
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 52 def api_key @api_key ||= begin key = @i18n_tasks.translation_config[:watsonx_api_key] fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.watsonx_translate.errors.no_api_key') if key.blank? key end end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 73 def model @model ||= @i18n_tasks.translation_config[:watsonx_model].presence || 'meta-llama/llama-3-2-90b-vision-instruct' end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 61 def project_id @project_id ||= begin project_id = @i18n_tasks.translation_config[:watsonx_project_id] if project_id.blank? fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.watsonx_translate.errors.no_project_id') end project_id end end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 77 def system_prompt @system_prompt ||= @i18n_tasks.translation_config[:watsonx_system_prompt].presence || DEFAULT_SYSTEM_PROMPT end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 95 def translate(values, from, to) prompt = [ '<|eot_id|><|start_header_id|>system<|end_header_id|>', format(system_prompt, from: from, to: to), '<|eot_id|><|start_header_id|>user<|end_header_id|>Translate this array:', "<|eot_id|><|start_header_id|>user<|end_header_id|>#{values.to_json}", '<|eot_id|><|start_header_id|>assistant<|end_header_id|>' ].join response = translator.generate_text( model_id: model, project_id: project_id, input: prompt, parameters: { decoding_method: :greedy, max_new_tokens: 2048, repetition_penalty: 1 } ) response.dig('results', 0, 'generated_text') end
Source
# File lib/i18n/tasks/translators/watsonx_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 += results.size end results.flatten end
Source
# File lib/i18n/tasks/translators/watsonx_translator.rb, line 48 def translator @translator ||= WatsonxClient.new(key: api_key) end