-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (51 loc) · 2.07 KB
/
server.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
const express = require('express');
const axios = require('axios');
const app = express();
// START - https://www.digitalocean.com/community/tutorials/use-expressjs-to-deliver-html-files
const path = require('path');
// END
const PORT = 3000;
// Middleware to parse JSON requests
app.use(express.json());
// START - https://www.digitalocean.com/community/tutorials/use-expressjs-to-deliver-html-files
// sendFile will go here
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/index.html'));
});
// END
app.post('/generate-chords', async (req, res) => {
const genre = req.body.genre;
const promptText = `Generate a chord progression for a ${genre} song with a verse, bridge, and chorus.`;
try {
// https://help.openai.com/en/articles/6283125-what-happened-to-engines -- had to change it from https://api.openai.com/v1/engines/davinci/completions
// Also, went to https://platform.openai.com/playground?lang=node.js&mode=chat&model=gpt-3.5-turbo and reviewed the code snippet to figure out how to form the object.
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: "gpt-3.5-turbo",
messages: [
{
"role": "system",
"content": promptText
}
],
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
}, {
headers: {
'Authorization': 'Bearer YOUR_API_KEY', // Replace with your OpenAI API key
'Content-Type': 'application/json'
}
});
res.json(response.data.choices[0].message.content.trim());
} catch (error) {
res.status(500).send('Error generating chord progression');
// START - Response Debugging
// res.status(500).send('Error generating chord progression: ' + error.response.data.error.message);
// END
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});