Clean-up vectors upon deleting a file from Data Bank
This commit is contained in:
parent
8434f6e6cf
commit
2eafa2a212
|
@ -448,6 +448,7 @@ export const event_types = {
|
||||||
CHARACTER_DELETED: 'characterDeleted',
|
CHARACTER_DELETED: 'characterDeleted',
|
||||||
CHARACTER_DUPLICATED: 'character_duplicated',
|
CHARACTER_DUPLICATED: 'character_duplicated',
|
||||||
SMOOTH_STREAM_TOKEN_RECEIVED: 'smooth_stream_token_received',
|
SMOOTH_STREAM_TOKEN_RECEIVED: 'smooth_stream_token_received',
|
||||||
|
FILE_ATTACHMENT_DELETED: 'file_attachment_deleted',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const eventSource = new EventEmitter();
|
export const eventSource = new EventEmitter();
|
||||||
|
|
|
@ -558,6 +558,7 @@ async function deleteFileFromServer(url) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await eventSource.emit(event_types.FILE_ATTACHMENT_DELETED, url);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toastr.error(String(error), 'Could not delete file');
|
toastr.error(String(error), 'Could not delete file');
|
||||||
|
|
|
@ -353,7 +353,8 @@ async function processFiles(chat) {
|
||||||
message.mes = message.mes.substring(message.extra.fileLength);
|
message.mes = message.mes.substring(message.extra.fileLength);
|
||||||
|
|
||||||
const fileName = message.extra.file.name;
|
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);
|
const hashesInCollection = await getSavedHashes(collectionId);
|
||||||
|
|
||||||
// File is already in the collection
|
// File is already in the collection
|
||||||
|
@ -748,6 +749,37 @@ async function queryMultipleCollections(collectionIds, searchText, topK) {
|
||||||
return await response.json();
|
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.
|
* Purges the vector index for a collection.
|
||||||
* @param {string} collectionId Collection ID to purge
|
* @param {string} collectionId Collection ID to purge
|
||||||
|
@ -1025,4 +1057,5 @@ jQuery(async () => {
|
||||||
eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
|
eventSource.on(event_types.MESSAGE_SWIPED, onChatEvent);
|
||||||
eventSource.on(event_types.CHAT_DELETED, purgeVectorIndex);
|
eventSource.on(event_types.CHAT_DELETED, purgeVectorIndex);
|
||||||
eventSource.on(event_types.GROUP_CHAT_DELETED, purgeVectorIndex);
|
eventSource.on(event_types.GROUP_CHAT_DELETED, purgeVectorIndex);
|
||||||
|
eventSource.on(event_types.FILE_ATTACHMENT_DELETED, purgeFileVectorIndex);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue