class I18n::Tasks::Translators::OpenAiTranslator

Constants

BATCH_SIZE

max allowed texts per request

DEFAULT_SYSTEM_PROMPT

Public Class Methods

new(*) click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 23
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

Public Instance Methods

no_results_error_message() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 47
def no_results_error_message
  I18n.t('i18n_tasks.openai_translate.errors.no_results')
end
options_for_html() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 39
def options_for_html
  {}
end
options_for_plain() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 43
def options_for_plain
  {}
end
options_for_translate_values(from:, to:, **options) click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 32
def options_for_translate_values(from:, to:, **options)
  options.merge(
    from: from,
    to: to
  )
end

Private Instance Methods

api_key() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 57
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
model() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 66
def model
  @model ||= @i18n_tasks.translation_config[:openai_model].presence || 'gpt-3.5-turbo'
end
system_prompt() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 70
def system_prompt
  @system_prompt ||= @i18n_tasks.translation_config[:openai_system_prompt].presence || DEFAULT_SYSTEM_PROMPT
end
translate(values, from, to) click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 86
def translate(values, from, to)
  messages = [
    {
      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
    }
  ]

  response = translator.chat(
    parameters: {
      model: model,
      messages: messages,
      temperature: 0.0
    }
  )

  translations = response.dig('choices', 0, 'message', 'content')
  error = response['error']

  fail "AI error: #{error}" if error.present?

  translations
end
translate_values(list, from:, to:) click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 74
def translate_values(list, from:, to:)
  results = []

  list.each_slice(BATCH_SIZE) do |batch|
    translations = translate(batch, from, to)

    results << JSON.parse(translations)
  end

  results.flatten
end
translator() click to toggle source
# File lib/i18n/tasks/translators/openai_translator.rb, line 53
def translator
  @translator ||= OpenAI::Client.new(access_token: api_key)
end