Skip to content

Commit c4445b5

Browse files
committed
Migrate to ibm-watson
1 parent 53e82f7 commit c4445b5

File tree

3 files changed

+95
-61
lines changed

3 files changed

+95
-61
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Node-RED Watson Nodes for IBM Cloud
1111
- Node-RED & IBM-Watson & Use of promises on API invokation & IAM URL construct migration & Removal of default endpoint of
1212
- Speech to Text node
1313
- Speech to Text Corpus Builder node
14-
- Natural Language Understandig node
14+
- Natural Language Understanding node
15+
- Natural Language Classifier node
1516
- New NLU Model Manager node.
17+
- NLC CreateClassifier is broken until defect on ibm-watson is fixed.
1618

1719
### New in version 0.8.0
1820
- In the 0.8.x releases the nodes are migrated to a node-red 1.0.x input

services/natural_language_classifier/v1.html

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
</div>
4040

4141
<div class="form-row credentials">
42-
<label>&nbsp;</label>
43-
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
44-
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
45-
</div>
46-
<div class="form-row">
4742
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
4843
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/natural-language-classifier/api">
4944
</div>
@@ -52,16 +47,17 @@
5247
<label for="node-input-mode"><i class="fa fa-question"></i> Mode</label>
5348
<select type="text" id="node-input-mode" style="display: inline-block; width: 70%;" >
5449
<option value="classify">Classify</option>
55-
<option value="create">Train</option>
56-
<option value="deleteClassifier">Remove</option>
57-
<option value="listClassifiers">List</option>
50+
<option value="createClassifier">Train</option>
51+
<option value="listClassifiers">List</option>
52+
<option value="getClassifier">Classifier details</option>
53+
<option value="deleteClassifier">Remove</option>
5854
</select>
5955
</div>
60-
<div class="form-row mode classify">
56+
<div class="form-row mode classify getClassifier deleteClassifier">
6157
<label for="node-input-classifier"><i class="fa fa-tag"></i> Classifier ID</label>
6258
<input type="text" id="node-input-classifier" placeholder="Classifier ID">
6359
</div>
64-
<div class="form-row mode create">
60+
<div class="form-row mode createClassifier">
6561
<label for="node-input-language"><i class="fa fa-language"></i> Language</label>
6662
<select type="text" id="node-input-language" style="display: inline-block; width: 70%;" >
6763
<option value="ar">Arabic</option>
@@ -113,12 +109,15 @@
113109
<p>Set the mode in the node configuration window.</p>
114110
<p>The request is triggered by a request to the input node
115111
<p>The returned list of classifiers will be returned on <code>msg.payload</code>.</p>
116-
<p><b>Remove Mode</b>.</p>
112+
<p><b>Classifier Details Mode</b>.</p>
113+
<p>Set the mode in the node configuration window.</p>
114+
<p>If not set in the configuration the Classifier ID should be passed in on
115+
<code>msg.payload</code> as a String<p>
116+
<p>The details of the classifier be returned on <code>msg.payload</code>.</p>
117+
<p><b>Remove Mode</b>.</p>
117118
<p>Set the mode in the node configuration window.</p>
118-
<p>The Classifier ID should be passed in on <code>msg.payload</code> as a String<p>
119-
<p>The status of the delete be returned on <code>msg.payload</code>. It will contain {} if successful</p>
120-
<p><b>More Information</b>.</p>
121-
<p>For more information about the Natural Language Classifier service, read the <a href="http://www.ibm.com/watson/developercloud/natural-language-classifier/api/v1/">documentation</a>.</p>
119+
<p>The status of the delete be returned on <code>msg.payload</code>. It will contain {} if successful.
120+
<p>For more information about the Natural Language Classifier service, read the <a href="http://www.ibm.com/watson/developercloud/natural-language-classifier/api/v1/">documentation</a>.</p>
122121
</script>
123122

124123
<script type="text/javascript">
@@ -145,9 +144,6 @@
145144
$('.form-row.mode.' + mode).show();
146145
$('.form-row.mode:not(.' + mode + ')').hide();
147146
});
148-
149-
nlcV1.CreateIListener($('#node-input-default-endpoint'),
150-
$('#node-input-service-endpoint'), true);
151147
};
152148

