Skip to content

A platform for community discussion. Free, open, simple.

FIX: Improve prompt and check returned value conforms to standard (#35763)

Natalie Tay5af43b64ce59
diff --git a/plugins/discourse-ai/lib/personas/locale_detector.rb b/plugins/discourse-ai/lib/personas/locale_detector.rb
index 4eb05a5add0..4dcf8e5208c 100644
--- a/plugins/discourse-ai/lib/personas/locale_detector.rb
+++ b/plugins/discourse-ai/lib/personas/locale_detector.rb
@@ -34,19 +34,22 @@ module DiscourseAi
5. Avoid using `und` and prefer `en` over `en-US` or `en-GB` unless the text specifically indicates a regional variant.
- Two example scenarios:
- Input: "Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?"
- Output: "en"
-
- Input: [quote]\nNon smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.\nOgni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.\n[/quote]\n¿Cuál es el mensaje principal de esta cita?
- Output: "es"
-
Important: Base your analysis solely on the provided text. Do not use any external information or make assumptions about the text's origin or context beyond what is explicitly provided.
- Your response must be a language code, and nothing else.
+ Your response must be a language code, and nothing else. Do not wrap your response in quotes or any other characters.
PROMPT
end
+ def examples
+ [
+ ["Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?", "en"],
+ [
+ "[quote]\nNon smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.\nOgni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.\n[/quote]\n¿Cuál es el mensaje principal de esta cita?",
+ "es",
+ ],
+ ]
+ end
+
def temperature
0
end
diff --git a/plugins/discourse-ai/lib/translation/category_locale_detector.rb b/plugins/discourse-ai/lib/translation/category_locale_detector.rb
index 544cf66788b..e8394a1b192 100644
--- a/plugins/discourse-ai/lib/translation/category_locale_detector.rb
+++ b/plugins/discourse-ai/lib/translation/category_locale_detector.rb
@@ -10,6 +10,8 @@ module DiscourseAi
return if text.blank?
detected_locale = LanguageDetector.new(text).detect
+ return if detected_locale.blank?
+
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
category.update_column(:locale, locale)
locale
diff --git a/plugins/discourse-ai/lib/translation/language_detector.rb b/plugins/discourse-ai/lib/translation/language_detector.rb
index c8323802fde..2262f68a8b2 100644
--- a/plugins/discourse-ai/lib/translation/language_detector.rb
+++ b/plugins/discourse-ai/lib/translation/language_detector.rb
@@ -3,6 +3,9 @@
module DiscourseAi
module Translation
class LanguageDetector
+ # reject non-language code responses by IETF language tag: https://datatracker.ietf.org/doc/html/rfc5646
+ LANGUAGE_TAG_REGEXP = /\A[A-Za-z]{2,4}(-[A-Za-z]{4})?(-([A-Za-z]{2}|[0-9]{3}))?\z/
+
DETECTION_CHAR_LIMIT = 1000
def initialize(text, topic: nil, post: nil)
@@ -49,7 +52,8 @@ module DiscourseAi
next if partial.strip.blank?
result << partial
end
- result
+
+ result.match?(LANGUAGE_TAG_REGEXP) ? result : nil
end
end
end
diff --git a/plugins/discourse-ai/lib/translation/post_locale_detector.rb b/plugins/discourse-ai/lib/translation/post_locale_detector.rb
index 4338ca61d07..83775b01c8e 100644
--- a/plugins/discourse-ai/lib/translation/post_locale_detector.rb
+++ b/plugins/discourse-ai/lib/translation/post_locale_detector.rb
@@ -12,6 +12,8 @@ module DiscourseAi
locale = SiteSetting.default_locale
else
detected_locale = LanguageDetector.new(text, post:).detect
+ return if detected_locale.blank?
+
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
end
diff --git a/plugins/discourse-ai/lib/translation/topic_locale_detector.rb b/plugins/discourse-ai/lib/translation/topic_locale_detector.rb
index 81ac302529b..080f786cded 100644
--- a/plugins/discourse-ai/lib/translation/topic_locale_detector.rb
+++ b/plugins/discourse-ai/lib/translation/topic_locale_detector.rb
@@ -7,6 +7,8 @@ module DiscourseAi
return if topic.blank?
detected_locale = LanguageDetector.new(topic.title.dup, topic:).detect
+ return if detected_locale.blank?
+
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
topic.update_column(:locale, locale)
locale
diff --git a/plugins/discourse-ai/spec/lib/translation/category_locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/category_locale_detector_spec.rb
index 3883552f51e..7b06c0cde12 100644
--- a/plugins/discourse-ai/spec/lib/translation/category_locale_detector_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/category_locale_detector_spec.rb
@@ -25,6 +25,14 @@ describe DiscourseAi::Translation::CategoryLocaleDetector do
expect(described_class.detect_locale(nil)).to eq(nil)
end
+ it "returns nil if detected locale is blank and does not update category" do
+ text = "#{category.name}\n\n#{category.description}"
+ language_detector_stub({ text: text, locale: nil })
+
+ expect(described_class.detect_locale(category)).to eq(nil)
+ expect { described_class.detect_locale(category) }.not_to change { category }
+ end
+
it "updates the category locale with the detected locale" do
text = "#{category.name}\n\n#{category.description}"
language_detector_stub({ text: text, locale: "zh_CN" })
diff --git a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
index dc2341a050a..a3095519772 100644
--- a/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
@@ -10,16 +10,31 @@ describe DiscourseAi::Translation::LanguageDetector do
before do
enable_current_plugin
assign_fake_provider_to(:ai_default_llm_model)
+ SiteSetting.ai_translation_enabled = true
end
describe ".detect" do
let(:locale_detector) { described_class.new("meow") }
- let(:llm_response) { "hur dur hur dur!" }
+ let(:llm_response) { "en-US" }
it "creates the correct prompt" do
+ expected_system_prompt = DiscourseAi::Personas::LocaleDetector.new.system_prompt
+
allow(DiscourseAi::Completions::Prompt).to receive(:new).with(
- persona.system_prompt,
- messages: [{ type: :user, content: "meow", id: "user" }],
+ expected_system_prompt,
+ messages: [
+ { type: :user, content: "Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?" },
+ { type: :model, content: "en" },
+ {
+ type: :user,
+ content:
+ "[quote]\nNon smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.\nOgni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.\n[/quote]\n¿Cuál es el mensaje principal de esta cita?",
+ },
+ { type: :model, content: "es" },
+ { type: :user, content: "meow" },
+ ],
+ post_id: nil,
+ topic_id: nil,
).and_call_original
DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do
@@ -27,34 +42,42 @@ describe DiscourseAi::Translation::LanguageDetector do
end
end
- it "sends the language detection prompt to the ai helper model" do
- mock_prompt = instance_double(DiscourseAi::Completions::Prompt)
- mock_llm = instance_double(DiscourseAi::Completions::Llm)
-
- structured_output =
- DiscourseAi::Completions::StructuredOutput.new({ locale: { type: "string" } })
- structured_output << { locale: llm_response }.to_json
-
- allow(DiscourseAi::Completions::Prompt).to receive(:new).and_return(mock_prompt)
- allow(DiscourseAi::Completions::Llm).to receive(:proxy).with(
- SiteSetting.ai_default_llm_model,
- ).and_return(mock_llm)
- allow(mock_llm).to receive(:generate).with(
- mock_prompt,
- user: Discourse.system_user,
- feature_name: "translation",
- response_format: persona.response_format,
- ).and_return(structured_output)
-
- locale_detector.detect
- end
-
it "returns the language from the llm's response in the language tag" do
DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do
locale_detector.detect
end
end
+ it "returns nil when the llm's response is not a valid language tag" do
+ DiscourseAi::Completions::Llm.with_prepared_responses(["not a language code"]) do
+ expect(locale_detector.detect).to eq(nil)
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([""]) do
+ expect(locale_detector.detect).to eq(nil)
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["1234"]) do
+ expect(locale_detector.detect).to eq(nil)
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["en-US-INCORRECT"]) do
+ expect(locale_detector.detect).to eq(nil)
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["en-US"]) do
+ expect(locale_detector.detect).to eq("en-US")
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["en"]) do
+ expect(locale_detector.detect).to eq("en")
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["sr-Latn"]) do
+ expect(locale_detector.detect).to eq("sr-Latn")
+ end
+ end
+
it "skips detection when provided blank text" do
blank_detector = described_class.new(" ")
allow(AiPersona).to receive(:find_by).and_call_original
diff --git a/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb
index d4d2af6ba1b..48120685f14 100644
--- a/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/post_locale_detector_spec.rb
@@ -18,6 +18,13 @@ describe DiscourseAi::Translation::PostLocaleDetector do
expect(described_class.detect_locale(nil)).to eq(nil)
end
+ it "returns nil if language detector returns nil and does not update post" do
+ language_detector_stub({ text: post.cooked, locale: nil, post: })
+
+ expect(described_class.detect_locale(post)).to eq(nil)
+ expect { described_class.detect_locale(post) }.not_to change { post }
+ end
+
it "updates the post locale with the detected locale" do
language_detector_stub({ text: post.cooked, locale: "zh_CN", post: })
expect { described_class.detect_locale(post) }.to change { post.reload.locale }.from(nil).to(
diff --git a/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb b/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb
index 8f4c7d6dd35..37ac2f7643a 100644
--- a/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/topic_locale_detector_spec.rb
@@ -20,6 +20,13 @@ describe DiscourseAi::Translation::TopicLocaleDetector do
expect(described_class.detect_locale(nil)).to eq(nil)
end
+ it "returns nil if language detector returns nil and does not update topic" do
+ language_detector_stub({ text: "This is a cat topic", locale: nil, topic: })
+
+ expect(described_class.detect_locale(topic)).to eq(nil)
+ expect { described_class.detect_locale(topic) }.not_to change { topic }
+ end
+
it "updates the topic locale with the detected locale" do
language_detector_stub({ text: "This is a cat topic", locale: "zh_CN", topic: })
expect { described_class.detect_locale(topic) }.to change { topic.reload.locale }.from(