Technologies:
Tolerim
a day ago
'SSE functions properly on a local environment, but fails to work when deployed on Vercel.'
app.post('/completions', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('X-Accel-Buffering', 'no');
res.setHeader('Connection', 'keep-alive');
const headers = {
'Authorization': `Bearer MYAUTHTOKEN`
};
const body = {
'messages': []
};
axios
.post(
'https://api.openai.com/v1/chat/completions',
body,
{
headers: headers,
responseType: 'stream'
}
)
.then((openairesponse) => {
openairesponse.data.pipe(res);
})
.catch((err) => {
res.status(500).send(err);
})
});
Answers(1)
Tolerim
a day ago
Verified Answer
It seems like the issue might be with the way Vercel handles streaming responses. Vercel may be buffering the entire response before sending it to the client.
One solution to try would be to remove the 'Transfer-Encoding': 'chunked' header from the server code and see if that fixes the issue. You may also want to try setting the 'Content-Length' header to a very high number to try and force Vercel to use chunked encoding.
Another option could be to use a different hosting provider that supports the type of streaming response you are trying to send.