Skip to content

Commit

Permalink
fix lint error
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <[email protected]>
  • Loading branch information
raintygao committed Nov 29, 2023
1 parent 979404d commit f331a8d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 73 deletions.
39 changes: 10 additions & 29 deletions public/hooks/use_feed_back.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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;
});
Expand All @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions server/routes/chat_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -293,4 +307,24 @@ 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 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 });
}
}
);
}
44 changes: 0 additions & 44 deletions server/routes/langchain_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenSearchDashboardsResponse<HttpResponsePayload | ResponseError>> => {
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,
});
}
}
);
}
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 @@ -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<string, string | boolean>
): Promise<SessionOptResponse> {
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));
}
}
}

0 comments on commit f331a8d

Please sign in to comment.