UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

86 lines (85 loc) 2.8 kB
import { InMemoryCache } from '@websolutespa/payload-plugin-bowl'; import { ResponseBadRequest, ResponseSuccess } from '@websolutespa/payload-utils/server'; import { addDataAndFileToRequest } from 'payload'; import { options } from '../options'; import { fetchRobot } from '../utils/robot'; import { errorHandler } from './error.handler'; const CACHE_ = new InMemoryCache(); const getLlmModels = async (payload, app, provider)=>{ const apiKey = app.settings.llmConfig.secrets.apiKey; if (!apiKey) { throw { status: 401, message: 'LLM Api Key is missing. Enter a valid key, then save and reload the app' }; } const cacheKey = `${apiKey}_${provider}_models`; if (CACHE_.has(cacheKey)) { return CACHE_.get(cacheKey); } const models = await fetchRobot(`api/llm/${provider}/models`, 'POST', app.settings.llmConfig.secrets); CACHE_.set(cacheKey, models); return models; }; // function based on the original appHandler, return apps that are also disabled export const getApp = async (payload, req)=>{ const { appKey, apiKey } = req.data; const referrer = req.headers.get('referrer') || req.headers.get('referer') || ''; if (!appKey) { throw { status: 401, message: 'Unauthorized: appKey is missing' }; } if (!apiKey) { throw { status: 401, message: 'Unauthorized: apiKey is missing' }; } if (referrer !== '') { throw { status: 401, message: `Unauthorized: referrer is not allowed [${referrer}]` }; } const response = await payload.find({ collection: options.slug.llmApp, where: { 'settings.credentials.appKey': { equals: appKey }, 'settings.credentials.apiKey': { equals: apiKey } }, depth: 3, showHiddenFields: true, pagination: false, overrideAccess: true }); if (!response.docs.length) { throw { status: 404, message: 'Not Found: application not found' }; } const app = response.docs[0]; return app; }; export const llmModelsHandler = async (req)=>{ try { const { payload } = req; const provider = req.routeParams?.provider; if (typeof provider !== 'string') { return ResponseBadRequest('missing provider'); } await addDataAndFileToRequest(req); const app = await getApp(payload, req); const models = await getLlmModels(payload, app, provider); return ResponseSuccess(models); } catch (error) { return errorHandler(error); } }; //# sourceMappingURL=llmModels.handler.js.map