Merge pull request #1120 from ManishMadan2882/main

Load sources on shared conversations
This commit is contained in:
Alex
2024-09-11 20:02:54 +01:00
committed by GitHub
5 changed files with 56 additions and 4 deletions

View File

@@ -469,8 +469,8 @@ def api_search():
if "api_key" in data:
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"]
source = {"active_docs":data_key["source"]}
user_api_key = data["api_key"]
elif "active_docs" in data:
source = {"active_docs": data["active_docs"]}
user_api_key = None

View File

@@ -671,8 +671,6 @@ def get_publicly_shared_conversations(identifier: str):
conversation_queries = conversation["queries"][
: (shared["first_n_queries"])
]
for query in conversation_queries:
query.pop("sources") ## avoid exposing sources
else:
return (
jsonify(

View File

@@ -144,6 +144,7 @@ export const SharedConversation = () => {
key={`${index}ANSWER`}
message={query.response}
type={'ANSWER'}
sources={query.sources ?? []}
></ConversationBubble>
);
} else if (query.error) {

View File

@@ -190,6 +190,27 @@ export function handleSearch(
.catch((err) => console.log(err));
}
export function handleSearchViaApiKey(
question: string,
api_key: string,
history: Array<any> = [],
) {
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,

View File

@@ -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<Answer, { question: string }>(
// 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,20 @@ export const sharedConversationSlice = createSlice({
...query,
};
},
updateStreamingSource(
state,
action: PayloadAction<{ index: number; query: Partial<Query> }>,
) {
const { index, query } = action.payload;
if (!state.queries[index].sources) {
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(
state,
action: PayloadAction<{ index: number; message: string }>,
@@ -213,6 +244,7 @@ export const {
updateStreamingQuery,
addQuery,
saveToLocalStorage,
updateStreamingSource,
} = sharedConversationSlice.actions;
export const selectStatus = (state: RootState) => state.conversation.status;