Translation · Translate text between languages1 / 3

Translate text between languages

Example on GitHub(packages/sdk/examples/translation/translation-bergamot.ts)

We're starting a new chapter on translation, and we're going to translate a string between two languages.

Bergamot is the on-device translation engine. Loading it looks like loading an LLM: pass a model constant, hand the engine a string, and get a translation back. Bergamot models are tiny (single-language pairs, around 30 MB), so they're cheap to keep in memory alongside other models.

Bergamot is a tiny neural translation model from Mozilla, single-language pairs around 30 MB each. The engine: "Bergamot" flag picks the right backend. The Bergamot load would look like the following:

const modelId = await loadModel({
  modelSrc: BERGAMOT_EN_FR,
  modelConfig: {
    engine: "Bergamot",
    from: "en",
    to: "fr",
    beamsize: 1,
  },
});

The inference step picks up where loadModel left off: hand the modelId to translate({ text }), await result.text, get the translated string. The two new flags are modelType (which addon to route the call to) and stream: false (sync, no duplex session):

const result = translate({
  modelId,
  text: "Hello, world.",
  modelType: "nmtcpp-translation",
  stream: false,
});
const translatedText = await result.text;
console.log(`EN -> FR: "${translatedText}"`);

The modelType: "nmtcpp-translation" flag tells the SDK which addon to route the call through. Without it, the SDK can't pick the right engine for translation vs transcription vs text generation.

Note: each translation model is a single language pair. For EN to DE you'd load BERGAMOT_EN_DE instead. The SDK doesn't translate between non-direct pairs without explicit pivot configuration.

Questions

Question 1 of 2

What is a key characteristic of each Bergamot model that limits BERGAMOT_EN_FR to English-French only?

Question 2 of 2

What does modelType: 'nmtcpp-translation' do on the translate() call?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.