From 2a974ff257fede37e5c4bf90750efa03375c541b Mon Sep 17 00:00:00 2001 From: wozeparrot Date: Fri, 14 Jun 2024 18:22:19 +0000 Subject: [PATCH] fix: no readablestream await of, too new (#4965) --- examples/tinychat/index.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/tinychat/index.js b/examples/tinychat/index.js index 8b1c43ff5d..ffb1379afe 100644 --- a/examples/tinychat/index.js +++ b/examples/tinychat/index.js @@ -33,7 +33,7 @@ document.addEventListener("alpine:init", () => { if (this.home === 0) this.home = 1; // ensure that going back in history will go back to home - window.history.pushState({}, '', '/'); + window.history.pushState({}, "", "/"); // add message to list this.cstate.messages.push({ role: "user", content: value }); @@ -45,7 +45,9 @@ document.addEventListener("alpine:init", () => { // start receiving server sent events let gottenFirstChunk = false; - for await (const chunk of this.openaiChatCompletion(this.cstate.messages)) { + for await ( + const chunk of this.openaiChatCompletion(this.cstate.messages) + ) { if (!gottenFirstChunk) { this.cstate.messages.push({ role: "ai", content: "" }); gottenFirstChunk = true; @@ -96,20 +98,20 @@ document.addEventListener("alpine:init", () => { throw new Error("Failed to fetch"); } - for await ( - const event of response.body.pipeThrough(new TextDecoderStream()) - .pipeThrough(new EventSourceParserStream()) - ) { - if (event.type === "event") { - const json = JSON.parse(event.data); - + const reader = response.body.pipeThrough(new TextDecoderStream()) + .pipeThrough(new EventSourceParserStream()).getReader(); + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + if (value.type === "event") { + const json = JSON.parse(value.data); if (json.choices) { const choice = json.choices[0]; - - // see if the completion is done - if (choice.finish_reason === "stop") break; - - // yield the completion + if (choice.finish_reason === "stop") { + break; + } yield choice.delta.content; } }