Skip to content

Commit 83f5bad

Browse files
committed
Merge branch 'remoteManagement' into crokeso
2 parents 71faa94 + b0451b3 commit 83f5bad

File tree

1 file changed

+106
-11
lines changed

1 file changed

+106
-11
lines changed

klite.embd

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23822,22 +23822,115 @@ Current version indicated by LITEVER below.
2382223822
return dotproduct / (mA * mB);
2382323823
}
2382423824

23825+
let getEmbeddingsFromCache = asyncRunner(function* () //gets a cached embedding, recomputes if missing
23826+
{
23827+
//if db is not initialized, init it
23828+
const isIndexedDBSupported = !!window.indexedDB;
23829+
if(isIndexedDBSupported && Object.keys(embeddings_cache).length === 0)
23830+
{
23831+
let iddb = yield indexeddb_load('EmbeddingsCache', '');
23832+
if (iddb && iddb != "") {
23833+
try {
23834+
embeddings_cache = JSON.parse(iddb);
23835+
} catch (e) {
23836+
console.log("Fail to parse embeddings cache");
23837+
}
23838+
}
23839+
}
23840+
return embeddings_cache;
23841+
});
23842+
23843+
let docStepSize = 100
23844+
let paginatedEmbeddingGen = asyncRunner(function* (allDocuments) {
23845+
for (let i = 0; i < allDocuments.length; i += docStepSize) {
23846+
showToast(`Generating ${i + 1} / ${allDocuments.length} embeddings...`, 15000);
23847+
23848+
let docs = allDocuments.slice(i, i + docStepSize)
23849+
23850+
let reqOpt = {
23851+
method: 'POST', // or 'PUT'
23852+
headers: get_kobold_header(),
23853+
body: JSON.stringify({
23854+
"input": docs.map(c => c.snippet),
23855+
"truncate": true
23856+
}),
23857+
};
23858+
if (globalabortcontroller) {
23859+
reqOpt.signal = globalabortcontroller.signal;
23860+
}
23861+
let sub_endpt = apply_proxy_url(`${custom_kobold_endpoint}/api/extra/embeddings`);
23862+
let resp = yield fetch(sub_endpt, reqOpt).catch(err=>{
23863+
console.log("error while fetching embeddings: " + err);
23864+
});
23865+
let json = yield resp.json()
23866+
for (let j = 0; j < docs.length && j < json.data.length; j++) {
23867+
docs[j].embedding = json.data[j].embedding
23868+
docs[j].modelUsed = json.model
23869+
}
23870+
}
23871+
return allDocuments
23872+
});
23873+
2382523874
// Generator-style "async" version of getEmbeddings
2382623875
const getEmbeddings = function* (paragraphs) {
23827-
let embeddings = [];
23828-
for (let i = 0; i < paragraphs.length; i++) {
23829-
showToast(`Generating ${i + 1} / ${paragraphs.length} embeddings...`);
23830-
let documentContent = paragraphs[i].snippet;
23831-
let documentName = paragraphs[i].document;
23832-
let documentContentHash = cyrb_hash(`${documentContent}`, 0, 4);
23876+
let usePaginatedChunking = paragraphs.length > 1 && documentdb_provider !== 3;
23877+
if (usePaginatedChunking)
23878+
{
23879+
const isIndexedDBSupported = !!window.indexedDB;
23880+
23881+
let embeddings = [];
23882+
23883+
let docsFromCache = []
23884+
let newEmbeddings = []
23885+
let embeddingObj = yield getEmbeddingsFromCache();
23886+
for (let i = 0; i < paragraphs.length; i++)
23887+
{
23888+
let documentName = paragraphs[i].document, documentContent = paragraphs[i].snippet;
23889+
let documentContentHash = cyrb_hash(`${documentContent}`, 0, 4);
23890+
23891+
if (!!embeddingObj[documentContentHash]) {
23892+
docsFromCache.push(embeddingObj[documentContentHash])
23893+
}
23894+
else {
23895+
newEmbeddings.push({
23896+
document: documentName ? documentName : "",
23897+
hash: documentContentHash,
23898+
snippet: documentContent
23899+
})
23900+
}
23901+
}
23902+
let newDocsWithEmbeddings = []
23903+
if (newEmbeddings.length > 0)
23904+
{
23905+
newDocsWithEmbeddings = yield paginatedEmbeddingGen(newEmbeddings)
23906+
newDocsWithEmbeddings.forEach(doc => {
23907+
embeddingObj[doc.hash] = doc;
23908+
})
23909+
if(isIndexedDBSupported)
23910+
{
23911+
indexeddb_save('EmbeddingsCache',JSON.stringify(embeddingObj));
23912+
}
23913+
}
2383323914

23834-
let embeddingObj = yield DBComputeAndLoadEmbedding(documentContentHash, documentName, documentContent);
23915+
return [...docsFromCache, ...newDocsWithEmbeddings];
23916+
}
23917+
else
23918+
{
23919+
let embeddings = [];
23920+
for (let i = 0; i < paragraphs.length; i++) {
23921+
showToast(`Generating ${i + 1} / ${paragraphs.length} embeddings...`);
23922+
let documentContent = paragraphs[i].snippet;
23923+
let documentName = paragraphs[i].document;
23924+
let documentContentHash = cyrb_hash(`${documentContent}`, 0, 4);
23925+
23926+
let embeddingObj = yield DBComputeAndLoadEmbedding(documentContentHash, documentName, documentContent);
2383523927

23836-
if (embeddingObj) {
23837-
embeddings.push(embeddingObj);
23928+
if (embeddingObj) {
23929+
embeddings.push(embeddingObj);
23930+
}
2383823931
}
23932+
return embeddings;
2383923933
}
23840-
return embeddings;
2384123934
};
2384223935

2384323936
// Compute search embedding
@@ -42691,7 +42784,9 @@ flowchart TD\n${treeToViewOutput.outputText.trim()}`
4269142784
}
4269242785

4269342786
#memory_tab_container, #wi_tab_container, #documentdb_tab_container {
42694-
height: 90%
42787+
height: 90%;
42788+
max-height: 90%;
42789+
overflow-y: auto;
4269542790
}
4269642791

4269742792
#wilist:not(.sidepanelsize) {

0 commit comments

Comments
 (0)