8000 Search docs not inside the /stream in the stream request by dartpain · Pull Request #830 · arc53/DocsGPT · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Search docs not inside the /stream in the stream request #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions application/api/answer/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,9 @@ def complete_stream(question, docsearch, chat_history, api_key, prompt_id, conve
source_log_docs = []
for doc in docs:
if doc.metadata:
data = json.dumps({"type": "source", "doc": doc.page_content[:10], "metadata": doc.metadata})
source_log_docs.append({"title": doc.metadata['title'].split('/')[-1], "text": doc.page_content})
else:
data = json.dumps({"type": "source", "doc": doc.page_content[:10]})
source_log_docs.append({"title": doc.page_content, "text": doc.page_content})
yield f"data:{data}\n\n"

if len(chat_history) > 1:
tokens_current_history = 0
Expand Down Expand Up @@ -343,3 +340,32 @@ def api_answer():
traceback.print_exc()
print(str(e))
return bad_request(500, str(e))


@answer.route("/api/search", methods=["POST"])
def api_search():
data = request.get_json()
# get parameter from url question
question = data["question"]

if not embeddings_key_set:
embeddings_key = data["embeddings_key"]
else:
embeddings_key = settings.EMBEDDINGS_KEY
if "active_docs" in data:
vectorstore = get_vectorstore({"active_docs": data["active_docs"]})
else:
vectorstore = ""
docsearch = VectorCreator.create_vectorstore(settings.VECTOR_STORE, vectorstore, embeddings_key)

docs = docsearch.search(question, k=2)

source_log_docs = []
for doc in docs:
if doc.metadata:
source_log_docs.append({"title": doc.metadata['title'].split('/')[-1], "text": doc.page_content})
else:
source_log_docs.append({"title": doc.page_content, "text": doc.page_content})
#yield f"data:{data}\n\n"
return source_log_docs

78 changes: 64 additions & 14 deletions frontend/src/conversation/conversationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ export function fetchAnswerApi(
promptId: string | null,
): Promise<
| {
result: any;
answer: any;
sources: any;
conversationId: any;
query: string;
}
result: any;
answer: any;
sources: any;
conversationId: any;
query: string;
}
| {
result: any;
answer: any;
sources: any;
query: string;
conversationId: any;
title: any;
}
result: any;
answer: any;
sources: any;
query: string;
conversationId: any;
title: any;
}
> {
let namePath = selectedDocs.name;
if (selectedDocs.language === namePath) {
Expand Down Expand Up @@ -128,7 +128,6 @@ export function fetchAnswerSteaming(
conversation_id: conversationId,
prompt_id: promptId,
};

fetch(apiHost + '/stream', {
method: 'POST',
headers: {
Expand Down Expand Up @@ -183,7 +182,58 @@ export function fetchAnswerSteaming(
});
});
}
export function searchEndpoint(
question: string,
10000 apiKey: string,
selectedDocs: Doc,
conversation_id: string | null,
history: Array<any> = [],
) {
/*
"active_docs": "default",
"question": "Summarise",
"conversation_id": null,
"history": "[]" */
let namePath = selectedDocs.name;
if (selectedDocs.language === namePath) {
namePath = '.project';
}

let docPath = 'default';
if (selectedDocs.location === 'local') {
docPath = 'local' + '/' + selectedDocs.name + '/';
} else if (selectedDocs.location === 'remote') {
docPath =
selectedDocs.language +
'/' +
namePath +
'/' +
selectedDocs.version +
'/' +
selectedDocs.model +
'/';
}

const body = {
question: question,
active_docs: docPath,
conversation_id,
history
};
return fetch(`${apiHost}/api/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(
body
),
}).then((response) => response.json())
.then((data) => {
return data;
})
.catch(err => console.log(err))
}
export function sendFeedback(
prompt: string,
response: string,
Expand Down
39 changes: 20 additions & 19 deletions frontend/src/conversation/conversationSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import store from '../store';
import { fetchAnswerApi, fetchAnswerSteaming } from './conversationApi';
import { searchEndpoint } from './conversationApi';
import { Answer, ConversationState, Query, Status } from './conversationModels';
import { getConversations } from '../preferences/preferenceApi';
import { setConversations } from '../preferences/preferenceSlice';
Expand Down Expand Up @@ -29,6 +30,7 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
(event) => {
const data = JSON.parse(event.data);


// check if the 'end' event has been received
if (data.type === 'end') {
// set status to 'idle'
Expand All @@ -40,24 +42,22 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
.catch((error) => {
console.error('Failed to fetch conversations: ', error);
});
} else if (data.type === 'source') {
// check if data.metadata exists
let result;
if (data.metadata && data.metadata.title) {
const titleParts = data.metadata.title.split('/');
result = {
title: titleParts[titleParts.length - 1],
text: data.doc,
};
} else {
result = { title: data.doc, text: data.doc };
}
dispatch(
updateStreamingSource({
index: state.conversation.queries.length - 1,
query: { sources: [result] },
}),
);

searchEndpoint( //search for sources post streaming
question,
state.preference.apiKey,
state.preference.selectedDocs!,
state.conversation.conversationId,
state.conversation.queries
).then(sources => {
//dispatch streaming sources
dispatch(
updateStreamingSource({
index: state.conversation.queries.length - 1,
query: { sources },
}),
);
});
} else if (data.type === 'id') {
dispatch(
updateConversationId({
Expand Down Expand Up @@ -165,9 +165,10 @@ export const conversationSlice = createSlice({
state,
action: PayloadAction<{ index: number; query: Partial<Query> }>,
) {

const { index, query } = action.payload;
if (!state.queries[index].sources) {
state.queries[index].sources = [query.sources![0]];
state.queries[index].sources = query?.sources;
} else {
state.queries[index].sources!.push(query.sources![0]);
}
Expand Down
0