Tip
ai.matey works well with ai.captain
To help work with chrome's experimental window.ai API this package provides:
-
Documentation for the window.ai API here
-
A mock implementation of window.ai and it's sub-modules that can be used for testing.
-
An Open AI API-compatible client that mirrors window.ai. It can be used as a drop-in replacement for the window.ai object if it is not available or not working.
Documentation for the window.ai API is provided here: https://github.com/johnhenry/ai.matey/blob/main/docs/api.md
- Main sources:
To use the mock implementation, import the mock from ai.matey/mock
;
npm install ai.matey
import ai from "ai.matey/mock";
//...
import * as ai from "https://cdn.jsdelivr.net/npm/ai.matey@0.0.14/mock/index.mjs";
//...
OR
import * as ai from "https://ga.jspm.io/npm:ai.matey@0.0.14/mock/index.mjs";
//...
import * as ai from "ai.matey/mock";
const model = await ai.languageModel.create();
const poem = await model.prompt("write a poem");
console.log(poem);
To use the polyfill implementation, import the polyfill from ai.matey/mock/polyfill
;
This will automatically detect if the window.ai object is already defined and will not overwrite it.
import "ai.matey/mock/polyfill";
const model = await window.ai.languageModel.create();
const poem = await model.prompt("write a poem");
console.log(poem);
To overwrite the window.ai object, import the polyfill from ai.matey/mock/polyfill-overwrite
;
import "ai.matey/mock/polyfill-overwrite";
const model = await window.ai.languageModel.create();
const poem = await model.prompt("write a poem");
console.log(poem);
Use the OpenAI compatible client standalone, or as a drop-in replacement for window.ai
Note, that unlike with the mock implementation, the OpenAI client requires instantiation.
To use the mock implementation, import the mock from ai.matey/openai
;
npm install ai.matey
import AI from "ai.matey/openai";
const ai = new AI(/* options */);
//...
import AI from "https://cdn.jsdelivr.net/npm/ai.matey@0.0.14/openai/index.mjs";
const ai = new AI(/* options */);
//...
OR
import AI from "https://ga.jspm.io/npm:ai.matey@0.0.14/openai/index.mjs";
const ai = new AI(/* options */);
//...
import AI from "ai.matey/openai";
window.ai = new AI({
endpoint: "http://localhost:11434/v1/chat/completions",
credentials: {
apiKey: "123",
},
model: "llama3.2:latest",
});
const model = await window.ai.languageModel.create();
const poem = await model.prompt("write a poem");
console.log(poem);
Check out playground in this repositrory. Run a static sterver (npx live-server .) and navigate to playground.html