The following code streams some data from OpenAI's chat completion API that responds using server sent events.
export async function promptStream(
apiKey: string,
userId: string,
prompt: Prompt,
handleNewChunk: (chunk: string) => Promise<void>
) {
const req = https.request(
{
hostname: "api.openai.com",
port: 443,
path: "/v1/chat/completions",
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
},
},
function (res) {
res.on("data", async (data) => {
handleNewChunk(data)
}
});
res.on("end", () => {
console.log("No more data in response.");
});
}
);
const body = JSON.stringify({
...prompt,
stream: true,
});
req.write(body);
req.end();
}
When I run this function locally using a Firebase functions emulator, it works perfectly fine: https://www.loom.com/share/bfebefe0fc6a40df972ee1be179123db
However, after deploying to Cloud Functions gen2 (which is built on top of Cloud Run) and invoking it, there's a 10-15 second delay, then all of the text comes flooding in at once.