#Using the stream option in Javascript
9 messages · Page 1 of 1 (latest)
Something like this
// server will just proxy the req to the openAi
const response = await fetch('/api/stream', {
method: 'POST',
headers: {
accept: 'text/event-stream',
'content-type': 'application/json'
},
body: JSON.stringify(body)
});
async function readAllChunks(readableStream) {
const reader = readableStream.getReader();
let done, value;
while (!done) {
({ value, done } = await reader.read());
if (done) {
return 'Done';
}
const regex = /data: .*?(\{.*\})/g;
const dataString = new TextDecoder().decode(value);
const match = [...dataString.matchAll(regex)];
streamedAnswer.value += match.reduce((aac, match) => {
const dataObject = JSON.parse(match[1]);
return dataObject.choices[0].text;
}, '');
// streamedAnswer is a reactive store that will update the UI
console.log('streamedAnswer.value', streamedAnswer.value);
}
}
await readAllChunks(response.body);
the only issue is that the response omits the usage
trying to figure that out
did you ever figure out the usage part yet?
Can you share it maybe in Codesandbox? I am struggling to get it to work too...
@real meadow What am I missing here ? I added openai key, and throw an error!