From 20c877f75bbb9a05eec028fe54bd051306ad1d73 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 31 May 2023 15:42:17 +0100 Subject: [PATCH] working fe --- application/app.py | 1 + .../src/conversation/conversationSlice.ts | 104 +++++++++--------- 2 files changed, 50 insertions(+), 55 deletions(-) diff --git a/application/app.py b/application/app.py index 5e823faa..789f3294 100644 --- a/application/app.py +++ b/application/app.py @@ -149,6 +149,7 @@ def stream(): #question = data["question"] # get parameter from url question question = request.args.get('question') + history = request.args.get('history') #question = "Hi" return Response(complete_stream(question), mimetype='text/event-stream') diff --git a/frontend/src/conversation/conversationSlice.ts b/frontend/src/conversation/conversationSlice.ts index ad30381e..713f9649 100644 --- a/frontend/src/conversation/conversationSlice.ts +++ b/frontend/src/conversation/conversationSlice.ts @@ -1,67 +1,66 @@ import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; import store from '../store'; import { fetchAnswerApi, fetchAnswerSteaming } from './conversationApi'; -import { Answer, ConversationState, Query } from './conversationModels'; -import { Dispatch } from 'react'; +import { Answer, ConversationState, Query, Status } from './conversationModels'; const initialState: ConversationState = { queries: [], status: 'idle', }; -const API_STREAMING = import.meta.env.API_STREAMING || true; +const API_STREAMING = import.meta.env.API_STREAMING || false; -export const fetchAnswer = createAsyncThunk< - Answer, - { question: string }, - { dispatch: Dispatch; state: RootState } ->('fetchAnswer', async ({ question }, { dispatch, getState }) => { - const state = getState(); +export const fetchAnswer = createAsyncThunk( + 'fetchAnswer', + async ({ question }, { dispatch, getState }) => { + const state = getState() as RootState; - if (state.preference) { - if (API_STREAMING) { - fetchAnswerSteaming( - question, - state.preference.apiKey, - state.preference.selectedDocs!, - (event) => { - const data = JSON.parse(event.data); - console.log(data); + if (state.preference) { + if (API_STREAMING) { + fetchAnswerSteaming( + question, + state.preference.apiKey, + state.preference.selectedDocs!, + (event) => { + const data = JSON.parse(event.data); + console.log(data); - // check if the 'end' event has been received - if (data.type === 'end') { - // set status to 'idle' - dispatch(conversationSlice.actions.setStatus('idle')); - } else { - const result = data.answer; - dispatch( - updateStreamingQuery({ - index: state.conversation.queries.length - 1, - query: { response: result }, - }), - ); - } - }, - ); - } else { - const answer = await fetchAnswerApi( - question, - state.preference.apiKey, - state.preference.selectedDocs!, - state.conversation.queries, - ); - if (answer) { - dispatch( - updateQuery({ - index: state.conversation.queries.length - 1, - query: { response: answer.answer }, - }), + // check if the 'end' event has been received + if (data.type === 'end') { + // set status to 'idle' + dispatch(conversationSlice.actions.setStatus('idle')); + } else { + const result = data.answer; + dispatch( + updateStreamingQuery({ + index: state.conversation.queries.length - 1, + query: { response: result }, + }), + ); + } + }, ); - dispatch(conversationSlice.actions.setStatus('idle')); + } else { + const answer = await fetchAnswerApi( + question, + state.preference.apiKey, + state.preference.selectedDocs!, + state.conversation.queries, + ); + if (answer) { + dispatch( + updateQuery({ + index: state.conversation.queries.length - 1, + query: { response: answer.answer }, + }), + ); + dispatch(conversationSlice.actions.setStatus('idle')); + } } } - } -}); + return { answer: '', query: question, result: '' }; + }, +); export const conversationSlice = createSlice({ name: 'conversation', @@ -96,7 +95,7 @@ export const conversationSlice = createSlice({ ...action.payload.query, }; }, - setStatus(state, action: PayloadAction) { + setStatus(state, action: PayloadAction) { state.status = action.payload; }, }, @@ -105,11 +104,6 @@ export const conversationSlice = createSlice({ .addCase(fetchAnswer.pending, (state) => { state.status = 'loading'; }) - .addCase(fetchAnswer.fulfilled, (state, action) => { - state.status = 'idle'; - state.queries[state.queries.length - 1].response = - action.payload.answer; - }) .addCase(fetchAnswer.rejected, (state, action) => { state.status = 'failed'; state.queries[state.queries.length - 1].error =