UNPKG

@websolutespa/payload-plugin-bowl-llm

Version:

LLM plugin for Bowl PayloadCms plugin

127 lines (126 loc) 4.42 kB
import { ResponseSuccess } from '@websolutespa/payload-utils/server'; import fs from 'fs'; import mime from 'mime-types'; import path from 'path'; import { addDataAndFileToRequest } from 'payload'; import { v4 as uuid } from 'uuid'; import { options } from '../options'; import { deleteFolderRecursive } from '../utils/knowledgebase'; import { errorHandler } from './error.handler'; import { pythonRulesVectorDbHandler } from './python/pythonRulesVectorDb.handler'; export const rulesEndpointHandler = async (req)=>{ try { await addDataAndFileToRequest(req); const { payload } = req; // get the app for which the rules should be generated const { appId } = req.data; const app = await req.payload.findByID({ collection: options.slug.llmApp, id: appId, overrideAccess: true }); const result = await rulesHandler(payload, app); return ResponseSuccess(result); } catch (error) { return errorHandler(error); } }; export function getRulesPath(basePath, appName) { return path.join(basePath, 'rules', appName); } export async function rulesHandler(payload, app) { let apps; if (app) { // single app rules generation (from the UI) apps = [ app ]; } else { // all apps rules generation (via cronjob) const response = await payload.find({ collection: options.slug.llmApp, pagination: false, overrideAccess: true }); apps = response.docs; } const importedApps = []; const tempRulesPath = path.join(process.cwd(), 'lib', 'rules', uuid()); for (const app of apps){ try { // get active app rules const rules = await payload.find({ collection: options.slug.llmRule, where: { isActive: { equals: true }, llmApp: { equals: app.id } }, overrideAccess: true }); if (!rules.docs.length) { continue; } const appPath = path.join(tempRulesPath, app.name); fs.mkdirSync(appPath, { recursive: true }); const dbVectorFilename = await pythonRulesVectorDbHandler({ rules: rules.docs.map((doc)=>doc.description), secrets: app.settings.llmConfig.secrets, provider: app.settings.llmConfig.provider, model: app.settings.llmConfig.model, downloadPath: appPath }); // save db vector file in the LlmApp const dbVectorPath = path.join(appPath, dbVectorFilename); const dbVectorDoc = await payload.create({ collection: options.slug.llmVectorDb, data: { notes: `Rules app "${app.name}"`, llmApp: app.id }, file: { data: fs.readFileSync(dbVectorPath), mimetype: mime.lookup(dbVectorPath), name: dbVectorFilename, size: fs.statSync(dbVectorPath).size }, overrideAccess: true }); await payload.update({ collection: options.slug.llmApp, id: app.id, data: { settings: { rules: { vectorDbFile: dbVectorDoc.id } } }, overrideAccess: true }); // delete obsolete rules directory const rulesPath = getRulesPath(process.cwd(), app.name); deleteFolderRecursive(rulesPath); importedApps.push({ name: app.name, vectorDbFile: dbVectorDoc.id, status: 'success' }); } catch (error) { importedApps.push({ name: app.name, status: 'error', error: error.message }); } } // delete temp directory deleteFolderRecursive(tempRulesPath); return importedApps; } //# sourceMappingURL=rules.handler.js.map