Skip to content

Commit

Permalink
Add streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
zachey01 committed Jul 16, 2024
1 parent 9be6a2f commit a9c5e35
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/gpt4js.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/Providers/ChatCompletion/Alibaba.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
import Provider from "./provider.js";
import baseHeaders from "../../Utils/baseHeaders.js";
import startStreaming from "../../Utils/stream.js";

class AlibabaProvider extends Provider {
async chatCompletion(messages, options) {
Expand All @@ -23,8 +24,15 @@ class AlibabaProvider extends Provider {
method: "POST",
}
);
const text = await response.json();
return text.choices[0].message.content;

if (options.stream === true) {
startStreaming(response).then((chunk) => {
return chunk;
});
} else {
const text = await response.json();
return text.choices[0].message.content;
}
} catch (error) {
console.error("Error:", error);
throw error;
Expand Down
11 changes: 9 additions & 2 deletions src/Providers/ChatCompletion/ChatBotRu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
import Provider from "./provider.js";
import baseHeaders from "../../Utils/baseHeaders.js";
import startStreaming from "../../Utils/stream.js";

class ChatBotRuProvider extends Provider {
async chatCompletion(messages, options) {
Expand Down Expand Up @@ -32,8 +33,14 @@ class ChatBotRuProvider extends Provider {
}
);

const text = await response.json();
return text.choices[0].message.content;
if (options.stream === true) {
startStreaming(response).then((chunk) => {
return chunk;
});
} else {
const text = await response.json();
return text.choices[0].delta.content;
}
} catch (error) {
console.error("Error:", error);
throw error;
Expand Down
43 changes: 43 additions & 0 deletions src/Utils/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
async function startStreaming(response) {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
const buffer = [];
const seenChunks = new Set();

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}

buffer.push(decoder.decode(value, { stream: true }));
let combinedChunks = buffer.join("");
buffer.length = 0; // Clear the buffer

let chunks = combinedChunks.split("\n");

chunks.forEach((chunk) => {
chunk = chunk.replace(/^data: /, "");
if (chunk.trim() !== "" && !seenChunks.has(chunk)) {
seenChunks.add(chunk);
if (chunk.trim() === "[DONE]") {
return; // Skip processing [DONE] responses
}
try {
let chunkObj = JSON.parse(chunk);
console.log(chunkObj.choices[0].delta.content);
} catch (error) {
console.error("Error parsing chunk:", error);
}
}
});
}

return "";
}

export default startStreaming;

0 comments on commit a9c5e35

Please sign in to comment.