For many of my demos, I take the easy way out and if the status for the API is not "available", I just abort. That's wrong of course, but was simpler, and as I'm just building demos, I figured it was ok to cut some corners.
Now - I'm trying to be better about that - and wanted to update my default code to better handle these cases. I started working on a somewhat complex wrapper that I could reuse and I realized I had over-engineered it. I have a simpler solution, and would love a sanity check.
This sample code is for Prompt, but obviously I'd use it for all. Ok, given you've already checked for the presence of window.LanguageModel and status was either available or downloadable, consider this code:
async function doAITest() {
console.log('doAITest');
session = await LanguageModel.create({
monitor(m) {
m.addEventListener('downloadprogress', e => {
console.log(`Downloading prompt model: ${e.loaded * 100}%`);
});
}
});
console.log('hello', session);
}
From what I see - on a virgin profile, I get the download messages, and when it's done, the final console runs. On reload after download, I get the final console.
So outside of ensuring I give proper user feedback (more than a console message), this simple code is perfectly fine to handle both cases, right?