Translation · Stream a batch of translations3 / 3

Stream a batch of translations

Example on GitHub(packages/sdk/examples/translation/translation-bergamot-batch-stream.ts)

With batch translation already covered, streaming lets Bergamot return each translation as it's ready, instead of everything at once.

stream: true turns the same request into a duplex session. Rather than holding the batch until its last entry finishes, the engine hands over each translation as it completes, so a long list shows steady progress.

We can switch to streaming by setting stream: true:

const result = translate({
  modelId,
  text: texts,
  modelType: "nmtcpp-translation",
  stream: true,
});

A streaming request doesn't resolve result.translations. Iterate result.tokenStream instead, and track an index yourself so you can match each translation to the entry that produced it:

let index = 0;
for await (const translation of result.tokenStream) {
  console.log(`${index + 1}. ${texts[index]} -> "${translation}"`);
  index++;
}
console.log(`Translated ${index} texts`);

The stream produces the translated string, and texts[index] gives you its source.

Note: despite the name, tokenStream yields one finished translation per entry here, not one token at a time.

Questions

Question 1 of 2

What turns a batch translate() call into a streaming one?

Question 2 of 2

Why does the loop keep its own index counter instead of relying on result.tokenStream to pair translations with their source?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.