Skip to content

Commit

Permalink
Merge pull request #1120 from ManishMadan2882/main
Browse files Browse the repository at this point in the history
Load sources on shared conversations
  • Loading branch information
dartpain authored Sep 11, 2024
2 parents e55c68d + 8ca5905 commit 686a482
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions application/api/answer/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions application/api/user/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions frontend/src/conversation/SharedConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const SharedConversation = () => {
key={`${index}ANSWER`}
message={query.response}
type={'ANSWER'}
sources={query.sources ?? []}
></ConversationBubble>
);
} else if (query.error) {
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/conversation/conversationHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/conversation/sharedConversationSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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 }>,
Expand Down Expand Up @@ -213,6 +244,7 @@ export const {
updateStreamingQuery,
addQuery,
saveToLocalStorage,
updateStreamingSource,
} = sharedConversationSlice.actions;

export const selectStatus = (state: RootState) => state.conversation.status;
Expand Down

0 comments on commit 686a482

Please sign in to comment.