|
| 1 | +import app from "../../wit_ai.app.mjs"; |
| 2 | +import utils from "../../common/utils.mjs"; |
| 3 | +import constants from "../../common/constants.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "wit_ai-add-utterance", |
| 7 | + name: "Add Utterance", |
| 8 | + description: "Add a sample utterance to train your app with an intent. [See the documentation](https://wit.ai/docs/http/20240304#post__utterances).", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + app, |
| 13 | + text: { |
| 14 | + type: "string", |
| 15 | + label: "Text", |
| 16 | + description: "The text (sentence) you want your app to understand.", |
| 17 | + }, |
| 18 | + outOfScope: { |
| 19 | + type: "boolean", |
| 20 | + label: "Out of Scope", |
| 21 | + description: "To train your app to recognize utterances that it should not handle set this to `true`.", |
| 22 | + optional: true, |
| 23 | + default: false, |
| 24 | + reloadProps: true, |
| 25 | + }, |
| 26 | + }, |
| 27 | + async additionalProps() { |
| 28 | + if (this.outOfScope === true) { |
| 29 | + return {}; |
| 30 | + } |
| 31 | + |
| 32 | + const data = await this.app.listIntents(); |
| 33 | + const intents = data.map(({ name }) => name); |
| 34 | + const intentOptions = intents.concat(Object.values(constants.BUILTIN_INTENTS)); |
| 35 | + |
| 36 | + return { |
| 37 | + intent: { |
| 38 | + type: "string", |
| 39 | + label: "Intent", |
| 40 | + description: "The name of the intent you wish to create or train.", |
| 41 | + options: intentOptions, |
| 42 | + }, |
| 43 | + entities: { |
| 44 | + type: "string[]", |
| 45 | + label: "Entities", |
| 46 | + description: "The list of entities appearing in this sentence, that you want your app to extract once it is trained. Each entity must be a JSON string with the following properties:\n- `entity` (string, required): The entity name, including the role (e.g., `temperature:temperature` or `wit$temperature:temperature`).\n- `start` (integer, required): The starting index within the text.\n- `end` (integer, required): The ending index within the text.\n- `body` (string, required): The span of the text for the entity.\n- `entities` (array, required): List of entities found within the composite entity.\n\nExample: `{\"entity\":\"wit$temperature:temperature\",\"start\":19,\"end\":26,\"body\":\"34 degrees\",\"entities\":[]}`", |
| 47 | + optional: true, |
| 48 | + }, |
| 49 | + traits: { |
| 50 | + type: "string[]", |
| 51 | + label: "Traits", |
| 52 | + description: "The list of traits that are relevant for the utterance. Each trait must be a JSON string with the following properties:\n- `trait` (string, required): The trait name. This can be a trait you created, or a built-in one. i.e `faq` or `wit$sentiment`.\n- `value` (string, required): The value for the trait, e.g. `positive`.\n\nExample: `{\"trait\":\"wit$sentiment\",\"value\":\"neutral\"}`", |
| 53 | + optional: true, |
| 54 | + }, |
| 55 | + }; |
| 56 | + }, |
| 57 | + methods: { |
| 58 | + addUtterance(args = {}) { |
| 59 | + return this.app.post({ |
| 60 | + path: "/utterances", |
| 61 | + ...args, |
| 62 | + }); |
| 63 | + }, |
| 64 | + }, |
| 65 | + async run({ $ }) { |
| 66 | + const { |
| 67 | + addUtterance, |
| 68 | + text, |
| 69 | + intent, |
| 70 | + entities, |
| 71 | + traits, |
| 72 | + } = this; |
| 73 | + |
| 74 | + const response = await addUtterance({ |
| 75 | + $, |
| 76 | + data: [ |
| 77 | + { |
| 78 | + text, |
| 79 | + intent, |
| 80 | + entities: utils.parseArray(entities), |
| 81 | + traits: utils.parseArray(traits), |
| 82 | + }, |
| 83 | + ], |
| 84 | + }); |
| 85 | + |
| 86 | + $.export("$summary", "Successfully added an utterance to your app."); |
| 87 | + return response; |
| 88 | + }, |
| 89 | +}; |
0 commit comments