GithubHelp home page GithubHelp logo

hexastack / wit-api Goto Github PK

View Code? Open in Web Editor NEW
17.0 8.0 0.0 105 KB

Node lib to easily interact with Wit.ai HTTP API

License: BSD 4-Clause "Original" or "Old" License

JavaScript 100.00%
wit facebook messenger nodejs api ai nlp natural-language-processing wit-api

wit-api's Introduction

wit-api

wit-api is a node package that allows you to easily configure, train and consume your NLP through Wit.ai HTTP API.

Note: This is the version 1.x.y, it has no dependency, and it uses promises.

Note: Version 1.0.0 is stable, and will recieve only additional utils methods

Installation

npm i wit-api --save

Initialization

const Wit = require('./src/Wit')

async function run () {
  try {
    // Instantiation
    const wit = new Wit('<YOUR-SERVER-ACCESS-TOKEN>')
    // Syncronize with remote
    await wit.sync()
  } catch (e) {
    console.error(e)
  }
}

run()

Additionally, you can pass options to the instantiation, like: new Wit('<YOUR-SERVER-ACCESS-TOKEN>', { debug: true, timeout: 30000, version: '20170307'})

  • debug: will display more insights about the http requests
  • timeout: in milliseconds, indicate when the http request times-out
  • version: indicate what version of wit.ai to use, this lib ain't work for versions prior to 20170307

Entities, Vlaues & Expressions

async function run () {
  try {
    const wit = new Wit('<YOUR-SERVER-ACCESS-TOKEN>')
    await wit.sync()
    // Adding a new entity
    await wit.entities.add('cloth')
    // The newly added entity is sync-ed in the `wit` object
    // and can be accessed through `wit.entities.cloth`

    // For script reusability, it is recomended check the entity existance before adding it
    if (!wit.entities.size) {
      await wit.entities.add('size')
    }

    // Update an entity
    await wit.entities.cloth.update({ doc: 'An item from the store', lookups: ['keywords'] })

    // We can also manipulate the entities values and expression through the `.update` function
    // Also additional options are present, like metadata
    await wit.entities.size.update({
      doc: 'Size',
      lookups: ['free-text', 'keywords'],
      values: [
        {value: 'extra small', expressions: ['36'], metadata: 'XS'},
        {value: 'small', expressions: ['37', '38'], metadata: 'S'},
        {value: 'medium', expressions: ['39', '40'], metadata: 'M'},
        {value: 'large', expressions: ['41', '42'], metadata: 'L'},
        {value: 'extra large', expressions: ['43', '44'], metadata: 'XL'}
      ]
    })

    // Deleting an entity
    await wit.entities.cloth.destroy()
  } catch (e) {
    console.error(e)
  }
}

run()

Training

// This goes insde an `async function`
await wit.services.sample.train([
  {
    text: 'I would like to have a jean please.',
    entities: [
      {
        entity: 'intent',
        value: 'order'
      },
      {
        entity: 'product',
        value 'pant',
        start: '22',
        end: '26'
      }
    ]
  }
])
// wit.service.sample.train can take multiples training sample

// You can reverse a training by using .forget
await wit.services.sample.forget([
  {text: 'I would like to have a jean please.'}
])

Guessing (text & speech)

This is actually where we use the api for understanding

All the guess methods retuen a Guess Object

// this goes inside an `async function`

// Text:
const guess = await wit.services.guess.message('I want to try a coton black shirt, preferably size 43')
console.log(guess)
/* example of a result:
{ _text: 'I want to try a coton black shirt, preferably size 43',
  entities:
   { product: [ { confidence: 1, value: 'shirt', type: 'value' } ],
     shirtTextile: [ { confidence: 1, value: 'coton', type: 'value' } ],
     color:
      [ { metadata: '#000000',
          confidence: 1,
          value: 'black',
          type: 'value' } ],
     size: [ { confidence: 1, value: 'XL', type: 'value' } ],
     intent: [ { confidence: 0.72456770482335, value: 'try' } ] },
  msg_id: '1AY90y2ewdQhotazg'
}
*/

// Language:
await wit.services.guess.language('je m\'appel foobar')

// You can also have both language guessing and text guessing by using
await wit.services.guess.all('I want to try a coton black shirt, preferably size 43')

// Audio:
await wit.services.guess.speech('path/to/an/audio/file.wav')

Both .message and .speech can take additional options, in like the following

const options = {n: 5}
await wit.services.guess.message(text, options)

More about these options at https://wit.ai/docs/http/20170307#get__message_link

Guess Methods

Once the guessing service was called, it returns a Guess Object, theses objects have utils methods.

For instance to return only relevant guesses (with confidance above 0.95)

const guess = await wit.services.guess.all('I want to try a coton black shirt, preferably size 43', {n: 5})
console.log(guess.bestGuesses(.95))
/* 
would print:
{
  "shirtTextile": {"confidence": 1, "value": "coton", "type": "value"},
  "color": {"metadata": "#000000", "confidence": 1, "value": "black", "type": "value"},
  "product": {"confidence": 1, "value": "shirt", "type": "value"},
  "size": {"confidence": 1, "value": "XL", "type": "value"},
  "intent": {"confidence": 0.98808266775175, "value": "request"},
  "language": {"locale": "en_XX", "confidence": 1}
}
*/

// Additionally you can filter and softfilter guessing results using:
guess.filter(.95)
guess.softFilter(1)
with love from Hexastack

wit-api's People

Contributors

bashz avatar marrouchi avatar prasanthnarasimha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wit-api's Issues

Unable to wit.entity.update()

