WebExtensions AI API

Note

The extension developer is responsible to comply with Mozilla’s add-on policies as well as regulatory rules when providing AI features, such as the EU AI Act.

The Firefox AI Platform API can be used from web extensions via a trial API we’ve added in 134. This API is enabled by default in Nightly. For Beta and Release, toggle the following flags in about:config:

  • browser.ml.enable → true

  • extensions.ml.enabled → true

WebExtensions that use the trialML optional permission will be able to use the API.

The permission is added to your manifest.json file as follows:

{
    "optional_permissions": ["trialML"],
}

The WebExtensions inference API wraps the Firefox AI API and comes in four endpoints under the browser.trial.ml namespace:

  • createEngine: creates an inference engine.

  • runEngine: runs an inference engine.

  • onProgress: listener for engine events

  • deleteCachedModels: delete model(s) files

Below is a full example of using the engine to summarize a content:

// 1. Initialize the event listener
browser.trial.ml.onProgress.addListener(progressData => {
  console.log(progressData);
});

// 2. Create the engine, may trigger downloads.
await browser.trial.ml.createEngine({
  modelHub: "huggingface",
  taskName: "summarization",
});

// 3. Call the engine
const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' +
'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' +
'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' +
'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' +
'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' +
'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' +
'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' +
'tallest free-standing structure in France after the Millau Viaduct.';

const res = await browser.trial.ml.runEngine({
  args: [text],
});

// 4. Get the results.
console.log(res[0]["summary_text"]);

// 5. Delete the downloaded model files
await browser.trial.ml.deleteCachedModels();

The createEngine call will trigger downloads in case the model files are not already cached in OPFS. This means that the first call to createEngine may last for a while, which need to be taken into account when building the web extension. Subsequent calls will be much faster.

Engine arguments

When calling that API, the object you pass to it can contain the following arguments (a subset of the arguments of the platform API):

  • taskName: The name of the task the pipeline is configured for. MANDATORY

  • modelHub: The model hub to use, can be huggingface or mozilla. When used, modelHubRootUrl and modelHubUrlTemplate are ignored.

  • modelId: The identifier for the specific model to be used by the pipeline.

  • modelRevision: The revision for the specific model to be used by the pipeline.

  • tokenizerId: The identifier for the tokenizer associated with the model, used for pre-processing inputs.

  • tokenizerRevision: The revision for the tokenizer associated with the model, used for pre-processing inputs.

  • processorId: The identifier for any processor required by the model, used for additional input processing.

  • processorRevision: The revision for any processor required by the model, used for additional input processing.