-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (49 loc) · 1.48 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
// Ai chatBoat
const express = require('express');
//openAI modile start with
const OPENAI_API_KEY = "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";
const { Configuration, OpenAIApi } = require("openai");
const cors = require("cors");
const configuration = new Configuration({
apiKey: OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// openai.listEngines().then ((response) =>{
// console.log(response);
// });
//openAI modile finish
const app = express();
app.use(cors());
app.use(express.json());
app.get("/ping", (req, res) => {
res.json({
message: "pong",
});
});
// responsing the ans and question both
app.post("/chat", (req, res) => {
const question = req.body.question;
openai.createCompletion({
model: "text-davinci-003",
prompt: question,
max_tokens: 4000,
temperature: 0,
}).then(response => {
console.log({ response });
return response?.data?.choices?.[0]?.text;
}).then((answer) => {
console.log({ answer });
const array = answer?.split("\n").filter((value) => value).map((value) => value.trim());
return array;
})
.then((answer) => {
res.json({
answer: answer,
propt: question,
});
})
console.log({ question });
});
app.listen(3000, () => {
console.log("server is listening on port 3000");
})