feat(routes): add config route

This commit is contained in:
ItzCrazyKns 2024-04-23 16:46:14 +05:30
parent a86378e726
commit 3ffbddd237
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
2 changed files with 60 additions and 0 deletions

58
src/routes/config.ts Normal file
View File

@ -0,0 +1,58 @@
import express from 'express';
import { getAvailableProviders } from '../lib/providers';
import {
getChatModel,
getChatModelProvider,
getOllamaApiEndpoint,
getOpenaiApiKey,
updateConfig,
} from '../config';
const router = express.Router();
router.get('/', async (_, res) => {
const config = {};
const providers = await getAvailableProviders();
for (const provider in providers) {
delete providers[provider]['embeddings'];
}
config['providers'] = {};
for (const provider in providers) {
config['providers'][provider] = Object.keys(providers[provider]);
}
config['selectedProvider'] = getChatModelProvider();
config['selectedChatModel'] = getChatModel();
config['openeaiApiKey'] = getOpenaiApiKey();
config['ollamaApiUrl'] = getOllamaApiEndpoint();
res.status(200).json(config);
});
router.post('/', async (req, res) => {
const config = req.body;
const updatedConfig = {
GENERAL: {
CHAT_MODEL_PROVIDER: config.selectedProvider,
CHAT_MODEL: config.selectedChatModel,
},
API_KEYS: {
OPENAI: config.openeaiApiKey,
},
API_ENDPOINTS: {
OLLAMA: config.ollamaApiUrl,
},
};
updateConfig(updatedConfig);
res.status(200).json({ message: 'Config updated' });
});
export default router;

View File

@ -1,8 +1,10 @@
import express from 'express'; import express from 'express';
import imagesRouter from './images'; import imagesRouter from './images';
import configRouter from './config';
const router = express.Router(); const router = express.Router();
router.use('/images', imagesRouter); router.use('/images', imagesRouter);
router.use('/config', configRouter);
export default router; export default router;