Sources in responses

This commit is contained in:
Alex
2023-06-11 22:56:34 +01:00
parent 577d58c92b
commit 610adcbefc
6 changed files with 147 additions and 78 deletions

View File

@@ -28,6 +28,14 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
if (data.type === 'end') {
// set status to 'idle'
dispatch(conversationSlice.actions.setStatus('idle'));
} else if (data.type === 'source') {
const result = data.doc;
dispatch(
updateStreamingSource({
index: state.conversation.queries.length - 1,
query: { sources: [{ title: result, text: result }] },
}),
);
} else {
const result = data.answer;
dispatch(
@@ -50,7 +58,7 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
dispatch(
updateQuery({
index: state.conversation.queries.length - 1,
query: { response: answer.answer },
query: { response: answer.answer, sources: answer.sources },
}),
);
dispatch(conversationSlice.actions.setStatus('idle'));
@@ -83,6 +91,17 @@ export const conversationSlice = createSlice({
};
}
},
updateStreamingSource(
state,
action: PayloadAction<{ index: number; query: Partial<Query> }>,
) {
const index = action.payload.index;
if (!state.queries[index].sources) {
state.queries[index].sources = [action.payload.query.sources![0]];
} else {
state.queries[index].sources!.push(action.payload.query.sources![0]);
}
},
updateQuery(
state,
action: PayloadAction<{ index: number; query: Partial<Query> }>,
@@ -116,6 +135,10 @@ export const selectQueries = (state: RootState) => state.conversation.queries;
export const selectStatus = (state: RootState) => state.conversation.status;
export const { addQuery, updateQuery, updateStreamingQuery } =
conversationSlice.actions;
export const {
addQuery,
updateQuery,
updateStreamingQuery,
updateStreamingSource,
} = conversationSlice.actions;
export default conversationSlice.reducer;