Skip to content

Commit

Permalink
Change on-device example to use the origin trial API (#1337)
Browse files Browse the repository at this point in the history
* Add scaffolding for Prompt API OT example

* Rewrite existing example to use the OT

* Add trial token and key

* Update error message, add `minimum_chrome_version`

* Fix feature detection

* Some README refinements

* Correct instructions

* OT refinement
  • Loading branch information
tomayac authored Oct 31, 2024
1 parent 31704be commit b939ca2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
17 changes: 12 additions & 5 deletions functional-samples/ai.gemini-on-device/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# On-device AI with Gemini Nano

This sample demonstrates how to use the experimental Gemini Nano API in Chrome. To learn more about the API and how to sign-up for the preview, head over to [Built-in AI on developer.chrome.com](https://developer.chrome.com/docs/ai/built-in).
This sample demonstrates how to use the experimental Gemini Nano API available in the context of an origin trial in Chrome with Chrome Extensions. To learn more about the API and how to sign-up for the origin trial, head over to [Built-in AI on developer.chrome.com](https://developer.chrome.com/docs/ai/built-in).

## Overview

The extension provides a chat interface using the prompt API with Chrome's built-in Gemini Nano model.
The extension provides a chat interface using the Prompt API with Chrome's built-in Gemini Nano model.

## Running this extension

1. Clone this repository.
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
3. Click the extension icon.
4. Interact with the prompt API in the sidebar.
1. Launch Chrome with the following flag, which makes sure the origin trial token in `manifest.json` is accepted by the browser. This won't be necessary once the origin trial is live.

`--origin-trial-public-key=dRCs+TocuKkocNKa0AtZ4awrt9XKH2SQCI6o4FY6BNA=`
1. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
1. Click the extension icon.
1. Interact with the Prompt API in the sidebar.

## Creating your own extension

If you use this sample as the foundation for your own extension, be sure to update the `"trial_tokens"` field [with your own origin trial token](https://developer.chrome.com/docs/web-platform/origin-trials?hl=en#extensions) and to remove the `"key"` field in `manifest.json`.
5 changes: 4 additions & 1 deletion functional-samples/ai.gemini-on-device/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"background": {
"service_worker": "background.js"
},
"permissions": ["sidePanel"],
"permissions": ["sidePanel", "aiLanguageModelOriginTrial"],
"trial_tokens": ["A6hC1yvazc5QTqSESgEo3uwIUs1KBM4VB93yiTdtgFlFlYNJjf18zmzScpaCepaYpNLaMJ4dNkDps+vbrk7oEw8AAAB9eyJvcmlnaW4iOiAiY2hyb21lLWV4dGVuc2lvbjovL2lkZm1qYmJqY21qamtoYWlvZmltbWNtY29qYmlnbmpmIiwgImZlYXR1cmUiOiAiQUlQcm9tcHRBUElGb3JFeHRlbnNpb24iLCAiZXhwaXJ5IjogMTk4OTQwNzUwMH0="],
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi99ZKuRYfZMQ80XryBYKNA4DJTHPMEiNkiF6TLqTXMp4Cb23I/DTblxlEELWyM1vDuXeFI7jqXDuM6YVHd7zTtA1hGbGgLm9w9+d763x2wCRbxud+dXIXW4IoD4CqXpCLscDD7Vrar/I5XbcmdA9jjlCXMGS2OKNRhYMjNpaVhFCi2jzWid+92DZSpPbdM9iBODGfwnFNp16tFX8/dDx3w23FneeOVHV3JjNIWO3uT60AhZWOtcQlERuMBJmEkKAgqE8T3ZjoZe4ALupfvDxI7khP29+gBXhgF/r6T0RAbB1LfxubhyLk/X7aC/sw3umCemqvc+knvKRBLHMVl1nHQIDAQAB",
"minimum_chrome_version": "131",
"side_panel": {
"default_path": "sidepanel/index.html"
},
Expand Down
8 changes: 4 additions & 4 deletions functional-samples/ai.gemini-on-device/sidepanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let session;
async function runPrompt(prompt, params) {
try {
if (!session) {
session = await window.ai.languageModel.create(params);
session = await chrome.aiOriginTrial.languageModel.create(params);
}
return session.prompt(prompt);
} catch (e) {
Expand All @@ -35,11 +35,11 @@ async function reset() {
}

async function initDefaults() {
if (!window.ai) {
showResponse('Error: window.ai not supported in this browser');
if (!('aiOriginTrial' in chrome)) {
showResponse('Error: chrome.aiOriginTrial not supported in this browser');
return;
}
const defaults = await window.ai.languageModel.capabilities();
const defaults = await chrome.aiOriginTrial.languageModel.capabilities();
console.log('Model default:', defaults);
sliderTemperature.value = defaults.temperature;
sliderTopK.value = defaults.topK;
Expand Down

0 comments on commit b939ca2

Please sign in to comment.