diff --git a/app/services/discourse_ai/translator.rb b/app/services/discourse_ai/translator.rb index ff2cba1b..53a2051b 100644 --- a/app/services/discourse_ai/translator.rb +++ b/app/services/discourse_ai/translator.rb @@ -3,11 +3,13 @@ module DiscourseAi class Translator PROMPT_TEMPLATE = <<~TEXT.freeze - You are a highly skilled linguist and web programmer, with expertise in many languages, and very well versed in HTML. + You are a highly skilled linguist of many languages and have expert knowledge in HTML. Your task is to identify the language of the text I provide and accurately translate it into this language locale "%{target_language}" while preserving the meaning, tone, and nuance of the original text. - The text will contain html tags, which must absolutely be preserved in the translation. + The text may or may not contain html tags. If they do, preserve them. Maintain proper grammar, spelling, and punctuation in the translated version. - Wrap the translated text in a tag. + You will find the text between XML tags. + Include your translation between XML tags. + Do not write explanations. TEXT def initialize(text, target_language) @@ -19,7 +21,7 @@ def translate prompt = DiscourseAi::Completions::Prompt.new( build_prompt(@target_language), - messages: [{ type: :user, content: @text, id: "user" }], + messages: [{ type: :user, content: "#{@text}", id: "user" }], ) llm_translation = @@ -29,7 +31,7 @@ def translate feature_name: "translator-translate", ) - (Nokogiri::HTML5.fragment(llm_translation).at("translation")&.inner_html || llm_translation) + (Nokogiri::HTML5.fragment(llm_translation).at("output")&.inner_html || "").strip end private diff --git a/db/migrate/20250224120505_cleanup_ai_translations.rb b/db/migrate/20250224120505_cleanup_ai_translations.rb new file mode 100644 index 00000000..55f7dc6d --- /dev/null +++ b/db/migrate/20250224120505_cleanup_ai_translations.rb @@ -0,0 +1,16 @@ +# 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 diff --git a/spec/services/discourse_ai/translator_spec.rb b/spec/services/discourse_ai/translator_spec.rb index d3f10c7d..e78d3da3 100644 --- a/spec/services/discourse_ai/translator_spec.rb +++ b/spec/services/discourse_ai/translator_spec.rb @@ -17,13 +17,15 @@ it "creates the correct prompt" do allow(DiscourseAi::Completions::Prompt).to receive(:new).with( <<~TEXT, - You are a highly skilled linguist and web programmer, with expertise in many languages, and very well versed in HTML. + You are a highly skilled linguist of many languages and have expert knowledge in HTML. Your task is to identify the language of the text I provide and accurately translate it into this language locale "de" while preserving the meaning, tone, and nuance of the original text. - The text will contain html tags, which must absolutely be preserved in the translation. + The text may or may not contain html tags. If they do, preserve them. Maintain proper grammar, spelling, and punctuation in the translated version. - Wrap the translated text in a tag. + You will find the text between XML tags. + Include your translation between XML tags. + Do not write explanations. TEXT - messages: [{ type: :user, content: text_to_translate, id: "user" }], + messages: [{ type: :user, content: "cats are great", id: "user" }], ).and_call_original described_class.new(text_to_translate, target_language).translate @@ -48,7 +50,7 @@ it "returns the translation from the llm's response in the translation tag" do DiscourseAi::Completions::Llm.with_prepared_responses( - ["hur dur hur dur!"], + ["hur dur hur dur!"], ) do expect( described_class.new(text_to_translate, target_language).translate, @@ -56,11 +58,9 @@ end end - it "returns the raw response if the translation tag is not present" do + it "returns an empty string if the translation tag is not present" do DiscourseAi::Completions::Llm.with_prepared_responses(["raw response."]) do - expect( - described_class.new(text_to_translate, target_language).translate, - ).to eq "raw response." + expect(described_class.new(text_to_translate, target_language).translate).to eq "" end end end diff --git a/spec/services/discourse_ai_spec.rb b/spec/services/discourse_ai_spec.rb index e46b4b4b..a2979c56 100644 --- a/spec/services/discourse_ai_spec.rb +++ b/spec/services/discourse_ai_spec.rb @@ -42,7 +42,7 @@ it "translates the post and returns [locale, translated_text]" do DiscourseAi::Completions::Llm.with_prepared_responses( - ["some translated text"], + ["some translated text"], ) do locale, translated_text = DiscourseTranslator::DiscourseAi.translate(post) expect(locale).to eq "de"