Open-source inference server and production cluster for all the models your agent needs.
Python bgecolbertdata-pipelinedeep-learningembeddingsinferenceinference-serverinformation-retrievalllmmlmlopsnatural-language-processingnlppythonrerankingretrievalretrieval-augmented-generationsemantic-searchspladevector-search
fix(ctranslate2): rejoin French elision only for a French target
Shai Shmuel4488cc041e30
diff --git a/packages/sie_server/src/sie_server/adapters/ctranslate2/_moses_entities.py b/packages/sie_server/src/sie_server/adapters/ctranslate2/_moses_entities.pyindex 9edbbceb9..e0a89e70e 100644--- a/packages/sie_server/src/sie_server/adapters/ctranslate2/_moses_entities.py+++ b/packages/sie_server/src/sie_server/adapters/ctranslate2/_moses_entities.py@@ -9,11 +9,12 @@ The repair is deliberately conservative:* An entity is only unescaped when the input text does not contain itliterally. A request that really translates ``&`` keeps it.-* Spacing is only repaired where the form is unambiguous: French elision- (``l ' offre`` becomes ``l'offre``) and balanced, space-delimited+* Spacing is only repaired where the form is unambiguous: French elision,+ when the prompt targets French (``<2fr>``, ``<2fr_CA>``), so+ ``l ' offre`` becomes ``l'offre``; and balanced, space-delimited``"`` pairs (``" Bonjour "`` becomes ``"Bonjour"``).Everything else is unescaped in place and keeps its spaces, so- ``M & M`` becomes ``M & M``.+ ``R & D`` becomes ``R & D``."""from __future__ import annotations@@ -41,11 +42,15 @@ _QUOT = """# c', d', j', l', m', n', s', t', the qu' family (qu', jusqu', lorsqu',# quelqu'...) and aujourd'hui. Only the elided word and the word after it# decide the join, so a single-quoted span such as ``dit ' oui '``-# is left spaced.+# is left spaced, while a quoted single letter that is itself an elided form+# (``' l ' est``) is joined to the word after it. The join only runs+# when the prompt targets French._ELISION = re.compile(r"(?<!\w)([cdjlmnst]|[^\W\d_]*qu|aujourd) ' (?=[^\W\d_])",re.IGNORECASE,)+# MADLAD-400's target tag for French and its regional forms.+_FRENCH_TARGET = re.compile(r"\s*<2fr(?:_[A-Za-z]+)?>")def unescape_moses_entities(text: str, source: str) -> str:@@ -53,13 +58,14 @@ def unescape_moses_entities(text: str, source: str) -> str:Args:text: Decoded model output.- source: The input text the output was generated from. Entities that- occur literally in it are kept as they are.+ source: The input text the output was generated from, target tag+ included. Entities that occur literally in it are kept as they+ are, and French elision is only repaired for a French target tag."""escaped = frozenset(entity for entity in _ENTITIES if entity in text and entity not in source)if not escaped:return text- if _APOS in escaped:+ if _APOS in escaped and _FRENCH_TARGET.match(source):text = _ELISION.sub(r"\1'", text)if _QUOT in escaped:text = _attach_quote_pairs(text)@@ -71,9 +77,9 @@ def _attach_quote_pairs(text: str) -> str:The tokenizer turns ``"word"`` into ``" word "``. The output isonly respaced when every escaped quote can be paired in order, each pair- encloses text with exactly one space on either side, an opening quote does- not follow a word character directly and a closing quote does not precede- one. A literal ``"`` elsewhere in the output, an odd count or any other+ encloses text with exactly one space on either side, an opening quote+ starts the text or follows whitespace and a closing quote does not precede+ a word character. A literal ``"`` elsewhere in the output, an odd count or any othershape leaves the spacing as it is."""if '"' in text:diff --git a/packages/sie_server/src/sie_server/adapters/ctranslate2/generation.py b/packages/sie_server/src/sie_server/adapters/ctranslate2/generation.pyindex 791333395..1e4fd6ea3 100644--- a/packages/sie_server/src/sie_server/adapters/ctranslate2/generation.py+++ b/packages/sie_server/src/sie_server/adapters/ctranslate2/generation.py@@ -28,7 +28,7 @@ from sie_server.adapters._generation_base import ()from sie_server.adapters._spec import AdapterSpecfrom sie_server.adapters._types import ERR_NOT_LOADED, ComputePrecision-from sie_server.adapters.ctranslate2._moses_entities import unescape_moses_entities+from sie_server.adapters.ctranslate2._moses_entities import unescape_moses_entities as _unescape_moses_entitiesfrom sie_server.types.grammar import GrammarSpecfrom sie_server.types.inputs import ImageInput, VideoInput@@ -616,7 +616,7 @@ class CTranslate2GenerationAdapter(GenerationAdapter):output_tokens = result_tokens[:-1] if ended else result_tokenstext = self._decode_tokens(output_tokens)if self._unescape_moses_entities:- text = unescape_moses_entities(text, request.prepared.prompt)+ text = _unescape_moses_entities(text, request.prepared.prompt)completion_tokens = len(output_tokens)finish_reason: Literal["stop", "length"] = "stop" if ended else "length"request.loop.call_soon_threadsafe(request.complete, text, completion_tokens, finish_reason)diff --git a/packages/sie_server/tests/adapters/test_ctranslate2_generation.py b/packages/sie_server/tests/adapters/test_ctranslate2_generation.pyindex 1d5d598f3..3682662ea 100644--- a/packages/sie_server/tests/adapters/test_ctranslate2_generation.py+++ b/packages/sie_server/tests/adapters/test_ctranslate2_generation.py@@ -38,6 +38,8 @@ class _FakeTokenizer:" ami"," &"," co",+ "<2fr>",+ "<2en>",]self.eos_token_id = 1@@ -362,9 +364,11 @@ _ESCAPED_OUTPUT = ["l", " '", " ami", " &", " co"]@pytest.mark.parametrize(("unescape_moses_entities", "prompt", "expected"),[- (False, "hello world", "l ' ami & co"),- (True, "hello world", "l'ami & co"),- (True, "hello & world", "l'ami & co"),+ (False, "<2fr> hello world", "l ' ami & co"),+ (True, "<2fr> hello world", "l'ami & co"),+ (True, "<2fr> hello & world", "l'ami & co"),+ # Elision is only rejoined for a French target tag.+ (True, "<2en> hello world", "l ' ami & co"),],)def test_output_unescapes_moses_entities_only_when_enabled(diff --git a/packages/sie_server/tests/adapters/test_ctranslate2_moses_entities.py b/packages/sie_server/tests/adapters/test_ctranslate2_moses_entities.pyindex 18ab11342..96e9379e4 100644--- a/packages/sie_server/tests/adapters/test_ctranslate2_moses_entities.py+++ b/packages/sie_server/tests/adapters/test_ctranslate2_moses_entities.py@@ -87,6 +87,21 @@ def test_entities_present_in_the_source_are_kept() -> None:assert unescape_moses_entities("l ' ami de Tom & Jerry", source) == "l'ami de Tom & Jerry"+def test_elision_is_only_repaired_for_a_french_target() -> None:+ escaped = "Rock ' n ' Roll"++ assert unescape_moses_entities(escaped, "<2en> Rock and roll") == "Rock ' n ' Roll"+ assert unescape_moses_entities("l ' ami", "<2fr_CA> the friend") == "l'ami"+++def test_a_quoted_elided_letter_is_joined_like_an_elision() -> None:+ # Pinned on purpose: a quoted single letter that is an elided form is+ # indistinguishable from an elision, so it joins the word after it.+ escaped = "la lettre ' l ' est muette"++ assert unescape_moses_entities(escaped, _SOURCE) == "la lettre ' l'est muette"++def test_apostrophe_present_in_the_source_keeps_its_spacing() -> None:source = "<2fr> Say ' hello"