Skip to content

Commit

Permalink
Integrate feedback(Thumb up/down) with memory API (#37)
Browse files Browse the repository at this point in the history
* fix lint error

Signed-off-by: tygao <tygao@amazon.com>

* remove username and tenant in feedback body

Signed-off-by: tygao <tygao@amazon.com>

* remove extra statement

Signed-off-by: tygao <tygao@amazon.com>

* use object to store feedback

Signed-off-by: tygao <tygao@amazon.com>

* add constants for ml backend api

Signed-off-by: tygao <tygao@amazon.com>

* update

Signed-off-by: tygao <tygao@amazon.com>

* rebase route

Signed-off-by: tygao <tygao@amazon.com>

---------

Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao authored and SuZhou-Joe committed Dec 5, 2023
1 parent 1054c65 commit 4ff6726
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 95 deletions.
38 changes: 4 additions & 34 deletions public/hooks/use_feed_back.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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;
});
Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions server/routes/chat_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ const getTracesRoute = {
},
};

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(
Expand Down Expand Up @@ -321,4 +333,29 @@ export function registerChatRoutes(router: IRouter, routeOptions: RoutesOptions)
}
}
);

router.put(
feedbackRoute,
async (
context,
request,
response
): Promise<IOpenSearchDashboardsResponse<HttpResponsePayload | ResponseError>> => {
const storageService = createStorageService(context);
const { interactionId } = request.params;

try {
const updateResponse = await storageService.updateInteraction(interactionId, {
feedback: request.body,
});
return response.ok({ body: { ...updateResponse, success: true } });
} catch (error) {
context.assistant_plugin.logger.error(error);
return response.custom({
statusCode: error.statusCode || 500,
body: error.message,
});
}
}
);
}
59 changes: 0 additions & 59 deletions server/routes/feedback_routes.ts

This file was deleted.

2 changes: 0 additions & 2 deletions server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import { RoutesOptions } from '../types';
import { IRouter } from '../../../../src/core/server';
import { registerChatRoutes } from './chat_routes';
import { registerFeedbackRoutes } from './feedback_routes';

export function setupRoutes(router: IRouter, routeOptions: RoutesOptions) {
registerChatRoutes(router, routeOptions);
registerFeedbackRoutes(router);
}
28 changes: 28 additions & 0 deletions server/services/storage/agent_framework_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,32 @@ export class AgentFrameworkStorageService implements StorageService {
throw new Error('get traces failed, reason:' + JSON.stringify(error.meta?.body));
}
}

async updateInteraction(
interactionId: string,
additionalInfo: Record<string, Record<string, boolean | string>>
): Promise<SessionOptResponse> {
try {
const response = await this.client.transport.request({
method: 'PUT',
path: `${ML_COMMONS_BASE_API}/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));
}
}
}

0 comments on commit 4ff6726

Please sign in to comment.