QVAC Logo

translate( )

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

function translate(params): object;

Description

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

Parameters

NameTypeRequired?Description
paramsobjectTranslation configuration object

params

FieldTypeRequired?Description
modelIdstringModel identifier
textstringText to translate
modelType"nmt" | "llm"Type of translation model
streambooleanWhether to stream the translation
fromstringSource language (auto-detect if not specified)
tostringTarget language

Returns

object — Object with tokenStream generator and text/stats properties

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

Throws

When translation fails with an error message or when language detection fails

Example

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

for await (const token of result.tokenStream) {
  console.log(token);
}

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

console.log(await response.text);

On this page