Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion pontoon/machinery/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
from django.core.cache import cache
from django.core.management import call_command

from pontoon.base.models import Resource
from pontoon.machinery.utils import (
get_google_automl_translation,
get_llm_string_id,
get_machinery_service_cache_key,
)
from pontoon.test.factories import LocaleFactory
from pontoon.test.factories import (
EntityFactory,
LocaleFactory,
ResourceFactory,
SectionFactory,
)


@pytest.fixture
Expand Down Expand Up @@ -94,3 +101,64 @@ def test_warmup_automl_command_bypasses_cache(automl_project_id, automl_locale):

# The warmup made a real request even though a cached value was present.
assert client.translate_text.call_count == 1


@pytest.mark.django_db
@pytest.mark.parametrize(
"format, section_key, entity_key, expected",
[
# Formats without a section key
(Resource.Format.PROPERTIES, [], ["open-browser"], "open-browser"),
# Formats with the file name or section name as the section key
(
Resource.Format.XLIFF,
["Client/Client.strings"],
["Client/Client.strings", "Settings.Title"],
"Settings.Title",
),
(
Resource.Format.XCODE,
["Localizable.strings"],
["Localizable.strings", "Settings.Title"],
"Settings.Title",
),
(Resource.Format.INI, ["Strings"], ["Strings", "label"], "label"),
(Resource.Format.ANDROID, ["!ENTITY"], ["!ENTITY", "brandName"], "brandName"),
# Parts identical to the source string are dropped: the id in XLIFF and
# XCODE files, or the message id in gettext, optionally followed by the
# message context
(
Resource.Format.XCODE,
["Localizable.strings"],
["Localizable.strings", "Open browser"],
None,
),
(Resource.Format.GETTEXT, [], ["Open browser"], None),
(Resource.Format.GETTEXT, [], ["Open browser", "toolbar"], "toolbar"),
# Nested keys are joined
(
Resource.Format.PLAIN_JSON,
[],
["toolbar", "open-browser"],
"toolbar.open-browser",
),
# Missing key
(Resource.Format.PROPERTIES, [], [], None),
],
)
def test_get_llm_string_id(format, section_key, entity_key, expected):
resource = ResourceFactory(format=format)
section = SectionFactory(key=section_key, resource=resource)
entity = EntityFactory(
key=entity_key, string="Open browser", resource=resource, section=section
)

assert get_llm_string_id(entity) == expected


@pytest.mark.django_db
def test_get_llm_string_id_without_section():
resource = ResourceFactory(format=Resource.Format.PROPERTIES)
entity = EntityFactory(key=["open-browser"], resource=resource, section=None)

assert get_llm_string_id(entity) == "open-browser"
65 changes: 63 additions & 2 deletions pontoon/machinery/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from pontoon.base.models import (
Entity,
Locale,
Resource,
)
from pontoon.test.factories import (
EntityFactory,
ProjectLocaleFactory,
ResourceFactory,
SectionFactory,
TeamCommentFactory,
TermFactory,
Expand Down Expand Up @@ -319,12 +321,13 @@ def test_view_gpt_transform_context(member, locale_a, openai_api_key):

# Create entity with full context: key, comment, group (section) comment,
# resource comment
section = SectionFactory(key=["nav"], comment="Navigation section")
resource = ResourceFactory(path="ui.properties", format=Resource.Format.PROPERTIES)
section = SectionFactory(key=[], comment="Navigation section", resource=resource)
entity = EntityFactory(
key=["open-browser"],
string="Open browser",
comment="Button label",
resource=section.resource,
resource=resource,
section=section,
)
entity.resource.comment = "Main UI file"
Expand Down Expand Up @@ -374,6 +377,64 @@ def test_view_gpt_transform_context(member, locale_a, openai_api_key):
assert '"browser" (noun) → "navigateur"' in user_message


@pytest.mark.django_db
@pytest.mark.parametrize(
"format, section_key, entity_key, expected",
[
# For XLIFF (e.g. iOS projects), the section key is the file name
(
Resource.Format.XLIFF,
["Client/Client.strings"],
["Client/Client.strings", "Settings.Title"],
"STRING ID:\nSettings.Title",
),
# For gettext, the message id is the source string itself
(Resource.Format.GETTEXT, [], ["Open browser"], None),
],
)
def test_view_gpt_transform_string_id(
member, locale_a, openai_api_key, format, section_key, entity_key, expected
):
url = reverse("pontoon.gpt_transform")
cache.clear()

mock_response = MagicMock()
mock_response.choices[0].message.content = "translated"

resource = ResourceFactory(format=format)
section = SectionFactory(key=section_key, resource=resource)
entity = EntityFactory(
key=entity_key,
string="Open browser",
resource=resource,
section=section,
)

with patch("pontoon.machinery.openai_service.OpenAI") as MockOpenAI:
MockOpenAI.return_value.chat.completions.create.return_value = mock_response

member.client.post(
url,
{
"english_text": "Open browser",
"translated_text": "Ouvrir le navigateur",
"characteristic": "formal",
"locale": locale_a.code,
"entity_pk": entity.pk,
},
)

call_args = MockOpenAI.return_value.chat.completions.create.call_args
system_message = call_args.kwargs["messages"][0]["content"]
user_message = call_args.kwargs["messages"][1]["content"]
if expected is None:
assert "STRING ID" not in user_message
assert "STRING ID" not in system_message
else:
assert expected in user_message
assert "STRING ID:" in system_message


@pytest.mark.django_db
def test_view_caighdean(client, entity_a):
gd = Locale.objects.get(code="gd")
Expand Down
25 changes: 24 additions & 1 deletion pontoon/machinery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
from django.db.models import Q
from django.db.models.functions import JSONObject

from pontoon.base.models import Locale, Project, ProjectLocale, TranslationMemoryEntry
from pontoon.base.models import (
Entity,
Locale,
Project,
ProjectLocale,
TranslationMemoryEntry,
)
from pontoon.base.placeables import get_placeables
from pontoon.base.utils import get_search_phrases

Expand All @@ -42,6 +48,23 @@ def set_machinery_service_cache_key(key, value):
cache.set(key, value, settings.MACHINERY_SERVICE_CACHE_TIMEOUT)


def get_llm_string_id(entity: Entity) -> str | None:
"""
String identifier to pass to the LLM prompt as additional context,
or None if the entity doesn't have a meaningful one.
"""
key = entity.key
section_key = entity.section.key if entity.section_id is not None else []
# Entity.key is the section key followed by the entry's own id, so drop
# the section part.
if section_key and key[: len(section_key)] == section_key:
key = key[len(section_key) :]
# Drop parts that only repeat the source string: e.g. the msgid in gettext,
# or ids in XLIFF and XCODE files that are set to the source string.
key = [part for part in key if part != entity.string]
return ".".join(key) or None


def get_google_translate_data(text, locale, format="text", preserve_placeables=False):
translation = (
get_google_automl_translation(text, locale, format, preserve_placeables)
Expand Down
3 changes: 2 additions & 1 deletion pontoon/machinery/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pontoon.machinery.utils import (
get_concordance_search_data,
get_google_translate_data,
get_llm_string_id,
get_microsoft_translator_data,
get_translation_memory_data,
)
Expand Down Expand Up @@ -290,7 +291,7 @@ def gpt_transform(request):
entity = Entity.objects.select_related("resource", "section").get(
pk=entity_pk
)
entity_key = entity.key[0] if entity.key else None
entity_key = get_llm_string_id(entity)
entity_comment = entity.comment or None
group_comment = (entity.section.comment if entity.section else None) or None
resource_comment = entity.resource.comment or None
Expand Down
Loading