Adapt to Context
Improving Quality with Context
Lara supports 2 forms of providing Context to improve translation quality.
- Internal Context (Default & Automatic): The context is inside the text to translate. No action is needed from the user, this is Lara default option. Lara automatically uses relevant parts of the full document as context when translating each sentence.
- External Context (Advanced): The context is external to text to translate. This feature allow you to pass a texts as context that does need translation but can help the translation. This has mostly three use cases:
- You want to translate a short title of a blog post but you also want to provide some of the post content to help the model disambiguate.
- You want to translate the last message of a chat providing as context the messages before in the right order.
- You want to integrate Lara into a CAT tool for professional translators where content has been already segmented in sentences and, when translating the current sentence, you want to provide the sentences around as context.
Good News
Unlike generic LLMs, the characters provided to Lara as context are not charged. This can lower the cost by an order of magnitude compared to using a generic LLMs.
Internal Context
By default, when translating a text longer than a sentence, Lara splits the text into sentences and translates in parallel all sentences using the context around each sentence as an extra model input.
With this approach Lara gets you both low-latency and context capabilities.
res = lara.translate("After finishing medical school, Barbara moved to a new city. The doctor has been practicing for five years now. Her patients appreciate her dedication.",
source='en-US', target='it-IT')
print(res.translation)
const res = await lara.translate("After finishing medical school, Barbara moved to a new city. The doctor has been practicing for five years now. Her patients appreciate her dedication.",
'en-US', 'it-IT');
console.log(res.translation)
TextResult result = lara.translate("After finishing medical school, Barbara moved to a new city. The doctor has been practicing for five years now. Her patients appreciate her dedication.",
"en-US", "it-IT");
System.out.println(result.getTranslation());
$result = $lara->translate('After finishing medical school, Barbara moved to a new city. The doctor has been practicing for five years now. Her patients appreciate her dedication.',
'en-US', 'it-IT');
echo $result->getTranslation();
The internal context is translated into Italian as "La dottoressa esercita da cinque anni." with an appropriate use of the context for "doctor", which is translated as "dottoressa".
External Context
Use TextBlock
objects to include some parts of the text as untranslated but context-providing segments.
res = lara.translate([
TextBlock(text="Adventure novels, mysteries, cookbooks—wait, who packed those?", translatable=False),
TextBlock(text="Suddenly, it doesn’t feel so deserted after all.", translatable=True),
TextBlock(text="Every page you turn is a new journey, and the best part?", translatable=False)
],
source="en",
target="it"
)
print(len(res.translation))
for textBlock in res.translation:
print(textBlock.text)
const res = await lara.translate([
{text: 'Adventure novels, mysteries, cookbooks—wait, who packed those?', translatable: false},
{text: 'Suddenly, it doesn’t feel so deserted after all.', translatable: true},
{text: 'Every page you turn is a new journey, and the best part?', translatable: false}
],
'en-US',
'it-IT'
);
console.log(res.translation.length)
for (const textBlock of res.translation) {
console.log(textBlock.text)
}
TextBlock[] textBlocks = {
new TextBlock("Adventure novels, mysteries, cookbooks—wait, who packed those?", false),
new TextBlock("Suddenly, it doesn’t feel so deserted after all.", true),
new TextBlock("Every page you turn is a new journey, and the best part?", false)
};
TextResult res = lara.translateBlocks(textBlocks, "en-US", "it-IT");
System.out.println(res.getTranslations().size());
for(String translation : res.getTranslations()) {
System.out.println(translation);
}
$res = $lara->translate(
array(
new TextBlock('Adventure novels, mysteries, cookbooks—wait, who packed those?', false),
new TextBlock('Suddenly, it doesn’t feel so deserted after all.', true),
new TextBlock('Every page you turn is a new journey, and the best part?', false),
),
'en-US',
'it-IT'
);
echo sizeof($res->getTranslation())."\n";
foreach ($res->getTranslation() as $translation) {
echo $translation."\n";
}
Only the second segment is translated, the others remain as they are, but provide context and aren't billed.
Updated 16 days ago