This commit is contained in:
Alex
2023-07-26 22:14:54 +01:00
parent 486a1bc9de
commit 382c3930a2
7 changed files with 337 additions and 25 deletions

View File

@@ -8,7 +8,24 @@ export function fetchAnswerApi(
apiKey: string,
selectedDocs: Doc,
history: Array<any> = [],
): Promise<Answer> {
conversationId: string | null,
): Promise<
| {
result: any;
answer: any;
sources: any;
conversationId: any;
query: string;
}
| {
result: any;
answer: any;
sources: any;
query: string;
conversationId: any;
title: any;
}
> {
let namePath = selectedDocs.name;
if (selectedDocs.language === namePath) {
namePath = '.project';
@@ -44,6 +61,7 @@ export function fetchAnswerApi(
embeddings_key: apiKey,
history: history,
active_docs: docPath,
conversation_id: conversationId,
}),
})
.then((response) => {
@@ -55,7 +73,13 @@ export function fetchAnswerApi(
})
.then((data) => {
const result = data.answer;
return { answer: result, query: question, result, sources: data.sources };
return {
answer: result,
query: question,
result,
sources: data.sources,
conversationId: data.conversation_id,
};
});
}
@@ -64,6 +88,7 @@ export function fetchAnswerSteaming(
apiKey: string,
selectedDocs: Doc,
history: Array<any> = [],
conversationId: string | null,
onEvent: (event: MessageEvent) => void,
): Promise<Answer> {
let namePath = selectedDocs.name;
@@ -97,8 +122,9 @@ export function fetchAnswerSteaming(
embeddings_key: apiKey,
active_docs: docPath,
history: JSON.stringify(history),
conversation_id: conversationId,
};
fetch(apiHost + '/stream', {
method: 'POST',
headers: {
@@ -107,48 +133,51 @@ export function fetchAnswerSteaming(
body: JSON.stringify(body),
})
.then((response) => {
if (!response.body) throw Error("No response body");
if (!response.body) throw Error('No response body');
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
var counterrr = 0
const processStream = ({ done, value }: ReadableStreamReadResult<Uint8Array>) => {
let counterrr = 0;
const processStream = ({
done,
value,
}: ReadableStreamReadResult<Uint8Array>) => {
if (done) {
console.log(counterrr);
return;
}
counterrr += 1;
const chunk = decoder.decode(value);
const lines = chunk.split("\n");
const lines = chunk.split('\n');
for (let line of lines) {
if (line.trim() == "") {
if (line.trim() == '') {
continue;
}
if (line.startsWith('data:')) {
line = line.substring(5);
}
const messageEvent: MessageEvent = new MessageEvent("message", {
const messageEvent: MessageEvent = new MessageEvent('message', {
data: line,
});
onEvent(messageEvent); // handle each message
}
reader.read().then(processStream).catch(reject);
}
};
reader.read().then(processStream).catch(reject);
})
.catch((error) => {
console.error('Connection failed:', error);
reject(error);
});
});
});
}
export function sendFeedback(