Git Product home page Git Product logo

voiceflow_lexv2's Introduction

Voiceflow x Lex V2 Custom Integration

Prerequisites

Here are the tools you will need for this project:

  1. Amazon Lex V2 Bot
  2. Voiceflow Account

Setting up the Project

Install and run the project:

  1. Clone this repo:
git clone https://github.com/zslamkov/voiceflow_lexv2.git
  1. Install dependencies:
npm install

Signing AWS HTTP Requests

To allow for all of our AWS requests to be signed with an AWSv4 signature, we will be using the aws-axios package.

We can complete the connection with only a few lines of code

const client = axios.create();

const interceptor = aws4Interceptor(
  {
    region: "us-east-1",
    service: "lex",
  },
  {
    accessKeyId: process.env.AWS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET,
  }
);

client.interceptors.request.use(interceptor);

Retrieve Intent Data

We will be sending an unstructured utterance to the Lex V2 RecognizeText endpoint in order to receive a response with:

  1. The triggered intent name
  2. Entity key/values
  3. Confidence score

The below function illustrates the request and how to transform the response into a format ready for Voiceflow.

async function detectIntent(textInput, sessionID) {
  const response = await client
    .post(
      `https://runtime-v2-lex.us-east-1.amazonaws.com/bots/${botID}/botAliases/${aliasID}/botLocales/${botLocale}/sessions/${sessionID}/text`,
      { text: textInput }
    )
    .then(
      (response) => {
        console.log(response.data.interpretations[0].nluConfidence.score);
        return response;
      },
      (err) => {
        console.log(err);
      }
    );

  const intent = response.data.interpretations[0].intent.name;
  const entities = response.data.interpretations[0].intent.slots;
  const nluConfidence = response.data.interpretations[0].nluConfidence.score;

  const createIntent = (name, value) => ({ name, value });
  const arr = [];

  for (const [key, value] of Object.entries(entities)) {
    if (value != null) {
      const intents = createIntent(key, value.value.interpretedValue);
      arr.push(intents);
    }
  }

  return { response: intent, entities: arr, confidence: nluConfidence };
}

Node.js App

The below app allows us to interact with our chat assistant via the CLI.

We will be retrieving the userID by having the user enter their name. Once completed, we will send a launch request to Voiceflow to start the conversation and send the first steps to the channel.

async function main() {
  const userID = await cli.prompt("> What is your name?");
  // send a simple launch request starting the dialog
  let isRunning = await interact(userID, { type: "launch" });

Now on each new interaction from the user, we will be passing the userID and text input through the detectIntent function and returning the intent object that will be used in the next line of code to populate the request payload to Voiceflow.

  while (isRunning) {
    const nextInput = await cli.prompt("> Say something");
    // send a simple text type request with the user input
    let intent = await detectIntent(nextInput, userID);
    
    isRunning = await interact(userID, {
      type: "intent",
      payload: {
        intent: {
          name: intent.response,
        },
        entities: intent.entities,
        confidence: intent.confidence,
      },
    });
  }
  console.log("The end! Start me again with `npm start`");
}

main();

Run

What it might look like in action:

$ npm start

> What is your name?: zoran
what can I do for you?
...
> Say something: send email
who is the recipient?
...
> Say something: [email protected]
what is the title of your email?
...
> Say something: How was your day?
sending the email for [email protected] called "How was your day?". Is that correct?
...
> Say something: yes
successfully sent the email for [email protected] called "How was your day?"
The end! Start me again with `npm start`

voiceflow_lexv2's People

Contributors

zslamkov avatar

Watchers

 avatar

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.