Skip to content

AISummarizer Usage Guide

This document provides a guide on how to use the AISummarizer module from TypeScript code in a JavaScript environment.

Overview

AISummarizer is a service that offers AI-based text summarization capabilities with different options for summarization types, lengths, and formats.

Prerequisites

  1. Acknowledge Google's Generative AI Prohibited Uses Policy.

  2. Download and Install Chrome Canary:

  3. Ensure your Chrome Canary version is equal to or newer than 129.0.6639.0.

  4. Check Device Requirements:

  5. Make sure your device has at least 22 GB of free storage space.
  6. The Gemini Nano model will be deleted if available storage falls below 10 GB post-download.
  7. If you are already using the Prompt API, storage requirements are already met.

  8. Enable Gemini Nano:

  9. If you haven't set up the Prompt API, follow these steps:

    1. Open Chrome and navigate to chrome://flags/#optimization-guide-on-device-model.
    2. Enable BypassPerfRequirement.
    3. Relaunch Chrome to save changes.
  10. Enable the Summarization API:

  11. Navigate to chrome://flags/#summarization-api-for-gemini-nano.
  12. Enable it for local experimentation.
  13. Relaunch Chrome.

  14. Finalize Setup:

  15. Open Chrome DevTools and execute await ai.summarizer.create(); in the console to initiate the model download.
  16. Execute await ai.summarizer.capabilities(); repeatedly until the response indicates "readily". This process may take 3 to 5 minutes.
  17. If the message reads "The model was available but there was not an execution config available...", you might need to wait a day for configuration updates.
  18. Refer to the troubleshooting section if problems persist.

Using AISummarizer

Step 1: Check Capabilities

Before creating a summarizer session, check the availability of the model.

async function checkCapabilities() {
  const capabilities = await window.ai.summarizer.capabilities();
  console.log("Model Availability:", capabilities.available);
}

checkCapabilities();

Step 2: Create a Summarizer Session

Create a summarizer session using the appropriate enum values.

async function createSummarizerSession() {
  const options = {
    type: 'tl;dr',  // Enum value
    length: 'medium',  // Enum value
    format: 'plain-text'  // Enum value
  };

  const summarizerSession = await window.ai.summarizer.create(options);
  return summarizerSession;
}

Step 3: Use the Summarizer

Once you have a session, summarize the text.

async function summarizeText(text) {
  const session = await createSummarizerSession();

  // Wait for the session to be ready
  await session.ready;

  const summary = await session.summarize(text);
  console.log("Summary:", summary);

  // Clean up
  session.destroy();
}

summarizeText("Your text to be summarized goes here.");

Handling Download Progress

If the model requires downloading, handle progress with an event listener.

function handleDownloadProgress(prop, event) {
  console.log(`Property: ${prop}, Loaded: ${event.loaded}, Total: ${event.total}`);
}

async function summarizeWithProgress(text) {
  const session = await createSummarizerSession();

  // Add event listener for download progress
  session.addEventListener = handleDownloadProgress;

  await session.ready; // Wait for readiness
  const summary = await session.summarize(text);
  console.log("Summary:", summary);

  session.destroy();
}

summarizeWithProgress("Text to summarize with progress handling.");