Translation
Text-to-text neural machine translation (NMT) — i.e., translate text between different languages.
Overview
Translation uses your choice of either qvac-fabric-llm.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 idtext: 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:
For how to use each function, see SDK — API reference.
Models
You should load a model compatible with your chosen inference engine:
qvac-fabric-llm.cpp(default): Bergamot or IndicTrans2. Bergamot uses intgemm*.bin+*.spmvocab 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:
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);
}Tip: all examples throughout this documentation are self-contained and runnable. For instructions on how to run them, see the JS/TS quickstart or the Python quickstart.