-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (30 loc) · 947 Bytes
/
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
const { Configuration, OpenAIApi } = require("openai");
const express = require("express");
const cors = require('cors');
require('dotenv').config()
const app = express();
app.use(cors());
app.use(express.json());
const configuration = new Configuration({
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
//model takes 35 secs to bring response
const askGPT = async (prompt) => {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
});
console.log(response["data"]["choices"][0]["message"]["content"]);
return response["data"]["choices"][0]["message"]["content"];
};
const getData = async (req, res) => {
const prompt = req.query.prompt;
const data = await askGPT(prompt);
//erfoie
return res.status(200).send(data);
};
app.get("/chatbot", getData);
app.listen(8000,()=>{
console.log("server running....!!");
})