diff --git a/public/hooks/use_feed_back.tsx b/public/hooks/use_feed_back.tsx index 30ef375b..ea0ce047 100644 --- a/public/hooks/use_feed_back.tsx +++ b/public/hooks/use_feed_back.tsx @@ -10,25 +10,8 @@ import { useChatContext } from '../contexts/chat_context'; import { useCore } from '../contexts/core_context'; import { useChatState } from './use_chat_state'; -interface AccountResponse { - data: { user_name: string; user_requested_tenant: string; roles: string[] }; -} - interface SendFeedbackBody { - metadata: { - type: 'event_analytics' | 'chat' | 'ppl_submit'; - sessionId?: string; - traceId?: string; - error?: boolean; - user: string; - tenant: string; - }; - input: string; - output: string; - correct: boolean | undefined; - // Currently unused but required. - expectedOutput: string; - comment: string; + satisfaction: boolean; } export const useFeedback = () => { @@ -39,7 +22,7 @@ export const useFeedback = () => { const sendFeedback = async (message: IOutput, correct: boolean) => { const outputMessage = message; - // Markdown type output all has traceId. + // Markdown type output all has traceId. The traceId of message is equal to interaction id. const outputMessageIndex = chatState.messages.findIndex((item) => { return item.type === 'output' && item.traceId === message.traceId; }); @@ -50,25 +33,12 @@ export const useFeedback = () => { return; } - const { username, tenant } = chatContext.currentAccount; const body: SendFeedbackBody = { - metadata: { - type: 'chat', // currently type is only chat in feedback - sessionId: chatContext.sessionId, - traceId: outputMessage.traceId, - error: false, - user: username, - tenant, - }, - input: inputMessage.content, - output: outputMessage.content, - correct, - expectedOutput: '', - comment: '', + satisfaction: correct, }; try { - await core.services.http.post(ASSISTANT_API.FEEDBACK, { + await core.services.http.put(`${ASSISTANT_API.FEEDBACK}/${message.traceId}`, { body: JSON.stringify(body), }); setFeedbackResult(correct); diff --git a/server/routes/chat_routes.ts b/server/routes/chat_routes.ts index 632babe4..2744f05d 100644 --- a/server/routes/chat_routes.ts +++ b/server/routes/chat_routes.ts @@ -105,6 +105,18 @@ const updateSessionRoute = { }, }; +const feedbackRoute = { + path: `${ASSISTANT_API.FEEDBACK}/{interactionId}`, + validate: { + params: schema.object({ + interactionId: schema.string(), + }), + body: schema.object({ + satisfaction: schema.boolean(), + }), + }, +}; + export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions) { const createStorageService = (context: RequestHandlerContext) => new AgentFrameworkStorageService( @@ -293,4 +305,26 @@ export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions) } } ); + + router.put( + feedbackRoute, + async ( + context, + request, + response + ): Promise> => { + const storageService = createStorageService(context); + const { interactionId } = request.params; + + try { + const getResponse = await storageService.updateInteraction(interactionId, { + feedback: request.body, + }); + return response.ok({ body: getResponse }); + } catch (error) { + context.assistant_plugin.logger.error(error); + return response.custom({ statusCode: error.statusCode || 500, body: error.message }); + } + } + ); } diff --git a/server/routes/langchain_routes.ts b/server/routes/langchain_routes.ts index a59e703f..08d90c27 100644 --- a/server/routes/langchain_routes.ts +++ b/server/routes/langchain_routes.ts @@ -130,48 +130,4 @@ export function registerLangchainRoutes(router: IRouter) { } } ); - - router.post( - { - path: ASSISTANT_API.FEEDBACK, - validate: { - body: schema.object({ - metadata: schema.object({ - user: schema.string(), - tenant: schema.string(), - type: schema.string(), - sessionId: schema.maybe(schema.string()), - traceId: schema.maybe(schema.string()), - error: schema.maybe(schema.boolean()), - selectedIndex: schema.maybe(schema.string()), - }), - input: schema.string(), - output: schema.string(), - correct: schema.boolean(), - expectedOutput: schema.string(), - comment: schema.string(), - }), - }, - }, - async ( - context, - request, - response - ): Promise> => { - try { - await context.core.opensearch.client.asCurrentUser.index({ - index: LLM_INDEX.FEEDBACK, - body: { ...request.body, timestamp: new Date().toISOString() }, - }); - - return response.ok(); - } catch (error) { - console.error(error); - return response.custom({ - statusCode: error.statusCode || 500, - body: error.message, - }); - } - } - ); } diff --git a/server/services/storage/agent_framework_storage_service.ts b/server/services/storage/agent_framework_storage_service.ts index d80f5b7e..b61c323d 100644 --- a/server/services/storage/agent_framework_storage_service.ts +++ b/server/services/storage/agent_framework_storage_service.ts @@ -181,4 +181,32 @@ export class AgentFrameworkStorageService implements StorageService { throw new Error('update converstaion failed, reason:' + JSON.stringify(error.meta?.body)); } } + + async updateInteraction( + interactionId: string, + additionalInfo: Record> + ): Promise { + try { + const response = await this.client.transport.request({ + method: 'PUT', + path: `/_plugins/_ml/memory/interaction/${interactionId}/_update`, + body: { + additional_info: additionalInfo, + }, + }); + if (response.statusCode === 200) { + return { + success: true, + }; + } else { + return { + success: false, + statusCode: response.statusCode, + message: JSON.stringify(response.body), + }; + } + } catch (error) { + throw new Error('update interaction failed, reason:' + JSON.stringify(error.meta?.body)); + } + } }