-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming-express-backend.js
69 lines (57 loc) · 1.6 KB
/
streaming-express-backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const fetch = require('node-fetch');
const streaming = express();
const key = ''; // Your GPT Key
streaming.post('/sse-values', async (req, res) => {
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
try {
const response = await fetch(`https://api.openai.com/v1/chat/completions`, {
method: 'POST',
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: req.body.text,
},
],
stream: true,
}),
});
response.body.on('data', chunk => {
const dataString = chunk.toString();
const dataLines = dataString
.split('\n')
.filter(line => line.trim() !== '');
dataLines.forEach(line => {
if (line.startsWith('data:')) {
const data = line.substring(5).trim();
if (data !== '[DONE]') {
res.write(`data: ${data}\n\n`);
} else {
res.write(`data: [DONE]\n\n`);
res.end();
}
}
});
});
response.body.on('end', () => {
res.end();
});
} catch (error) {
console.error('Error streaming OpenAI response:', error);
res.write(`data: ${JSON.stringify({error: 'Streaming failed.'})}\n\n`);
res.end();
}
});
module.exports = {
streaming,
};