Skip to content

Commit 8453a31

Browse files
Merge pull request #121 from intelligentnode/120-verify-openai-streaming
Fix the streaming issue
2 parents 179f9d1 + d94623f commit 8453a31

File tree

7 files changed

+50
-41
lines changed

7 files changed

+50
-41
lines changed

IntelliNode/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

IntelliNode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "intellinode",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "Evaluate and integrate with latest AI models including ChatGPT, Llama, Diffusion, Deepseek, Gemini, and Hugging Face.",
55
"main": "index.js",
66
"keywords": [

IntelliNode/test/integration/Chatbot.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ async function testSageMakerLLamaCase() {
174174

175175
async function testStreamOpenaiChatGPTCase1() {
176176
console.log('\nchat test case 1: \n')
177-
const mode = "You are a helpful astronomy assistant.";
178-
const input = new ChatGPTInput(mode);
179-
input.addUserMessage("what is the story of batman the dark night with less than 10 words");
177+
const mode = "You are a helpful assistant.";
178+
const input = new ChatGPTInput(mode, options={model: 'o3-mini'});
179+
input.addUserMessage("what is the story of batman the dark night");
180180

181181
let fullText = '';
182182
for await (const contentText of bot.stream(input)) {

IntelliNode/utils/StreamParser.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,49 @@ Copyright 2023 Github.com/Barqawiz/IntelliNode
66
Licensed under the Apache License, Version 2.0 (the "License");
77
*/
88
class GPTStreamParser {
9-
constructor(isLog = false) {
10-
this.buffer = '';
11-
this.isLog = false;
12-
}
9+
constructor(isLog = false) {
10+
this.buffer = '';
11+
this.isLog = isLog;
12+
}
1313

14-
async *feed(data) {
15-
this.buffer += data;
14+
async *feed(data) {
15+
this.buffer += data;
1616

17-
if (this.buffer.startsWith("data: [DONE]")) {
18-
if (this.isLog) {
19-
console.log("Parsing finished.");
20-
}
21-
this.buffer = '';
22-
return;
23-
}
17+
// check if the buffer contains events
18+
while (this.buffer.includes('\n\n')) {
19+
// find the end of the first complete event
20+
const eventEndIndex = this.buffer.indexOf('\n\n');
21+
let rawData = this.buffer.slice(0, eventEndIndex).trim();
22+
23+
// remove the processed event
24+
this.buffer = this.buffer.slice(eventEndIndex + 2);
2425

25-
if (this.buffer.includes('\n\n')) {
26-
const eventEndIndex = this.buffer.indexOf('\n\n');
27-
const rawData = this.buffer.slice(0, eventEndIndex + 1).trim();
26+
// look for the stop signal
27+
if (rawData === "data: [DONE]") {
28+
if (this.isLog) {
29+
console.log("Parsing finished.");
30+
}
31+
// stop processing if the stream is done
32+
return;
33+
}
2834

29-
if (!rawData.startsWith("data: ")) {
30-
// skip it, if not a data line.
31-
this.buffer = this.buffer.slice(eventEndIndex + 2);
32-
return;
33-
}
34-
// remove initial "data: " from rawData.
35-
const jsonData = JSON.parse(rawData.slice(6));
36-
const contentText = jsonData.choices[0]?.delta?.content;
37-
if (contentText) {
38-
yield contentText;
39-
}
40-
this.buffer = this.buffer.slice(eventEndIndex + 2);
35+
// skip lines without "data: "
36+
if (!rawData.startsWith("data: ")) {
37+
continue;
38+
}
39+
40+
try {
41+
// parse the JSON
42+
const jsonData = JSON.parse(rawData.substring(6));
43+
const contentText = jsonData.choices?.[0]?.delta?.content;
44+
if (contentText) {
45+
yield contentText;
4146
}
47+
} catch (error) {
48+
console.error("Error parsing JSON in stream:", error);
49+
}
4250
}
51+
}
4352
}
4453
class CohereStreamParser {
4554
constructor(isLog = false) {

samples/command_sample/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/command_sample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
"dependencies": {
2323
"@aws-sdk/client-s3": "^3.363.0",
2424
"dotenv": "^16.0.3",
25-
"intellinode": "^2.0.4"
25+
"intellinode": "^2.0.5"
2626
}
2727
}

samples/command_sample/test_chatbot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function callChatbot(apiKey, provider) {
77
const chatbot = new Chatbot(apiKey, provider);
88

99
const system = 'You are a helpful assistant.';
10-
const input = new ChatGPTInput(system, options={model: "o1"});
10+
const input = new ChatGPTInput(system, options={model: "o3-mini"});
1111
input.addUserMessage('what is the story of batman the dark night with less than 50 words');
1212
input.numberOfOutputs = 1;
1313

@@ -21,7 +21,7 @@ async function callChatbotStream(apiKey, provider) {
2121
const chatbot = new Chatbot(apiKey, provider);
2222

2323
const system = 'You are a helpful assistant.';
24-
const input = new ChatGPTInput(system);
24+
const input = new ChatGPTInput(system, options={model: "o3-mini"});
2525
input.addUserMessage('what is the story of batman the dark night with less than 50 words');
2626
input.numberOfOutputs = 1;
2727

0 commit comments

Comments
 (0)