lightning-joyce / chromeai

Chrome Built-in AI Demo page
https://chromeai.co
MIT License
112 stars 14 forks source link

window.ai.createTextSession() has been renamed #9

Open obovoid opened 2 weeks ago

obovoid commented 2 weeks ago

Only for Canary Version: 130.0.6679.0 or above (check chrome://version)

As the title says, createTextSession is no longer valid in the newest canary version.

The new way to create a session is now:

const model = await window.ai.assistant.create()
await model.prompt("What is 2+2?"); // -> ' 2 + 2 = 4'

To get the ready state of the ai you would do:

const status = await window.ai.assistant.capabilities()
const ready = status.available

if (ready === 'readily') {
    // create ai session
} else {
    throw new Error('The AI is not ready!')
}

and lastly, before doing anything with the ai check if ai is a property to avoid errors:

const buildSupported = window.hasOwnProperty("ai") === true
if (buildSupported) {
   // Ai functions
}

A full example of how I would recommend the AI features here:

async function createAISession() {
    const buildSupported = window.hasOwnProperty("ai") === true
    if (buildSupported) {
        const status = await window.ai.assistant.capabilities()
        const ready = status.available
        if (ready === 'readily') {
            const model = await window.ai.assistant.create()
            return model;
        } else {
            throw new Error('The AI is not ready! Please try again later!')
        }
    } else {
        throw new Error('Your current chrome build doesn\'t support window.ai features. Please check the documentation and repeat the steps if you\'ve missed any')
    }
}

with this, the AI should be fully initialized and usable

UNONATHING commented 2 weeks ago

PixPin_2024-08-26_16-57-47

I have the exact problem with DEV 130.0.6669.2, it was working on 127~129.

I used your code to try, but the result showed undefined.

obovoid commented 2 weeks ago

PixPin_2024-08-26_16-57-47

I have the exact problem with DEV 130.0.6669.2, it was working on 127~129.

I used your code to try, but the result showed undefined.

It's only undefined because you are just defining the functions. To use the AI with my code you would need to define the model:

const model = await createAISession(); await model.prompt("Your request");

WebReflection commented 1 week ago

I had to manually check for updates and this sealed the deal ... the state was "after-download" in my case which gave me a good enough hint to check that thing ... I wonder if components will get a "check updates on IDLE" at some point so that these keep being updated without manual intervention from the users.

Screenshot from 2024-09-06 13-50-52

WebReflection commented 1 week ago

@obovoid out of curiosity ... where did you find these details and where could we find more details around the API? 🤔 Thanks

WebReflection commented 1 week ago

FWIWI this is how I would instead provide a helper ... not too different but at least it provides a solution in non-updated cases 😇

async function createAISession() {
  try {
    const { ai: { assistant } } = globalThis;
    if ((await assistant.capabilities()).available === 'readily')
      return await assistant.create();
    throw new Error(`chrome://components/ "Optimization Guide On Device Model" update required`);
  }
  catch (error) {
    throw error;
  }
}