GithubHelp home page GithubHelp logo

tcarrio / dialogflow-as-code Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 156 KB

[Mirror] Declarative management of Dialogflow resources

Home Page: https://git.sr.ht/~tcarrio/dialogflow-as-code

License: Other

JavaScript 4.02% TypeScript 95.98%
dialogflow nlp automation devops

dialogflow-as-code's Introduction

npm (scoped) builds.sr.ht status License

dialogflow-as-code

This package allows the creation of Dialogflow resources by using the nodejs-dialogflow project along with the types we have helped define in @types/dialogflow. It was developed using Node.js and TypeScript.

The builder helpers can be used for generating resources, however the module also allows for objects to be directly passed to the builder. The builders eventually generate the same types expected by the nodejs-dialogflow API, so whichever you find easier will work fine. For some of the more advanced types, it can be much easier to work with the builders. The following provides several examples of defining Dialogflow resources and how they eventually tie together to be created and synced to a Dialogflow agent:

// Sample Entity Type Builder
export const etFruit = entityType()
  .d("fruit")
  .e([syn("apple"), syn("strawberry")])
  .k(ek.list)
  .build();
// Sample Entity Type
export const etSample: EntityType = {
  displayName: "sample",
  entities: [{ value: "sample", synonyms: ["piece", "swab", "choice"] }],
  kind: "KIND_MAP",
  autoExpansionMode: "AUTO_EXPANSION_MODE_DEFAULT",
};
// Sample Context Builder
export const cxFruit = cx()
  .n("fruit-context")
  .lc(5)
  .p("date-time-original", "string_value")
  .build();
// Sample Events
export enum Event {
  FEEDBACK = "FEEDBACK",
  YES = "YES",
  NO = "NO",
}
// Sample Intent
// prettier-ignore
export const ntFruitInfo = intent("fruitInfo")
  .priority(Priority.LOW)
  .webhook(true)
  .trainingPhrases([
    tp(["describe the ", pb("sample"), " of ", etFruit, " over ", det("date-time")]),
    tp(["how was the ", pb("sample"), " of ", etFruit]),
    tp([pb("sample"), " of ", etFruit, " ", det("date-time")]),
    tp([pb("sample"), " of ", etFruit]),
    tp(["what was the ", pb("sample"), "inputContextsf ", etFruit, " ", det("date-time"), "?"]),
    tp(["what was the ", pb("sample"), " of ", etFruit]),
  ])
  .messages([
    msg("text").set(["I'm sorry Dave, I can't do that"]).build(),
    msg("text").set(["Second response"]).build(),
  ])
  .events([Event.FEEDBACK])
  .outputContexts([cxFruit])
  .followUpOf(ntFruitReminder);
// Sample Resource Build and Sync Script
const svcAcctKeyJson: string = "./service-account-key.json";
const svcAcctConfig: DialogflowServiceAccount = require(`.${svcAcctKeyJson}`);
Container.set(KEY_FILENAME, svcAcctKeyJson);
Container.set(DIALOGFLOW_CONFIG, svcAcctConfig);

const resources = Container.get(DialogflowBuilder)
  .entityTypes([etSample, etFruit])
  .intents([ntFruitInfo, ntFruitReminder])
  .build();

Container.get(DialogflowCreator).sync(resources);

Note: You will need to create a service account on the Dialogflow website and provide it at the project level as service-account-key.json. The file path and config object are passed to the DialogflowBuilder. The path can be changed, this is just the sample and converted project expectations.

If you would like to to take it for a spin, you can run the provided project sample:

git clone https://git.sr.ht/~tcarrio/dialogflow-as-code
pushd dialogflow-as-code
npm i
npm run sample

dialogflow-converter

This project contains a module for converting resources exported from Dialogflow into resources for use by dialogflow-as-code. These are rudimentary at the moment, with future improvements planned for the templating structure and full feature-set of dialogflow-as-code.

Run the conversion by calling npm start -- -i $input_path -o $output_path. It can also be directly invoked with node, by calling node lib/converter/dialogflow-converter.js.

If this package was installed, it is available at the bin directory of the installation scope (locally at node_modules/.bin/dialogflow-as-code or globally at $NODE_HOME/bin/dialogflow-as-code).

The easiest way to get started with building out a dialogflow-as-code project is to npm i -g dialogflow-as-code. With this available you can use its CLI to specify input and output directories for generating the code from an extracted Dialogflow export.

Usage: dialogflow-as-code [options]

Generate a Dialogflow-as-Code project from a Dialogflow export

Options:
  -V, --version       output the version number
  -o, --output <dir>  Output directory (default: ./output)
  -i, --input <dir>   Input directory (default: ./input)
  -h, --help          output usage information

The converter also makes use of prettier to format the project after its creation.

dialogflow-as-code's People

Contributors

tcarrio avatar

Watchers

James Cloos avatar  avatar  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.