Fixed request length bug, changed to as less used port

This commit is contained in:
Idan
2023-06-23 14:56:14 +03:00
parent 92373b25a9
commit 2404899e28
13 changed files with 92 additions and 50 deletions

View File

@@ -1,2 +1,2 @@
# Please put appropriate value
VITE_API_HOST=http://localhost:5001
VITE_API_HOST=http://localhost:7091

View File

@@ -91,22 +91,64 @@ export function fetchAnswerSteaming(
});
return new Promise<Answer>((resolve, reject) => {
const url = new URL(apiHost + '/stream');
url.searchParams.append('question', question);
url.searchParams.append('api_key', apiKey);
url.searchParams.append('embeddings_key', apiKey);
url.searchParams.append('active_docs', docPath);
url.searchParams.append('history', JSON.stringify(history));
const eventSource = new EventSource(url.href);
eventSource.onmessage = onEvent;
eventSource.onerror = (error) => {
console.log('Connection failed.');
eventSource.close();
const body = {
question: question,
api_key: apiKey,
embeddings_key: apiKey,
active_docs: docPath,
history: JSON.stringify(history),
};
});
fetch(apiHost + '/stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
.then((response) => {
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>) => {
if (done) {
console.log(counterrr);
return;
}
counterrr += 1;
const chunk = decoder.decode(value);
const lines = chunk.split("\n");
for (let line of lines) {
if (line.trim() == "") {
continue;
}
if (line.startsWith('data:')) {
line = line.substring(5);
}
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(