Debug function to purge all vectors

This commit is contained in:
Cohee 2024-06-22 17:41:02 +03:00
parent b8830e34d3
commit 8564d6faa8
2 changed files with 49 additions and 1 deletions

View File

@ -20,7 +20,7 @@ import {
renderExtensionTemplateAsync,
doExtrasFetch, getApiUrl,
} from '../../extensions.js';
import { collapseNewlines } from '../../power-user.js';
import { collapseNewlines, registerDebugFunction } from '../../power-user.js';
import { SECRET_KEYS, secret_state, writeSecret } from '../../secrets.js';
import { getDataBankAttachments, getDataBankAttachmentsForSource, getFileAttachment } from '../../chats.js';
import { debounce, getStringHash as calculateHash, waitUntilCondition, onlyUnique, splitRecursive, trimToStartSentence, trimToEndSentence } from '../../utils.js';
@ -989,6 +989,28 @@ async function purgeVectorIndex(collectionId) {
}
}
/**
* Purges all vector indexes.
*/
async function purgeAllVectorIndexes() {
try {
const response = await fetch('/api/vector/purge-all', {
method: 'POST',
headers: getRequestHeaders(),
});
if (!response.ok) {
throw new Error('Failed to purge all vector indexes');
}
console.log('Vectors: Purged all vector indexes');
toastr.success('All vector indexes purged', 'Purge successful');
} catch (error) {
console.error('Vectors: Failed to purge all', error);
toastr.error('Failed to purge all vector indexes', 'Purge failed');
}
}
function toggleSettings() {
$('#vectors_files_settings').toggle(!!settings.enabled_files);
$('#vectors_chats_settings').toggle(!!settings.enabled_chats);
@ -1578,4 +1600,11 @@ jQuery(async () => {
],
returns: ARGUMENT_TYPE.LIST,
}));
registerDebugFunction('purge-everything', 'Purge all vector indices', 'Obliterate all stored vectors for all sources. No mercy.', async () => {
if (!confirm('Are you sure?')) {
return;
}
await purgeAllVectorIndexes();
});
});

View File

@ -1,5 +1,6 @@
const vectra = require('vectra');
const path = require('path');
const fs = require('fs');
const express = require('express');
const sanitize = require('sanitize-filename');
const { jsonParser } = require('../express-common');
@ -440,6 +441,24 @@ router.post('/delete', jsonParser, async (req, res) => {
}
});
router.post('/purge-all', jsonParser, async (req, res) => {
try {
for (const source of SOURCES) {
const sourcePath = path.join(req.user.directories.vectors, sanitize(source));
if (!fs.existsSync(sourcePath)) {
continue;
}
await fs.promises.rm(sourcePath, { recursive: true });
console.log(`Deleted vector source store at ${sourcePath}`);
}
return res.sendStatus(200);
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
router.post('/purge', jsonParser, async (req, res) => {
try {
if (!req.body.collectionId) {