A browser's built-in AI is a proposed feature that allows web developers to directly access and use the AI capabilities built into the browser (like Chrome or Edge) from their own website's code or browser extension's JavaScript code.
This repo is a demo usage of the build-in Prompt API and a simple benchmark.
To run this benchmark, you must first enable the experimental Prompt API. Follow the official instructions for your browser:
You can run the benchmark directly or host it locally.
To run the benchmark, simply open URL.
-
Clone the repository:
git clone https://github.com/wenqinI/built-in-ai-benchmark
-
Open the file:
Navigate into the cloned directory and open the
index.htmlfile in the browser you just configured.
The application provides two modes for testing the in-browser AI model.
- Function: Manually send a single prompt to the AI model.
- Output: Displays the AI-generated response along with performance metrics for that single query. This is ideal for interactive testing.
- Function: Automatically sends a predefined prompt to the AI model for 5 consecutive rounds.
- Output: Calculates and displays the average performance metrics across all rounds, providing a more stable performance measurement.
The Prompt API delivers its response in chunks, where each chunk can contain one or more tokens. Consequently, the primary performance metric displayed is chunks per second.
The streaming nature of the API, which produces these chunks, is illustrated by the promptStreaming method:
// Prompt the model and stream the result chunk by chunk
const stream = session.promptStreaming("Write me an extra-long poem.");
for await (const chunk of stream) {
console.log(chunk);
}To provide a more familiar metric, this benchmark also calculates tokens per second. This is achieved by using the measureInputUsage() method to convert the output string into tokens, as detailed in the Prompt API documentation.
Feedback and contributions are welcome! Feel free to open an issue to report bugs, suggest features, or submit a pull request with improvements.