Clean-up vectors upon deleting a file from Data Bank

This commit is contained in:
Cohee 2024-04-18 23:07:16 +03:00
parent 8434f6e6cf
commit 2eafa2a212
3 changed files with 36 additions and 1 deletions

View File

@ -448,6 +448,7 @@ export const event_types = {
CHARACTER_DELETED: 'characterDeleted',
CHARACTER_DUPLICATED: 'character_duplicated',
SMOOTH_STREAM_TOKEN_RECEIVED: 'smooth_stream_token_received',
FILE_ATTACHMENT_DELETED: 'file_attachment_deleted',
};
export const eventSource = new EventEmitter();

View File

@ -558,6 +558,7 @@ async function deleteFileFromServer(url) {
throw new Error(error);
}
await eventSource.emit(event_types.FILE_ATTACHMENT_DELETED, url);
return true;
} catch (error) {
toastr.error(String(error), 'Could not delete file');

View File

@ -353,7 +353,8 @@ async function processFiles(chat) {
message.mes = message.mes.substring(message.extra.fileLength);
const fileName = message.extra.file.name;
const collectionId = `file_${getStringHash(fileName)}`;
const fileUrl = message.extra.file.url;
const collectionId = `file_${getStringHash(fileUrl)}`;
const hashesInCollection = await getSavedHashes(collectionId);
// File is already in the collection
@ -748,6 +749,37 @@ async function queryMultipleCollections(collectionIds, searchText, topK) {
return await response.json();
}
/**
* Purges the vector index for a file.
* @param {string} fileUrl File URL to purge
*/
async function purgeFileVectorIndex(fileUrl) {
try {
if (!settings.enabled_files) {
return;
}
console.log(`Vectors: Purging file vector index for ${fileUrl}`);
const collectionId = `file_${getStringHash(fileUrl)}`;
const response = await fetch('/api/vector/purge', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
collectionId: collectionId,
}),
});
if (!response.ok) {
throw new Error(`Could not delete vector index for collection ${collectionId}`);
}
console.log(`Vectors: Purged vector index for collection ${collectionId}`);
} catch (error) {
console.error('Vectors: Failed to purge file', error);
}
}
/**
* Purges the vector index for a collection.
* @param {string} collectionId Collection ID to purge
@ -1025,4 +1057,5 @@ jQuery(async () => {
eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
eventSource.on(event_types.CHAT_DELETED, purgeVectorIndex);
eventSource.on(event_types.GROUP_CHAT_DELETED, purgeVectorIndex);
eventSource.on(event_types.FILE_ATTACHMENT_DELETED, purgeFileVectorIndex);
});