Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
JYC0413 committed Jan 17, 2024
1 parent 4d762de commit 289bb10
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,45 @@ export const Chat = memo(({stopConversationRef}: Props) => {
while (!done || queue.length !== 0) {
const {value} = await reader.read();
let chunkValue = decoder.decode(value);
const regex = /}(?={)/g;
const parts = chunkValue.split(regex);
const objects = parts.map(part => {
part = part.trim();
if (!part.startsWith('{')) {
part = '{' + part;
}
if (!part.endsWith('}')) {
part = part + '}';
}
return JSON.parse(part);
});

objects.forEach(obj => {
if (obj && obj["choices"]) {
obj["choices"].forEach((obj1: { [x: string]: { [x: string]: any; }; }) => {
if (obj1) {
if (obj1["finish_reason"] && obj1["finish_reason"] !== null) {
done = true;
} else {
if (!done && obj1["delta"] && obj1["delta"]["content"]) {
console.log("push data:", obj1["delta"]["content"])
queue.push(obj1["delta"]["content"]);
}
}
}
})
const regex = /"content":"([^"]+)"/g;

let match;

while (match = regex.exec(chunkValue)) {
if(match[1]){
queue.push(match[1]);
}
});
}
// const regex = /}(?={)/g;
// const parts = chunkValue.split(regex);
// console.log("parts", parts)
// const objects = parts.map(part => {
// part = part.trim();
// if (!part.startsWith('{')) {
// part = '{' + part;
// }
// if (!part.endsWith('}')) {
// part = part + '}';
// }
// return JSON.parse(part);
// });
//
// objects.forEach(obj => {
// if (obj && obj["choices"]) {
// obj["choices"].forEach((obj1: { [x: string]: { [x: string]: any; }; }) => {
// if (obj1) {
// if (obj1["finish_reason"] && obj1["finish_reason"] !== null) {
// done = true;
// } else {
// if (!done && obj1["delta"] && obj1["delta"]["content"]) {
// console.log("push data:", obj1["delta"]["content"])
// queue.push(obj1["delta"]["content"]);
// }
// }
// }
// })
// }
// });

for (const item of queue) {
const thisWord = queue.shift();
Expand Down

0 comments on commit 289bb10

Please sign in to comment.