Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new prompt API #1264

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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