-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
68 lines (62 loc) · 1.54 KB
/
router.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
import express from "express";
const router = express.Router();
import * as dotenv from "dotenv";
import { Configuration, OpenAIApi } from "openai";
dotenv.config();
const configration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configration);
let removeComments = (code) => {
let lines = code.split("\n");
let result = [];
for (let line of lines) {
let i = line.indexOf("//");
if (i === -1) {
result.push(line);
} else {
result.push(line.slice(0, i));
}
}
return result.join("\n");
};
router.get("/", async (req, res) => {
try {
res.status(200).send({
message: "hello from muhibGPT ",
});
} catch (error) {
console.log(error);
res.status(500).send({
error,
});
}
});
router.post("/", async (req, res) => {
try {
const prompt = req.body.prompt;
console.log(prompt);
const response = await openai.createCompletion({
model: "code-davinci-002",
prompt: `${prompt}`,
temperature: 0.5,
max_tokens: 4000,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0,
});
const data = response.data.choices[0].text;
const formattedData = data
.split("\n")
.map((line) => line.replace(/^\+/, ""))
.join("\n");
const commentsRem = removeComments(formattedData);
console.log(commentsRem);
res.status(200).send(commentsRem);
console.log(prompt);
} catch (error) {
console.log(error);
res.status(500).send({ error });
}
});
export default router;