mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 08:33:20 +00:00
integrate /api/search endpoint, get sources post stream
This commit is contained in:
@@ -12,20 +12,20 @@ export function fetchAnswerApi(
|
|||||||
promptId: string | null,
|
promptId: string | null,
|
||||||
): Promise<
|
): Promise<
|
||||||
| {
|
| {
|
||||||
result: any;
|
result: any;
|
||||||
answer: any;
|
answer: any;
|
||||||
sources: any;
|
sources: any;
|
||||||
conversationId: any;
|
conversationId: any;
|
||||||
query: string;
|
query: string;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
result: any;
|
result: any;
|
||||||
answer: any;
|
answer: any;
|
||||||
sources: any;
|
sources: any;
|
||||||
query: string;
|
query: string;
|
||||||
conversationId: any;
|
conversationId: any;
|
||||||
title: any;
|
title: any;
|
||||||
}
|
}
|
||||||
> {
|
> {
|
||||||
let namePath = selectedDocs.name;
|
let namePath = selectedDocs.name;
|
||||||
if (selectedDocs.language === namePath) {
|
if (selectedDocs.language === namePath) {
|
||||||
@@ -128,7 +128,6 @@ export function fetchAnswerSteaming(
|
|||||||
conversation_id: conversationId,
|
conversation_id: conversationId,
|
||||||
prompt_id: promptId,
|
prompt_id: promptId,
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch(apiHost + '/stream', {
|
fetch(apiHost + '/stream', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -183,7 +182,58 @@ export function fetchAnswerSteaming(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
export function searchEndpoint(
|
||||||
|
question: string,
|
||||||
|
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(
|
export function sendFeedback(
|
||||||
prompt: string,
|
prompt: string,
|
||||||
response: string,
|
response: string,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||||
import store from '../store';
|
import store from '../store';
|
||||||
import { fetchAnswerApi, fetchAnswerSteaming } from './conversationApi';
|
import { fetchAnswerApi, fetchAnswerSteaming } from './conversationApi';
|
||||||
|
import { searchEndpoint } from './conversationApi';
|
||||||
import { Answer, ConversationState, Query, Status } from './conversationModels';
|
import { Answer, ConversationState, Query, Status } from './conversationModels';
|
||||||
import { getConversations } from '../preferences/preferenceApi';
|
import { getConversations } from '../preferences/preferenceApi';
|
||||||
import { setConversations } from '../preferences/preferenceSlice';
|
import { setConversations } from '../preferences/preferenceSlice';
|
||||||
@@ -29,6 +30,7 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
|
|||||||
(event) => {
|
(event) => {
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
|
|
||||||
|
|
||||||
// check if the 'end' event has been received
|
// check if the 'end' event has been received
|
||||||
if (data.type === 'end') {
|
if (data.type === 'end') {
|
||||||
// set status to 'idle'
|
// set status to 'idle'
|
||||||
@@ -40,24 +42,22 @@ export const fetchAnswer = createAsyncThunk<Answer, { question: string }>(
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error('Failed to fetch conversations: ', error);
|
console.error('Failed to fetch conversations: ', error);
|
||||||
});
|
});
|
||||||
} else if (data.type === 'source') {
|
|
||||||
// check if data.metadata exists
|
searchEndpoint( //search for sources post streaming
|
||||||
let result;
|
question,
|
||||||
if (data.metadata && data.metadata.title) {
|
state.preference.apiKey,
|
||||||
const titleParts = data.metadata.title.split('/');
|
state.preference.selectedDocs!,
|
||||||
result = {
|
state.conversation.conversationId,
|
||||||
title: titleParts[titleParts.length - 1],
|
state.conversation.queries
|
||||||
text: data.doc,
|
).then(sources => {
|
||||||
};
|
//dispatch streaming sources
|
||||||
} else {
|
dispatch(
|
||||||
result = { title: data.doc, text: data.doc };
|
updateStreamingSource({
|
||||||
}
|
index: state.conversation.queries.length - 1,
|
||||||
dispatch(
|
query: { sources },
|
||||||
updateStreamingSource({
|
}),
|
||||||
index: state.conversation.queries.length - 1,
|
);
|
||||||
query: { sources: [result] },
|
});
|
||||||
}),
|
|
||||||
);
|
|
||||||
} else if (data.type === 'id') {
|
} else if (data.type === 'id') {
|
||||||
dispatch(
|
dispatch(
|
||||||
updateConversationId({
|
updateConversationId({
|
||||||
@@ -165,9 +165,10 @@ export const conversationSlice = createSlice({
|
|||||||
state,
|
state,
|
||||||
action: PayloadAction<{ index: number; query: Partial<Query> }>,
|
action: PayloadAction<{ index: number; query: Partial<Query> }>,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
const { index, query } = action.payload;
|
const { index, query } = action.payload;
|
||||||
if (!state.queries[index].sources) {
|
if (!state.queries[index].sources) {
|
||||||
state.queries[index].sources = [query.sources![0]];
|
state.queries[index].sources = query?.sources;
|
||||||
} else {
|
} else {
|
||||||
state.queries[index].sources!.push(query.sources![0]);
|
state.queries[index].sources!.push(query.sources![0]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user