Use Express router for horde endpoint

This commit is contained in:
valadaptive 2023-12-04 12:51:02 -05:00
parent 414c9bd5fb
commit 2e990bf336
2 changed files with 198 additions and 203 deletions

View File

@ -3609,7 +3609,7 @@ require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser);
require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser);
// LLM and SD Horde generation
require('./src/endpoints/horde').registerEndpoints(app, jsonParser);
app.use('/api/horde', require('./src/endpoints/horde').router);
// Vector storage DB
require('./src/endpoints/vectors').registerEndpoints(app, jsonParser);

View File

@ -1,7 +1,9 @@
const fetch = require('node-fetch').default;
const express = require('express');
const AIHorde = require('../ai_horde');
const { getVersion, delay } = require('../util');
const { readSecret, SECRET_KEYS } = require('./secrets');
const { jsonParser } = require('../express-common');
const ANONYMOUS_KEY = '0000000000';
@ -52,13 +54,9 @@ function sanitizeHordeImagePrompt(prompt) {
return prompt;
}
/**
*
* @param {import("express").Express} app
* @param {any} jsonParser
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/horde/generate-text', jsonParser, async (request, response) => {
const router = express.Router();
router.post('/generate-text', jsonParser, async (request, response) => {
const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY;
const url = 'https://horde.koboldai.net/api/v2/generate/text/async';
@ -86,9 +84,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.send({ error: true });
}
});
});
app.post('/api/horde/sd-samplers', jsonParser, async (_, response) => {
router.post('/sd-samplers', jsonParser, async (_, response) => {
try {
const ai_horde = await getHordeClient();
const samplers = Object.values(ai_horde.ModelGenerationInputStableSamplers);
@ -97,9 +95,9 @@ function registerEndpoints(app, jsonParser) {
console.error(error);
response.sendStatus(500);
}
});
});
app.post('/api/horde/sd-models', jsonParser, async (_, response) => {
router.post('/sd-models', jsonParser, async (_, response) => {
try {
const ai_horde = await getHordeClient();
const models = await ai_horde.getModels();
@ -108,9 +106,9 @@ function registerEndpoints(app, jsonParser) {
console.error(error);
response.sendStatus(500);
}
});
});
app.post('/api/horde/caption-image', jsonParser, async (request, response) => {
router.post('/caption-image', jsonParser, async (request, response) => {
try {
const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY;
const ai_horde = await getHordeClient();
@ -160,9 +158,9 @@ function registerEndpoints(app, jsonParser) {
console.error(error);
response.sendStatus(500);
}
});
});
app.post('/api/horde/user-info', jsonParser, async (_, response) => {
router.post('/user-info', jsonParser, async (_, response) => {
const api_key_horde = readSecret(SECRET_KEYS.HORDE);
if (!api_key_horde) {
@ -177,9 +175,9 @@ function registerEndpoints(app, jsonParser) {
console.error(error);
return response.sendStatus(500);
}
});
});
app.post('/api/horde/generate-image', jsonParser, async (request, response) => {
router.post('/generate-image', jsonParser, async (request, response) => {
if (!request.body.prompt) {
return response.sendStatus(400);
}
@ -264,9 +262,6 @@ function registerEndpoints(app, jsonParser) {
console.error(error);
return response.sendStatus(500);
}
});
}
});
module.exports = {
registerEndpoints,
};
module.exports = { router };