@tiledesk/tiledesk-server 2.4.73 → 2.4.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/package.json +2 -2
- package/routes/faq_kb.js +30 -6
- package/services/trainingService.js +86 -67
- package/test/faqRoute.js +6 -12
- package/test/faqkbRoute.js +46 -46
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiledesk/tiledesk-server",
|
|
3
3
|
"description": "The Tiledesk server module",
|
|
4
|
-
"version": "2.4.
|
|
4
|
+
"version": "2.4.74",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"start": "node ./bin/www",
|
|
7
7
|
"pretest": "mongodb-runner start",
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"@tiledesk/tiledesk-dialogflow-connector": "^1.8.4",
|
|
43
43
|
"@tiledesk/tiledesk-json-rules-engine": "^4.0.3",
|
|
44
44
|
"@tiledesk/tiledesk-kaleyra-proxy": "^0.1.7",
|
|
45
|
-
"@tiledesk/tiledesk-messenger-connector": "0.1.11",
|
|
46
45
|
"@tiledesk/tiledesk-rasa-connector": "^1.0.10",
|
|
47
46
|
"@tiledesk/tiledesk-tybot-connector": "^0.2.17",
|
|
48
47
|
"@tiledesk/tiledesk-whatsapp-connector": "^0.1.52",
|
|
48
|
+
"@tiledesk/tiledesk-messenger-connector": "^0.1.12",
|
|
49
49
|
"@tiledesk/tiledesk-telegram-connector": "^0.1.8",
|
|
50
50
|
"amqplib": "^0.5.5",
|
|
51
51
|
"app-root-path": "^3.0.0",
|
package/routes/faq_kb.js
CHANGED
|
@@ -15,7 +15,7 @@ var configGlobal = require('../config/global');
|
|
|
15
15
|
const faq = require('../models/faq');
|
|
16
16
|
var jwt = require('jsonwebtoken');
|
|
17
17
|
const uuidv4 = require('uuid/v4');
|
|
18
|
-
|
|
18
|
+
const trainingService = require('../services/trainingService');
|
|
19
19
|
|
|
20
20
|
let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
|
|
21
21
|
|
|
@@ -130,10 +130,12 @@ router.post('/train', function (req, res) {
|
|
|
130
130
|
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
-
router.post('/
|
|
134
|
-
|
|
133
|
+
router.post('/aitrain/', async (req, res) => {
|
|
134
|
+
|
|
135
|
+
let id_faq_kb = req.body.id_faq_kb;
|
|
136
|
+
let webhook_enabled = req.body.webhook_enabled;
|
|
135
137
|
|
|
136
|
-
Faq_kb.findById(id_faq_kb, (err, chatbot) => {
|
|
138
|
+
Faq_kb.findById(id_faq_kb, async (err, chatbot) => {
|
|
137
139
|
if (err) {
|
|
138
140
|
return res.status(400).send({ success: false, error: err })
|
|
139
141
|
}
|
|
@@ -141,8 +143,29 @@ router.post('/train/:id_faq_kb', async (req, res) => {
|
|
|
141
143
|
return res.status(404).send({ sucess: false, error: "Chatbot not found" });
|
|
142
144
|
}
|
|
143
145
|
if (chatbot.intentsEngine === 'tiledesk-ai') {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
|
|
147
|
+
// Option 1: emit event
|
|
148
|
+
//faqBotEvent.emit('faq_train.train', id_faq_kb, webhook_enabled);
|
|
149
|
+
|
|
150
|
+
// Option 2: call service directly
|
|
151
|
+
trainingService.train(null, id_faq_kb, webhook_enabled).then((training_result) => {
|
|
152
|
+
winston.info("training result: ", training_result);
|
|
153
|
+
let response = {
|
|
154
|
+
succes: true,
|
|
155
|
+
message: "Training started"
|
|
156
|
+
}
|
|
157
|
+
if (webhook_enabled === false) {
|
|
158
|
+
response.queue_message = training_result;
|
|
159
|
+
}
|
|
160
|
+
return res.status(200).send(response);
|
|
161
|
+
|
|
162
|
+
}).catch((err) => {
|
|
163
|
+
winston.error("training error: ", err);
|
|
164
|
+
return res.status(200).send({ success: false, message: "Trained not started", error: err });
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
146
169
|
} else {
|
|
147
170
|
return res.status(200).send({ success: true, message: "Trained not started", reason: "Training available for intentsEngine equal to tiledesk-ai only" })
|
|
148
171
|
}
|
|
@@ -1010,6 +1033,7 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
|
|
|
1010
1033
|
router.post('/:faq_kbid/training', function (req, res) {
|
|
1011
1034
|
|
|
1012
1035
|
winston.debug(req.body);
|
|
1036
|
+
winston.info(req.params.faq_kbid + "/training called" );
|
|
1013
1037
|
|
|
1014
1038
|
var update = {};
|
|
1015
1039
|
update.trained = true;
|
|
@@ -6,78 +6,97 @@ const axios = require("axios").default;
|
|
|
6
6
|
var configGlobal = require('../config/global');
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
let
|
|
9
|
+
let training_api_url = process.env.CHATBOT_TRAINING_API_URL || "http://34.65.210.38/model/enqueuetrain";
|
|
10
|
+
let token = process.env.TRAINING_BOT_JWT_TOKEN;
|
|
10
11
|
|
|
11
12
|
class TrainingService {
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
train(eventName, id_faq_kb) {
|
|
15
|
-
|
|
16
|
-
Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
|
|
17
|
-
|
|
18
|
-
if (err) {
|
|
19
|
-
winston.error("train error: ", err)
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (!faq_kb) {
|
|
24
|
-
winston.error("faq_kb is undefined");
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (faq_kb.intentsEngine !== 'tiledesk-ai') {
|
|
29
|
-
winston.debug("intentsEngine: off")
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
winston.debug("intentsEngine: on")
|
|
34
|
-
Faq.find({ id_faq_kb: id_faq_kb }, (err, faqs) => {
|
|
15
|
+
async train(eventName, id_faq_kb, webhook_enabled) {
|
|
35
16
|
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
|
|
19
|
+
Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
|
|
20
|
+
|
|
36
21
|
if (err) {
|
|
37
|
-
winston.error("
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"nlu": [],
|
|
47
|
-
|
|
48
|
-
// curl -v -X PUT -H 'Content-Type:application/json' -u admin@tiledesk.com:adminadmin -d '{"trained":false}' http://localhost:3000/63ed15febb8a5eb3b247fdfd/bots/64551b3422cdfb93ddb1b784
|
|
49
|
-
"webhook_url": process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training",
|
|
50
|
-
"token" : process.env.TRAINING_BOT_JWT_TOKEN
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
faqs.forEach((f) => {
|
|
54
|
-
if (f.enabled == true) {
|
|
55
|
-
let intent = {
|
|
56
|
-
"intent": f.intent_display_name,
|
|
57
|
-
"examples": []
|
|
58
|
-
}
|
|
59
|
-
let questions = f.question.split("\n");
|
|
60
|
-
intent.examples = questions;
|
|
61
|
-
json.nlu.push(intent);
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
winston.debug("training json: \n" + JSON.stringify(json, null, 2));
|
|
66
|
-
|
|
67
|
-
axios({
|
|
68
|
-
url: chatbot_training_api_url,
|
|
69
|
-
headers: {
|
|
70
|
-
'Content-Type': 'application/json'
|
|
71
|
-
},
|
|
72
|
-
data: json,
|
|
73
|
-
method: 'POST'
|
|
74
|
-
}).then((resbody) => {
|
|
75
|
-
winston.info("[Training] resbody: ", resbody.data);
|
|
76
|
-
return true;
|
|
77
|
-
}).catch((err) => {
|
|
78
|
-
winston.error("[Training] error: ", err.response.data);
|
|
79
|
-
})
|
|
22
|
+
winston.error("train error: ", err)
|
|
23
|
+
// return null;
|
|
24
|
+
reject(null);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!faq_kb) {
|
|
28
|
+
winston.error("faq_kb is undefined");
|
|
29
|
+
// return null;
|
|
30
|
+
reject(null);
|
|
80
31
|
}
|
|
32
|
+
|
|
33
|
+
if (faq_kb.intentsEngine !== 'tiledesk-ai') {
|
|
34
|
+
winston.debug("intentsEngine: off")
|
|
35
|
+
// return null;
|
|
36
|
+
reject(null);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
winston.debug("intentsEngine: on")
|
|
40
|
+
Faq.find({ id_faq_kb: id_faq_kb }, async (err, faqs) => {
|
|
41
|
+
|
|
42
|
+
if (err) {
|
|
43
|
+
winston.error("[Training] find all error: ", err);
|
|
44
|
+
} else {
|
|
45
|
+
|
|
46
|
+
let json = {
|
|
47
|
+
"configuration": {
|
|
48
|
+
"language": faq_kb.language,
|
|
49
|
+
"pipeline":["lstm"]
|
|
50
|
+
},
|
|
51
|
+
"model": faq_kb._id,
|
|
52
|
+
"nlu": [],
|
|
53
|
+
//"webhook_url": process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training",
|
|
54
|
+
// curl -v -X PUT -H 'Content-Type:application/json' -u admin@tiledesk.com:adminadmin -d '{"trained":false}' http://localhost:3000/63ed15febb8a5eb3b247fdfd/bots/64551b3422cdfb93ddb1b784
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (webhook_enabled === true) {
|
|
58
|
+
json.webhook_url = process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training"
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
faqs.forEach((f) => {
|
|
62
|
+
if (f.enabled == true) {
|
|
63
|
+
let intent = {
|
|
64
|
+
"intent": f.intent_display_name,
|
|
65
|
+
"examples": []
|
|
66
|
+
}
|
|
67
|
+
if (f.question) {
|
|
68
|
+
let questions = f.question.split("\n");
|
|
69
|
+
intent.examples = questions;
|
|
70
|
+
json.nlu.push(intent);
|
|
71
|
+
} else {
|
|
72
|
+
winston.debug("faq question null!")
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
winston.debug("training json: \n", json);
|
|
78
|
+
|
|
79
|
+
await axios({
|
|
80
|
+
url: training_api_url,
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
'Authorization': token
|
|
84
|
+
},
|
|
85
|
+
data: json,
|
|
86
|
+
method: 'POST'
|
|
87
|
+
}).then((resbody) => {
|
|
88
|
+
winston.debug("[Training] resbody: ", resbody.data);
|
|
89
|
+
// return true;
|
|
90
|
+
resolve(resbody.data);
|
|
91
|
+
}).catch((err) => {
|
|
92
|
+
winston.error("[Training] error: ", err);
|
|
93
|
+
// return false;
|
|
94
|
+
reject(false);
|
|
95
|
+
// winston.error("[Training] error: ", err);
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
81
100
|
})
|
|
82
101
|
|
|
83
102
|
})
|
|
@@ -89,9 +108,9 @@ class TrainingService {
|
|
|
89
108
|
start() {
|
|
90
109
|
winston.info('TrainingService start');
|
|
91
110
|
|
|
92
|
-
faqBotEvent.on('faq_train.train', (id_faq_kb) => {
|
|
111
|
+
faqBotEvent.on('faq_train.train', (id_faq_kb, webhook_enabled) => {
|
|
93
112
|
setImmediate(() => {
|
|
94
|
-
trainingService.train('faq_train.train', id_faq_kb);
|
|
113
|
+
trainingService.train('faq_train.train', id_faq_kb, webhook_enabled);
|
|
95
114
|
})
|
|
96
115
|
})
|
|
97
116
|
|
package/test/faqRoute.js
CHANGED
|
@@ -28,7 +28,7 @@ describe('FaqKBRoute', () => {
|
|
|
28
28
|
|
|
29
29
|
describe('/create', () => {
|
|
30
30
|
|
|
31
|
-
it('create
|
|
31
|
+
it('create', (done) => {
|
|
32
32
|
|
|
33
33
|
// this.timeout();
|
|
34
34
|
|
|
@@ -469,9 +469,7 @@ describe('FaqKBRoute', () => {
|
|
|
469
469
|
.auth(email, pwd)
|
|
470
470
|
.send({ "name": "testbot", type: "internal", template: "example" })
|
|
471
471
|
.end((err, res) => {
|
|
472
|
-
if (log) {
|
|
473
|
-
console.log("res.body", res.body);
|
|
474
|
-
}
|
|
472
|
+
if (log) { console.log("res.body", res.body); }
|
|
475
473
|
res.should.have.status(200);
|
|
476
474
|
res.body.should.be.a('object');
|
|
477
475
|
expect(res.body.name).to.equal("testbot");
|
|
@@ -483,9 +481,7 @@ describe('FaqKBRoute', () => {
|
|
|
483
481
|
.get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb + "&page=0&limit=25&text=looking")
|
|
484
482
|
.auth(email, pwd)
|
|
485
483
|
.end((err, res) => {
|
|
486
|
-
if (log) {
|
|
487
|
-
console.log("found these faqs: \n", res.body);
|
|
488
|
-
}
|
|
484
|
+
if (log) { console.log("found these faqs: \n", res.body); }
|
|
489
485
|
res.should.have.status(200);
|
|
490
486
|
res.body.should.be.an('array');
|
|
491
487
|
|
|
@@ -509,11 +505,9 @@ describe('FaqKBRoute', () => {
|
|
|
509
505
|
chai.request(server)
|
|
510
506
|
.post('/' + savedProject._id + '/faq_kb')
|
|
511
507
|
.auth(email, pwd)
|
|
512
|
-
.send({ "name": "testbot", type: "internal", template: "
|
|
508
|
+
.send({ "name": "testbot", type: "internal", template: "blank", intentsEngine: 'tiledesk-ai' })
|
|
513
509
|
.end((err, res) => {
|
|
514
|
-
if (log) {
|
|
515
|
-
console.log("res.body", res.body);
|
|
516
|
-
}
|
|
510
|
+
if (log) { console.log("res.body", res.body); }
|
|
517
511
|
res.should.have.status(200);
|
|
518
512
|
res.body.should.be.a('object');
|
|
519
513
|
expect(res.body.name).to.equal("testbot");
|
|
@@ -523,7 +517,7 @@ describe('FaqKBRoute', () => {
|
|
|
523
517
|
chai.request(server)
|
|
524
518
|
.post('/' + savedProject._id + '/faq')
|
|
525
519
|
.auth(email, pwd)
|
|
526
|
-
.send({ id_faq_kb: id_faq_kb, question: "question1", answer: "answer1" })
|
|
520
|
+
.send({ id_faq_kb: id_faq_kb, question: "question1\nciao\nbuongiorno", answer: "answer1" })
|
|
527
521
|
.end((err, res) => {
|
|
528
522
|
if (log) { console.log("intentEngin on resbody (create faq): \n", res.body); }
|
|
529
523
|
res.should.have.status(200);
|
package/test/faqkbRoute.js
CHANGED
|
@@ -73,52 +73,52 @@ describe('FaqKBRoute', () => {
|
|
|
73
73
|
|
|
74
74
|
}).timeout(20000);
|
|
75
75
|
|
|
76
|
-
it('train with tiledesk-ai
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
})
|
|
76
|
+
// it('train with tiledesk-ai', (done) => {
|
|
77
|
+
// var email = "test-signup-" + Date.now() + "@email.com";
|
|
78
|
+
// var pwd = "pwd";
|
|
79
|
+
|
|
80
|
+
// userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
|
|
81
|
+
// projectService.create("test-faqkb-create", savedUser._id).then((savedProject) => {
|
|
82
|
+
|
|
83
|
+
// chai.request(server)
|
|
84
|
+
// .post('/' + savedProject._id + '/faq_kb')
|
|
85
|
+
// .auth(email, pwd)
|
|
86
|
+
// .send({ "name": "testbot", type: "internal", template: "example", intentsEngine: "tiledesk-ai" })
|
|
87
|
+
// .end((err, res) => {
|
|
88
|
+
// if (log) {
|
|
89
|
+
// console.log("res.body", res.body);
|
|
90
|
+
// }
|
|
91
|
+
// res.should.have.status(200);
|
|
92
|
+
// res.body.should.be.a('object');
|
|
93
|
+
// expect(res.body.name).to.equal("testbot");
|
|
94
|
+
// var id_faq_kb = res.body._id;
|
|
95
|
+
|
|
96
|
+
// chai.request(server)
|
|
97
|
+
// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb)
|
|
98
|
+
// .auth(email, pwd)
|
|
99
|
+
// .end((err, res) => {
|
|
100
|
+
// if (log) { console.log("faq_list: ", res.body); }
|
|
101
|
+
// res.should.have.status(200);
|
|
102
|
+
// res.body.should.be.an('array').that.is.not.empty;
|
|
103
|
+
|
|
104
|
+
// chai.request(server)
|
|
105
|
+
// .post('/' + savedProject._id + '/faq_kb/aitrain')
|
|
106
|
+
// .auth(email, pwd)
|
|
107
|
+
// .send({ id_faq_kb: id_faq_kb, webhook_enabled: false })
|
|
108
|
+
// .end((err, res) => {
|
|
109
|
+
// if (log) { console.log("train res.body: ", res.body); }
|
|
110
|
+
|
|
111
|
+
// done();
|
|
112
|
+
// })
|
|
113
|
+
|
|
114
|
+
// })
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
// });
|
|
119
|
+
// })
|
|
120
|
+
// })
|
|
121
|
+
// })
|
|
122
122
|
|
|
123
123
|
|
|
124
124
|
it('create with template example', (done) => {
|