-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:b4s36t4/local-rag
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 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,69 @@ | ||
import type { SearchResult } from "voy-search"; | ||
import { ModelInstance } from "./BaseInstance"; | ||
import type { MLCEngine } from "@mlc-ai/web-llm"; | ||
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; | ||
import { WorkerPostTypes } from "../const"; | ||
|
||
export class ChatSQL extends ModelInstance { | ||
static instance: ModelInstance | null = null; | ||
model: MLCEngine | null = null; | ||
splitter: RecursiveCharacterTextSplitter | null = null; | ||
// loader: WebPDFLoader | null = null | ||
worker: Worker | null = null; | ||
|
||
cacheHandle: FileSystemDirectoryHandle | null = null; | ||
|
||
static cacheNameStatic: string = "chat_sql"; | ||
|
||
constructor(model: MLCEngine, worker: Worker) { | ||
super("chat_sql", "chat_sql"); | ||
this.model = model; | ||
this.splitter = new RecursiveCharacterTextSplitter({ | ||
chunkSize: 500, | ||
chunkOverlap: 12, | ||
}); | ||
this.worker = worker; | ||
|
||
this.worker.addEventListener("message", (event) => { | ||
const { type } = event.data; | ||
|
||
if (type === WorkerPostTypes.embedDocument) { | ||
console.log("[Embed] Document embed created"); | ||
if (!event.data.data?.length) { | ||
console.log( | ||
"%c[Embed Error]", | ||
"background-color: red;padding:2px;color:white", | ||
"Can't embed" | ||
); | ||
return; | ||
} | ||
window.db.add({ | ||
embeddings: [ | ||
{ | ||
embeddings: event.data.data.at(0), | ||
id: crypto.randomUUID(), | ||
title: event.data.payload, | ||
url: "", | ||
}, | ||
], | ||
}); | ||
console.log("[Embed] Added into Vector Search"); | ||
} | ||
|
||
return; | ||
}); | ||
} | ||
|
||
addIndex(data: string | string[]): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
search(question: number[], count?: number): SearchResult { | ||
return window.db.search(question as any, count || 5); | ||
} | ||
saveToCache(): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
loadCache(): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
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