-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jeasonstudio/feat-llm-polyfill
feat: add chrome-ai polyfill when browser not support
- Loading branch information
Showing
14 changed files
with
199 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chrome-ai": minor | ||
--- | ||
|
||
feat: add chrome-ai polyfill when chrome version not ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use client'; | ||
|
||
require('chrome-ai/polyfill'); | ||
import { | ||
CornerDownLeft, | ||
Command, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './language-model'; | ||
export * from './embedding-model'; | ||
export * from './chromeai'; | ||
export * from './polyfill/session'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { polyfillChromeAI } from './session'; | ||
|
||
polyfillChromeAI(globalThis.__polyfill_ai_options__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
FilesetResolver, | ||
LlmInference, | ||
ProgressListener, | ||
} from '@mediapipe/tasks-genai'; | ||
import { | ||
ChromeAISession, | ||
ChromeAISessionAvailable, | ||
ChromeAISessionOptions, | ||
ChromePromptAPI, | ||
PolyfillChromeAIOptions, | ||
} from '../global'; | ||
import createDebug from 'debug'; | ||
|
||
const debug = createDebug('chromeai:polyfill'); | ||
|
||
class PolyfillChromeAISession implements ChromeAISession { | ||
public constructor(private llm: LlmInference) { | ||
debug('PolyfillChromeAISession created', llm); | ||
} | ||
|
||
private generateText = async (prompt: string): Promise<string> => { | ||
const response = await this.llm.generateResponse(prompt); | ||
debug('generateText', prompt, response); | ||
return response; | ||
}; | ||
|
||
private streamText = (prompt: string): ReadableStream<string> => { | ||
debug('streamText', prompt); | ||
const stream = new ReadableStream<string>({ | ||
start: (controller) => { | ||
const listener: ProgressListener = ( | ||
partialResult: string, | ||
done: boolean | ||
) => { | ||
controller.enqueue(partialResult); | ||
if (done) { | ||
controller.close(); | ||
} | ||
}; | ||
this.llm.generateResponse(prompt, listener); | ||
}, | ||
cancel: (reason) => { | ||
console.warn('stream text canceled', reason); | ||
}, | ||
}); | ||
debug('streamText', prompt); | ||
return stream; | ||
}; | ||
|
||
public destroy = async () => this.llm.close(); | ||
public prompt = this.generateText; | ||
public execute = this.generateText; | ||
public promptStreaming = this.streamText; | ||
public executeStreaming = this.streamText; | ||
} | ||
|
||
/** | ||
* Model: https://huggingface.co/oongaboongahacker/Gemini-Nano | ||
*/ | ||
export class PolyfillChromeAI implements ChromePromptAPI { | ||
private aiOptions: PolyfillChromeAIOptions = { | ||
// 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/', | ||
}; | ||
|
||
public constructor(aiOptions: Partial<PolyfillChromeAIOptions> = {}) { | ||
this.aiOptions = Object.assign(this.aiOptions, aiOptions); | ||
debug('PolyfillChromeAI created', this.aiOptions); | ||
this.modelAssetBuffer = fetch(this.aiOptions.llmModelAssetPath).then( | ||
(response) => response.body!.getReader() | ||
)!; | ||
} | ||
|
||
private modelAssetBuffer: Promise<ReadableStreamDefaultReader>; | ||
|
||
private canCreateSession = async (): Promise<ChromeAISessionAvailable> => { | ||
// TODO@jeasonstudio: | ||
// * if browser do not support WebAssembly/WebGPU, return 'no'; | ||
// * check if modelAssetBuffer is downloaded, if not, return 'after-download'; | ||
return 'readily'; | ||
}; | ||
private defaultSessionOptions = | ||
async (): Promise<ChromeAISessionOptions> => ({ | ||
temperature: 0.800000011920929, | ||
topK: 3, | ||
}); | ||
|
||
private createSession = async ( | ||
options?: ChromeAISessionOptions | ||
): Promise<ChromeAISession> => { | ||
const argv = options ?? (await this.defaultSessionOptions()); | ||
const fileset = await FilesetResolver.forGenAiTasks( | ||
this.aiOptions.filesetBasePath | ||
); | ||
const llm = await LlmInference.createFromOptions(fileset, { | ||
baseOptions: { | ||
modelAssetBuffer: await this.modelAssetBuffer, | ||
}, | ||
temperature: argv.temperature, | ||
topK: argv.topK, | ||
}); | ||
const session = new PolyfillChromeAISession(llm); | ||
debug('createSession', options, session); | ||
return session; | ||
}; | ||
|
||
public canCreateGenericSession = this.canCreateSession; | ||
public canCreateTextSession = this.canCreateSession; | ||
public defaultGenericSessionOptions = this.defaultSessionOptions; | ||
public defaultTextSessionOptions = this.defaultSessionOptions; | ||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters