forked from XiaoMo/ChatGPT-Next-Web
feat: animate streaming response to make more smooth
This commit is contained in:
parent
9876a1aeca
commit
536ace8e10
@ -115,12 +115,33 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
|
|
||||||
if (shouldStream) {
|
if (shouldStream) {
|
||||||
let responseText = "";
|
let responseText = "";
|
||||||
|
let remainText = "";
|
||||||
let finished = false;
|
let finished = false;
|
||||||
|
|
||||||
|
// animate response to make it looks smooth
|
||||||
|
function animateResponseText() {
|
||||||
|
if (finished || controller.signal.aborted) {
|
||||||
|
responseText += remainText;
|
||||||
|
console.log("[Response Animation] finished");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remainText.length > 0) {
|
||||||
|
responseText += remainText[0];
|
||||||
|
remainText = remainText.slice(1);
|
||||||
|
options.onUpdate?.(responseText, remainText[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestAnimationFrame(animateResponseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start animaion
|
||||||
|
animateResponseText();
|
||||||
|
|
||||||
const finish = () => {
|
const finish = () => {
|
||||||
if (!finished) {
|
if (!finished) {
|
||||||
options.onFinish(responseText);
|
|
||||||
finished = true;
|
finished = true;
|
||||||
|
options.onFinish(responseText + remainText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -183,8 +204,7 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
};
|
};
|
||||||
const delta = json.choices[0]?.delta?.content;
|
const delta = json.choices[0]?.delta?.content;
|
||||||
if (delta) {
|
if (delta) {
|
||||||
responseText += delta;
|
remainText += delta;
|
||||||
options.onUpdate?.(responseText, delta);
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[Request] parse error", text);
|
console.error("[Request] parse error", text);
|
||||||
|
Loading…
Reference in New Issue
Block a user