@websolutespa/payload-plugin-bowl-llm
Version:
LLM plugin for Bowl PayloadCms plugin
102 lines (101 loc) • 4.16 kB
JavaScript
import { ResponseError, ResponseSuccess } from '@websolutespa/payload-utils/server';
import payload, { addDataAndFileToRequest } from 'payload';
import { options } from '../options';
import { fetchRobot } from '../utils/robot';
import { appHandler } from './app.handler';
import { errorHandler } from './error.handler';
const postRobotFeedback = async (app, thread, llmMessage)=>{
// get the user message from the thread: it's the message before the llmMessage
const userMessageIndex = thread.message.findIndex((msg)=>msg.messageId === llmMessage.messageId) - 1;
const userMessage = thread.message[userMessageIndex];
// get the provider from the app settings
const provider = 'nebuly'; // TODO: add new provider field in the app, return that value
const request = {
api_key: '',
provider: provider,
user_id: thread.id,
comment: llmMessage.feedback?.notes || '',
rating: llmMessage.feedback?.rating || 0,
anonymize: true,
timestamp: new Date().toISOString(),
message_id: llmMessage.messageId,
message_input: userMessage?.content || '',
message_output: llmMessage.content || ''
};
// provider specific settings
switch(provider){
case 'nebuly':
request.api_key = app.settings?.llmConfig?.secrets?.nebulyApiKey || '';
break;
}
// post feedback to robot
//payload.logger.info(`Sending feedback to ROBOT: ${JSON.stringify(request)}`);
try {
const response = await fetchRobot('api/llm/feedback', 'POST', request);
payload.logger.info(`Feedback sent to ROBOT successfully. ROBOT response: ${JSON.stringify(response)}`);
} catch (error) {
payload.logger.error(`Error while sending feedback to ROBOT: ${error}`);
throw error;
}
};
export const feedbackHandler = async (req)=>{
try {
await addDataAndFileToRequest(req);
const app = await appHandler(req);
const feedback = req.data;
const thread = await req.payload.findByID({
collection: options.slug.llmThread,
id: feedback.threadId,
overrideAccess: true
});
if (!thread) {
throw new Error(`Thread with id ${feedback.threadId} not found`);
}
if (!thread.message.length) {
throw new Error(`Thread with id ${feedback.threadId} has no messages`);
}
let threadMessage;
if (feedback.messageId) {
// if messageId is provided, check if it exists in the thread
threadMessage = thread.message.find((msg)=>msg.messageId === feedback.messageId);
if (!threadMessage) {
throw new Error(`Message with id ${feedback.messageId} not found in thread ${feedback.threadId}`);
}
} else {
// if no messageId is provided, we assume the feedback is for the last message
threadMessage = thread.message[thread.message.length - 1];
}
if (!threadMessage) {
return ResponseError('Thread not found');
}
// add feedback to the message
threadMessage.feedback = {
rating: feedback.feedbackRating,
notes: feedback.feedbackMessage || ''
};
// update the message in the thread
thread.message = thread.message.map((msg)=>msg.messageId === threadMessage.messageId ? threadMessage : msg);
// save feedback in db
await req.payload.update({
collection: options.slug.llmThread,
id: thread.id,
data: {
message: thread.message
},
overrideAccess: true
});
// post feedback to robot
try {
await postRobotFeedback(app, thread, threadMessage);
} catch (error) {
throw new Error(`Failed to post feedback to ROBOT: ${error}`);
}
return ResponseSuccess({
status: 200,
message: 'feedback saved successfully'
});
} catch (error) {
return errorHandler(error);
}
};
//# sourceMappingURL=feedback.handler.js.map