#Streaming help

2 messages · Page 1 of 1 (latest)

flint isle
#

Hello, I am trying to use stream: true but I can't get it to work. This is my code:

const source = new EventSource(
"https://api.openai.com/v1/engines/curie/completions",
{
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + OPENAI_API_KEY,
},
method: "POST",
payload: JSON.stringify({
prompt: fileTextToCursor,
temperature: 0.75,
top_p: 0.95,
max_tokens: 3,
stream: true,
stop: ["\n\n"],
}),
}
);

source.addEventListener("ping", (event) => {
 console.log(event.data);
 });
near vector
#
// 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);