-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thumb up/down feedback implement (#15)
* feat: implment thumb up/down feedback Signed-off-by: tygao <tygao@amazon.com> * use traceId to replace index Signed-off-by: tygao <tygao@amazon.com> * use primary color and remove disabled Signed-off-by: tygao <tygao@amazon.com> * use index and only enable feedback in markdown type Signed-off-by: tygao <tygao@amazon.com> * chore: nit Signed-off-by: tygao <tygao@amazon.com> * use findLast to get last input Signed-off-by: tygao <tygao@amazon.com> * nit Signed-off-by: tygao <tygao@amazon.com> * use meesage as params Signed-off-by: tygao <tygao@amazon.com> * update comment Signed-off-by: tygao <tygao@amazon.com> * remove extra statement Signed-off-by: tygao <tygao@amazon.com> * rename and simply Signed-off-by: tygao <tygao@amazon.com> --------- Signed-off-by: tygao <tygao@amazon.com>
- Loading branch information
Showing
3 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { useState } from 'react'; | ||
import { ASSISTANT_API } from '../../common/constants/llm'; | ||
import { IOutput } from '../../common/types/chat_saved_object_attributes'; | ||
import { useChatContext } from '../contexts/chat_context'; | ||
import { useCore } from '../contexts/core_context'; | ||
import { useChatState } from './use_chat_state'; | ||
|
||
interface SendFeedbackBody { | ||
metadata: { | ||
type: 'event_analytics' | 'chat' | 'ppl_submit'; | ||
sessionId?: string; | ||
traceId?: string; | ||
error?: boolean; | ||
}; | ||
input: string; | ||
output: string; | ||
correct: boolean | undefined; | ||
// Currently unused but required. | ||
expectedOutput: string; | ||
comment: string; | ||
} | ||
|
||
export const useFeedback = () => { | ||
const chatContext = useChatContext(); | ||
const core = useCore(); | ||
const { chatState } = useChatState(); | ||
const [feedbackResult, setFeedbackResult] = useState<undefined | boolean>(undefined); | ||
|
||
const sendFeedback = async (message: IOutput, correct: boolean) => { | ||
const outputMessage = message; | ||
// Markdown type output all has traceId. | ||
const outputMessageIndex = chatState.messages.findIndex((item) => { | ||
return item.type === 'output' && item.traceId === message.traceId; | ||
}); | ||
const inputMessage = chatState.messages | ||
.slice(0, outputMessageIndex) | ||
.findLast((item) => item.type === 'input'); | ||
if (!inputMessage) { | ||
return; | ||
} | ||
|
||
const body: SendFeedbackBody = { | ||
metadata: { | ||
type: 'chat', // currently type is only chat in feedback | ||
sessionId: chatContext.sessionId, | ||
traceId: outputMessage.traceId, | ||
error: false, | ||
}, | ||
input: inputMessage.content, | ||
output: outputMessage.content, | ||
correct, | ||
expectedOutput: '', | ||
comment: '', | ||
}; | ||
|
||
try { | ||
const response = await core.services.http.post(ASSISTANT_API.FEEDBACK, { | ||
body: JSON.stringify(body), | ||
}); | ||
setFeedbackResult(correct); | ||
} catch (error) { | ||
console.error('send feedback error'); | ||
} | ||
}; | ||
|
||
return { sendFeedback, feedbackResult }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters