Skip to content

Commit

Permalink
Add Composer integration
Browse files Browse the repository at this point in the history
Summary: as title, see `CHANGES.md` + prettier

Reviewed By: jayyteee

Differential Revision: D38368445

fbshipit-source-id: 251701fc147e98019bb50bcb7e06517089c06f39
  • Loading branch information
patapizza authored and facebook-github-bot committed Aug 3, 2022
1 parent 8237f61 commit d18b975
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 42 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
## Composer alpha integration

- Composer integration: `.runComposerAudio()`, `.runComposer()` (and raw `.converse()`
+ `.event()`) + `actions` support
- Bumped API version to `20220801`.
- interactive now uses Composer for text inputs, use `!message` for `GET /message` and `!converse` for Composer audio inputs
- added pizza example

## v6.4.0

- Add `POST /synthesize` integration.
- Add `POST /dictation` integration.
- New example using `synthesize()` and `dictation()`.

## v6.3.0

- `speech()` emits `partialUnderstanding` events to support live understanding.
- `apiVersion` updated to `20220608` and its type is now a number.

Expand Down
97 changes: 90 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@ See `examples/messenger.js` for a thoroughly documented tutorial.

The Wit module provides a Wit class with the following methods:

- `runComposerAudio` - the [Composer](https://wit.ai/docs/recipes#composer) integration for voice;
- `runComposer` - the [Composer](https://wit.ai/docs/recipes#composer) integration for other inputs;
- `converse` - the Wit [converse](https://wit.ai/docs/http/#post__converse_link) API;
- `event` - the Wit [event](https://wit.ai/docs/http#post__event_link) API;
- `message` - the Wit [message](https://wit.ai/docs/http#get__message_link) API;
- `speech` - the Wit [speech](https://wit.ai/docs/http#post__speech_link) API.
- `speech` - the Wit [speech](https://wit.ai/docs/http#post__speech_link) API;
- `dictation` - the Wit [dictation](https://wit.ai/docs/http#post__dictation_link) API;
- `synthetize` - the Wit [synthetize](https://wit.ai/docs/http#post__synthetize_link) API.

You can also require a library function to test out your Wit app in the terminal. `require('node-wit').interactive`

### Wit class

The Wit constructor takes the following parameters:

- `accessToken` - the access token of your Wit instance
- `logger` - (optional) the object handling the logging.
- `accessToken` - the access token of your Wit instance;
- `actions` - the object of [client action definitions for Composer](https://wit.ai/docs/recipes#run-custom-code);
- `logger` - (optional) the object handling the logging;
- `apiVersion` - (optional) the API version to use instead of the recommended one

The `logger` object should implement the methods `debug`, `info`, `warn` and `error`.
Expand All @@ -50,14 +57,83 @@ Example:
```js
const {Wit, log} = require('node-wit');

const actions = {
confirm_order(contextMap) {
return {context_map: {...contextMap, order_confirmation: 'PIZZA42'}};
},
};

const client = new Wit({
accessToken: MY_TOKEN,
actions,
logger: new log.Logger(log.DEBUG), // optional
});

console.log(client.message('set an alarm tomorrow at 7am'));
```

## .runComposerAudio()

The [Composer](https://wit.ai/docs/recipes#composer) integration for voice.

Takes the following parameters:

- `sessionId` - a unique string identifying the user session
- `contentType` - the Content-Type header
- `body` - the audio `Readable` stream
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object

Emits `partialTranscription`, `response` and `fullTranscription` events.
Run the provided `actions` as instructed by the API response, and calls back with the resulting updated context map (unless the action returns `stop: true`).
The Promise returns the final JSON payload of the last API call ([POST /converse](https://wit.ai/docs/http#post__converse_link) or [POST
/event](https://wit.ai/docs/http#post__event_link)).

See `lib/interactive.js` for an example.

## .runComposer()

The [Composer](https://wit.ai/docs/recipes#composer) integration for other
inputs, including text.

Takes the following parameters:

- `sessionId` - a unique string identifying the user session
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object
- `message` - the optional user text query

Emits `response` events.
Run the provided `actions` as instructed by the API response, and calls back with the resulting updated context map (unless the action returns `stop: true`).
The Promise returns the final JSON payload of the last [POST /event](https://wit.ai/docs/http#post__event_link) API call.

See `lib/interactive.js` for an example.

## .converse()

The Wit [converse](https://wit.ai/docs/http/#post__converse_link) API.

Takes the following parameters:

- `sessionId` - a unique string identifying the user session
- `contentType` - the Content-Type header
- `body` - the audio `Readable` stream
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object

Emits `partialTranscription` and `fullTranscription` events.

We recommend to use `.runComposerAudio()` instead of this raw API.

## .event()

The Wit [event](https://wit.ai/docs/http#post__event_link) API.

Takes the following parameters:

- `sessionId` - a unique string identifying the user session
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object
- `message` - the optional user text query

We recommend to use `.runComposer()` instead of this raw API.

### .message()

The Wit [message](https://wit.ai/docs/http/#get__message_link) API.
Expand All @@ -80,6 +156,8 @@ client
.catch(console.error);
```

See `lib/interactive.js` for another example integration.

### .speech()

The Wit [speech](https://wit.ai/docs/http#post__speech_link) API.
Expand Down Expand Up @@ -130,7 +208,14 @@ See `examples/synthesize-speech.js` for an example.
### interactive

Starts an interactive conversation with your Wit app.
Use `!speech` to send an audio request from the microphone, or enter any text input.

Full conversational interactions:
Use `!converse` to send an audio request from the microphone using Composer.
Enter any text input to send a text request using Composer.

One-off natural language requests:
Use `!speech` to send an audio request from the microphone.
Use `!message <your message>` to send a text request.

Example:

Expand All @@ -146,9 +231,7 @@ See the [docs](https://wit.ai/docs) for more information.
The default (recommended, latest) API version is set in `config.js`.

On May 13th, 2020, the `GET /message` API was updated to reflect the new data model: intents, traits and entities are now distinct.
We updated the SDK to the latest version: `20200513`.
You can target a specific version by passing the `apiVersion` parameter when
creating the `Wit` object.
You can target a specific version by passing the `apiVersion` parameter when creating the `Wit` object.

```json
{
Expand Down
68 changes: 68 additions & 0 deletions examples/pizza.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
*/

'use strict';

let Wit = null;
let interactive = null;
try {
// if running from repo
Wit = require('../').Wit;
interactive = require('../').interactive;
} catch (e) {
Wit = require('node-wit').Wit;
interactive = require('node-wit').interactive;
}

const accessToken = (() => {
if (process.argv.length !== 3) {
console.log('usage: node examples/pizza.js <wit-access-token>');
process.exit(1);
}
return process.argv[2];
})();

const actions = {
process_order(contextMap) {
const {order} = contextMap;
if (typeof order !== 'object') {
console.log('could not find order');
return {context_map: contextMap};
}

const pizze = Array.from(order.pizze || []);
const pizze_number = pizze.length;
if (pizze_number < 1) {
console.log('could not find any pizze in the order');
return {context_map: contextMap};
}

const processed = pizze.length;
const order_number = pizze[0].type.substring(0, 3).toUpperCase() + '-42X6';

return {context_map: {...contextMap, pizze_number, order_number}};
},
make_summary(contextMap) {
const {order} = contextMap;
if (typeof order !== 'object') {
console.log('could not find order');
return {context_map: contextMap};
}

const pizze = Array.from(order.pizze || []);
if (pizze.length < 1) {
console.log('could not find any pizze in the order');
return {context_map: contextMap};
}

const order_summary = pizze
.map(({size, type}) => 'a ' + size + ' ' + type)
.join(', ');

return {context_map: {...contextMap, order_summary}, stop: true};
},
};

const client = new Wit({accessToken, actions});
interactive(client);
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*/

module.exports = {
DEFAULT_API_VERSION: 20220608,
DEFAULT_API_VERSION: 20220801,
DEFAULT_WIT_URL: 'https://api.wit.ai',
};
Loading

0 comments on commit d18b975

Please sign in to comment.