153149
nlcV1.checkForPrepare = function () {
@@ -181,8 +177,7 @@
181177
language: {value: "en"},
182178
classifier: {value: ""},
183179
'collections-off' :{value: true},
184-
'default-endpoint' :{value: true},
185-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/natural-language-classifier/api'}
180+
'service-endpoint' :{value: ''}
186181
},
187182
credentials: {
188183
username: {type:"text"},

services/natural_language_classifier/v1.js

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
**/
1616

1717
module.exports = function(RED) {
18-
const SERVICE_IDENTIFIER = 'natural-language-classifier';
19-
const NaturalLanguageClassifierV1 =
20-
require('watson-developer-cloud/natural-language-classifier/v1');
18+
const SERVICE_IDENTIFIER = 'natural-language-classifier',
19+
NaturalLanguageClassifierV1 = require('ibm-watson/natural-language-classifier/v1'),
20+
{ IamAuthenticator } = require('ibm-watson/auth');
2121

2222
var pkg = require('../../package.json'),
2323
temp = require('temp'),
2424
fs = require('fs'),
2525
serviceutils = require('../../utilities/service-utils'),
2626
payloadutils = require('../../utilities/payload-utils'),
27+
responseutils = require('../../utilities/response-utils'),
2728
service = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
2829
username = null,
2930
password = null,
@@ -52,7 +53,7 @@ module.exports = function(RED) {
5253

5354
function Node(config) {
5455
RED.nodes.createNode(this, config);
55-
var node = this;
56+
let node = this;
5657

5758
// Perform basic check to see that credentials
5859
// are provided, altough they may still be
@@ -63,7 +64,7 @@ module.exports = function(RED) {
6364
apikey = sApikey || node.credentials.apikey;
6465

6566
endpoint = sEndpoint;
66-
if ((!config['default-endpoint']) && config['service-endpoint']) {
67+
if (config['service-endpoint']) {
6768
endpoint = config['service-endpoint'];
6869
}
6970

@@ -74,16 +75,34 @@ module.exports = function(RED) {
7475
}
7576
};
7677

78+
// default the mode if not what expected.
79+
node.modeCheck = function(msg) {
80+
switch(config.mode) {
81+
case 'classify':
82+
case 'createClassifier':
83+
case 'listClassifiers':
84+
case 'getClassifier':
85+
case 'deleteClassifier':
86+
break;
87+
default:
88+
config.mode = 'classify';
89+
}
90+
return Promise.resolve();
91+
};
92+
7793
// Sanity check on the payload, must be present
7894
node.payloadCheck = function(msg) {
79-
if (!msg.payload) {
80-
return Promise.reject('Payload is required');
81-
} else {
82-
return Promise.resolve();
95+
switch(config.mode) {
96+
case 'classify':
97+
case 'createClassifier':
98+
if (!msg.payload) {
99+
return Promise.reject('Payload is required');
100+
}
101+
break;
83102
}
103+
return Promise.resolve();
84104
};
85105

86-
87106
node.payloadCollectionCheck = function(msg, config, payloadData) {
88107
if ('classify' === config.mode) {
89108
if ('string' === typeof msg.payload && (! config['collections-off'])) {
@@ -136,9 +155,9 @@ module.exports = function(RED) {
136155
};
137156

138157

139-
// If this is a create then the paload will be a stream
158+
// If this is a create then the payload will be a stream
140159
node.checkForCreate = function(msg, config) {
141-
if ('create' !== config.mode) {
160+
if ('createClassifier' !== config.mode) {
142161
return Promise.resolve(null);
143162
} else if ('string' === typeof msg.payload) {
144163
return Promise.resolve(null);
@@ -161,27 +180,36 @@ module.exports = function(RED) {
161180
params.text = msg.payload;
162181
}
163182

164-
params.classifier_id = config.classifier;
183+
params.classifierId = config.classifier;
165184
if (msg.nlcparams && msg.nlcparams.classifier_id) {
166-
params.classifier_id = msg.nlcparams.classifier_id;
185+
params.classifierId = msg.nlcparams.classifier_id;
167186
}
168187
break;
169-
case 'create':
188+
case 'createClassifier':
189+
params.language = config.language;
190+
191+
let meta = {
192+
language : config.language
193+
};
194+
195+
params.trainingMetadata = JSON.stringify(meta);
196+
170197
if ('string' === typeof msg.payload) {
171-
params.training_data = msg.payload;
198+
params.trainingData = msg.payload;
172199
} else {
173-
params.training_data = fs.createReadStream(info.path);
200+
params.trainingData = fs.createReadStream(info.path);
174201
}
175-
176-
params.language = config.language;
177202
break;
178203
case 'deleteClassifier':
179204
case 'listClassifiers':
180-
params.classifier_id = msg.payload;
205+
case 'getClassifier':
206+
params.classifierId = config.classifier ? config.classifier : msg.payload;
181207
if (msg.nlcparams && msg.nlcparams.classifier_id) {
182-
params.classifier_id = msg.nlcparams.classifier_id;
208+
params.classifierId = msg.nlcparams.classifier_id;
183209
}
184210
break;
211+
case 'listClassifiers':
212+
break;
185213
default:
186214
message = 'Unknown Natural Language Classification mode, ' + config.mode;
187215
}
@@ -195,7 +223,8 @@ module.exports = function(RED) {
195223

196224
node.performOperation = function(msg, config, params) {
197225
var p = new Promise(function resolver(resolve, reject) {
198-
var natural_language_classifier = null,
226+
let natural_language_classifier = null,
227+
authSettings = {};
199228
serviceSettings = {
200229
version: 'v1',
201230
headers: {
@@ -204,11 +233,12 @@ module.exports = function(RED) {
204233
};
205234

206235
if (apikey) {
207-
serviceSettings.iam_apikey = apikey;
236+
authSettings.apikey = apikey;
208237
} else {
209-
serviceSettings.username = username;
210-
serviceSettings.password = password;
238+
authSettings.username = username;
239+
authSettings.password = password;
211240
}
241+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
212242

213243
if (endpoint) {
214244
serviceSettings.url = endpoint;
@@ -225,18 +255,25 @@ module.exports = function(RED) {
225255
if (err) {
226256
reject(err);
227257
} else {
228-
//console.log(response);
229258
switch (mode) {
230259
case 'classify':
260+
responseutils.parseResponseFor(msg, response, 'result');
231261
msg.payload = {
232-
classes: response.classes,
233-
top_class: response.top_class
262+
classes: msg.result.classes,
263+
top_class: msg.result.top_class
234264
};
235265
break;
236266
case 'classifyCollection':
237-
msg.payload = {
238-
collection: response.collection
239-
};
267+
responseutils.parseResponseFor(msg, response, 'collection');
268+
msg.payload = msg.collection;
269+
break;
270+
case 'listClassifiers':
271+
responseutils.parseResponseFor(msg, response, 'classifiers');
272+
msg.payload = msg.classifiers;
273+
break;
274+
case 'getClassifier':
275+
responseutils.parseResponseFor(msg, response, 'result');
276+
msg.payload = msg.result;
240277
break;
241278
default:
242279
msg.payload = response;
@@ -250,10 +287,14 @@ module.exports = function(RED) {
250287
};
251288

252289

253-
this.on('input', function(msg) {
290+
this.on('input', function(msg, send, done) {
254291
//var params = {}
255292
let payloadData = {};
293+
256294
node.verifyCredentials(msg)
295+
.then(function() {
296+
return node.modeCheck(msg);
297+
})
257298
.then(function() {
258299
return node.payloadCheck(msg);
259300
})
@@ -277,16 +318,12 @@ module.exports = function(RED) {
277318
.then(function() {
278319
temp.cleanup();
279320
node.status({});
280-
node.send(msg);
321+
send(msg);
322+
done();
281323
})
282324
.catch(function(err) {
283-
var messageTxt = err.error ? err.error : err;
284-
node.status({
285-
fill: 'red',
286-
shape: 'dot',
287-
text: messageTxt
288-
});
289-
node.error(messageTxt, msg);
325+
let errMsg = payloadutils.reportError(node, msg, err);
326+
done(errMsg);
290327
});
291328
});
292329
}

0 commit comments

Comments
 (0)