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); require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser);
// LLM and SD Horde generation // LLM and SD Horde generation
require('./src/endpoints/horde').registerEndpoints(app, jsonParser); app.use('/api/horde', require('./src/endpoints/horde').router);
// Vector storage DB // Vector storage DB
require('./src/endpoints/vectors').registerEndpoints(app, jsonParser); require('./src/endpoints/vectors').registerEndpoints(app, jsonParser);

View File

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