Skip to content

Commit

Permalink
Use newer ai.languageModel property for Chrome built-in AI, continuin…
Browse files Browse the repository at this point in the history
…g to support ai.assistant for backward compatibility.
  • Loading branch information
felixarntz committed Dec 1, 2024
1 parent 28a864a commit 0f52227
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/Accessing-AI-Services-in-JavaScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ try {

In JavaScript the AI Services plugin allows using another service `browser`, additionally to using the third party API based AI services. This service relies on [Chrome's built-in AI APIs](https://developer.chrome.com/docs/ai/built-in-apis), which allow using AI in the browser, running on device, which can be a great option for certain use-cases as it does not require paying for API access and does not involve sending your prompts to an external third-party API.

Note that these APIs are still in an experimental stage and are not yet rolled out completely. If you use Chrome and already have access to the APIs, you can use them through the AI Services plugin just like any other service's APIs. To see whether you have access, check if `window.ai.assistant` is available in your browser console. If not, you can [request to join the Early Preview Program](http://goo.gle/chrome-ai-dev-preview-join).
Note that these APIs are still in an experimental stage and are not yet rolled out completely. If you use Chrome and already have access to the APIs, you can use them through the AI Services plugin just like any other service's APIs. To see whether you have access, check if `window.ai.languageModel` is available in your browser console. If not, you can [request to join the Early Preview Program](http://goo.gle/chrome-ai-dev-preview-join).

### Customizing the default model configuration

Expand Down
7 changes: 5 additions & 2 deletions src/ai/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ export async function getBrowserServiceData() {
* Gets the AI capabilities that the browser supports.
*
* @since 0.1.0
* @since n.e.x.t Checks for newer `ai.languageModel` property.
*
* @param {Object} ai The browser AI API object.
* @return {Promise<string[]>} The list of AI capabilities.
*/
async function getBrowserAiCapabilities( ai ) {
const capabilities = [];

if ( ai.assistant ) {
const supportsTextGeneration = await ai.assistant.capabilities();
const llm = ai.languageModel || ai.assistant;

if ( llm ) {
const supportsTextGeneration = await llm.capabilities();
if (
supportsTextGeneration &&
supportsTextGeneration.available === 'readily'
Expand Down
9 changes: 6 additions & 3 deletions src/ai/classes/browser-generative-ai-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function prepareContentForBrowser( content ) {
* See https://github.com/explainers-by-googlers/prompt-api#examples for supported parameters.
*
* @since 0.3.0
* @since n.e.x.t Checks for newer `ai.languageModel` property.
*
* @param {Object} modelParams Model parameters.
* @return {Promise<Object>} The browser session.
Expand All @@ -79,20 +80,22 @@ async function createSession( modelParams ) {
}
}

const llm = window.ai.languageModel || window.ai.assistant;

if ( Object.keys( browserParams ).length === 0 ) {
return window.ai.assistant.create();
return llm.create();
}

try {
const session = await window.ai.assistant.create( browserParams );
const session = await llm.create( browserParams );
return session;
} catch ( error ) {
// eslint-disable-next-line no-console
console.warn(
'Failed to create browser session with modelParams, therefore creating default session. Original error:',
error
);
return window.ai.assistant.create();
return llm.create();
}
}

Expand Down

0 comments on commit 0f52227

Please sign in to comment.