Use Express router for vectors endpoint

This commit is contained in:
valadaptive 2023-12-04 13:01:52 -05:00
parent 414c9bd5fb
commit 68049afd84
2 changed files with 89 additions and 92 deletions

View File

@ -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);

View File

@ -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,13 +114,9 @@ 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 router.post('/query', jsonParser, async (req, res) => {
* @param {any} jsonParser - Express JSON parser
*/
async function registerEndpoints(app, jsonParser) {
app.post('/api/vector/query', jsonParser, async (req, res) => {
try { try {
if (!req.body.collectionId || !req.body.searchText) { if (!req.body.collectionId || !req.body.searchText) {
return res.sendStatus(400); return res.sendStatus(400);
@ -137,7 +135,7 @@ async function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/vector/insert', jsonParser, async (req, res) => { router.post('/insert', jsonParser, async (req, res) => {
try { try {
if (!Array.isArray(req.body.items) || !req.body.collectionId) { if (!Array.isArray(req.body.items) || !req.body.collectionId) {
return res.sendStatus(400); return res.sendStatus(400);
@ -155,7 +153,7 @@ async function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/vector/list', jsonParser, async (req, res) => { router.post('/list', jsonParser, async (req, res) => {
try { try {
if (!req.body.collectionId) { if (!req.body.collectionId) {
return res.sendStatus(400); return res.sendStatus(400);
@ -172,7 +170,7 @@ async function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/vector/delete', jsonParser, async (req, res) => { router.post('/delete', jsonParser, async (req, res) => {
try { try {
if (!Array.isArray(req.body.hashes) || !req.body.collectionId) { if (!Array.isArray(req.body.hashes) || !req.body.collectionId) {
return res.sendStatus(400); return res.sendStatus(400);
@ -190,7 +188,7 @@ async function registerEndpoints(app, jsonParser) {
} }
}); });
app.post('/api/vector/purge', jsonParser, async (req, res) => { router.post('/purge', jsonParser, async (req, res) => {
try { try {
if (!req.body.collectionId) { if (!req.body.collectionId) {
return res.sendStatus(400); return res.sendStatus(400);
@ -219,6 +217,5 @@ async function registerEndpoints(app, jsonParser) {
return res.sendStatus(500); return res.sendStatus(500);
} }
}); });
}
module.exports = { registerEndpoints }; module.exports = { router };