feat(routes): add suggestions route

This commit is contained in:
ItzCrazyKns 2024-05-09 20:42:03 +05:30
parent 0f6986fc9b
commit 09463999c2
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
2 changed files with 48 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import imagesRouter from './images';
import videosRouter from './videos'; import videosRouter from './videos';
import configRouter from './config'; import configRouter from './config';
import modelsRouter from './models'; import modelsRouter from './models';
import suggestionsRouter from './suggestions';
const router = express.Router(); const router = express.Router();
@ -10,5 +11,6 @@ router.use('/images', imagesRouter);
router.use('/videos', videosRouter); router.use('/videos', videosRouter);
router.use('/config', configRouter); router.use('/config', configRouter);
router.use('/models', modelsRouter); router.use('/models', modelsRouter);
router.use('/suggestions', suggestionsRouter);
export default router; export default router;

46
src/routes/suggestions.ts Normal file
View File

@ -0,0 +1,46 @@
import express from 'express';
import generateSuggestions from '../agents/suggestionGeneratorAgent';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { getAvailableChatModelProviders } from '../lib/providers';
import { HumanMessage, AIMessage } from '@langchain/core/messages';
import logger from '../utils/logger';
const router = express.Router();
router.post('/', async (req, res) => {
try {
let { chat_history, chat_model, chat_model_provider } = req.body;
chat_history = chat_history.map((msg: any) => {
if (msg.role === 'user') {
return new HumanMessage(msg.content);
} else if (msg.role === 'assistant') {
return new AIMessage(msg.content);
}
});
const chatModels = await getAvailableChatModelProviders();
const provider = chat_model_provider || Object.keys(chatModels)[0];
const chatModel = chat_model || Object.keys(chatModels[provider])[0];
let llm: BaseChatModel | undefined;
if (chatModels[provider] && chatModels[provider][chatModel]) {
llm = chatModels[provider][chatModel] as BaseChatModel | undefined;
}
if (!llm) {
res.status(500).json({ message: 'Invalid LLM model selected' });
return;
}
const suggestions = await generateSuggestions({ chat_history }, llm);
res.status(200).json({ suggestions: suggestions });
} catch (err) {
res.status(500).json({ message: 'An error has occurred.' });
logger.error(`Error in generating suggestions: ${err.message}`);
}
});
export default router;