Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openai translation - base_translator.rb:92:in `next': iteration reached an end (StopIteration) #525

Open
tobiasgr opened this issue Sep 27, 2023 · 8 comments

Comments

@tobiasgr
Copy link

Hi

When trying to execute the command "i18n-tasks translate-missing sv --from=en --backend=openai" (after realizing there was no support for dotenv variables), I keep getting the error below. I am running the latest commit with ref "249f0dd61b4e2a07befad96bed8e1f89b7527d3e", but also tried the one with the "openai" commit. Same problem.

How can I help debug this?

/Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:92:in `next': iteration reached an end (StopIteration)
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:92:in `parse_value'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:87:in `block in parse_value'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:87:in `map'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:87:in `parse_value'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:63:in `from_values'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:46:in `fetch_translations'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:35:in `block in translate_pairs'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:34:in `each'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:34:in `map'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:34:in `translate_pairs'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:17:in `block in translate_forest'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/data/tree/nodes.rb:16:in `each'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/data/tree/nodes.rb:16:in `each'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:16:in `inject'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translators/base_translator.rb:16:in `translate_forest'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/translation.rb:21:in `translate_forest'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/command/commands/missing.rb:51:in `translate_missing'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/command/commander.rb:26:in `run'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:51:in `block in run'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/gems/i18n-1.14.1/lib/i18n.rb:322:in `with_locale'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:49:in `run'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:18:in `block in start'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:211:in `auto_output_coloring'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:17:in `start'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/lib/i18n/tasks/cli.rb:11:in `start'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bundler/gems/i18n-tasks-249f0dd61b4e/bin/i18n-tasks:15:in `<top (required)>'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bin/i18n-tasks:25:in `load'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bin/i18n-tasks:25:in `<main>'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bin/ruby_executable_hooks:22:in `eval'
	from /Users/tobiasgroenlund/.rvm/gems/ruby-3.1.2/bin/ruby_executable_hooks:22:in `<main>'
@davidwessman
Copy link
Collaborator

Can you supply a minimal example to recreate it? How does the config file look?

@tobiasgr
Copy link
Author

To test it, I've created a new rails app (6.1.7.6), ruby (2.7.2), added ruby-openai and i18n-tasks. Copied a 300+ line en.yml file to the project and asked to translate it with:
i18n-tasks translate-missing da --from=en --backend=openai

It fails with the original error.

I comment-out all but 10 translatable lines in en.yml... it works.
reset da.yml
I comment-out all but 20 translatable lines in en.yml... it works.
reset da.yml
I comment-out all but 31 translatable lines in en.yml... it works.
reset da.yml
I comment-out all but 42 translatable lines in en.yml... it works.
reset da.yml
I comment-out all but 51 translatable lines in en.yml... it fails.
reset da.yml
I comment-out all but 42 translatable lines in en.yml that worked before... it fails.

Did some debugging in Openai code... it seems that the response from Openai sometimes removes duplicate translations, especially in the case of using the <<: syntax, where blocks are reused in multiple places. Input to Openai was x strings, but sometimes it only returned x-y strings, which is causing the error.

Example YAML:

---
en:
  account: Account
  account_details: Account details
  active_admin:
    dashboard: Dashboard
    move: Move
  activities:
    edit:
      image_change_order: You can change the order of the images. The top image will be the profile picture of the page and shown in the lists.
      image_important_text: NO NUDITY! Images has to be in landscape format, and not consist of multiple images put together.
      image_intro: Images speak a thousand words. It is important with some great images on your site. It makes your page interesting and affects peoples willingness to book your services.
      the_good_image: "The good image:"
      the_good_image_text: Images of people and the activity/event in action are the best. Crop the image so the most important part is centered, and in as high resolution as possible. And rather 1 great image than 3 mediocre ones.
    form: &form
      contact_details_header: Special contact details for this service
      contact_details_text: ONLY FILL OUT IF! standard contact details should not be used. Standard contact details can be set at your %{account_link}.
      duration_hint: Put same time in both fields if you do not want the time to be an interval. Possible display formats on the site is 1-2 (normal interval given), 1+ (only minimum interval set), or just 1 (value set in both fields).
      inorout_hint: Inside or outside?
      participants_max_hint: Max. kapacity of your service
      participants_min_hint: Minimum participants that makes sense (and they have to pay for)
      price_duration_header: Price, duration and participants
      price_duration_text: Fill out what makes sense for your service
      price_start_hint: Minimum payment for your service
      videos: Videos
      videos_text: Insert link to youtube or vimeo links here. Insert multiple by separating with comma.
    editform:
      <<: *form
    newform:
      <<: *form
  activity_can_be_booked_here: "%{name} can be booked here"
  activity_delivered_in_these_locations: "%{name} is available in"
  add_comment: Add comment

@jmeiss Have you had this issue?

@jmeiss
Copy link
Contributor

jmeiss commented Sep 29, 2023

I never faced this issue, probably because we don't use the <<: syntax in our i18n files, sorry :/

@tobiasgr
Copy link
Author

tobiasgr commented Sep 29, 2023

@jmeiss Thank you. I realize that the syntax is not really preserved in doin translations through this gem anyway, so probably a good choice 😅

A possible fix, that I got working for this kind of scenario, is changing the Openai model to "gpt-4", which keeps returning correct number of values no matter my yml setup... I am just not skilled enough to make a pull request to make the model a variable in i18n-tasks.yml instead of a hardcoded value in openai_translator.rb 😕

@davidwessman
Copy link
Collaborator

Unfortunately i18n-tasks does not support writing to YAML-files with anchors and references :/

@tobiasgr
Copy link
Author

@davidwessman Yeah, that was my conclusion as well. Will have to work around it, cause the times when I get this gem to work, it is pure magic 🥳

@jmeiss
Copy link
Contributor

jmeiss commented Sep 29, 2023

A possible fix, that I got working for this kind of scenario, is changing the Openai model to "gpt-4", which keeps returning correct number of values no matter my yml setup...

I'll keep it in mind if I work again on that code. I want to be able to let the user add a custom prompt to improve it based on your scenario. For instance, I noticed this morning that ChatGPT doesn't keep the pronouns. Example: "Vous" in French is sometimes translated as "tu" in Spanish which is not as formal.

@davidwessman
Copy link
Collaborator

Would be great if you could try to make a PR to improve the error message, otherwise I think we should close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants