Repository navigation
Pass better string ID to LLM Prompt for XLIFF and Gettext #4383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,31 @@ | |
| from django.conf import settings | ||
| from django.core.cache import cache | ||
|
|
||
| from pontoon.base.models import Entity, Resource | ||
| from pontoon.machinery.utils import ( | ||
| get_machinery_service_cache_key, | ||
| set_machinery_service_cache_key, | ||
| ) | ||
|
|
||
|
|
||
| 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) :] | ||
| if entity.resource.format == Resource.Format.GETTEXT: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we skip the ID when it equals entity.string, regardless of format? It's also the case for at least some XCODE/XLIFF files.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, didn't think of those. |
||
| # For gettext, don't include the msgid as it's the same as source text. | ||
| # key is (msgid,) or (msgid, msgctxt) | ||
| key = key[1:] | ||
| return ".".join(key) or None | ||
|
|
||
|
|
||
| class OpenAIService: | ||
| def __init__(self): | ||
| if not settings.OPENAI_API_KEY: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import pytest | ||
|
|
||
| from pontoon.base.models import Resource | ||
| from pontoon.machinery.openai_service import get_llm_string_id | ||
| from pontoon.test.factories import ( | ||
| EntityFactory, | ||
| ResourceFactory, | ||
| SectionFactory, | ||
| ) | ||
|
|
||
|
|
||
| @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"), | ||
| # For gettext, the message id is the source string, optionally | ||
| # followed by the message context | ||
| (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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this file is service-specific, shall we move this function outside of it, e.g.
utils.py?