# Translation (/ai-capabilities/translation)



## Overview

Translation uses your choice of either [`qvac-fabric-llm.cpp`](https://github.com/tetherto/qvac-fabric-llm.cpp) or [Bergamot](https://browser.mt) 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](https://github.com/tetherto/qvac/blob/main/packages/sdk/schemas/translation-config.ts).

## Functions

Use the following sequence of function calls:

1. [`loadModel()`](/reference/api#loadmodel)
2. [`translate()`](/reference/api#translate)
3. [`unloadModel()`](/reference/api#unloadmodel)

For how to use each function, see [SDK — API reference](/reference/api/).

## Models

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

* `qvac-fabric-llm.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](/introduction#models).

## Example

The following script shows an example of translation:

<Tabs>
  <Tab value="js" label="JavaScript" default>
    <WrapCode>
      ```js file=<rootDir>/packages/sdk/dist/examples/translation/translation-stream.js title="translation.js" lineNumbers
      import { loadModel, translate, unloadModel, BERGAMOT_EN_ES } from "@qvac/sdk";
      try {
          const modelId = await loadModel({
              modelSrc: BERGAMOT_EN_ES,
              modelConfig: {
                  engine: "Bergamot",
                  from: "en",
                  to: "es",
              },
          });
          console.log(`▸ Model loaded: ${modelId}`);
          const text = "Hello, how are you today? I hope you are having a wonderful day!";
          console.log("▸ Streaming Translation");
          const streamResult = translate({
              modelId,
              text,
              modelType: "nmtcpp-translation",
              stream: true,
          });
          process.stdout.write("Translated text EN -> ES: ");
          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);
          process.exit(1);
      }
      ```
    </WrapCode>
  </Tab>

  <Tab value="ts" label="TypeScript">
    <WrapCode>
      ```ts file=<rootDir>/packages/sdk/examples/translation/translation-stream.ts title="translation.ts" lineNumbers
      import { loadModel, translate, unloadModel, BERGAMOT_EN_ES } from "@qvac/sdk";

      try {
        const modelId = await loadModel({
          modelSrc: BERGAMOT_EN_ES,
          modelConfig: {
            engine: "Bergamot",
            from: "en",
            to: "es",
          },
        });

        console.log(`▸ Model loaded: ${modelId}`);

        const text =
          "Hello, how are you today? I hope you are having a wonderful day!";

        console.log("▸ Streaming Translation");
        const streamResult = translate({
          modelId,
          text,
          modelType: "nmtcpp-translation",
          stream: true,
        });

        process.stdout.write("Translated text EN -> ES: ");
        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);
        process.exit(1);
      }
      ```
    </WrapCode>
  </Tab>
</Tabs>

<Callout type="success">
  **Tip:** all examples throughout this documentation are self-contained and runnable. For instructions on how to run them, see [SDK quickstart](/quickstart).
</Callout>
