Skip to content
Snippets Groups Projects
Unverified Commit 44dddcd4 authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

1417 completion timeout (#2374)


* Refactor handleDefaultStreamResponseV2 function for better error handling

* run yarn lint

* small error handling changes

* update error handling flow and scope of vars

* add back space

---------

Co-authored-by: default avatarRoman <rrojaski@gmail.com>
parent e6c4eb3f
Branches
Tags
No related merge requests found
...@@ -22,41 +22,55 @@ function handleDefaultStreamResponseV2(response, stream, responseProps) { ...@@ -22,41 +22,55 @@ function handleDefaultStreamResponseV2(response, stream, responseProps) {
const handleAbort = () => clientAbortedHandler(resolve, fullText); const handleAbort = () => clientAbortedHandler(resolve, fullText);
response.on("close", handleAbort); response.on("close", handleAbort);
for await (const chunk of stream) { // Now handle the chunks from the streamed response and append to fullText.
const message = chunk?.choices?.[0]; try {
const token = message?.delta?.content; for await (const chunk of stream) {
const message = chunk?.choices?.[0];
if (token) { const token = message?.delta?.content;
fullText += token;
writeResponseChunk(response, { if (token) {
uuid, fullText += token;
sources: [], writeResponseChunk(response, {
type: "textResponseChunk", uuid,
textResponse: token, sources: [],
close: false, type: "textResponseChunk",
error: false, textResponse: token,
}); close: false,
} error: false,
});
// LocalAi returns '' and others return null on chunks - the last chunk is not "" or null. }
// Either way, the key `finish_reason` must be present to determine ending chunk.
if ( // LocalAi returns '' and others return null on chunks - the last chunk is not "" or null.
message?.hasOwnProperty("finish_reason") && // Got valid message and it is an object with finish_reason // Either way, the key `finish_reason` must be present to determine ending chunk.
message.finish_reason !== "" && if (
message.finish_reason !== null message?.hasOwnProperty("finish_reason") && // Got valid message and it is an object with finish_reason
) { message.finish_reason !== "" &&
writeResponseChunk(response, { message.finish_reason !== null
uuid, ) {
sources, writeResponseChunk(response, {
type: "textResponseChunk", uuid,
textResponse: "", sources,
close: true, type: "textResponseChunk",
error: false, textResponse: "",
}); close: true,
response.removeListener("close", handleAbort); error: false,
resolve(fullText); });
break; // Break streaming when a valid finish_reason is first encountered response.removeListener("close", handleAbort);
resolve(fullText);
break; // Break streaming when a valid finish_reason is first encountered
}
} }
} catch (e) {
console.log(`\x1b[43m\x1b[34m[STREAMING ERROR]\x1b[0m ${e.message}`);
writeResponseChunk(response, {
uuid,
type: "abort",
textResponse: null,
sources: [],
close: true,
error: e.message,
});
resolve(fullText); // Return what we currently have - if anything.
} }
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment