Delete vectors on deleting chats

This commit is contained in:
Cohee
2023-09-09 22:15:47 +03:00
parent ed6417ebcd
commit af38971a01
5 changed files with 86 additions and 5 deletions

View File

@@ -27,7 +27,8 @@ async function getOpenAIVector(text) {
});
if (!response.ok) {
console.log('OpenAI request failed');
const text = await response.text();
console.log('OpenAI request failed', response.statusText, text);
throw new Error('OpenAI request failed');
}

View File

@@ -24,12 +24,13 @@ async function getVector(source, text) {
* Gets the index for the vector collection
* @param {string} collectionId - The collection ID
* @param {string} source - The source of the vector
* @param {boolean} create - Whether to create the index if it doesn't exist
* @returns {Promise<vectra.LocalIndex>} - The index for the collection
*/
async function getIndex(collectionId, source) {
async function getIndex(collectionId, source, create = true) {
const index = new vectra.LocalIndex(path.join(process.cwd(), 'vectors', sanitize(source), sanitize(collectionId)));
if (!await index.isIndexCreated()) {
if (create && !await index.isIndexCreated()) {
await index.createIndex();
}
@@ -185,6 +186,36 @@ async function registerEndpoints(app, jsonParser) {
return res.sendStatus(500);
}
});
app.post('/api/vector/purge', jsonParser, async (req, res) => {
try {
if (!req.body.collectionId) {
return res.sendStatus(400);
}
const collectionId = String(req.body.collectionId);
const sources = ['local', 'openai'];
for (const source of sources) {
const index = await getIndex(collectionId, source, false);
const exists = await index.isIndexCreated();
if (!exists) {
continue;
}
const path = index.folderPath;
await index.deleteIndex();
console.log(`Deleted vector index at ${path}`);
}
return res.sendStatus(200);
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
}
module.exports = { registerEndpoints };