-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (79 loc) · 2.87 KB
/
Copy pathserver.js
File metadata and controls
92 lines (79 loc) · 2.87 KB
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
const express = require('express');
const axios = require('axios');
const cors = require('cors');
require('dotenv').config();
const app = express();
const path = require('path')
const port = process.env.PORT || 5000;
app.use(express.json());
app.use(cors());
app.get("/", (req, res) => {
app.use(express.static(path.join(__dirname, "../", "client")));
res.sendFile(path.resolve(__dirname, "../", "client", "index.html"));
})
app.post('/convert', async (req, res) => {
try {
const { code, language } = req.body;
const response = await axios.post('https://api.openai.com/v1/engines/text-davinci-003/completions', {
prompt: `Convert this ${code} in this ${language} language`,
max_tokens: 100,
temperature: 0.7,
n: 1
}, {
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
});
const result = response.data.choices[0].text.trim();
res.json({ result });
} catch (error) {
console.error('Error:', error.response.data);
res.status(500).json({ error: 'Something went wrong while converting' });
}
});
app.post('/debug', async (req, res) => {
try {
const { code } = req.body;
const response = await axios.post('https://api.openai.com/v1/engines/text-davinci-003/completions', {
prompt: `Debug this ${code}`,
max_tokens: 100,
temperature: 0.7,
n: 1
}, {
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
});
const result = response.data.choices[0].text.trim();
res.json({ result });
} catch (error) {
console.error('Error:', error.response.data);
res.status(500).json({ error: 'Something went wrong while debugging' });
}
});
app.post('/checkquality', async (req, res) => {
try {
const { code } = req.body;
const response = await axios.post('https://api.openai.com/v1/engines/text-davinci-003/completions', {
prompt: `Perform quality check for this ${code} that is it implemented correctly or not`,
max_tokens: 100,
temperature: 0.7,
n: 1
}, {
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
});
const result = response.data.choices[0].text.trim();
res.json({ result });
} catch (error) {
console.error('Error:', error.response.data);
res.status(500).json({ error: 'Something went wrong while quality checking' });
}
});
app.listen(port, () => {
console.log(`Server is running on port http://localhost:${port}`);
});