-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (53 loc) · 1.71 KB
/
app.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
// app.js
const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// Middleware to attach headers
app.use((req, res, next) => {
// Retrieve the API key from the environment variable
const apiKey = process.env.openai;
const organization = process.env.organization;
// Attach headers to the axios instance
axios.defaults.headers.common['Authorization'] = `Bearer ${apiKey}`;
axios.defaults.headers.common['OpenAI-Organization'] = organization;
axios.defaults.headers.common['Content-Type'] = 'application/json';
// Add this to configure axios global defaults
axios.defaults.transformResponse = [data => data];
next();
});
// Endpoint to receive JSON payload and call OpenAI API
app.post('/api/chat', async (req, res) => {
try {
console.log(req.body)
// Make request
const response = await axios({
method: 'POST',
url: 'https://api.openai.com/v1/chat/completions',
data: req.body,
headers: {
'Content-Type': 'application/json'
},
responseType: 'stream'
});
// Set streaming headers
res.set('Content-Type', 'text/event-stream');
res.set('Connection', 'keep-alive');
// Stream response chunks
response.data.on('data', (chunk) => {
console.log(chunk.toString()) // convert to string
res.write(chunk);
});
response.data.on('end', () => {
res.end();
});
} catch (error) {
console.error('Error:', error.message);
//console.log(error.response.data)
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});