Skip to content

Commit

Permalink
Migrate to new prompt API
Browse files Browse the repository at this point in the history
...also add a check whether the browser supports the prompt API.
  • Loading branch information
sebastianbenz committed Aug 26, 2024
1 parent 90249bc commit 9c7004e
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions functional-samples/ai.gemini-on-device/sidepanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@ let session;
async function runPrompt(prompt, params) {
try {
if (!session) {
// Start by checking if it's possible to create a session based on the availability of the model, and the characteristics of the device.
const canCreate = await self.ai.canCreateTextSession();
// canCreate will be one of the following:
// * "readily": the model is available on-device and so creating will happen quickly
// * "after-download": the model is not available on-device, but the device is capable,
// so creating the session will start the download process (which can take a while).
// * "no": the model is not available for this device.
if (canCreate === 'no') {
console.warn('Built-in prompt API not available.');
throw new Error(
'Built-in prompt API not available. Join the preview program to learn how to enable it.'
);
}
console.log('Creating new text session');
session = await self.ai.createTextSession(params);
session = await window.ai.assistant.create(params);
}
return session.prompt(prompt);
} catch (e) {
Expand All @@ -49,7 +35,11 @@ async function reset() {
}

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

0 comments on commit 9c7004e

Please sign in to comment.