Skip to content

Add automatic LLM-generated suggestions Machinery (pilot) - #4394

Merged
mathjazz merged 8 commits into
mozilla:mainfrom
mathjazz:4388-automatic-llm
Aug 12, 2026
Merged

mathjazz merged 8 commits into
mozilla:mainfrom
mathjazz:4388-automatic-llm

Conversation

@mathjazz

@mathjazz mathjazz commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Fix #4388.

Until now the LLM was pull-based: the translator opened the AI dropdown on the Google Translate row and picked an option, and the result replaced that row's text in place.

The suggestion is now generated automatically and shown as a row of its own in Machinery when all of the following conditions are met:

  1. The string does not have an approved translation.
  2. The user is authenticated.
  3. The locale is listed in OPENAI_AUTO_SUGGESTION_LOCALES.

On DEV, the locale list includes fi, it and sl:

The AI dropdown is unchanged and keeps working in every locale.

To support evaluation, the endpoint logs duration, cache hit and token counts, and copies from Machinery are logged for every source. The two are needed together: machinery_sources only records a source when the suggestion is saved unedited, and a cached response costs nothing but still counts as a suggestion shown.

OpenAIService now takes references as a list of {source, text} rather than a single machine translation, so that the reference can later be dropped, or extended with other Machinery results, without changing the request shape. A single reference renders exactly as before, leaving the prompt untouched for every caller that exists today. The cache key gains a prompt version, so a future prompt change cannot serve responses generated by the previous one.

Until now the LLM was pull-based: the translator opened the AI dropdown
on the Google Translate row and picked an option, and the result replaced
that row's text in place.

The suggestion is now generated automatically and shown as a row of its
own. It is limited to strings without an approved translation, to
authenticated users, and to the locales listed in
OPENAI_AUTO_SUGGESTION_LOCALES, so that spend stays bounded while the
approach is evaluated.

The AI dropdown is unchanged and keeps working in every locale.

To support evaluation, the endpoint logs duration, cache hit and token
counts, and copies from Machinery are logged for every source. The two
are needed together: machinery_sources only records a source when the
suggestion is saved unedited, and a cached response costs nothing but
still counts as a suggestion shown.

OpenAIService now takes references as a list of {source, text} rather
than a single machine translation, so that the reference can later be
dropped, or extended with other Machinery results, without changing the
request shape. A single reference renders exactly as before, leaving the
prompt untouched for every caller that exists today. The cache key gains
a prompt version, so a future prompt change cannot serve responses
generated by the previous one.
@mathjazz
mathjazz requested review from eemeli and flodolo August 12, 2026 05:55

@flodolo flodolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document OPENAI_AUTO_SUGGESTION_LOCALES

Comment thread pontoon/machinery/openai_service.py Resolved

## Machinery LLM Translation
## Shows the translation source for an LLM-generated suggestion.
machinery-LLMTranslation--translation-source = AI SUGGESTION

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
machinery-LLMTranslation--translation-source = AI SUGGESTION
machinery-LLMTranslation--translation-source = LLM SUGGESTION

Given the sentiment about using AI.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have "Google Translate", why not

Suggested change
machinery-LLMTranslation--translation-source = AI SUGGESTION
machinery-LLMTranslation--translation-source = OPENAI

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one.

