Repository navigation
Add automatic LLM-generated suggestions Machinery (pilot) - #4394
Conversation
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.
flodolo
left a comment
There was a problem hiding this comment.
We need to document OPENAI_AUTO_SUGGESTION_LOCALES
|
|
||
| ## Machinery LLM Translation | ||
| ## Shows the translation source for an LLM-generated suggestion. | ||
| machinery-LLMTranslation--translation-source = AI SUGGESTION |
There was a problem hiding this comment.
| machinery-LLMTranslation--translation-source = AI SUGGESTION | |
| machinery-LLMTranslation--translation-source = LLM SUGGESTION |
Given the sentiment about using AI.
There was a problem hiding this comment.
As we have "Google Translate", why not
| machinery-LLMTranslation--translation-source = AI SUGGESTION | |
| machinery-LLMTranslation--translation-source = OPENAI |
There was a problem hiding this comment.
Not sure about this one.
The dropdown is also called "AI" and at least people understand what "AI" means.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :-( ).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
eemeli
left a comment
There was a problem hiding this comment.
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.
|
|
||
| def _parse_references(raw): | ||
| """Parse the `references` POST parameter, raising ValueError if malformed.""" | ||
| references = json.loads(raw or "[]") |
There was a problem hiding this comment.
| references = json.loads(raw or "[]") | |
| if not raw: | |
| return [] | |
| references = json.loads(raw) |
| `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. |
There was a problem hiding this comment.
JS & Python both retain the source order for JSON objects, so this could also be a dict[str, list[str]] object.
| "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 "", |
There was a problem hiding this comment.
This would be clearer to read as an f-string.
| code | ||
| for code in ( | ||
| c.strip() | ||
| for c in os.environ.get("OPENAI_AUTO_SUGGESTION_LOCALES", "").split(",") | ||
| ) | ||
| if code |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
As we have "Google Translate", why not
| machinery-LLMTranslation--translation-source = AI SUGGESTION | |
| machinery-LLMTranslation--translation-source = OPENAI |
| [ | ||
| { | ||
| source: 'google-translate', | ||
| text: results[0].translation, | ||
| }, | ||
| ], |
There was a problem hiding this comment.
Is there a reason why we're not including any TM or human-provided suggestions here?
There was a problem hiding this comment.
There was a problem hiding this comment.
The dropdown acts on the content of Google Translate, it wouldn't in the case of the LLM suggestion.
There was a problem hiding this comment.
Why wouldn't we make it so that the dropdown on the OpenAI suggestion would work on the OpenAI translation contents?
There was a problem hiding this comment.
Because the result should be the same?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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.
|
Am I wrong or we're not adding anything to the action log? How are we going to measure?
|
We are, here: |
|
I was looking at the wrong action log 😮💨 |
flodolo
left a comment
There was a problem hiding this comment.
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.
…atGPT That's to make them consistent with other Machinery sources. Includes a data migration, so historical machinery_sources attributions are rewritten rather than left under the old name.
|
Addressed review comments and deployed to DEV. |
eemeli
left a comment
There was a problem hiding this comment.
Looks decent; see inline for some code style quibbles to sort out before merging.
| if len(flat_references) == 1: | ||
| context_parts.append( | ||
| f"MACHINE TRANSLATION (for reference):\n{flat_references[0][1]}" | ||
| ) | ||
| elif flat_references: |
There was a problem hiding this comment.
This if...else should be a part of the match that's immediately below
| # Rejected rather than treated as `manual`, because an unrecognized | ||
| # value would otherwise skip the locale restriction below. |
| 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 "" |
There was a problem hiding this comment.
| prompt_tokens = result.prompt_tokens if result.prompt_tokens is not None else "" | |
| prompt_tokens = result.prompt_tokens or "" |
| 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 "" |
There was a problem hiding this comment.
| result.completion_tokens if result.completion_tokens is not None else "" | |
| result.completion_tokens or "" |
| * 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. |
There was a problem hiding this comment.
| * 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. |
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.

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:
OPENAI_AUTO_SUGGESTION_LOCALES.On DEV, the locale list includes
fi,itandsl: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_sourcesonly records a source when the suggestion is saved unedited, and a cached response costs nothing but still counts as a suggestion shown.OpenAIServicenow 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.