From 3ffbddd237b581c64119ba76854d170582cc8cce Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Tue, 23 Apr 2024 16:46:14 +0530 Subject: [PATCH] feat(routes): add config route --- src/routes/config.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/routes/index.ts | 2 ++ 2 files changed, 60 insertions(+) create mode 100644 src/routes/config.ts diff --git a/src/routes/config.ts b/src/routes/config.ts new file mode 100644 index 0000000..ecdec17 --- /dev/null +++ b/src/routes/config.ts @@ -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; diff --git a/src/routes/index.ts b/src/routes/index.ts index f2800cf..a3262e4 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,8 +1,10 @@ import express from 'express'; import imagesRouter from './images'; +import configRouter from './config'; const router = express.Router(); router.use('/images', imagesRouter); +router.use('/config', configRouter); export default router;