The dropdown is also called "AI" and at least people understand what "AI" means.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, but the world has changed a lot since we first implemented this, 2 years ago. Maybe we need to revisit the AI dropdown as well, and it can be done in a follow-up.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't use OPENAI, we're painting ourselves in a corner if we ever want to change provider (and that will require changes on the code side, given the naming :-( ).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it not make sense for us to note what service we're calling into here? I mean, that's what we've been doing with MT providers thus far. And if we add or change providers, then yes, we would want to update the label.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to OPENAI CHATGPT as agreed on Slack.

In 06e2fa0 I also consolidate code references around the name OpenAI ChatGPT, which is consistent with other Machinery service names.

promises.push(
fetchGoogleTranslation(plain, locale).then(async (results) => {
addResults(results);
// LLM suggestion refines the Google Translate output, so it can

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should send the request without waiting for Google Translate, because that wait would bias the results of the experiment. This would lead to the dropdown having a different response, but I think that's OK.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a decision on what we want to pilot LLM as the translator or LLM as the refiner.

I see value in doing both:

  • It would be great to see how the LLM performs as a potential replacement for MT.
  • I use the Rephrase feature a lot, so having to manually request it via the dropdown would be a disappointment.

Also note that the server-side log already times only the LLM call, so the response-time metric isn't biased by the wait.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to see how the LLM performs as a potential replacement for MT.

I think that's the long term plan, given the discussion about lack of improvements on the side of locales supported by AutoML.

I use the Rephrase feature a lot, so having to manually request it via the dropdown would be a disappointment.

This assumes that the result would be drastically different, and I'm not sure that's the case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without hard data, it's hard to speculate what's the value of including MT in the prompt.

Also, when we fix #2723, we should be able to provide results more or less instantly.

Comment thread pontoon/machinery/views.py Resolved

@eemeli eemeli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature looks pretty good on dev. It's a bit odd that the throbber shows up below the Google Translate result, but when we get the response, it's put above it.

For extra spiffyness, if we do expect a delay in getting the response, it'd be nice if the OpenAI suggestion throbber was the same height as the Google Translate one. Then in most cases, we'd end up resizing content in the machinery panel only once.

Comment thread pontoon/machinery/views.py Outdated

def _parse_references(raw):
"""Parse the `references` POST parameter, raising ValueError if malformed."""
references = json.loads(raw or "[]")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
references = json.loads(raw or "[]")
if not raw:
return []
references = json.loads(raw)

Comment thread pontoon/machinery/views.py Outdated
Comment on lines +291 to +294
`references` is a JSON list of `{"source": …, "text": …}` objects to pass to
the model as reference. A list rather than a single machine translation, so
that other suggestions can be added, or the reference dropped entirely,
without changing the request shape.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS & Python both retain the source order for JSON objects, so this could also be a dict[str, list[str]] object.

Comment thread pontoon/machinery/views.py Outdated
Comment on lines +382 to +390
"llm_suggestion trigger=%s locale=%s characteristic=%s cache_hit=%s "
"duration_ms=%d prompt_tokens=%s completion_tokens=%s",
trigger,
locale.code,
characteristic,
"true" if result.cache_hit else "false",
round((time.monotonic() - started) * 1000),
result.prompt_tokens if result.prompt_tokens is not None else "",
result.completion_tokens if result.completion_tokens is not None else "",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be clearer to read as an f-string.

Comment thread pontoon/settings/base.py Outdated
Comment on lines +191 to +196
code
for code in (
c.strip()
for c in os.environ.get("OPENAI_AUTO_SUGGESTION_LOCALES", "").split(",")
)
if code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
code
for code in (
c.strip()
for c in os.environ.get("OPENAI_AUTO_SUGGESTION_LOCALES", "").split(",")
)
if code
code
for c in os.environ.get("OPENAI_AUTO_SUGGESTION_LOCALES", "").split(",")
if (code := c.strip())


## Machinery LLM Translation
## Shows the translation source for an LLM-generated suggestion.
machinery-LLMTranslation--translation-source = AI SUGGESTION

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have "Google Translate", why not

Suggested change
machinery-LLMTranslation--translation-source = AI SUGGESTION
machinery-LLMTranslation--translation-source = OPENAI

Comment on lines +232 to +237
[
{
source: 'google-translate',
text: results[0].translation,
},
],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we're not including any TM or human-provided suggestions here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #4400.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should include the AI rephrase dropdown for this too, as we do for Google Translate. Not to necessarily prompt usage, but to act as an indicator that this is machine-powered.

I know that's not the intent of the dropdown, but that's how I end up visually parsing it:

Image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dropdown acts on the content of Google Translate, it wouldn't in the case of the LLM suggestion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't we make it so that the dropdown on the OpenAI suggestion would work on the OpenAI translation contents?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the result should be the same?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're setting temperature to 0, threshold cutoff to 1, so the answer should be deterministic.

The prompt only instructs the LLM to use the translation as a reference, not to do something different.

Use the provided machine translation as a reference, but you are not bound by it — rewrite freely to achieve the best result.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a user point of view, there is no indication at all that the OpenAI suggestion is a rephrasing of the Google Translate suggestion. The two are presented as two different suggestions coming from completely separate systems, and therefore as a user I'd expect the "AI" actions on them to produce different results.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no indication at all that the OpenAI suggestion is a rephrasing of the Google Translate suggestion

It is not. In fact, this is a possible scenario

Screenshot 2026-08-12 alle 11 21 16

The string is using as additional context for the prompt.

  • You are asking to add an AI dropdown to the LLM suggestion.
  • I expect the result of a "rephrase" done for this suggestion to be the same string, and we're just wasting a call to the API.

as a user I'd expect the "AI" actions on them to produce different results

There is no guarantee that the current "rephrase" is different from Google MT, that's just not how the system was designed. I don't think we should have a different translation for the sake of having a different translation, the goal is to get the best translation the system can come up based on the context provided.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. In fact, this is a possible scenario
Screenshot 2026-08-12 alle 11 21 16

To me, the position of the dropdown looks like a bug there; I'd expect it to be at the far right end, rather than in the middle -- it's proposing actions on the suggestion, and if we're doing that, then how/why is it relevant to a user that the suggestion originally is coming from Google Translate, as opposed to any other source?

[...] I don't think we should have a different translation for the sake of having a different translation, the goal is to get the best translation the system can come up based on the context provided.

Definitely agreed. I'm not looking for difference for its own sake, but to make the capabilities we already have for one MT system be made available for all MT systems, including LLM-based ones.

From a translator's PoV, it should not matter at all how the "OpenAI" or "AI Suggestion" is generated -- the fact that it's partly using the Google Translate result is an implementation detail. Similarly, if it makes sense for us to allow a Google Translate suggestion to be rephrased or made more (in)formal, the same logic suggests that the functionality ought to make sense for other MT results as well.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #4401.

@flodolo

flodolo commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Am I wrong or we're not adding anything to the action log? How are we going to measure?

Copies from Machinery: Log copy events from Machinery using UXActionLog (for all sources), including locale data.

@mathjazz

Copy link
Copy Markdown
Collaborator Author

Am I wrong or we're not adding anything to the action log? How are we going to measure?

We are, here:
https://github.com/mozilla/pontoon/pull/4394/changes#diff-5e769d3b74b9585fa41d6248aeb04074ac531c86d2e63631100b8b5461915fb0R77

@flodolo

flodolo commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

I was looking at the wrong action log 😮‍💨

@flodolo flodolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to block on the whole AI vs LLM vs GPT, but it would be nice to pick a lane and stick to it without having to edit this code again in the near future.

@mathjazz
mathjazz requested a review from eemeli August 12, 2026 19:33
@mathjazz

Copy link
Copy Markdown
Collaborator Author

Addressed review comments and deployed to DEV.

@eemeli eemeli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks decent; see inline for some code style quibbles to sort out before merging.

Comment thread pontoon/machinery/openai_service.py Outdated
Comment on lines +105 to +109
if len(flat_references) == 1:
context_parts.append(
f"MACHINE TRANSLATION (for reference):\n{flat_references[0][1]}"
)
elif flat_references:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if...else should be a part of the match that's immediately below

Comment thread pontoon/machinery/views.py Outdated
Comment on lines +313 to +314
# Rejected rather than treated as `manual`, because an unrecognized
# value would otherwise skip the locale restriction below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless comment.

Comment thread pontoon/machinery/views.py Outdated
return JsonResponse({"translation": transformed_text})
duration_ms = round((time.monotonic() - started) * 1000)
cache_hit = "true" if result.cache_hit else "false"
prompt_tokens = result.prompt_tokens if result.prompt_tokens is not None else ""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prompt_tokens = result.prompt_tokens if result.prompt_tokens is not None else ""
prompt_tokens = result.prompt_tokens or ""

Comment thread pontoon/machinery/views.py Outdated
cache_hit = "true" if result.cache_hit else "false"
prompt_tokens = result.prompt_tokens if result.prompt_tokens is not None else ""
completion_tokens = (
result.completion_tokens if result.completion_tokens is not None else ""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result.completion_tokens if result.completion_tokens is not None else ""
result.completion_tokens or ""

Comment on lines +42 to +45
* Sort by descending quality; entries without a quality score keep the order
* they arrived in, which puts the LLM suggestion last: it is only requested
* once Google Translate has resolved, and takes seconds longer than any of the
* other services.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Sort by descending quality; entries without a quality score keep the order
* they arrived in, which puts the LLM suggestion last: it is only requested
* once Google Translate has resolved, and takes seconds longer than any of the
* other services.
* Sort by descending quality; entries without a quality score
* sort last, keeping their previous order.

@mathjazz
mathjazz merged commit 62ebf00 into mozilla:main Aug 12, 2026
12 checks passed
@mathjazz
mathjazz deleted the 4388-automatic-llm branch August 12, 2026 22:05
mathjazz added a commit to mathjazz/pontoon-scripts that referenced this pull request Aug 25, 2026
mathjazz added a commit that referenced this pull request Sep 22, 2026
Refine using AI dropdown was not enabled for composed multi-value suggestions
and automatic LLM suggestions added in #4394 were not available for composed
multi-value suggestions at all.

This patch adds an `openai-chatgpt-composed/` endpoint that pairs each leaf of a
composed suggestion with the English it came from and refines them all in one
request. One call rather than one per leaf, so the shared context (string ID,
comments, terminology) is sent once and the model keeps a single register across
parts the user sees together.

The AI dropdown moves out of the Google Translate source label into a shared
component at the end of the header's source list. All composed suggestions
get their own badge, including the TM-only ones.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pilot LLM-generated suggestions in Machinery

3 participants