LocalMode
Core

Fill-Mask

Predict masked tokens in text for autocomplete and text analysis.

Predict masked tokens in text using fill-mask models. Useful for autocomplete, text analysis, and understanding language patterns.

See it in action

Try Smart Autocomplete for a working demo of these APIs.

fillMask()

Predict masked tokens in a single text:

import { fillMask } from '@localmode/core';
import { transformers } from '@localmode/transformers';

const model = transformers.fillMask('onnx-community/ModernBERT-base-ONNX');

const { predictions, usage } = await fillMask({
  model,
  text: 'The capital of France is [MASK].',
  topK: 5,
});

predictions.forEach((p) => {
  console.log(`${p.token}: ${p.score.toFixed(3)} — "${p.sequence}"`);
});
// Paris: 0.952 — "The capital of France is Paris."
// Lyon: 0.012 — "The capital of France is Lyon."
const controller = new AbortController();

setTimeout(() => controller.abort(), 5000);

const { predictions } = await fillMask({
  model,
  text: 'The capital of France is [MASK].',
  abortSignal: controller.signal,
});

FillMaskOptions

Prop

Type

FillMaskResult

Prop

Type

FillMaskPrediction

Prop

Type

fillMaskMany()

Predict masks for multiple texts:

import { fillMaskMany } from '@localmode/core';

const { results } = await fillMaskMany({
  model,
  texts: [
    'The weather today is [MASK].',
    'She is a [MASK] engineer.',
  ],
  topK: 3,
});

results.forEach((predictions, i) => {
  console.log(`Text ${i + 1}:`);
  predictions.forEach((p) => console.log(`  ${p.token}: ${p.score.toFixed(3)}`));
});

Custom Provider

Implement the FillMaskModel interface:

import type { FillMaskModel, DoFillMaskOptions, DoFillMaskResult } from '@localmode/core';

class MyFillMask implements FillMaskModel {
  readonly modelId = 'custom:my-fill-mask';
  readonly provider = 'custom';
  readonly maskToken = '[MASK]';

  async doFillMask(options: DoFillMaskOptions): Promise<DoFillMaskResult> {
    const { texts, topK } = options;

    // Your fill-mask logic here
    const results = texts.map((text) => [
      { token: 'example', score: 0.9, sequence: text.replace('[MASK]', 'example') },
    ]);

    return {
      results,
      usage: { inputTokens: texts.join('').length, durationMs: 0 },
    };
  }
}

For recommended models, provider-specific options, and practical recipes, see the Transformers Fill-Mask guide.

Next Steps

Showcase Apps

AppDescriptionLinks
Smart AutocompletePredict masked words for text completionDemo · Source

On this page