forked from RajKKapadia/YouTube-Openai-Dialogflow-ES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (70 loc) · 1.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const { Configuration, OpenAIApi } = require("openai");
require('dotenv').config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const textGeneration = async (prompt) => {
try {
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `Human: ${prompt}\nAI: `,
temperature: 0.9,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: ['Human:', 'AI:']
});
return {
status: 1,
response: `${response.data.choices[0].text}`
};
} catch (error) {
return {
status: 0,
response: ''
};
}
};
const webApp = express();
const PORT = process.env.PORT;
webApp.use(express.urlencoded({ extended: true }));
webApp.use(express.json());
webApp.use((req, res, next) => {
console.log(`Path ${req.path} with Method ${req.method}`);
next();
});
webApp.get('/', (req, res) => {
res.sendStatus(200);
});
webApp.post('/dialogflow', async (req, res) => {
let action = req.body.queryResult.action;
let queryText = req.body.queryResult.queryText;
if (action === 'input.unknown') {
let result = await textGeneration(queryText);
if (result.status == 1) {
res.send(
{
fulfillmentText: result.response
}
);
} else {
res.send(
{
fulfillmentText: `Sorry, I'm not able to help with that.`
}
);
}
} else {
res.send(
{
fulfillmentText: `No handler for the action ${action}.`
}
);
}
});
webApp.listen(PORT, () => {
console.log(`Server is up and running at ${PORT}`);
});