feat: Add Chrome AI support
This commit is contained in:
@@ -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
41
src/models/index.ts
Normal 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
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user