Emil
Emil
WWasp-lang
Created by haarismian on 6/8/2024 in #đŸ™‹questions
How do I implement text streaming from the openai api
try change return to yield
18 replies
WWasp-lang
Created by haarismian on 6/8/2024 in #đŸ™‹questions
How do I implement text streaming from the openai api
if you return. you only get once. if you yield. you will return multiple times.
18 replies
WWasp-lang
Created by haarismian on 6/8/2024 in #đŸ™‹questions
How do I implement text streaming from the openai api
can you give more context on your setup?.. here's how i would usually do it>>
// after fetch, here's how to consume the stream

const reader = res.body?.getReader();
while (true || !stopStreamref.current) {
const { done, value } = (await reader?.read()) as ReadResult;
if (done || stopStreamref.current) {
setOnStream(false);
break;
}
yield new TextDecoder("utf-8").decode(value);
}
// after fetch, here's how to consume the stream

const reader = res.body?.getReader();
while (true || !stopStreamref.current) {
const { done, value } = (await reader?.read()) as ReadResult;
if (done || stopStreamref.current) {
setOnStream(false);
break;
}
yield new TextDecoder("utf-8").decode(value);
}
and then do for loops on set state until the stream done
18 replies