Skip to content

Commit

Permalink
refactor: modify the polyfill code
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonstudio committed Jul 9, 2024
1 parent cfd217e commit 715eeb4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/polyfill/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PolyfillChromeAI } from './session';
import { polyfillChromeAI } from './session';

const ai = new PolyfillChromeAI(globalThis.__polyfill_ai_options__);
globalThis.ai = globalThis.ai || ai;
globalThis.model = globalThis.model || ai;
polyfillChromeAI(globalThis.__polyfill_ai_options__);
12 changes: 11 additions & 1 deletion src/polyfill/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class PolyfillChromeAISession implements ChromeAISession {
*/
export class PolyfillChromeAI implements ChromePromptAPI {
private aiOptions: PolyfillChromeAIOptions = {
llmModelAssetPath: 'http://localhost:63767/weights.bin',
// About 1.78GB, should cache by browser
llmModelAssetPath:
'https://huggingface.co/oongaboongahacker/Gemini-Nano/resolve/main/weights.bin',
filesetBasePath: 'https://unpkg.com/@mediapipe/tasks-genai/wasm/',
};

Expand Down Expand Up @@ -112,3 +114,11 @@ export class PolyfillChromeAI implements ChromePromptAPI {
public createGenericSession = this.createSession;
public createTextSession = this.createSession;
}

export const polyfillChromeAI = (
options?: Partial<PolyfillChromeAIOptions>
) => {
const ai = new PolyfillChromeAI(options);
globalThis.ai = globalThis.ai || ai;
globalThis.model = globalThis.model || ai;
};
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"plugins": [{ "name": "next" }],
"baseUrl": "./",
"paths": {
"@/*": ["src/*"],
"chrome-ai-polyfill": ["src/polyfill/index.ts"],
"chrome-ai/*": ["src/*"]
"@/*": ["./*"],
"chrome-ai-polyfill": ["./src/polyfill/index.ts"],
"chrome-ai/*": ["./src/*"]
},
"incremental": true
},
Expand Down

0 comments on commit 715eeb4

Please sign in to comment.