From f331a8d543b1ad314c5a078bc14245059fc76927 Mon Sep 17 00:00:00 2001 From: tygao Date: Wed, 29 Nov 2023 17:29:44 +0800 Subject: [PATCH 1/4] fix lint error Signed-off-by: tygao --- public/hooks/use_feed_back.tsx | 39 +++++----------- server/routes/chat_routes.ts | 34 ++++++++++++++ server/routes/langchain_routes.ts | 44 ------------------- .../agent_framework_storage_service.ts | 28 ++++++++++++ 4 files changed, 72 insertions(+), 73 deletions(-) diff --git a/public/hooks/use_feed_back.tsx b/public/hooks/use_feed_back.tsx index 30ef375b..71ffcd99 100644 --- a/public/hooks/use_feed_back.tsx +++ b/public/hooks/use_feed_back.tsx @@ -15,20 +15,9 @@ interface AccountResponse { } 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; + username: string; + tenant: string; + feedback: boolean; } export const useFeedback = () => { @@ -39,7 +28,9 @@ 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 { traceId } = outputMessage; + if (!traceId) return; const outputMessageIndex = chatState.messages.findIndex((item) => { return item.type === 'output' && item.traceId === message.traceId; }); @@ -52,23 +43,13 @@ export const useFeedback = () => { 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: '', + username, + tenant, + feedback: correct, }; try { - await core.services.http.post(ASSISTANT_API.FEEDBACK, { + await core.services.http.put(`${ASSISTANT_API.FEEDBACK}/${traceId}`, { body: JSON.stringify(body), }); setFeedbackResult(correct); diff --git a/server/routes/chat_routes.ts b/server/routes/chat_routes.ts index 632babe4..3b3154be 100644 --- a/server/routes/chat_routes.ts +++ b/server/routes/chat_routes.ts @@ -105,6 +105,20 @@ const updateSessionRoute = { }, }; +const feedbackRoute = { + path: `${ASSISTANT_API.FEEDBACK}/{interactionId}`, + validate: { + params: schema.object({ + interactionId: schema.string(), + }), + body: schema.object({ + username: schema.string(), + tenant: schema.string(), + feedback: schema.boolean(), + }), + }, +}; + export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions) { const createStorageService = (context: RequestHandlerContext) => new AgentFrameworkStorageService( @@ -293,4 +307,24 @@ 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, 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..ea41a749 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)); + } + } } From 6628eb0ab7b8159400e03841d6f3e8f6bb2ae1d8 Mon Sep 17 00:00:00 2001 From: tygao Date: Thu, 30 Nov 2023 11:00:17 +0800 Subject: [PATCH 2/4] remove username and tenant in feedback body Signed-off-by: tygao --- public/hooks/use_feed_back.tsx | 9 --------- server/routes/chat_routes.ts | 2 -- 2 files changed, 11 deletions(-) diff --git a/public/hooks/use_feed_back.tsx b/public/hooks/use_feed_back.tsx index 71ffcd99..5afe8f7d 100644 --- a/public/hooks/use_feed_back.tsx +++ b/public/hooks/use_feed_back.tsx @@ -10,13 +10,7 @@ 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 { - username: string; - tenant: string; feedback: boolean; } @@ -41,10 +35,7 @@ export const useFeedback = () => { return; } - const { username, tenant } = chatContext.currentAccount; const body: SendFeedbackBody = { - username, - tenant, feedback: correct, }; diff --git a/server/routes/chat_routes.ts b/server/routes/chat_routes.ts index 3b3154be..cffd5eda 100644 --- a/server/routes/chat_routes.ts +++ b/server/routes/chat_routes.ts @@ -112,8 +112,6 @@ const feedbackRoute = { interactionId: schema.string(), }), body: schema.object({ - username: schema.string(), - tenant: schema.string(), feedback: schema.boolean(), }), }, From 51fd1b0d50cea91670ec47b0f70cfc48c3767ccc Mon Sep 17 00:00:00 2001 From: tygao Date: Thu, 30 Nov 2023 11:29:48 +0800 Subject: [PATCH 3/4] remove extra statement Signed-off-by: tygao --- public/hooks/use_feed_back.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/public/hooks/use_feed_back.tsx b/public/hooks/use_feed_back.tsx index 5afe8f7d..d498c792 100644 --- a/public/hooks/use_feed_back.tsx +++ b/public/hooks/use_feed_back.tsx @@ -23,8 +23,6 @@ export const useFeedback = () => { const sendFeedback = async (message: IOutput, correct: boolean) => { const outputMessage = message; // Markdown type output all has traceId. The traceId of message is equal to interaction id. - const { traceId } = outputMessage; - if (!traceId) return; const outputMessageIndex = chatState.messages.findIndex((item) => { return item.type === 'output' && item.traceId === message.traceId; }); @@ -40,7 +38,7 @@ export const useFeedback = () => { }; try { - await core.services.http.put(`${ASSISTANT_API.FEEDBACK}/${traceId}`, { + await core.services.http.put(`${ASSISTANT_API.FEEDBACK}/${message.traceId}`, { body: JSON.stringify(body), }); setFeedbackResult(correct); From 1b0dc9073ce065ef643967c08bfccf391f9b4fd5 Mon Sep 17 00:00:00 2001 From: tygao Date: Thu, 30 Nov 2023 15:10:20 +0800 Subject: [PATCH 4/4] use object to store feedback Signed-off-by: tygao --- public/hooks/use_feed_back.tsx | 4 ++-- server/routes/chat_routes.ts | 6 ++++-- server/services/storage/agent_framework_storage_service.ts | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/public/hooks/use_feed_back.tsx b/public/hooks/use_feed_back.tsx index d498c792..ea0ce047 100644 --- a/public/hooks/use_feed_back.tsx +++ b/public/hooks/use_feed_back.tsx @@ -11,7 +11,7 @@ import { useCore } from '../contexts/core_context'; import { useChatState } from './use_chat_state'; interface SendFeedbackBody { - feedback: boolean; + satisfaction: boolean; } export const useFeedback = () => { @@ -34,7 +34,7 @@ export const useFeedback = () => { } const body: SendFeedbackBody = { - feedback: correct, + satisfaction: correct, }; try { diff --git a/server/routes/chat_routes.ts b/server/routes/chat_routes.ts index cffd5eda..2744f05d 100644 --- a/server/routes/chat_routes.ts +++ b/server/routes/chat_routes.ts @@ -112,7 +112,7 @@ const feedbackRoute = { interactionId: schema.string(), }), body: schema.object({ - feedback: schema.boolean(), + satisfaction: schema.boolean(), }), }, }; @@ -317,7 +317,9 @@ export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions) const { interactionId } = request.params; try { - const getResponse = await storageService.updateInteraction(interactionId, request.body); + const getResponse = await storageService.updateInteraction(interactionId, { + feedback: request.body, + }); return response.ok({ body: getResponse }); } catch (error) { context.assistant_plugin.logger.error(error); diff --git a/server/services/storage/agent_framework_storage_service.ts b/server/services/storage/agent_framework_storage_service.ts index ea41a749..b61c323d 100644 --- a/server/services/storage/agent_framework_storage_service.ts +++ b/server/services/storage/agent_framework_storage_service.ts @@ -184,7 +184,7 @@ export class AgentFrameworkStorageService implements StorageService { async updateInteraction( interactionId: string, - additionalInfo: Record + additionalInfo: Record> ): Promise { try { const response = await this.client.transport.request({