There are two issues :

  • HTTP request method is sent as POST instead of PUT
  • request var is undefined in the update() method

Unable to add a new value a an entity - return entity "not-found'

Running unit tests returns :

nyc mocha --timeout 5000 --reporter nyan

17 --------------,------,
9 --------------| /_/\
0 --------------^|__( x .x)
-
------------- "" ""

17 passing (43s)
9 failing

  1. Wit
    Entity services
    Get one entity by name
    Should fetch an Entity:
    Error: expected [Error: Not Found] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.entity.get.then.catch.e (test/test.js:53:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  2. Wit
    Value services
    Should add a value:
    Error: expected [Error: expected [] to contain 'keywords'] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.value.add.then.catch.e (test/test.js:158:29)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  3. Wit
    Expression services
    Should remove an expression:
    Error: expected [Error: expected { status: 400,
    body: 'This entity needs at least one of keywords or trait lookup to be able to delete expressions',
    code: 'bad-request' } to have a property 'deleted'] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.expression.delete.then.catch.e (test/test.js:192:29)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  4. Wit
    Learning
    Train using one sample at a time
    Should yeild on a json response:
    Error: expected [Error: expected [Error: You tried to create 'wit$greetings'. Use POST /entities instead] to be falsy] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.train.then.catch.e (test/test.js:246:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  5. Wit
    Text messages
    Test on a learned text
    Should respond by an Intent Object:
    Error: expected [Error: expected {} to not be empty] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.message.then.catch.e (test/test.js:283:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  6. Wit
    Text messages
    Test on a forgotten text
    Should respond by an Intent Object containing greeting:
    Error: expected [Error: expected {} to not be empty] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.message.then.catch.e (test/test.js:301:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  7. Wit
    Speech
    Should respond by an Intent Object:
    Error: expected [Error: expected 'Hello' to equal 'hello'] to be falsy
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.ok (node_modules/expect.js/index.js:115:10)
    at Function.ok (node_modules/expect.js/index.js:499:17)
    at wit.speech.then.catch.e (test/test.js:319:29)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  8. Wit
    App Management
    Delete an App
    The latest wit instance shall not work
    Should trigger error:
    Error: expected [Error: expected { name: 'intent',
    id: '5b73fa2b-cf19-4591-a653-eb78a8dc719f',
    values: [],
    builtin: false,
    doc: 'User-defined entity',
    lang: 'en',
    lookups: [ 'trait' ],
    request: [Function] } to be falsy] to have a property 'code'
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.property (node_modules/expect.js/index.js:381:12)
    at newWit.entity.get.then.catch.e (test/test.js:406:31)
    at process._tickCallback (internal/process/next_tick.js:109:7)

  9. Wit
    Error handeling
    Api fails
    Should fail & return Error instance with a message and code props:
    Error: expected 'no-auth' to equal 'unknown'
    at Assertion.assert (node_modules/expect.js/index.js:96:13)
    at Assertion.be.Assertion.equal (node_modules/expect.js/index.js:216:10)
    at Assertion.(anonymous function) [as be] (node_modules/expect.js/index.js:69:24)
    at defectedWit.entity.list.then.catch.e (test/test.js:424:29)
    at process._tickCallback (internal/process/next_tick.js:109:7)

-----------------|----------|----------|----------|----------|-------------------|

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 80.22 56.76 80.77 80.15
wit-api 100 100 100 100
index.js 100 100 100 100
wit-api/src 88.78 59.18 95 88.72
Entity.js 93.18 84.62 100 93.02 47,74,78
Expression.js 85.71 50 100 85.71 27,31,42
Message.js 93.75 50 100 93.75 24
Sample.js 87.5 75 100 87.5 26,45,49
Speech.js 80 41.67 100 80 11,29,40,43,44,50
Value.js 85.71 33.33 100 85.71 29,33,44
Wit.js 92.5 66.67 57.14 92.5 53,58,63
wit-api/src/lib 57.33 52 33.33 57.33
App.js 100 66.67 100 100 1
Entity.js 33.33 33.33 9.09 33.33 ... 56,57,59,64,66
Intent.js 52.94 20 33.33 52.94 ... 11,13,14,15,18
requestAPI.js 85.71 87.5 100 85.71 5,11
----------------- ---------- ---------- ---------- ---------- -------------------
npm ERR! Test failed. See above for more details.

Wit.entity.delete() fails when making multiple calls

Wit.entity.delete() fails when making multiple calls. The second call always concatenates the entity id in URL with the previous one.

{ 
  method: 'DELETE',
  uri: '/entities/5b0eaea8-3531-4138-8b18-555a2968de07/5b0eab46-b8c8-4438-a1f3-24522bf0311c'
}

I guess this could be also the case for other methods ... must check.

Wit.train() not working

Got the following error when calling Wit.train() :

{
"error" : "You need to provide an array of samples",
"code" : "bad-request"
}

According to the documentation, the body must be an array. That said, maybe changing the method so i can support having an argument with one or a set of samples.

Train : Max text length is 280

When sending train(), WIT API is not accepting samples when text length is greater than 280.

Two solutions are available : Either slice the text or return an error

@bashz : What do you think ?

Package needs update to implement new V2 API specification

New parse payload have the following data structure :

{"text":"Bonjour","intents":[{"id":"467958847129146","name":"greeting","confidence":0.9986}],"entities":{},"traits":{"language":[{"id":"0596e762-b648-4e55-84ff-2fbaa4eca22b","value":"fr","confidence":0.9989}]}}

I don't know how much it will impact the current code. We must review the full API documentation.

@bashz i would recommend tagging the current branch before

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.