Skip to content

Commit 85f721f

Browse files
Improve Gemini samples (#1611)
* Migrate to latest version of the language model * Update readme to better describe the sample. * More readme updates * Consistent API naming and format lists
1 parent 43a7939 commit 85f721f

File tree

8 files changed

+1112
-68
lines changed

8 files changed

+1112
-68
lines changed

functional-samples/ai.gemini-in-the-cloud/package-lock.json

Lines changed: 1023 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functional-samples/ai.gemini-in-the-cloud/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"private": true,
88
"devDependencies": {
9-
"@google/generative-ai": "0.15.0",
9+
"@google/genai": "^1.36.0",
1010
"rollup": "4.22.4"
1111
}
1212
}

functional-samples/ai.gemini-in-the-cloud/sidepanel/index.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
GoogleGenerativeAI,
3-
HarmBlockThreshold,
4-
HarmCategory
5-
} from '../node_modules/@google/generative-ai/dist/index.mjs';
1+
import { GoogleGenAI } from '../node_modules/@google/genai/dist/index.mjs';
62

73
// Important! Do not expose your API in your extension code. You have to
84
// options:
@@ -17,7 +13,6 @@ import {
1713
const apiKey = '...';
1814

1915
let genAI = null;
20-
let model = null;
2116
let generationConfig = {
2217
temperature: 1
2318
};
@@ -30,27 +25,22 @@ const elementError = document.body.querySelector('#error');
3025
const sliderTemperature = document.body.querySelector('#temperature');
3126
const labelTemperature = document.body.querySelector('#label-temperature');
3227

33-
function initModel(generationConfig) {
34-
const safetySettings = [
35-
{
36-
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
37-
threshold: HarmBlockThreshold.BLOCK_NONE
38-
}
39-
];
40-
genAI = new GoogleGenerativeAI(apiKey);
41-
model = genAI.getGenerativeModel({
42-
model: 'gemini-1.5-flash',
43-
safetySettings,
44-
generationConfig
45-
});
46-
return model;
47-
}
48-
49-
async function runPrompt(prompt) {
28+
async function runPrompt(prompt, config) {
5029
try {
51-
const result = await model.generateContent(prompt);
52-
const response = await result.response;
53-
return response.text();
30+
const response = await genAI.models.generateContent({
31+
model: 'gemini-2.5-flash',
32+
contents: prompt,
33+
config: {
34+
safetySettings: [
35+
{
36+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
37+
threshold: 'BLOCK_NONE'
38+
}
39+
],
40+
temperature: config.temperature
41+
}
42+
});
43+
return response.text;
5444
} catch (e) {
5545
console.log('Prompt failed');
5646
console.error(e);
@@ -76,11 +66,11 @@ buttonPrompt.addEventListener('click', async () => {
7666
const prompt = inputPrompt.value.trim();
7767
showLoading();
7868
try {
79-
const generationConfig = {
80-
temperature: sliderTemperature.value
69+
const config = {
70+
temperature: parseFloat(sliderTemperature.value)
8171
};
82-
initModel(generationConfig);
83-
const response = await runPrompt(prompt, generationConfig);
72+
genAI = new GoogleGenAI({ apiKey });
73+
const response = await runPrompt(prompt, config);
8474
showResponse(response);
8575
} catch (e) {
8676
showError(e);
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
# Alt-texter: On-device multimodal AI with Gemini Nano - image understanding
1+
# Alt Texter: Generate accessible image descriptions with Chrome's built-in Prompt API
22

3-
This sample demonstrates how to use the image understanding capabilities of the multi-modal Gemini Nano API preview together with [Chrome's translation API](https://developer.chrome.com/docs/ai/translator-api). 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/extensions/ai/prompt-api).
3+
This sample demonstrates how to use Chrome's built-in AI APIs to generate alt text for images, making web content more accessible. It combines two on-device AI capabilities:
4+
5+
- **[Prompt API](https://developer.chrome.com/docs/extensions/ai/prompt-api)** with multimodal input (Gemini Nano) for image understanding
6+
- **[Translator API](https://developer.chrome.com/docs/ai/translator-api)** for translating descriptions into multiple languages
47

58
## Overview
69

7-
This extension adds a context menu entry for images on the web to generate an alt text description that is displayed in a popup window.
10+
Alt Texter adds a context menu entry for images on the web. When activated, it:
11+
12+
1. Analyzes the image using Gemini Nano's multimodal capabilities.
13+
2. Generates a concise, functional description following accessibility best practices (object-action-context framework).
14+
3. Displays the description in a popup where you can optionally translate it.
15+
4. Lets you copy the alt text to your clipboard for use elsewhere.
816

917
## Running this extension
1018

1119
1. Clone this repository.
12-
1. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
13-
1. Right click an image on a webpage and select "Generate alt text"
20+
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
21+
3. Right-click an image on a webpage and select "Generate alt text".
22+
4. Wait for the description to be generated, then optionally translate it or copy it to your clipboard.
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
# Audio-Scribe: On-device multimodal AI with Gemini Nano - audio transcription
1+
# Audio-Scribe: Transcribe audio messages with Chrome's multimodal Prompt API
22

3-
This sample demonstrates how to use the audio transcription capabilities of the multi-modal Gemini Nano API preview. 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/extensions/ai/prompt-api).
3+
This sample demonstrates how to use Chrome's built-in AI APIs to transcribe audio messages directly in the browser. It uses:
4+
5+
- **[Prompt API](https://developer.chrome.com/docs/extensions/ai/prompt-api)** with multimodal audio input (Gemini Nano) for on-device speech-to-text transcription
46

57
## Overview
68

7-
This extension adds a sidepanel that will, when opened, display a transcription of all audio files on a web page (currently it looks only for audio files created using `URL.createObjectUrl`).
9+
Audio-Scribe adds a side panel that automatically transcribes audio messages from chat applications. When activated, it:
10+
11+
1. Monitors the page for audio blobs created via `URL.createObjectURL`.
12+
2. Detects audio content and sends it to Gemini Nano for transcription.
13+
3. Streams the transcribed text in real-time to the side panel.
14+
4. Works with messaging apps like WhatsApp Web that use blob URLs for audio messages.
815

916
## Running this extension
1017

1118
1. Clone this repository.
12-
1. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
13-
1. Open the audio-scribe sidepanel by clicking the audio-scribe action or by pressing the `ALT + A` keyboard shortcut.
14-
1. Open a chat app in the browser, for example https://web.whatsapp.com/. You can also run the demo chat app via:
19+
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
20+
3. Open a chat app in the browser, for example https://web.whatsapp.com/. You can also run the included demo chat app:
1521
```
1622
npx serve demo-chat-app
1723
```
18-
1. All audio messages in the current chat will be transcribed in the side panel.
24+
4. Open the Audio-Scribe side panel by clicking the extension icon or pressing `Alt+A`.
25+
5. Play or load audio messages in the chat - they will be automatically transcribed in the side panel.
1926

2027
![Screenshot displaying a demo chat app with a few audio messages. On the right, there is the audio-scribe extension's sidepanel which displayes the transcribed text messages](assets/screenshot.png)
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
# Calendar Mate: On-device AI with Gemini Nano
22

3-
This sample demonstrates how to use the Gemini Nano prompt API for Chrome Extensions. To learn more about the API, head over to [Built-in AI on developer.chrome.com](https://developer.chrome.com/docs/extensions/ai/prompt-api).
3+
This sample demonstrates how to use Chrome's built-in Prompt API in an extension to extract calendar event details from natural language text. To learn more about the API, see [Prompt API on developer.chrome.com](https://developer.chrome.com/docs/extensions/ai/prompt-api).
44

55
## Overview
66

7-
The extension provides a chat interface using the Prompt API with Chrome's built-in Gemini Nano model.
7+
Calendar Mate allows users to quickly create Google Calendar events from any selected text on a webpage. Simply highlight text describing an event (e.g., "Team meeting on Friday at 3pm in Conference Room A"), right-click, and select "Create Calendar Event". The extension uses Gemini Nano to intelligently extract:
8+
9+
- Event title
10+
- Start and end date/time
11+
- Location
12+
- Description
13+
- Timezone
14+
15+
The extracted details are used to pre-populate a new Google Calendar event.
816

917
## Running this extension
1018

1119
1. Clone this repository.
12-
1. Run `npm install` in the project directory.
13-
1. Run `npm run build` in the project directory to build the extension.
14-
1. Load the newly created `dist` directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
15-
1. Click the extension icon.
16-
1. Interact with the Prompt API in the sidebar.
20+
2. Run `npm install` in the project directory.
21+
3. Run `npm run build` to build the extension.
22+
4. Load the `dist` directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
23+
5. Select any text on a webpage that describes an event.
24+
6. Right-click and choose "Create Calendar Event" from the context menu.
1725

18-
## Creating your own extension
26+
## How it works
1927

20-
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#extensions) and to remove the `"key"` field in `manifest.json`.
28+
1. **Context Menu**: The extension adds a "Create Calendar Event" option to Chrome's right-click context menu when text is selected.
29+
2. **AI Extraction**: When triggered, the selected text is sent to Gemini Nano with a prompt to extract event details as structured JSON.
30+
3. **Date Parsing**: The extracted date/time strings are parsed using the [any-date-parser](https://www.npmjs.com/package/any-date-parser) library.
31+
4. **Calendar Integration**: A Google Calendar URL is generated with the extracted details and opened in a new tab.

functional-samples/ai.gemini-on-device-summarization/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# On-device Summarization with Gemini Nano
22

3-
This sample demonstrates how to use the experimental Summarization API in Chrome. To learn more about the API and how to sign-up for the preview, head over to the [summarizer guide on developer.chrome.com](https://developer.chrome.com/docs/ai/summarizer-api).
3+
This sample demonstrates how to use Chrome's built-in Summarizer API to generate AI-powered summaries of web pages directly on the user's device. The summarization runs entirely locally using Gemini Nano, ensuring privacy and fast performance without requiring an internet connection or API keys.
4+
5+
To learn more about the Summarizer API, head over to the [Summarizer API guide on developer.chrome.com](https://developer.chrome.com/docs/ai/summarizer-api).
46

57
## Overview
68

7-
The extension summarizes the content of the currently open tab. It uses Mozilla's [readability](https://github.com/mozilla/readability) library to extract the content of the currently active tab and displays a summary of the page generated by [Chrome's experimental summarization API](https://developer.chrome.com/blog/august2024-summarization-ai) in a side panel.
9+
This extension adds a side panel that automatically displays AI-generated summaries of any web page you visit. It uses Mozilla's [Readability](https://github.com/mozilla/readability) library to extract the main content from web pages (stripping away navigation, ads, and other clutter), then passes that content to Chrome's built-in Summarizer API.
810

911
## Running this extension
1012

11-
1. Clone this repository
12-
1. Run `npm install` in this folder to install all dependencies.
13-
1. Run `npm run build` to build the extension.
14-
1. Load the newly created `dist` directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
15-
1. Click the extension icon to open the summary side panel.
16-
1. Open any web page, the page's content summary will automatically be displayed in the side panel.
13+
1. Clone this repository.
14+
2. Run `npm install` in this folder to install all dependencies.
15+
3. Run `npm run build` to build the extension.
16+
4. Load the newly created `dist` directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/get-started/tutorial/hello-world#load-unpacked).
17+
5. Click the extension icon to open the summary side panel.
18+
6. Open any web page. The page's content summary will automatically be displayed in the side panel.
1719

1820
## Creating your own extension
1921

package-lock.json

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)