feat: Add Chrome AI support

This commit is contained in:
n4ze3m
2024-06-30 20:45:06 +05:30
parent 52f9a2953a
commit d41ec2a89c
36 changed files with 463 additions and 47 deletions

View File

@@ -50,9 +50,16 @@ function formatPrompt(messages: BaseMessage[]): string {
return messages
.map((message) => {
if (typeof message.content !== "string") {
throw new Error(
"ChatChromeAI does not support non-string message content."
)
// console.log(message.content)
// throw new Error(
// "ChatChromeAI does not support non-string message content."
// )
if (message.content.length > 0) {
//@ts-ignore
return message.content[0]?.text || ""
}
return ""
}
return `${message._getType()}: ${message.content}`
})
@@ -147,10 +154,9 @@ export class ChatChromeAI extends SimpleChatModel<ChromeAICallOptions> {
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
if (!this.session) {
throw new Error("Session not found. Please call `.initialize()` first.")
await this.initialize()
}
const textPrompt = this.promptFormatter(messages)
const stream = this.session.promptStreaming(textPrompt)
const iterableStream = IterableReadableStream.fromReadableStream(stream)

41
src/models/index.ts Normal file
View File

@@ -0,0 +1,41 @@
import { ChatChromeAI } from "./ChatChromeAi"
import { ChatOllama } from "./ChatOllama"
export const pageAssistModel = async ({
model,
baseUrl,
keepAlive,
temperature,
topK,
topP,
numCtx,
seed
}: {
model: string
baseUrl: string
keepAlive: string
temperature: number
topK: number
topP: number
numCtx: number
seed: number
}) => {
switch (model) {
case "chrome::gemini-nano::page-assist":
return new ChatChromeAI({
temperature,
topK
})
default:
return new ChatOllama({
baseUrl,
keepAlive,
temperature,
topK,
topP,
numCtx,
seed,
model
})
}
}