-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
60 lines (36 loc) · 1.21 KB
/
index.mjs
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
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: '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' });
const generate = async (prompt) => {
const apiUrl = "https://api.openai.com/v1/chat/completions";
try {
const completionResponse = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: prompt }
],
model: "gpt-3.5-turbo",
});
return completionResponse.choices[0]["message"]["content"];
} catch (error) {
console.log(error);
return;
}
};
import http from 'http';
import express from 'express';
import cors from 'cors';
const app = express();
const server = http.createServer(app);
app.use(cors());
app.use(express.json());
app.post('/',async (req, res) => {
const aa= await generate(req.body.data);
const responseData ={
message:`${aa}`
};
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(JSON.stringify(responseData));
});
const PORT = 5000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});