Skip to content

Commit 4b08337

Browse files
DieberXavier
and
Xavier
authored
bug-fix: manipulate data by Unint8Array (#5214)
Co-authored-by: Xavier <[email protected]>
1 parent d594b0e commit 4b08337

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

libs/langchain-community/src/chat_models/baiduwenxin.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,22 @@ export class ChatBaiduWenxin
500500
this.accessToken = await this.getAccessToken();
501501
}
502502

503+
const findFirstNewlineIndex = (data: Uint8Array) => {
504+
for (let i = 0; i < data.length; ) {
505+
if (data[i] === 10) return i;
506+
if ((data[i] & 0b11100000) === 0b11000000) {
507+
i += 2;
508+
} else if ((data[i] & 0b11110000) === 0b11100000) {
509+
i += 3;
510+
} else if ((data[i] & 0b11111000) === 0b11110000) {
511+
i += 4;
512+
} else {
513+
i += 1;
514+
}
515+
}
516+
return -1;
517+
};
518+
503519
const makeCompletionRequest = async () => {
504520
const url = `${this.apiUrl}?access_token=${this.accessToken}`;
505521
const response = await fetch(url, {
@@ -532,7 +548,7 @@ export class ChatBaiduWenxin
532548
const reader = response.body.getReader();
533549

534550
const decoder = new TextDecoder("utf-8");
535-
let data = "";
551+
let dataArrayBuffer = new Uint8Array(0);
536552

537553
let continueReading = true;
538554
while (continueReading) {
@@ -541,17 +557,30 @@ export class ChatBaiduWenxin
541557
continueReading = false;
542558
break;
543559
}
544-
data += decoder.decode(value);
560+
// merge the data first then decode in case of the Chinese characters are split between chunks
561+
const mergedArray = new Uint8Array(
562+
dataArrayBuffer.length + value.length
563+
);
564+
mergedArray.set(dataArrayBuffer);
565+
mergedArray.set(value, dataArrayBuffer.length);
566+
dataArrayBuffer = mergedArray;
545567

546568
let continueProcessing = true;
547569
while (continueProcessing) {
548-
const newlineIndex = data.indexOf("\n");
570+
const newlineIndex = findFirstNewlineIndex(dataArrayBuffer);
549571
if (newlineIndex === -1) {
550572
continueProcessing = false;
551573
break;
552574
}
553-
const line = data.slice(0, newlineIndex);
554-
data = data.slice(newlineIndex + 1);
575+
576+
const lineArrayBuffer = dataArrayBuffer.slice(
577+
0,
578+
findFirstNewlineIndex(dataArrayBuffer)
579+
);
580+
const line = decoder.decode(lineArrayBuffer);
581+
dataArrayBuffer = dataArrayBuffer.slice(
582+
findFirstNewlineIndex(dataArrayBuffer) + 1
583+
);
555584

556585
if (line.startsWith("data:")) {
557586
const event = new MessageEvent("message", {

0 commit comments

Comments
 (0)