From 919a87bf478c8229f07c3fb5e886f408140d537d Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Wed, 11 Sep 2024 17:55:31 +0530 Subject: [PATCH 1/2] fix: load sources on shared conversations --- application/api/answer/routes.py | 2 +- application/api/user/routes.py | 2 -- .../src/conversation/SharedConversation.tsx | 1 + .../src/conversation/conversationHandlers.ts | 21 ++++++++++++++ .../conversation/sharedConversationSlice.ts | 29 +++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/application/api/answer/routes.py b/application/api/answer/routes.py index 89a26b1d..49147477 100644 --- a/application/api/answer/routes.py +++ b/application/api/answer/routes.py @@ -429,7 +429,7 @@ def api_search(): data_key = get_data_from_api_key(data["api_key"]) chunks = int(data_key["chunks"]) source = {"active_docs":data_key["source"]} - user_api_key = data_key["api_key"] + user_api_key = data["api_key"] elif "active_docs" in data: source = {"active_docs":data["active_docs"]} user_api_key = None diff --git a/application/api/user/routes.py b/application/api/user/routes.py index 91e343fc..d7ca3a2e 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -593,8 +593,6 @@ def get_publicly_shared_conversations(identifier: str): 404, ) conversation_queries = conversation["queries"][: (shared["first_n_queries"])] - for query in conversation_queries: - query.pop("sources") ## avoid exposing sources else: return ( jsonify( diff --git a/frontend/src/conversation/SharedConversation.tsx b/frontend/src/conversation/SharedConversation.tsx index 8619b0c2..139c8246 100644 --- a/frontend/src/conversation/SharedConversation.tsx +++ b/frontend/src/conversation/SharedConversation.tsx @@ -144,6 +144,7 @@ export const SharedConversation = () => { key={`${index}ANSWER`} message={query.response} type={'ANSWER'} + sources={query.sources ?? []} > ); } else if (query.error) { diff --git a/frontend/src/conversation/conversationHandlers.ts b/frontend/src/conversation/conversationHandlers.ts index eeefd0f8..338ed864 100644 --- a/frontend/src/conversation/conversationHandlers.ts +++ b/frontend/src/conversation/conversationHandlers.ts @@ -190,6 +190,27 @@ export function handleSearch( .catch((err) => console.log(err)); } +export function handleSearchViaApiKey( + question: string, + api_key: string, + history: Array = [], +) { + history = history.map((item) => { + return { prompt: item.prompt, response: item.response }; + }); + return conversationService + .search({ + question: question, + history: JSON.stringify(history), + api_key: api_key, + }) + .then((response) => response.json()) + .then((data) => { + return data; + }) + .catch((err) => console.log(err)); +} + export function handleSendFeedback( prompt: string, response: string, diff --git a/frontend/src/conversation/sharedConversationSlice.ts b/frontend/src/conversation/sharedConversationSlice.ts index 6d9ec936..b56666c5 100644 --- a/frontend/src/conversation/sharedConversationSlice.ts +++ b/frontend/src/conversation/sharedConversationSlice.ts @@ -6,6 +6,7 @@ import { createAsyncThunk } from '@reduxjs/toolkit'; import { handleFetchSharedAnswer, handleFetchSharedAnswerStreaming, + handleSearchViaApiKey, } from './conversationHandlers'; const API_STREAMING = import.meta.env.VITE_API_STREAMING === 'true'; @@ -44,6 +45,22 @@ export const fetchSharedAnswer = createAsyncThunk( // set status to 'idle' dispatch(sharedConversationSlice.actions.setStatus('idle')); dispatch(saveToLocalStorage()); + + state.sharedConversation.apiKey && + handleSearchViaApiKey( + question, + state.sharedConversation.apiKey, + state.sharedConversation.queries, + ).then((sources) => { + //dispatch streaming sources + sources && + dispatch( + updateStreamingSource({ + index: state.sharedConversation.queries.length - 1, + query: { sources: sources ?? [] }, + }), + ); + }); } else if (data.type === 'error') { // set status to 'failed' dispatch(sharedConversationSlice.actions.setStatus('failed')); @@ -164,6 +181,17 @@ export const sharedConversationSlice = createSlice({ ...query, }; }, + updateStreamingSource( + state, + action: PayloadAction<{ index: number; query: Partial }>, + ) { + const { index, query } = action.payload; + if (!state.queries[index].sources) { + state.queries[index].sources = query?.sources; + } else { + state.queries[index].sources!.push(query.sources![0]); + } + }, raiseError( state, action: PayloadAction<{ index: number; message: string }>, @@ -213,6 +241,7 @@ export const { updateStreamingQuery, addQuery, saveToLocalStorage, + updateStreamingSource, } = sharedConversationSlice.actions; export const selectStatus = (state: RootState) => state.conversation.status; From 8ca590559d7a389d82aad3b63e6ce8cb08ac2639 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 11 Sep 2024 19:01:42 +0100 Subject: [PATCH 2/2] fix(lint): types --- frontend/src/conversation/sharedConversationSlice.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/src/conversation/sharedConversationSlice.ts b/frontend/src/conversation/sharedConversationSlice.ts index b56666c5..f605c6bf 100644 --- a/frontend/src/conversation/sharedConversationSlice.ts +++ b/frontend/src/conversation/sharedConversationSlice.ts @@ -187,9 +187,12 @@ export const sharedConversationSlice = createSlice({ ) { const { index, query } = action.payload; if (!state.queries[index].sources) { - state.queries[index].sources = query?.sources; - } else { - state.queries[index].sources!.push(query.sources![0]); + state.queries[index].sources = query.sources ?? []; + } else if (query.sources && query.sources.length > 0) { + state.queries[index].sources = [ + ...(state.queries[index].sources ?? []), + ...query.sources, + ]; } }, raiseError(