feat(routes): add suggestions route
This commit is contained in:
parent
0f6986fc9b
commit
09463999c2
|
@ -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;
|
||||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue