QVAC Logo

translate( )

Translates text from one language to another using a specified translation model.

function translate(params): {
  tokenStream: AsyncGenerator<string>;
  text: Promise<string>;
  stats: Promise<TranslationStats | undefined>;
};

Supports both NMT (Neural Machine Translation) and LLM models.

Parameters

NameTypeRequired?DefaultDescription
params.modelIdstringThe identifier of the translation model
params.textstring | string[]The input text(s) to translate. NMT supports array; LLM only string.
params.modelType"nmt" | "llm"The type of translation model
params.streambooleantrueWhether to stream tokens or return complete response
params.fromstringauto-detectSource language code (LLM only)
params.tostring✓ (LLM)Target language code (LLM only)
params.contextstringAdditional context for translation (LLM only)

Returns

object — Object with the following fields:

FieldTypeDescription
tokenStreamAsyncGenerator<string>Stream of translated tokens
textPromise<string>Complete translated text
statsPromise<TranslationStats | undefined>Translation statistics

TranslationStats

FieldTypeDescription
processedTokensnumberNumber of tokens processed

Throws

ErrorWhen
TRANSLATION_FAILEDTranslation fails

Example

// Streaming mode (default)
const result = translate({
  modelId: "nmt-model",
  text: "Hello world",
  from: "en",
  to: "es",
  modelType: "llm",
});

for await (const token of result.tokenStream) {
  process.stdout.write(token);
}

// Non-streaming mode
const response = translate({
  modelId: "nmt-model",
  text: "Hello world",
  from: "en",
  to: "es",
  modelType: "llm",
  stream: false,
});

console.log(await response.text);

On this page