discourse-translator/db/migrate/20250224120505_cleanup_ai_translations.rb
Natalie Tay 5f24835801
FIX: Do not return AI content if not in <output> (#223)
There is a bug now where if the new DiscourseAi translator does not return values back in `<translator>` tags, we return the full response.

This commit scopes both the input and output in its respective tags, and returns an empty string if there is no output tag. 

The problem only exists on topic translations, not post translations, as posts contain html and the prompt for translating indicates there will be html (topics do not have them). So an accompanying migration has been added

example incorrect result:
```
"To provide a translation, I first need the original text in a specific language to translate it into \"pt\". Could you please provide the text you wish to translate, including any HTML tags?"
```
2025-02-25 10:53:57 +08:00

16 lines
371 B
Ruby

# frozen_string_literal: true
class CleanupAiTranslations < ActiveRecord::Migration[7.2]
def up
execute <<~SQL
DELETE FROM discourse_translator_topic_translations
WHERE translation LIKE 'To%'
AND translation ILIKE '%translat%'
AND LENGTH(translation) > 100;
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end