Use Express router for vectors endpoint
This commit is contained in:
parent
414c9bd5fb
commit
68049afd84
|
@ -3612,7 +3612,7 @@ require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser);
|
||||||
require('./src/endpoints/horde').registerEndpoints(app, jsonParser);
|
require('./src/endpoints/horde').registerEndpoints(app, jsonParser);
|
||||||
|
|
||||||
// Vector storage DB
|
// Vector storage DB
|
||||||
require('./src/endpoints/vectors').registerEndpoints(app, jsonParser);
|
app.use('/api/vector', require('./src/endpoints/vectors').router);
|
||||||
|
|
||||||
// Chat translation
|
// Chat translation
|
||||||
require('./src/endpoints/translate').registerEndpoints(app, jsonParser);
|
require('./src/endpoints/translate').registerEndpoints(app, jsonParser);
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const vectra = require('vectra');
|
const vectra = require('vectra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const express = require('express');
|
||||||
const sanitize = require('sanitize-filename');
|
const sanitize = require('sanitize-filename');
|
||||||
|
const { jsonParser } = require('../express-common');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the vector for the given text from the given source.
|
* Gets the vector for the given text from the given source.
|
||||||
|
@ -112,113 +114,108 @@ async function queryCollection(collectionId, source, searchText, topK) {
|
||||||
return { metadata, hashes };
|
return { metadata, hashes };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
const router = express.Router();
|
||||||
* Registers the endpoints for the vector API
|
|
||||||
* @param {express.Express} app - Express app
|
|
||||||
* @param {any} jsonParser - Express JSON parser
|
|
||||||
*/
|
|
||||||
async function registerEndpoints(app, jsonParser) {
|
|
||||||
app.post('/api/vector/query', jsonParser, async (req, res) => {
|
|
||||||
try {
|
|
||||||
if (!req.body.collectionId || !req.body.searchText) {
|
|
||||||
return res.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
router.post('/query', jsonParser, async (req, res) => {
|
||||||
const searchText = String(req.body.searchText);
|
try {
|
||||||
const topK = Number(req.body.topK) || 10;
|
if (!req.body.collectionId || !req.body.searchText) {
|
||||||
const source = String(req.body.source) || 'transformers';
|
return res.sendStatus(400);
|
||||||
|
|
||||||
const results = await queryCollection(collectionId, source, searchText, topK);
|
|
||||||
return res.json(results);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.sendStatus(500);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/api/vector/insert', jsonParser, async (req, res) => {
|
const collectionId = String(req.body.collectionId);
|
||||||
try {
|
const searchText = String(req.body.searchText);
|
||||||
if (!Array.isArray(req.body.items) || !req.body.collectionId) {
|
const topK = Number(req.body.topK) || 10;
|
||||||
return res.sendStatus(400);
|
const source = String(req.body.source) || 'transformers';
|
||||||
}
|
|
||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
const results = await queryCollection(collectionId, source, searchText, topK);
|
||||||
const items = req.body.items.map(x => ({ hash: x.hash, text: x.text, index: x.index }));
|
return res.json(results);
|
||||||
const source = String(req.body.source) || 'transformers';
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
await insertVectorItems(collectionId, source, items);
|
router.post('/insert', jsonParser, async (req, res) => {
|
||||||
return res.sendStatus(200);
|
try {
|
||||||
} catch (error) {
|
if (!Array.isArray(req.body.items) || !req.body.collectionId) {
|
||||||
console.error(error);
|
return res.sendStatus(400);
|
||||||
return res.sendStatus(500);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/api/vector/list', jsonParser, async (req, res) => {
|
const collectionId = String(req.body.collectionId);
|
||||||
try {
|
const items = req.body.items.map(x => ({ hash: x.hash, text: x.text, index: x.index }));
|
||||||
if (!req.body.collectionId) {
|
const source = String(req.body.source) || 'transformers';
|
||||||
return res.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
await insertVectorItems(collectionId, source, items);
|
||||||
const source = String(req.body.source) || 'transformers';
|
return res.sendStatus(200);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const hashes = await getSavedHashes(collectionId, source);
|
router.post('/list', jsonParser, async (req, res) => {
|
||||||
return res.json(hashes);
|
try {
|
||||||
} catch (error) {
|
if (!req.body.collectionId) {
|
||||||
console.error(error);
|
return res.sendStatus(400);
|
||||||
return res.sendStatus(500);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/api/vector/delete', jsonParser, async (req, res) => {
|
const collectionId = String(req.body.collectionId);
|
||||||
try {
|
const source = String(req.body.source) || 'transformers';
|
||||||
if (!Array.isArray(req.body.hashes) || !req.body.collectionId) {
|
|
||||||
return res.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
const hashes = await getSavedHashes(collectionId, source);
|
||||||
const hashes = req.body.hashes.map(x => Number(x));
|
return res.json(hashes);
|
||||||
const source = String(req.body.source) || 'transformers';
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
await deleteVectorItems(collectionId, source, hashes);
|
router.post('/delete', jsonParser, async (req, res) => {
|
||||||
return res.sendStatus(200);
|
try {
|
||||||
} catch (error) {
|
if (!Array.isArray(req.body.hashes) || !req.body.collectionId) {
|
||||||
console.error(error);
|
return res.sendStatus(400);
|
||||||
return res.sendStatus(500);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/api/vector/purge', jsonParser, async (req, res) => {
|
const collectionId = String(req.body.collectionId);
|
||||||
try {
|
const hashes = req.body.hashes.map(x => Number(x));
|
||||||
if (!req.body.collectionId) {
|
const source = String(req.body.source) || 'transformers';
|
||||||
return res.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const collectionId = String(req.body.collectionId);
|
await deleteVectorItems(collectionId, source, hashes);
|
||||||
|
return res.sendStatus(200);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const sources = ['transformers', 'openai'];
|
router.post('/purge', jsonParser, async (req, res) => {
|
||||||
for (const source of sources) {
|
try {
|
||||||
const index = await getIndex(collectionId, source, false);
|
if (!req.body.collectionId) {
|
||||||
|
return res.sendStatus(400);
|
||||||
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 };
|
const collectionId = String(req.body.collectionId);
|
||||||
|
|
||||||
|
const sources = ['transformers', '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 = { router };
|
||||||
|
|
Loading…
Reference in New Issue