QVAC Logo
Usage ExamplesAI tasks

Translation

Text-to-text neural machine translation (NMT) — i.e., translate text between different languages.

Overview

Translation uses your choice of either nmt.cpp or Bergamot as inference engine. Load any supported model using modelType: "nmt", and modelConfig.engine: "Bergamot" for Bergamot.

Translation input is defined by:

  • from: string: source language id (e.g., "en")
  • to: string: target language id
  • text: string | string[]: text to be translated

translate() returns an object containing text and when streaming is enabled, a tokenStream for real-time output.

For a list of supported languages and their ids (string abbreviations), see qvac-sdk/schemas/translation-config.ts.

Functions

Use the following sequence of function calls:

  1. loadModel()
  2. translate()
  3. unloadModel()

For how to use each function, see SDK — API reference.

Models

You should load a model compatible with your chosen inference engine:

  • nmt.cpp (default): Bergamot or IndicTrans2. Bergamot uses intgemm *.bin + *.spm vocab files; IndicTrans2 uses GGML *.bin.
  • Bergamot: Bergamot model bundle. Required files: model *.bin + vocab*.spm.

For models available as constants, see SDK — Models.

Example

The following script shows an example of translation:

translation.js
import { loadModel, translate, unloadModel, MARIAN_OPUS_EN_IT_Q4_0, } from "@qvac/sdk";
try {
    const modelId = await loadModel({
        modelSrc: MARIAN_OPUS_EN_IT_Q4_0,
        modelType: "nmt",
        modelConfig: {
            engine: "Opus",
            from: "en",
            to: "it",
        },
    });
    console.log(`✅ Model loaded: ${modelId}`);
    const text = "Hello, how are you today? I hope you are having a wonderful day!";
    console.log("\n--- Streaming Translation ---");
    const streamResult = translate({
        modelId,
        text,
        modelType: "nmt",
        stream: true,
    });
    process.stdout.write("Translated text EN -> IT: ");
    for await (const token of streamResult.tokenStream) {
        process.stdout.write(token);
    }
    console.log();
    const stats = await streamResult.stats;
    if (stats) {
        console.log(`Processing stats:`, stats);
    }
    await unloadModel({ modelId, clearStorage: false });
}
catch (error) {
    console.error("❌ Error:", error);
    process.exit(1);
}

Tip: all examples throughout this documentation are self-contained and runnable. For instructions on how to run them, see SDK quickstart.

On this page