From 9c7004ed00f796b497786ea16aa1f89cd10af40d Mon Sep 17 00:00:00 2001 From: Sebastian Benz Date: Mon, 26 Aug 2024 11:32:46 +0200 Subject: [PATCH] Migrate to new prompt API ...also add a check whether the browser supports the prompt API. --- .../ai.gemini-on-device/sidepanel/index.js | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/functional-samples/ai.gemini-on-device/sidepanel/index.js b/functional-samples/ai.gemini-on-device/sidepanel/index.js index 4be9bed98b..2f11c29f02 100644 --- a/functional-samples/ai.gemini-on-device/sidepanel/index.js +++ b/functional-samples/ai.gemini-on-device/sidepanel/index.js @@ -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) { @@ -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;