Skip to content

Commit 4b7a869

Browse files
committed
Major updates to API and structure.
Updated the API to be more node friendly; restructured to simplify validations; updated request handling to cover an edge case where data could become corrupt; requests now correctly speficy JSON content-type; and added tests.
1 parent a610636 commit 4b7a869

File tree

11 files changed

+324
-417
lines changed

11 files changed

+324
-417
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
samples
2+
test

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,52 @@
11
LUIS
22
==============
3-
LUIS is a service for language understanding that provides intent classification and entity extraction.
4-
In order to use the SDK you first need to create and publish an app on www.luis.ai where you will get your appId and appKey and input their values in the sample application file "sample.js".
53

6-
The SDK
7-
--------------
8-
The SDK can be used by creating 2 callback functions "onSuccess" and "onFailure" and passing them to the "predict" and "reply" functions to be called asynchronously in the cases of the request success or failure.
4+
LUIS is a service for language understanding that provides intent classification
5+
and entity extraction. In order to use the SDK you first need to create and
6+
publish an app on www.luis.ai where you will get your `appId` and `appKey` and
7+
input their values in the sample application file "sample.js".
8+
9+
10+
## Usage
11+
12+
```js
13+
const client = require('luis-sdk')({
14+
appId: 'your app ID',
15+
appKey: 'your app key',
16+
verbose: true
17+
})
18+
19+
client.predict('Enter text to predict', (err, response) => {
20+
if (err) throw err
21+
22+
console.dir(response)
23+
24+
response.entities.forEach((e, i) => {
25+
console.log(`${i + 1} - ${e.entity}`)
26+
})
27+
})
28+
29+
```
30+
31+
## Sample Application
32+
33+
The sample application allows you to perform the Predict and Reply operations and
34+
to view the following parts of the parsed response:
935

10-
Sample Application
11-
--------------
12-
The sample application allows you to perform the Predict and Reply operations and to view the following parts of the parsed response:
1336
- Query
1437
- Top Intent
1538
- Entities
16-
- Dialog status, parameter name, and prompt
39+
- Dialog status, parameter name, and prompt
1740

1841
License
1942
=======
2043

21-
All Microsoft Cognitive Services SDKs and samples are licensed with the MIT License. For more details, see
22-
[LICENSE](</LICENSE.md>).
44+
All Microsoft Cognitive Services SDKs and samples are licensed with the MIT
45+
License. For more details, see [LICENSE](</LICENSE.md>).
2346

2447
Developer Code of Conduct
2548
=======
2649

27-
Developers using Cognitive Services, including this client library & sample, are required to follow the “[Developer Code of Conduct for Microsoft Cognitive Services](http://go.microsoft.com/fwlink/?LinkId=698895)”.
28-
50+
Developers using Cognitive Services, including this client library & sample, are
51+
required to follow the “[Developer Code of Conduct for Microsoft Cognitive
52+
Services](http://go.microsoft.com/fwlink/?LinkId=698895)”.

index.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict'
2+
3+
const Assert = require('assert')
4+
const Https = require('https')
5+
const Util = require('util')
6+
7+
const Concat = require('concat-stream')
8+
9+
const LUISURL = 'api.projectoxford.ai'
10+
const LUISPredictMask = '/luis/v2.0/apps/%s?subscription-key=%s&q=%s&verbose=%s'
11+
const LUISReplyMask = '/luis/v2.0/apps/%s?subscription-key=%s&q=%s&contextid=%s&verbose=%s'
12+
13+
/**
14+
* Sends an API request.
15+
*
16+
* @param {object} options - Request options.
17+
* @param {function} done - Callback function.
18+
*/
19+
function sendRequest (options, done) {
20+
let req = Https.request(options, (response) => {
21+
response.pipe(Concat((data) => {
22+
let body
23+
data = data.toString()
24+
25+
if (data && data.length > 0) {
26+
body = JSON.parse(data)
27+
28+
if (response.statusCode !== 200) return done(new Error(body.message))
29+
30+
return done(null, body)
31+
}
32+
33+
done(new Error('Invalid or empty response from LUIS.'))
34+
}))
35+
})
36+
37+
req.on('error', done.bind(null))
38+
req.end()
39+
}
40+
41+
/**
42+
* This is the interface of the LUIS SDK
43+
* Constructs a LUISClient with the corresponding user's App ID and Subscription
44+
* Keys.
45+
*
46+
* @param {object} initData - Configuration object to initialize the client.
47+
* @param {string} initData.appId - Application ID for your LUIS app.
48+
* @param {String} initData.appKey - Application Key for your LUIS app.
49+
* @param {boolean} initData.verbose - Whether to receive verbose output.
50+
* @returns {{predict: function, reply: function}} - Interface methods.
51+
*/
52+
module.exports = (initData) => {
53+
Assert.ok(initData, 'Configuration object required to initialize LUIS Client')
54+
Assert.ok(initData.appId, 'You must specify an \'appId\'')
55+
Assert.ok(initData.appKey, 'You must specify an \'appKey\'')
56+
57+
initData.verbose = Boolean(initData.verbose || true)
58+
59+
return {
60+
/**
61+
* Initiates the prediction procedure.
62+
*
63+
* @param {string} text - The text to be analyzed and predicted.
64+
* @param {function} done - Callback function.
65+
*/
66+
predict: (text, done) => {
67+
if (!text) return done(new Error('Text cannot be empty'))
68+
69+
let options = {
70+
hostname: LUISURL,
71+
path: Util.format(LUISPredictMask, initData.appId, initData.appKey, encodeURIComponent(text), initData.verbose),
72+
method: 'GET',
73+
headers: { 'Content-Type': 'application/json' }
74+
}
75+
76+
sendRequest(options, done)
77+
},
78+
/**
79+
* Initiates the prediction procedure
80+
*
81+
* @param {string} text - The text to be analyzed and predicted.
82+
* @param {string} context - Context of the request.
83+
* @param {string} forceSet - Optional fording set.
84+
* @param {function} done - Callback function.
85+
*/
86+
reply: (text, context, forceSet, done) => {
87+
if (!text) return done(new Error('Text cannot be empty'))
88+
if (typeof forceSet === 'function') {
89+
done = forceSet
90+
forceSet = null
91+
}
92+
93+
let options = {
94+
hostname: LUISURL,
95+
path: Util.format(LUISReplyMask, initData.appId, initData.appKey, encodeURIComponent(text), context, initData.verbose),
96+
method: 'GET',
97+
headers: { 'Content-Type': 'application/json' }
98+
}
99+
100+
if (forceSet) options.path += `&forceset=${forceSet}`
101+
102+
sendRequest(options, done)
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)