-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (48 loc) · 1.55 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
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const https = require('https');
const fs = require('fs');
// Load SSL key and certificate
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
const app = express();
app.use(cors()); // Enable CORS for all routes
app.use(express.json());
// Endpoint to get a response from GPT-3.
app.post('/get-gpt-response', async (req, res) => {
try {
const apiKey = process.env.OPENAI_API_KEY;
const prompt = req.body.prompt;
const endpoint = 'https://api.openai.com/v1/chat/completions';
const response = await axios.post(
endpoint,
{
model: 'gpt-4-0314',
messages: [{ role: 'user', content: prompt }]
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
}
);
const gptResponse = response.data.choices[0].message.content;
res.json({ response: gptResponse });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello, world!' });
});
const PORT = process.env.PORT || 3000;
// Create HTTPS server
https.createServer(options, app).listen(PORT, () => {
console.log(`HTTPS server running on port ${PORT}`);
});