Use Express router for openai endpoint

This commit is contained in:
valadaptive 2023-12-04 12:53:17 -05:00
parent 414c9bd5fb
commit 2d19645c4e
2 changed files with 184 additions and 190 deletions

View File

@ -3576,7 +3576,7 @@ async function fetchJSON(url, args = {}) {
// ** END **
// OpenAI API
require('./src/endpoints/openai').registerEndpoints(app, jsonParser, urlencodedParser);
app.use('/api/openai', require('./src/endpoints/openai').router);
// Tokenizers
require('./src/endpoints/tokenizers').registerEndpoints(app, jsonParser);

View File

@ -1,16 +1,13 @@
const { readSecret, SECRET_KEYS } = require('./secrets');
const fetch = require('node-fetch').default;
const express = require('express');
const FormData = require('form-data');
const fs = require('fs');
const { jsonParser, urlencodedParser } = require('../express-common');
/**
* Registers the OpenAI endpoints.
* @param {import("express").Express} app Express app
* @param {any} jsonParser JSON parser
* @param {any} urlencodedParser Form data parser
*/
function registerEndpoints(app, jsonParser, urlencodedParser) {
app.post('/api/openai/caption-image', jsonParser, async (request, response) => {
const router = express.Router();
router.post('/caption-image', jsonParser, async (request, response) => {
try {
let key = '';
@ -86,9 +83,9 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
console.error(error);
response.status(500).send('Internal server error');
}
});
});
app.post('/api/openai/transcribe-audio', urlencodedParser, async (request, response) => {
router.post('/transcribe-audio', urlencodedParser, async (request, response) => {
try {
const key = readSecret(SECRET_KEYS.OPENAI);
@ -134,9 +131,9 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
console.error('OpenAI transcription failed', error);
response.status(500).send('Internal server error');
}
});
});
app.post('/api/openai/generate-voice', jsonParser, async (request, response) => {
router.post('/generate-voice', jsonParser, async (request, response) => {
try {
const key = readSecret(SECRET_KEYS.OPENAI);
@ -173,9 +170,9 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
console.error('OpenAI TTS generation failed', error);
response.status(500).send('Internal server error');
}
});
});
app.post('/api/openai/generate-image', jsonParser, async (request, response) => {
router.post('/generate-image', jsonParser, async (request, response) => {
try {
const key = readSecret(SECRET_KEYS.OPENAI);
@ -208,9 +205,6 @@ function registerEndpoints(app, jsonParser, urlencodedParser) {
console.error(error);
response.status(500).send('Internal server error');
}
});
}
});
module.exports = {
registerEndpoints,
};
module.exports = { router };