-
Notifications
You must be signed in to change notification settings - Fork 108
/
index.js
106 lines (99 loc) · 3.66 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const {Configuration, OpenAIApi} = require("openai");
const app = express();
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const { v4: uuidv4 } = require('uuid');
require("dotenv").config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
app.use(cors());
app.use(express.json());
app.use('/', express.static(__dirname + '/client')); // Serves resources from client folder
// Set up Multer to handle file uploads
const upload = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
const extension = path.extname(file.originalname);
const filename = uuidv4() + extension;
cb(null, filename);
}
}),
limits: { fileSize: 1024 * 1024 * 10 }, // 10 MB
fileFilter: function (req, file, cb) {
const allowedExtensions = ['.mp3', '.mp4', '.mpeg', '.mpga', '.m4a', '.wav', '.webm'];
const extension = path.extname(file.originalname);
if (allowedExtensions.includes(extension)) {
cb(null, true);
} else {
cb(new Error('Invalid file type.'));
}
}
});
app.post('/transcribe', upload.single('audio'), async (req, res) => {
try {
const resp = await openai.createTranscription(
fs.createReadStream(req.file.path),
"whisper-1",
'text'
);
return res.send(resp.data.text);
} catch (error) {
const errorMsg = error.response ? error.response.data.error : `${error}`;
console.log(errorMsg)
return res.status(500).send(errorMsg);
} finally {
fs.unlinkSync(req.file.path);
}
});
app.post('/get-prompt-result', async (req, res) => {
// Get the prompt from the request body
const {prompt, model = 'gpt'} = req.body;
// Check if prompt is present in the request
if (!prompt) {
// Send a 400 status code and a message indicating that the prompt is missing
return res.status(400).send({error: 'Prompt is missing in the request'});
}
try {
// Use the OpenAI SDK to create a completion
// with the given prompt, model and maximum tokens
if (model === 'image') {
const result = await openai.createImage({
prompt,
response_format: 'url',
size: '512x512'
});
return res.send(result.data.data[0].url);
}
if (model === 'chatgpt') {
const result = await openai.createChatCompletion({
model:"gpt-3.5-turbo",
messages: [
{ role: "user", content: prompt }
]
})
return res.send(result.data.choices[0]?.message?.content);
}
const completion = await openai.createCompletion({
model: 'text-davinci-003', // model name
prompt: `Please reply below question in markdown format.\n ${prompt}`, // input prompt
max_tokens: 4000
});
// Send the generated text as the response
return res.send(completion.data.choices[0].text);
} catch (error) {
const errorMsg = error.response ? error.response.data.error : `${error}`;
console.error(errorMsg);
// Send a 500 status code and the error message as the response
return res.status(500).send(errorMsg);
}
});
const port = process.env.PORT || 3001;
app.listen(port, () => console.log(`Listening on port ${port}`));