rasa-pro 3.8.16__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rasa-pro might be problematic. Click here for more details.

Files changed (644) hide show
  1. README.md +380 -0
  2. rasa/__init__.py +10 -0
  3. rasa/__main__.py +151 -0
  4. rasa/anonymization/__init__.py +2 -0
  5. rasa/anonymization/anonymisation_rule_yaml_reader.py +91 -0
  6. rasa/anonymization/anonymization_pipeline.py +287 -0
  7. rasa/anonymization/anonymization_rule_executor.py +260 -0
  8. rasa/anonymization/anonymization_rule_orchestrator.py +120 -0
  9. rasa/anonymization/schemas/config.yml +47 -0
  10. rasa/anonymization/utils.py +117 -0
  11. rasa/api.py +146 -0
  12. rasa/cli/__init__.py +5 -0
  13. rasa/cli/arguments/__init__.py +0 -0
  14. rasa/cli/arguments/data.py +81 -0
  15. rasa/cli/arguments/default_arguments.py +165 -0
  16. rasa/cli/arguments/evaluate.py +65 -0
  17. rasa/cli/arguments/export.py +51 -0
  18. rasa/cli/arguments/interactive.py +74 -0
  19. rasa/cli/arguments/run.py +204 -0
  20. rasa/cli/arguments/shell.py +13 -0
  21. rasa/cli/arguments/test.py +211 -0
  22. rasa/cli/arguments/train.py +263 -0
  23. rasa/cli/arguments/visualize.py +34 -0
  24. rasa/cli/arguments/x.py +30 -0
  25. rasa/cli/data.py +292 -0
  26. rasa/cli/e2e_test.py +566 -0
  27. rasa/cli/evaluate.py +222 -0
  28. rasa/cli/export.py +251 -0
  29. rasa/cli/inspect.py +63 -0
  30. rasa/cli/interactive.py +164 -0
  31. rasa/cli/license.py +65 -0
  32. rasa/cli/markers.py +78 -0
  33. rasa/cli/project_templates/__init__.py +0 -0
  34. rasa/cli/project_templates/calm/actions/__init__.py +0 -0
  35. rasa/cli/project_templates/calm/actions/action_template.py +27 -0
  36. rasa/cli/project_templates/calm/actions/add_contact.py +30 -0
  37. rasa/cli/project_templates/calm/actions/db.py +57 -0
  38. rasa/cli/project_templates/calm/actions/list_contacts.py +22 -0
  39. rasa/cli/project_templates/calm/actions/remove_contact.py +35 -0
  40. rasa/cli/project_templates/calm/config.yml +12 -0
  41. rasa/cli/project_templates/calm/credentials.yml +33 -0
  42. rasa/cli/project_templates/calm/data/flows/add_contact.yml +31 -0
  43. rasa/cli/project_templates/calm/data/flows/list_contacts.yml +14 -0
  44. rasa/cli/project_templates/calm/data/flows/remove_contact.yml +29 -0
  45. rasa/cli/project_templates/calm/db/contacts.json +10 -0
  46. rasa/cli/project_templates/calm/domain/add_contact.yml +33 -0
  47. rasa/cli/project_templates/calm/domain/list_contacts.yml +14 -0
  48. rasa/cli/project_templates/calm/domain/remove_contact.yml +31 -0
  49. rasa/cli/project_templates/calm/domain/shared.yml +5 -0
  50. rasa/cli/project_templates/calm/e2e_tests/cancelations/user_cancels_during_a_correction.yml +16 -0
  51. rasa/cli/project_templates/calm/e2e_tests/cancelations/user_changes_mind_on_a_whim.yml +7 -0
  52. rasa/cli/project_templates/calm/e2e_tests/corrections/user_corrects_contact_handle.yml +20 -0
  53. rasa/cli/project_templates/calm/e2e_tests/corrections/user_corrects_contact_name.yml +19 -0
  54. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_adds_contact_to_their_list.yml +15 -0
  55. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_lists_contacts.yml +5 -0
  56. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_removes_contact.yml +11 -0
  57. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_removes_contact_from_list.yml +12 -0
  58. rasa/cli/project_templates/calm/endpoints.yml +45 -0
  59. rasa/cli/project_templates/default/actions/__init__.py +0 -0
  60. rasa/cli/project_templates/default/actions/actions.py +27 -0
  61. rasa/cli/project_templates/default/config.yml +44 -0
  62. rasa/cli/project_templates/default/credentials.yml +33 -0
  63. rasa/cli/project_templates/default/data/nlu.yml +91 -0
  64. rasa/cli/project_templates/default/data/rules.yml +13 -0
  65. rasa/cli/project_templates/default/data/stories.yml +30 -0
  66. rasa/cli/project_templates/default/domain.yml +34 -0
  67. rasa/cli/project_templates/default/endpoints.yml +42 -0
  68. rasa/cli/project_templates/default/tests/test_stories.yml +91 -0
  69. rasa/cli/project_templates/tutorial/actions.py +22 -0
  70. rasa/cli/project_templates/tutorial/config.yml +11 -0
  71. rasa/cli/project_templates/tutorial/credentials.yml +33 -0
  72. rasa/cli/project_templates/tutorial/data/flows.yml +8 -0
  73. rasa/cli/project_templates/tutorial/domain.yml +17 -0
  74. rasa/cli/project_templates/tutorial/endpoints.yml +45 -0
  75. rasa/cli/run.py +136 -0
  76. rasa/cli/scaffold.py +268 -0
  77. rasa/cli/shell.py +141 -0
  78. rasa/cli/studio/__init__.py +0 -0
  79. rasa/cli/studio/download.py +51 -0
  80. rasa/cli/studio/studio.py +110 -0
  81. rasa/cli/studio/train.py +59 -0
  82. rasa/cli/studio/upload.py +85 -0
  83. rasa/cli/telemetry.py +90 -0
  84. rasa/cli/test.py +280 -0
  85. rasa/cli/train.py +260 -0
  86. rasa/cli/utils.py +453 -0
  87. rasa/cli/visualize.py +40 -0
  88. rasa/cli/x.py +205 -0
  89. rasa/constants.py +37 -0
  90. rasa/core/__init__.py +17 -0
  91. rasa/core/actions/__init__.py +0 -0
  92. rasa/core/actions/action.py +1450 -0
  93. rasa/core/actions/action_clean_stack.py +59 -0
  94. rasa/core/actions/action_run_slot_rejections.py +207 -0
  95. rasa/core/actions/action_trigger_chitchat.py +31 -0
  96. rasa/core/actions/action_trigger_flow.py +109 -0
  97. rasa/core/actions/action_trigger_search.py +31 -0
  98. rasa/core/actions/constants.py +2 -0
  99. rasa/core/actions/forms.py +737 -0
  100. rasa/core/actions/loops.py +111 -0
  101. rasa/core/actions/two_stage_fallback.py +186 -0
  102. rasa/core/agent.py +557 -0
  103. rasa/core/auth_retry_tracker_store.py +122 -0
  104. rasa/core/brokers/__init__.py +0 -0
  105. rasa/core/brokers/broker.py +126 -0
  106. rasa/core/brokers/file.py +58 -0
  107. rasa/core/brokers/kafka.py +322 -0
  108. rasa/core/brokers/pika.py +387 -0
  109. rasa/core/brokers/sql.py +86 -0
  110. rasa/core/channels/__init__.py +55 -0
  111. rasa/core/channels/audiocodes.py +463 -0
  112. rasa/core/channels/botframework.py +339 -0
  113. rasa/core/channels/callback.py +85 -0
  114. rasa/core/channels/channel.py +419 -0
  115. rasa/core/channels/console.py +243 -0
  116. rasa/core/channels/development_inspector.py +93 -0
  117. rasa/core/channels/facebook.py +422 -0
  118. rasa/core/channels/hangouts.py +335 -0
  119. rasa/core/channels/inspector/.eslintrc.cjs +25 -0
  120. rasa/core/channels/inspector/.gitignore +23 -0
  121. rasa/core/channels/inspector/README.md +54 -0
  122. rasa/core/channels/inspector/assets/favicon.ico +0 -0
  123. rasa/core/channels/inspector/assets/rasa-chat.js +2 -0
  124. rasa/core/channels/inspector/custom.d.ts +3 -0
  125. rasa/core/channels/inspector/dist/assets/arc-5623b6dc.js +1 -0
  126. rasa/core/channels/inspector/dist/assets/array-9f3ba611.js +1 -0
  127. rasa/core/channels/inspector/dist/assets/c4Diagram-d0fbc5ce-685c106a.js +10 -0
  128. rasa/core/channels/inspector/dist/assets/classDiagram-936ed81e-8cbed007.js +2 -0
  129. rasa/core/channels/inspector/dist/assets/classDiagram-v2-c3cb15f1-5889cf12.js +2 -0
  130. rasa/core/channels/inspector/dist/assets/createText-62fc7601-24c249d7.js +7 -0
  131. rasa/core/channels/inspector/dist/assets/edges-f2ad444c-7dd06a75.js +4 -0
  132. rasa/core/channels/inspector/dist/assets/erDiagram-9d236eb7-62c1e54c.js +51 -0
  133. rasa/core/channels/inspector/dist/assets/flowDb-1972c806-ce49b86f.js +6 -0
  134. rasa/core/channels/inspector/dist/assets/flowDiagram-7ea5b25a-4067e48f.js +4 -0
  135. rasa/core/channels/inspector/dist/assets/flowDiagram-v2-855bc5b3-85583a23.js +1 -0
  136. rasa/core/channels/inspector/dist/assets/flowchart-elk-definition-abe16c3d-59fe4051.js +139 -0
  137. rasa/core/channels/inspector/dist/assets/ganttDiagram-9b5ea136-47e3a43b.js +266 -0
  138. rasa/core/channels/inspector/dist/assets/gitGraphDiagram-99d0ae7c-5a2ac0d9.js +70 -0
  139. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-128cfa44.ttf +0 -0
  140. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-21dbcb97.woff +0 -0
  141. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-222b5e26.svg +329 -0
  142. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-9ad89b2a.woff2 +0 -0
  143. rasa/core/channels/inspector/dist/assets/index-268a75c0.js +1040 -0
  144. rasa/core/channels/inspector/dist/assets/index-2c4b9a3b-dfb8efc4.js +1 -0
  145. rasa/core/channels/inspector/dist/assets/index-3ee28881.css +1 -0
  146. rasa/core/channels/inspector/dist/assets/infoDiagram-736b4530-b0c470f2.js +7 -0
  147. rasa/core/channels/inspector/dist/assets/init-77b53fdd.js +1 -0
  148. rasa/core/channels/inspector/dist/assets/journeyDiagram-df861f2b-2edb829a.js +139 -0
  149. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-60c05ee4.woff +0 -0
  150. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-8335d9b8.svg +438 -0
  151. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-9cc39c75.ttf +0 -0
  152. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-ead13ccf.woff2 +0 -0
  153. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-16705655.woff2 +0 -0
  154. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-5aeb07f9.woff +0 -0
  155. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-9c459044.ttf +0 -0
  156. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-9e2898a4.svg +435 -0
  157. rasa/core/channels/inspector/dist/assets/layout-b6873d69.js +1 -0
  158. rasa/core/channels/inspector/dist/assets/line-1efc5781.js +1 -0
  159. rasa/core/channels/inspector/dist/assets/linear-661e9b94.js +1 -0
  160. rasa/core/channels/inspector/dist/assets/mindmap-definition-beec6740-2d2e727f.js +109 -0
  161. rasa/core/channels/inspector/dist/assets/ordinal-ba9b4969.js +1 -0
  162. rasa/core/channels/inspector/dist/assets/path-53f90ab3.js +1 -0
  163. rasa/core/channels/inspector/dist/assets/pieDiagram-dbbf0591-9d3ea93d.js +35 -0
  164. rasa/core/channels/inspector/dist/assets/quadrantDiagram-4d7f4fd6-06a178a2.js +7 -0
  165. rasa/core/channels/inspector/dist/assets/requirementDiagram-6fc4c22a-0bfedffc.js +52 -0
  166. rasa/core/channels/inspector/dist/assets/sankeyDiagram-8f13d901-d76d0a04.js +8 -0
  167. rasa/core/channels/inspector/dist/assets/sequenceDiagram-b655622a-37bb4341.js +122 -0
  168. rasa/core/channels/inspector/dist/assets/stateDiagram-59f0c015-f52f7f57.js +1 -0
  169. rasa/core/channels/inspector/dist/assets/stateDiagram-v2-2b26beab-4a986a20.js +1 -0
  170. rasa/core/channels/inspector/dist/assets/styles-080da4f6-7dd9ae12.js +110 -0
  171. rasa/core/channels/inspector/dist/assets/styles-3dcbcfbf-46e1ca14.js +159 -0
  172. rasa/core/channels/inspector/dist/assets/styles-9c745c82-4a97439a.js +207 -0
  173. rasa/core/channels/inspector/dist/assets/svgDrawCommon-4835440b-823917a3.js +1 -0
  174. rasa/core/channels/inspector/dist/assets/timeline-definition-5b62e21b-9ea72896.js +61 -0
  175. rasa/core/channels/inspector/dist/assets/xychartDiagram-2b33534f-b631a8b6.js +7 -0
  176. rasa/core/channels/inspector/dist/index.html +39 -0
  177. rasa/core/channels/inspector/index.html +37 -0
  178. rasa/core/channels/inspector/jest.config.ts +13 -0
  179. rasa/core/channels/inspector/package.json +48 -0
  180. rasa/core/channels/inspector/setupTests.ts +2 -0
  181. rasa/core/channels/inspector/src/App.tsx +170 -0
  182. rasa/core/channels/inspector/src/components/DiagramFlow.tsx +97 -0
  183. rasa/core/channels/inspector/src/components/DialogueInformation.tsx +187 -0
  184. rasa/core/channels/inspector/src/components/DialogueStack.tsx +151 -0
  185. rasa/core/channels/inspector/src/components/ExpandIcon.tsx +16 -0
  186. rasa/core/channels/inspector/src/components/FullscreenButton.tsx +45 -0
  187. rasa/core/channels/inspector/src/components/LoadingSpinner.tsx +19 -0
  188. rasa/core/channels/inspector/src/components/NoActiveFlow.tsx +21 -0
  189. rasa/core/channels/inspector/src/components/RasaLogo.tsx +32 -0
  190. rasa/core/channels/inspector/src/components/SaraDiagrams.tsx +39 -0
  191. rasa/core/channels/inspector/src/components/Slots.tsx +91 -0
  192. rasa/core/channels/inspector/src/components/Welcome.tsx +54 -0
  193. rasa/core/channels/inspector/src/helpers/formatters.test.ts +385 -0
  194. rasa/core/channels/inspector/src/helpers/formatters.ts +239 -0
  195. rasa/core/channels/inspector/src/helpers/utils.ts +42 -0
  196. rasa/core/channels/inspector/src/main.tsx +13 -0
  197. rasa/core/channels/inspector/src/theme/Button/Button.ts +29 -0
  198. rasa/core/channels/inspector/src/theme/Heading/Heading.ts +31 -0
  199. rasa/core/channels/inspector/src/theme/Input/Input.ts +27 -0
  200. rasa/core/channels/inspector/src/theme/Link/Link.ts +10 -0
  201. rasa/core/channels/inspector/src/theme/Modal/Modal.ts +47 -0
  202. rasa/core/channels/inspector/src/theme/Table/Table.tsx +38 -0
  203. rasa/core/channels/inspector/src/theme/Tooltip/Tooltip.ts +12 -0
  204. rasa/core/channels/inspector/src/theme/base/breakpoints.ts +8 -0
  205. rasa/core/channels/inspector/src/theme/base/colors.ts +88 -0
  206. rasa/core/channels/inspector/src/theme/base/fonts/fontFaces.css +29 -0
  207. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.eot +0 -0
  208. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.svg +329 -0
  209. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.ttf +0 -0
  210. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.woff +0 -0
  211. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.woff2 +0 -0
  212. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.eot +0 -0
  213. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.svg +438 -0
  214. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.ttf +0 -0
  215. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.woff +0 -0
  216. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.woff2 +0 -0
  217. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.eot +0 -0
  218. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.svg +435 -0
  219. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.ttf +0 -0
  220. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.woff +0 -0
  221. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.woff2 +0 -0
  222. rasa/core/channels/inspector/src/theme/base/radii.ts +9 -0
  223. rasa/core/channels/inspector/src/theme/base/shadows.ts +7 -0
  224. rasa/core/channels/inspector/src/theme/base/sizes.ts +7 -0
  225. rasa/core/channels/inspector/src/theme/base/space.ts +15 -0
  226. rasa/core/channels/inspector/src/theme/base/styles.ts +13 -0
  227. rasa/core/channels/inspector/src/theme/base/typography.ts +24 -0
  228. rasa/core/channels/inspector/src/theme/base/zIndices.ts +19 -0
  229. rasa/core/channels/inspector/src/theme/index.ts +101 -0
  230. rasa/core/channels/inspector/src/types.ts +64 -0
  231. rasa/core/channels/inspector/src/vite-env.d.ts +1 -0
  232. rasa/core/channels/inspector/tests/__mocks__/fileMock.ts +1 -0
  233. rasa/core/channels/inspector/tests/__mocks__/matchMedia.ts +16 -0
  234. rasa/core/channels/inspector/tests/__mocks__/styleMock.ts +1 -0
  235. rasa/core/channels/inspector/tests/renderWithProviders.tsx +14 -0
  236. rasa/core/channels/inspector/tsconfig.json +26 -0
  237. rasa/core/channels/inspector/tsconfig.node.json +10 -0
  238. rasa/core/channels/inspector/vite.config.ts +8 -0
  239. rasa/core/channels/inspector/yarn.lock +6156 -0
  240. rasa/core/channels/mattermost.py +229 -0
  241. rasa/core/channels/rasa_chat.py +126 -0
  242. rasa/core/channels/rest.py +210 -0
  243. rasa/core/channels/rocketchat.py +175 -0
  244. rasa/core/channels/slack.py +620 -0
  245. rasa/core/channels/socketio.py +274 -0
  246. rasa/core/channels/telegram.py +298 -0
  247. rasa/core/channels/twilio.py +169 -0
  248. rasa/core/channels/twilio_voice.py +367 -0
  249. rasa/core/channels/vier_cvg.py +374 -0
  250. rasa/core/channels/webexteams.py +135 -0
  251. rasa/core/concurrent_lock_store.py +210 -0
  252. rasa/core/constants.py +107 -0
  253. rasa/core/evaluation/__init__.py +0 -0
  254. rasa/core/evaluation/marker.py +267 -0
  255. rasa/core/evaluation/marker_base.py +925 -0
  256. rasa/core/evaluation/marker_stats.py +294 -0
  257. rasa/core/evaluation/marker_tracker_loader.py +103 -0
  258. rasa/core/exceptions.py +29 -0
  259. rasa/core/exporter.py +284 -0
  260. rasa/core/featurizers/__init__.py +0 -0
  261. rasa/core/featurizers/precomputation.py +410 -0
  262. rasa/core/featurizers/single_state_featurizer.py +402 -0
  263. rasa/core/featurizers/tracker_featurizers.py +1172 -0
  264. rasa/core/http_interpreter.py +89 -0
  265. rasa/core/information_retrieval/__init__.py +0 -0
  266. rasa/core/information_retrieval/faiss.py +116 -0
  267. rasa/core/information_retrieval/information_retrieval.py +72 -0
  268. rasa/core/information_retrieval/milvus.py +59 -0
  269. rasa/core/information_retrieval/qdrant.py +102 -0
  270. rasa/core/jobs.py +63 -0
  271. rasa/core/lock.py +139 -0
  272. rasa/core/lock_store.py +344 -0
  273. rasa/core/migrate.py +404 -0
  274. rasa/core/nlg/__init__.py +3 -0
  275. rasa/core/nlg/callback.py +147 -0
  276. rasa/core/nlg/contextual_response_rephraser.py +270 -0
  277. rasa/core/nlg/generator.py +230 -0
  278. rasa/core/nlg/interpolator.py +143 -0
  279. rasa/core/nlg/response.py +155 -0
  280. rasa/core/nlg/summarize.py +69 -0
  281. rasa/core/policies/__init__.py +0 -0
  282. rasa/core/policies/ensemble.py +329 -0
  283. rasa/core/policies/enterprise_search_policy.py +717 -0
  284. rasa/core/policies/enterprise_search_prompt_template.jinja2 +62 -0
  285. rasa/core/policies/flow_policy.py +205 -0
  286. rasa/core/policies/flows/__init__.py +0 -0
  287. rasa/core/policies/flows/flow_exceptions.py +44 -0
  288. rasa/core/policies/flows/flow_executor.py +582 -0
  289. rasa/core/policies/flows/flow_step_result.py +43 -0
  290. rasa/core/policies/intentless_policy.py +924 -0
  291. rasa/core/policies/intentless_prompt_template.jinja2 +22 -0
  292. rasa/core/policies/memoization.py +538 -0
  293. rasa/core/policies/policy.py +716 -0
  294. rasa/core/policies/rule_policy.py +1276 -0
  295. rasa/core/policies/ted_policy.py +2146 -0
  296. rasa/core/policies/unexpected_intent_policy.py +1015 -0
  297. rasa/core/processor.py +1331 -0
  298. rasa/core/run.py +315 -0
  299. rasa/core/secrets_manager/__init__.py +0 -0
  300. rasa/core/secrets_manager/constants.py +32 -0
  301. rasa/core/secrets_manager/endpoints.py +391 -0
  302. rasa/core/secrets_manager/factory.py +233 -0
  303. rasa/core/secrets_manager/secret_manager.py +262 -0
  304. rasa/core/secrets_manager/vault.py +576 -0
  305. rasa/core/test.py +1337 -0
  306. rasa/core/tracker_store.py +1664 -0
  307. rasa/core/train.py +107 -0
  308. rasa/core/training/__init__.py +89 -0
  309. rasa/core/training/converters/__init__.py +0 -0
  310. rasa/core/training/converters/responses_prefix_converter.py +119 -0
  311. rasa/core/training/interactive.py +1742 -0
  312. rasa/core/training/story_conflict.py +381 -0
  313. rasa/core/training/training.py +93 -0
  314. rasa/core/utils.py +344 -0
  315. rasa/core/visualize.py +70 -0
  316. rasa/dialogue_understanding/__init__.py +0 -0
  317. rasa/dialogue_understanding/coexistence/__init__.py +0 -0
  318. rasa/dialogue_understanding/coexistence/constants.py +4 -0
  319. rasa/dialogue_understanding/coexistence/intent_based_router.py +189 -0
  320. rasa/dialogue_understanding/coexistence/llm_based_router.py +261 -0
  321. rasa/dialogue_understanding/coexistence/router_template.jinja2 +12 -0
  322. rasa/dialogue_understanding/commands/__init__.py +45 -0
  323. rasa/dialogue_understanding/commands/can_not_handle_command.py +61 -0
  324. rasa/dialogue_understanding/commands/cancel_flow_command.py +116 -0
  325. rasa/dialogue_understanding/commands/chit_chat_answer_command.py +48 -0
  326. rasa/dialogue_understanding/commands/clarify_command.py +77 -0
  327. rasa/dialogue_understanding/commands/command.py +85 -0
  328. rasa/dialogue_understanding/commands/correct_slots_command.py +288 -0
  329. rasa/dialogue_understanding/commands/error_command.py +67 -0
  330. rasa/dialogue_understanding/commands/free_form_answer_command.py +9 -0
  331. rasa/dialogue_understanding/commands/handle_code_change_command.py +64 -0
  332. rasa/dialogue_understanding/commands/human_handoff_command.py +57 -0
  333. rasa/dialogue_understanding/commands/knowledge_answer_command.py +48 -0
  334. rasa/dialogue_understanding/commands/noop_command.py +45 -0
  335. rasa/dialogue_understanding/commands/set_slot_command.py +125 -0
  336. rasa/dialogue_understanding/commands/skip_question_command.py +66 -0
  337. rasa/dialogue_understanding/commands/start_flow_command.py +98 -0
  338. rasa/dialogue_understanding/generator/__init__.py +6 -0
  339. rasa/dialogue_understanding/generator/command_generator.py +257 -0
  340. rasa/dialogue_understanding/generator/command_prompt_template.jinja2 +57 -0
  341. rasa/dialogue_understanding/generator/flow_document_template.jinja2 +4 -0
  342. rasa/dialogue_understanding/generator/flow_retrieval.py +410 -0
  343. rasa/dialogue_understanding/generator/llm_command_generator.py +637 -0
  344. rasa/dialogue_understanding/generator/nlu_command_adapter.py +157 -0
  345. rasa/dialogue_understanding/patterns/__init__.py +0 -0
  346. rasa/dialogue_understanding/patterns/cancel.py +111 -0
  347. rasa/dialogue_understanding/patterns/cannot_handle.py +43 -0
  348. rasa/dialogue_understanding/patterns/chitchat.py +37 -0
  349. rasa/dialogue_understanding/patterns/clarify.py +97 -0
  350. rasa/dialogue_understanding/patterns/code_change.py +41 -0
  351. rasa/dialogue_understanding/patterns/collect_information.py +90 -0
  352. rasa/dialogue_understanding/patterns/completed.py +40 -0
  353. rasa/dialogue_understanding/patterns/continue_interrupted.py +42 -0
  354. rasa/dialogue_understanding/patterns/correction.py +278 -0
  355. rasa/dialogue_understanding/patterns/default_flows_for_patterns.yml +243 -0
  356. rasa/dialogue_understanding/patterns/human_handoff.py +37 -0
  357. rasa/dialogue_understanding/patterns/internal_error.py +47 -0
  358. rasa/dialogue_understanding/patterns/search.py +37 -0
  359. rasa/dialogue_understanding/patterns/skip_question.py +38 -0
  360. rasa/dialogue_understanding/processor/__init__.py +0 -0
  361. rasa/dialogue_understanding/processor/command_processor.py +578 -0
  362. rasa/dialogue_understanding/processor/command_processor_component.py +39 -0
  363. rasa/dialogue_understanding/stack/__init__.py +0 -0
  364. rasa/dialogue_understanding/stack/dialogue_stack.py +178 -0
  365. rasa/dialogue_understanding/stack/frames/__init__.py +19 -0
  366. rasa/dialogue_understanding/stack/frames/chit_chat_frame.py +27 -0
  367. rasa/dialogue_understanding/stack/frames/dialogue_stack_frame.py +137 -0
  368. rasa/dialogue_understanding/stack/frames/flow_stack_frame.py +157 -0
  369. rasa/dialogue_understanding/stack/frames/pattern_frame.py +10 -0
  370. rasa/dialogue_understanding/stack/frames/search_frame.py +27 -0
  371. rasa/dialogue_understanding/stack/utils.py +211 -0
  372. rasa/e2e_test/__init__.py +0 -0
  373. rasa/e2e_test/constants.py +10 -0
  374. rasa/e2e_test/e2e_test_case.py +322 -0
  375. rasa/e2e_test/e2e_test_result.py +34 -0
  376. rasa/e2e_test/e2e_test_runner.py +659 -0
  377. rasa/e2e_test/e2e_test_schema.yml +67 -0
  378. rasa/engine/__init__.py +0 -0
  379. rasa/engine/caching.py +464 -0
  380. rasa/engine/constants.py +17 -0
  381. rasa/engine/exceptions.py +14 -0
  382. rasa/engine/graph.py +625 -0
  383. rasa/engine/loader.py +36 -0
  384. rasa/engine/recipes/__init__.py +0 -0
  385. rasa/engine/recipes/config_files/default_config.yml +44 -0
  386. rasa/engine/recipes/default_components.py +99 -0
  387. rasa/engine/recipes/default_recipe.py +1252 -0
  388. rasa/engine/recipes/graph_recipe.py +79 -0
  389. rasa/engine/recipes/recipe.py +93 -0
  390. rasa/engine/runner/__init__.py +0 -0
  391. rasa/engine/runner/dask.py +256 -0
  392. rasa/engine/runner/interface.py +49 -0
  393. rasa/engine/storage/__init__.py +0 -0
  394. rasa/engine/storage/local_model_storage.py +248 -0
  395. rasa/engine/storage/resource.py +110 -0
  396. rasa/engine/storage/storage.py +203 -0
  397. rasa/engine/training/__init__.py +0 -0
  398. rasa/engine/training/components.py +176 -0
  399. rasa/engine/training/fingerprinting.py +64 -0
  400. rasa/engine/training/graph_trainer.py +256 -0
  401. rasa/engine/training/hooks.py +164 -0
  402. rasa/engine/validation.py +839 -0
  403. rasa/env.py +5 -0
  404. rasa/exceptions.py +69 -0
  405. rasa/graph_components/__init__.py +0 -0
  406. rasa/graph_components/converters/__init__.py +0 -0
  407. rasa/graph_components/converters/nlu_message_converter.py +48 -0
  408. rasa/graph_components/providers/__init__.py +0 -0
  409. rasa/graph_components/providers/domain_for_core_training_provider.py +87 -0
  410. rasa/graph_components/providers/domain_provider.py +71 -0
  411. rasa/graph_components/providers/flows_provider.py +74 -0
  412. rasa/graph_components/providers/forms_provider.py +44 -0
  413. rasa/graph_components/providers/nlu_training_data_provider.py +56 -0
  414. rasa/graph_components/providers/responses_provider.py +44 -0
  415. rasa/graph_components/providers/rule_only_provider.py +49 -0
  416. rasa/graph_components/providers/story_graph_provider.py +43 -0
  417. rasa/graph_components/providers/training_tracker_provider.py +55 -0
  418. rasa/graph_components/validators/__init__.py +0 -0
  419. rasa/graph_components/validators/default_recipe_validator.py +552 -0
  420. rasa/graph_components/validators/finetuning_validator.py +302 -0
  421. rasa/hooks.py +113 -0
  422. rasa/jupyter.py +63 -0
  423. rasa/keys +1 -0
  424. rasa/markers/__init__.py +0 -0
  425. rasa/markers/marker.py +269 -0
  426. rasa/markers/marker_base.py +828 -0
  427. rasa/markers/upload.py +74 -0
  428. rasa/markers/validate.py +21 -0
  429. rasa/model.py +118 -0
  430. rasa/model_testing.py +457 -0
  431. rasa/model_training.py +535 -0
  432. rasa/nlu/__init__.py +7 -0
  433. rasa/nlu/classifiers/__init__.py +3 -0
  434. rasa/nlu/classifiers/classifier.py +5 -0
  435. rasa/nlu/classifiers/diet_classifier.py +1874 -0
  436. rasa/nlu/classifiers/fallback_classifier.py +192 -0
  437. rasa/nlu/classifiers/keyword_intent_classifier.py +188 -0
  438. rasa/nlu/classifiers/llm_intent_classifier.py +519 -0
  439. rasa/nlu/classifiers/logistic_regression_classifier.py +240 -0
  440. rasa/nlu/classifiers/mitie_intent_classifier.py +156 -0
  441. rasa/nlu/classifiers/regex_message_handler.py +56 -0
  442. rasa/nlu/classifiers/sklearn_intent_classifier.py +309 -0
  443. rasa/nlu/constants.py +77 -0
  444. rasa/nlu/convert.py +40 -0
  445. rasa/nlu/emulators/__init__.py +0 -0
  446. rasa/nlu/emulators/dialogflow.py +55 -0
  447. rasa/nlu/emulators/emulator.py +49 -0
  448. rasa/nlu/emulators/luis.py +86 -0
  449. rasa/nlu/emulators/no_emulator.py +10 -0
  450. rasa/nlu/emulators/wit.py +56 -0
  451. rasa/nlu/extractors/__init__.py +0 -0
  452. rasa/nlu/extractors/crf_entity_extractor.py +672 -0
  453. rasa/nlu/extractors/duckling_entity_extractor.py +206 -0
  454. rasa/nlu/extractors/entity_synonyms.py +178 -0
  455. rasa/nlu/extractors/extractor.py +470 -0
  456. rasa/nlu/extractors/mitie_entity_extractor.py +293 -0
  457. rasa/nlu/extractors/regex_entity_extractor.py +220 -0
  458. rasa/nlu/extractors/spacy_entity_extractor.py +95 -0
  459. rasa/nlu/featurizers/__init__.py +0 -0
  460. rasa/nlu/featurizers/dense_featurizer/__init__.py +0 -0
  461. rasa/nlu/featurizers/dense_featurizer/convert_featurizer.py +449 -0
  462. rasa/nlu/featurizers/dense_featurizer/dense_featurizer.py +57 -0
  463. rasa/nlu/featurizers/dense_featurizer/lm_featurizer.py +772 -0
  464. rasa/nlu/featurizers/dense_featurizer/mitie_featurizer.py +170 -0
  465. rasa/nlu/featurizers/dense_featurizer/spacy_featurizer.py +132 -0
  466. rasa/nlu/featurizers/featurizer.py +89 -0
  467. rasa/nlu/featurizers/sparse_featurizer/__init__.py +0 -0
  468. rasa/nlu/featurizers/sparse_featurizer/count_vectors_featurizer.py +840 -0
  469. rasa/nlu/featurizers/sparse_featurizer/lexical_syntactic_featurizer.py +539 -0
  470. rasa/nlu/featurizers/sparse_featurizer/regex_featurizer.py +269 -0
  471. rasa/nlu/featurizers/sparse_featurizer/sparse_featurizer.py +9 -0
  472. rasa/nlu/model.py +24 -0
  473. rasa/nlu/persistor.py +240 -0
  474. rasa/nlu/run.py +27 -0
  475. rasa/nlu/selectors/__init__.py +0 -0
  476. rasa/nlu/selectors/response_selector.py +990 -0
  477. rasa/nlu/test.py +1943 -0
  478. rasa/nlu/tokenizers/__init__.py +0 -0
  479. rasa/nlu/tokenizers/jieba_tokenizer.py +148 -0
  480. rasa/nlu/tokenizers/mitie_tokenizer.py +75 -0
  481. rasa/nlu/tokenizers/spacy_tokenizer.py +72 -0
  482. rasa/nlu/tokenizers/tokenizer.py +239 -0
  483. rasa/nlu/tokenizers/whitespace_tokenizer.py +106 -0
  484. rasa/nlu/utils/__init__.py +35 -0
  485. rasa/nlu/utils/bilou_utils.py +462 -0
  486. rasa/nlu/utils/hugging_face/__init__.py +0 -0
  487. rasa/nlu/utils/hugging_face/registry.py +108 -0
  488. rasa/nlu/utils/hugging_face/transformers_pre_post_processors.py +311 -0
  489. rasa/nlu/utils/mitie_utils.py +113 -0
  490. rasa/nlu/utils/pattern_utils.py +168 -0
  491. rasa/nlu/utils/spacy_utils.py +312 -0
  492. rasa/plugin.py +90 -0
  493. rasa/server.py +1536 -0
  494. rasa/shared/__init__.py +0 -0
  495. rasa/shared/constants.py +181 -0
  496. rasa/shared/core/__init__.py +0 -0
  497. rasa/shared/core/constants.py +168 -0
  498. rasa/shared/core/conversation.py +46 -0
  499. rasa/shared/core/domain.py +2106 -0
  500. rasa/shared/core/events.py +2507 -0
  501. rasa/shared/core/flows/__init__.py +7 -0
  502. rasa/shared/core/flows/flow.py +353 -0
  503. rasa/shared/core/flows/flow_step.py +146 -0
  504. rasa/shared/core/flows/flow_step_links.py +319 -0
  505. rasa/shared/core/flows/flow_step_sequence.py +70 -0
  506. rasa/shared/core/flows/flows_list.py +211 -0
  507. rasa/shared/core/flows/flows_yaml_schema.json +217 -0
  508. rasa/shared/core/flows/nlu_trigger.py +117 -0
  509. rasa/shared/core/flows/steps/__init__.py +24 -0
  510. rasa/shared/core/flows/steps/action.py +51 -0
  511. rasa/shared/core/flows/steps/call.py +64 -0
  512. rasa/shared/core/flows/steps/collect.py +112 -0
  513. rasa/shared/core/flows/steps/constants.py +5 -0
  514. rasa/shared/core/flows/steps/continuation.py +36 -0
  515. rasa/shared/core/flows/steps/end.py +22 -0
  516. rasa/shared/core/flows/steps/internal.py +44 -0
  517. rasa/shared/core/flows/steps/link.py +51 -0
  518. rasa/shared/core/flows/steps/no_operation.py +48 -0
  519. rasa/shared/core/flows/steps/set_slots.py +50 -0
  520. rasa/shared/core/flows/steps/start.py +30 -0
  521. rasa/shared/core/flows/validation.py +527 -0
  522. rasa/shared/core/flows/yaml_flows_io.py +278 -0
  523. rasa/shared/core/generator.py +907 -0
  524. rasa/shared/core/slot_mappings.py +235 -0
  525. rasa/shared/core/slots.py +647 -0
  526. rasa/shared/core/trackers.py +1159 -0
  527. rasa/shared/core/training_data/__init__.py +0 -0
  528. rasa/shared/core/training_data/loading.py +90 -0
  529. rasa/shared/core/training_data/story_reader/__init__.py +0 -0
  530. rasa/shared/core/training_data/story_reader/story_reader.py +129 -0
  531. rasa/shared/core/training_data/story_reader/story_step_builder.py +168 -0
  532. rasa/shared/core/training_data/story_reader/yaml_story_reader.py +888 -0
  533. rasa/shared/core/training_data/story_writer/__init__.py +0 -0
  534. rasa/shared/core/training_data/story_writer/story_writer.py +76 -0
  535. rasa/shared/core/training_data/story_writer/yaml_story_writer.py +442 -0
  536. rasa/shared/core/training_data/structures.py +838 -0
  537. rasa/shared/core/training_data/visualization.html +146 -0
  538. rasa/shared/core/training_data/visualization.py +603 -0
  539. rasa/shared/data.py +192 -0
  540. rasa/shared/engine/__init__.py +0 -0
  541. rasa/shared/engine/caching.py +26 -0
  542. rasa/shared/exceptions.py +129 -0
  543. rasa/shared/importers/__init__.py +0 -0
  544. rasa/shared/importers/importer.py +705 -0
  545. rasa/shared/importers/multi_project.py +203 -0
  546. rasa/shared/importers/rasa.py +100 -0
  547. rasa/shared/importers/utils.py +34 -0
  548. rasa/shared/nlu/__init__.py +0 -0
  549. rasa/shared/nlu/constants.py +45 -0
  550. rasa/shared/nlu/interpreter.py +10 -0
  551. rasa/shared/nlu/training_data/__init__.py +0 -0
  552. rasa/shared/nlu/training_data/entities_parser.py +209 -0
  553. rasa/shared/nlu/training_data/features.py +374 -0
  554. rasa/shared/nlu/training_data/formats/__init__.py +10 -0
  555. rasa/shared/nlu/training_data/formats/dialogflow.py +162 -0
  556. rasa/shared/nlu/training_data/formats/luis.py +87 -0
  557. rasa/shared/nlu/training_data/formats/rasa.py +135 -0
  558. rasa/shared/nlu/training_data/formats/rasa_yaml.py +605 -0
  559. rasa/shared/nlu/training_data/formats/readerwriter.py +245 -0
  560. rasa/shared/nlu/training_data/formats/wit.py +52 -0
  561. rasa/shared/nlu/training_data/loading.py +137 -0
  562. rasa/shared/nlu/training_data/lookup_tables_parser.py +30 -0
  563. rasa/shared/nlu/training_data/message.py +477 -0
  564. rasa/shared/nlu/training_data/schemas/__init__.py +0 -0
  565. rasa/shared/nlu/training_data/schemas/data_schema.py +85 -0
  566. rasa/shared/nlu/training_data/schemas/nlu.yml +53 -0
  567. rasa/shared/nlu/training_data/schemas/responses.yml +70 -0
  568. rasa/shared/nlu/training_data/synonyms_parser.py +42 -0
  569. rasa/shared/nlu/training_data/training_data.py +732 -0
  570. rasa/shared/nlu/training_data/util.py +223 -0
  571. rasa/shared/providers/__init__.py +0 -0
  572. rasa/shared/providers/openai/__init__.py +0 -0
  573. rasa/shared/providers/openai/clients.py +43 -0
  574. rasa/shared/providers/openai/session_handler.py +110 -0
  575. rasa/shared/utils/__init__.py +0 -0
  576. rasa/shared/utils/cli.py +72 -0
  577. rasa/shared/utils/common.py +308 -0
  578. rasa/shared/utils/constants.py +1 -0
  579. rasa/shared/utils/io.py +403 -0
  580. rasa/shared/utils/llm.py +405 -0
  581. rasa/shared/utils/pykwalify_extensions.py +26 -0
  582. rasa/shared/utils/schemas/__init__.py +0 -0
  583. rasa/shared/utils/schemas/config.yml +2 -0
  584. rasa/shared/utils/schemas/domain.yml +142 -0
  585. rasa/shared/utils/schemas/events.py +212 -0
  586. rasa/shared/utils/schemas/model_config.yml +46 -0
  587. rasa/shared/utils/schemas/stories.yml +173 -0
  588. rasa/shared/utils/yaml.py +777 -0
  589. rasa/studio/__init__.py +0 -0
  590. rasa/studio/auth.py +252 -0
  591. rasa/studio/config.py +127 -0
  592. rasa/studio/constants.py +16 -0
  593. rasa/studio/data_handler.py +352 -0
  594. rasa/studio/download.py +350 -0
  595. rasa/studio/train.py +136 -0
  596. rasa/studio/upload.py +408 -0
  597. rasa/telemetry.py +1583 -0
  598. rasa/tracing/__init__.py +0 -0
  599. rasa/tracing/config.py +338 -0
  600. rasa/tracing/constants.py +38 -0
  601. rasa/tracing/instrumentation/__init__.py +0 -0
  602. rasa/tracing/instrumentation/attribute_extractors.py +663 -0
  603. rasa/tracing/instrumentation/instrumentation.py +939 -0
  604. rasa/tracing/instrumentation/intentless_policy_instrumentation.py +142 -0
  605. rasa/tracing/instrumentation/metrics.py +206 -0
  606. rasa/tracing/metric_instrument_provider.py +125 -0
  607. rasa/utils/__init__.py +0 -0
  608. rasa/utils/beta.py +83 -0
  609. rasa/utils/cli.py +27 -0
  610. rasa/utils/common.py +635 -0
  611. rasa/utils/converter.py +53 -0
  612. rasa/utils/endpoints.py +303 -0
  613. rasa/utils/io.py +326 -0
  614. rasa/utils/licensing.py +319 -0
  615. rasa/utils/log_utils.py +174 -0
  616. rasa/utils/mapper.py +210 -0
  617. rasa/utils/ml_utils.py +145 -0
  618. rasa/utils/plotting.py +362 -0
  619. rasa/utils/singleton.py +23 -0
  620. rasa/utils/tensorflow/__init__.py +0 -0
  621. rasa/utils/tensorflow/callback.py +112 -0
  622. rasa/utils/tensorflow/constants.py +116 -0
  623. rasa/utils/tensorflow/crf.py +492 -0
  624. rasa/utils/tensorflow/data_generator.py +440 -0
  625. rasa/utils/tensorflow/environment.py +161 -0
  626. rasa/utils/tensorflow/exceptions.py +5 -0
  627. rasa/utils/tensorflow/layers.py +1565 -0
  628. rasa/utils/tensorflow/layers_utils.py +113 -0
  629. rasa/utils/tensorflow/metrics.py +281 -0
  630. rasa/utils/tensorflow/model_data.py +991 -0
  631. rasa/utils/tensorflow/model_data_utils.py +500 -0
  632. rasa/utils/tensorflow/models.py +936 -0
  633. rasa/utils/tensorflow/rasa_layers.py +1094 -0
  634. rasa/utils/tensorflow/transformer.py +640 -0
  635. rasa/utils/tensorflow/types.py +6 -0
  636. rasa/utils/train_utils.py +572 -0
  637. rasa/utils/yaml.py +54 -0
  638. rasa/validator.py +1035 -0
  639. rasa/version.py +3 -0
  640. rasa_pro-3.8.16.dist-info/METADATA +528 -0
  641. rasa_pro-3.8.16.dist-info/NOTICE +5 -0
  642. rasa_pro-3.8.16.dist-info/RECORD +644 -0
  643. rasa_pro-3.8.16.dist-info/WHEEL +4 -0
  644. rasa_pro-3.8.16.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,2507 @@
1
+ import abc
2
+ import copy
3
+ import json
4
+ import logging
5
+ import structlog
6
+ import re
7
+ from abc import ABC
8
+
9
+ import jsonpickle
10
+ import time
11
+ import uuid
12
+ from dateutil import parser
13
+ from datetime import datetime
14
+ from typing import (
15
+ List,
16
+ Dict,
17
+ Text,
18
+ Any,
19
+ Type,
20
+ Optional,
21
+ TYPE_CHECKING,
22
+ Iterable,
23
+ cast,
24
+ Tuple,
25
+ TypeVar,
26
+ )
27
+
28
+ import rasa.shared.utils.common
29
+ import rasa.shared.utils.io
30
+ from typing import Union
31
+
32
+ from rasa.shared.constants import DOCS_URL_TRAINING_DATA
33
+ from rasa.shared.core.constants import (
34
+ LOOP_NAME,
35
+ EXTERNAL_MESSAGE_PREFIX,
36
+ ACTION_NAME_SENDER_ID_CONNECTOR_STR,
37
+ IS_EXTERNAL,
38
+ USE_TEXT_FOR_FEATURIZATION,
39
+ LOOP_INTERRUPTED,
40
+ ENTITY_LABEL_SEPARATOR,
41
+ ACTION_SESSION_START_NAME,
42
+ ACTION_LISTEN_NAME,
43
+ )
44
+ from rasa.shared.exceptions import UnsupportedFeatureException
45
+ from rasa.shared.nlu.constants import (
46
+ ENTITY_ATTRIBUTE_TYPE,
47
+ INTENT,
48
+ TEXT,
49
+ ENTITIES,
50
+ COMMANDS,
51
+ ENTITY_ATTRIBUTE_VALUE,
52
+ ACTION_TEXT,
53
+ ACTION_NAME,
54
+ INTENT_NAME_KEY,
55
+ ENTITY_ATTRIBUTE_ROLE,
56
+ ENTITY_ATTRIBUTE_GROUP,
57
+ PREDICTED_CONFIDENCE_KEY,
58
+ INTENT_RANKING_KEY,
59
+ ENTITY_ATTRIBUTE_TEXT,
60
+ ENTITY_ATTRIBUTE_START,
61
+ ENTITY_ATTRIBUTE_CONFIDENCE,
62
+ ENTITY_ATTRIBUTE_END,
63
+ FULL_RETRIEVAL_INTENT_NAME_KEY,
64
+ )
65
+
66
+
67
+ if TYPE_CHECKING:
68
+ from typing_extensions import TypedDict
69
+
70
+ from rasa.shared.core.trackers import DialogueStateTracker
71
+
72
+ EntityPrediction = TypedDict(
73
+ "EntityPrediction",
74
+ {
75
+ ENTITY_ATTRIBUTE_TEXT: Text, # type: ignore[misc]
76
+ ENTITY_ATTRIBUTE_START: Optional[float],
77
+ ENTITY_ATTRIBUTE_END: Optional[float],
78
+ ENTITY_ATTRIBUTE_VALUE: Text,
79
+ ENTITY_ATTRIBUTE_CONFIDENCE: float,
80
+ ENTITY_ATTRIBUTE_TYPE: Text,
81
+ ENTITY_ATTRIBUTE_GROUP: Optional[Text],
82
+ ENTITY_ATTRIBUTE_ROLE: Optional[Text],
83
+ "additional_info": Any,
84
+ },
85
+ total=False,
86
+ )
87
+
88
+ IntentPrediction = TypedDict(
89
+ "IntentPrediction", {INTENT_NAME_KEY: Text, PREDICTED_CONFIDENCE_KEY: float} # type: ignore[misc] # noqa: E501
90
+ )
91
+ NLUPredictionData = TypedDict(
92
+ "NLUPredictionData",
93
+ {
94
+ TEXT: Text, # type: ignore[misc]
95
+ INTENT: IntentPrediction,
96
+ INTENT_RANKING_KEY: List[IntentPrediction],
97
+ ENTITIES: List[EntityPrediction],
98
+ "message_id": Optional[Text],
99
+ "metadata": Dict,
100
+ },
101
+ total=False,
102
+ )
103
+ logger = logging.getLogger(__name__)
104
+ structlogger = structlog.get_logger()
105
+
106
+
107
+ def deserialise_events(serialized_events: List[Dict[Text, Any]]) -> List["Event"]:
108
+ """Convert a list of dictionaries to a list of corresponding events.
109
+
110
+ Example format:
111
+ [{"event": "slot", "value": 5, "name": "my_slot"}]
112
+ """
113
+ deserialised = []
114
+
115
+ for e in serialized_events:
116
+ if "event" in e:
117
+ event = Event.from_parameters(e)
118
+ if event:
119
+ deserialised.append(event)
120
+ else:
121
+ structlogger.warning(
122
+ "event.deserialization.failed", rasa_event=copy.deepcopy(event)
123
+ )
124
+
125
+ return deserialised
126
+
127
+
128
+ def deserialise_entities(entities: Union[Text, List[Any]]) -> List[Dict[Text, Any]]:
129
+ if isinstance(entities, str):
130
+ entities = json.loads(entities)
131
+
132
+ return [e for e in entities if isinstance(e, dict)]
133
+
134
+
135
+ def format_message(
136
+ text: Text, intent: Optional[Text], entities: Union[Text, List[Any]]
137
+ ) -> Text:
138
+ """Uses NLU parser information to generate a message with inline entity annotations.
139
+
140
+ Arguments:
141
+ text: text of the message
142
+ intent: intent of the message
143
+ entities: entities of the message
144
+
145
+ Return:
146
+ Message with entities annotated inline, e.g.
147
+ `I am from [Berlin]{`"`entity`"`: `"`city`"`}`.
148
+ """
149
+ from rasa.shared.nlu.training_data.formats.readerwriter import TrainingDataWriter
150
+ from rasa.shared.nlu.training_data import entities_parser
151
+
152
+ message_from_md = entities_parser.parse_training_example(text, intent)
153
+ deserialised_entities = deserialise_entities(entities)
154
+ return TrainingDataWriter.generate_message(
155
+ {"text": message_from_md.get(TEXT), "entities": deserialised_entities}
156
+ )
157
+
158
+
159
+ def split_events(
160
+ events: Iterable["Event"],
161
+ event_type_to_split_on: Type["Event"],
162
+ additional_splitting_conditions: Optional[Dict[Text, Any]] = None,
163
+ include_splitting_event: bool = True,
164
+ ) -> List[List["Event"]]:
165
+ """Splits events according to an event type and condition.
166
+
167
+ Examples:
168
+ Splitting events according to the event type `ActionExecuted` and the
169
+ `action_name` 'action_session_start' would look as follows:
170
+
171
+ >> _events = split_events(
172
+ events,
173
+ ActionExecuted,
174
+ {"action_name": "action_session_start"},
175
+ True
176
+ )
177
+
178
+ Args:
179
+ events: Events to split.
180
+ event_type_to_split_on: The event type to split on.
181
+ additional_splitting_conditions: Additional event attributes to split on.
182
+ include_splitting_event: Whether the events of the type on which the split
183
+ is based should be included in the returned events.
184
+
185
+ Returns:
186
+ The split events.
187
+ """
188
+ sub_events = []
189
+ current: List["Event"] = []
190
+
191
+ def event_fulfills_splitting_condition(evt: "Event") -> bool:
192
+ # event does not have the correct type
193
+ if not isinstance(evt, event_type_to_split_on):
194
+ return False
195
+
196
+ # the type is correct and there are no further conditions
197
+ if not additional_splitting_conditions:
198
+ return True
199
+
200
+ # there are further conditions - check those
201
+ return all(
202
+ getattr(evt, k, None) == v
203
+ for k, v in additional_splitting_conditions.items()
204
+ )
205
+
206
+ for event in events:
207
+ if event_fulfills_splitting_condition(event):
208
+ if current:
209
+ sub_events.append(current)
210
+
211
+ current = []
212
+ if include_splitting_event:
213
+ current.append(event)
214
+ else:
215
+ current.append(event)
216
+
217
+ if current:
218
+ sub_events.append(current)
219
+
220
+ return sub_events
221
+
222
+
223
+ def do_events_begin_with_session_start(events: List["Event"]) -> bool:
224
+ """Determines whether `events` begins with a session start sequence.
225
+
226
+ A session start sequence is a sequence of two events: an executed
227
+ `action_session_start` as well as a logged `session_started`.
228
+
229
+ Args:
230
+ events: The events to inspect.
231
+
232
+ Returns:
233
+ Whether `events` begins with a session start sequence.
234
+ """
235
+ if len(events) < 2:
236
+ return False
237
+
238
+ first = events[0]
239
+ second = events[1]
240
+
241
+ # We are not interested in specific metadata or timestamps. Action name and event
242
+ # type are sufficient for this check
243
+ return (
244
+ isinstance(first, ActionExecuted)
245
+ and first.action_name == ACTION_SESSION_START_NAME
246
+ and isinstance(second, SessionStarted)
247
+ )
248
+
249
+
250
+ def remove_parse_data(event: Dict[Text, Any]) -> Dict[Text, Any]:
251
+ """Reduce event details to the minimum necessary to be structlogged.
252
+
253
+ Deletes the parse_data key from the event if it exists.
254
+
255
+ Args:
256
+ event: The event to be reduced.
257
+
258
+ Returns:
259
+ A reduced copy of the event.
260
+ """
261
+ reduced_event = copy.deepcopy(event)
262
+ if "parse_data" in reduced_event:
263
+ del reduced_event["parse_data"]
264
+ return reduced_event
265
+
266
+
267
+ E = TypeVar("E", bound="Event")
268
+
269
+
270
+ class Event(ABC):
271
+ """Describes events in conversation and how the affect the conversation state.
272
+
273
+ Immutable representation of everything which happened during a conversation of the
274
+ user with the assistant. Tells the `rasa.shared.core.trackers.DialogueStateTracker`
275
+ how to update its state as the events occur.
276
+ """
277
+
278
+ type_name = "event"
279
+
280
+ def __init__(
281
+ self,
282
+ timestamp: Optional[float] = None,
283
+ metadata: Optional[Dict[Text, Any]] = None,
284
+ ) -> None:
285
+ self.timestamp = timestamp or time.time()
286
+ self.metadata = metadata or {}
287
+
288
+ def __ne__(self, other: Any) -> bool:
289
+ # Not strictly necessary, but to avoid having both x==y and x!=y
290
+ # True at the same time
291
+ return not (self == other)
292
+
293
+ @abc.abstractmethod
294
+ def as_story_string(self) -> Optional[Text]:
295
+ """Returns the event as story string.
296
+
297
+ Returns:
298
+ textual representation of the event or None.
299
+ """
300
+ # Every class should implement this
301
+ raise NotImplementedError
302
+
303
+ @staticmethod
304
+ def from_story_string(
305
+ event_name: Text,
306
+ parameters: Dict[Text, Any],
307
+ default: Optional[Type["Event"]] = None,
308
+ ) -> Optional[List["Event"]]:
309
+ event_class = Event.resolve_by_type(event_name, default)
310
+
311
+ if not event_class:
312
+ return None
313
+
314
+ return event_class._from_story_string(parameters)
315
+
316
+ @staticmethod
317
+ def from_parameters(
318
+ parameters: Dict[Text, Any], default: Optional[Type["Event"]] = None
319
+ ) -> Optional["Event"]:
320
+
321
+ event_name = parameters.get("event")
322
+ if event_name is None:
323
+ return None
324
+
325
+ event_class: Optional[Type[Event]] = Event.resolve_by_type(event_name, default)
326
+ if not event_class:
327
+ return None
328
+
329
+ return event_class._from_parameters(parameters)
330
+
331
+ @classmethod
332
+ def _from_story_string(
333
+ cls: Type[E], parameters: Dict[Text, Any]
334
+ ) -> Optional[List[E]]:
335
+ """Called to convert a parsed story line into an event."""
336
+ return [cls(parameters.get("timestamp"), parameters.get("metadata"))]
337
+
338
+ def as_dict(self) -> Dict[Text, Any]:
339
+ d = {"event": self.type_name, "timestamp": self.timestamp}
340
+
341
+ if self.metadata:
342
+ d["metadata"] = self.metadata
343
+
344
+ return d
345
+
346
+ def fingerprint(self) -> Text:
347
+ """Returns a unique hash for the event which is stable across python runs.
348
+
349
+ Returns:
350
+ fingerprint of the event
351
+ """
352
+ data = self.as_dict()
353
+ del data["timestamp"]
354
+ return rasa.shared.utils.io.get_dictionary_fingerprint(data)
355
+
356
+ @classmethod
357
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> Optional["Event"]:
358
+ """Called to convert a dictionary of parameters to a single event.
359
+
360
+ By default uses the same implementation as the story line
361
+ conversation ``_from_story_string``. But the subclass might
362
+ decide to handle parameters differently if the parsed parameters
363
+ don't origin from a story file.
364
+ """
365
+ result = cls._from_story_string(parameters)
366
+ if len(result) > 1:
367
+ logger.warning(
368
+ f"Event from parameters called with parameters "
369
+ f"for multiple events. This is not supported, "
370
+ f"only the first event will be returned. "
371
+ f"Parameters: {parameters}"
372
+ )
373
+ return result[0] if result else None
374
+
375
+ @staticmethod
376
+ def resolve_by_type(
377
+ type_name: Text, default: Optional[Type["Event"]] = None
378
+ ) -> Optional[Type["Event"]]:
379
+ """Returns a slots class by its type name."""
380
+ for cls in rasa.shared.utils.common.all_subclasses(Event):
381
+ if cls.type_name == type_name:
382
+ return cls
383
+ if type_name == "topic":
384
+ return None # backwards compatibility to support old TopicSet evts
385
+ elif default is not None:
386
+ return default
387
+ else:
388
+ raise ValueError(f"Unknown event name '{type_name}'.")
389
+
390
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
391
+ """Applies event to current conversation state.
392
+
393
+ Args:
394
+ tracker: The current conversation state.
395
+ """
396
+ pass
397
+
398
+ @abc.abstractmethod
399
+ def __eq__(self, other: Any) -> bool:
400
+ """Compares object with other object."""
401
+ # Every class should implement this
402
+ raise NotImplementedError()
403
+
404
+ def __str__(self) -> Text:
405
+ """Returns text representation of event."""
406
+ return f"{self.__class__.__name__}()"
407
+
408
+
409
+ class AlwaysEqualEventMixin(Event, ABC):
410
+ """Class to deduplicate common behavior for events without additional attributes."""
411
+
412
+ def __eq__(self, other: Any) -> bool:
413
+ """Compares object with other object."""
414
+ if not isinstance(other, self.__class__):
415
+ return NotImplemented
416
+
417
+ return True
418
+
419
+
420
+ class SkipEventInMDStoryMixin(Event, ABC):
421
+ """Skips the visualization of an event in Markdown stories."""
422
+
423
+ def as_story_string(self) -> None:
424
+ """Returns the event as story string.
425
+
426
+ Returns:
427
+ None, as this event should not appear inside the story.
428
+ """
429
+ return
430
+
431
+
432
+ class UserUttered(Event):
433
+ """The user has said something to the bot.
434
+
435
+ As a side effect a new `Turn` will be created in the `Tracker`.
436
+ """
437
+
438
+ type_name = "user"
439
+
440
+ def __init__(
441
+ self,
442
+ text: Optional[Text] = None,
443
+ intent: Optional[Dict] = None,
444
+ entities: Optional[List[Dict]] = None,
445
+ parse_data: Optional["NLUPredictionData"] = None,
446
+ timestamp: Optional[float] = None,
447
+ input_channel: Optional[Text] = None,
448
+ message_id: Optional[Text] = None,
449
+ metadata: Optional[Dict] = None,
450
+ use_text_for_featurization: Optional[bool] = None,
451
+ ) -> None:
452
+ """Creates event for incoming user message.
453
+
454
+ Args:
455
+ text: Text of user message.
456
+ intent: Intent prediction of user message.
457
+ entities: Extracted entities.
458
+ parse_data: Detailed NLU parsing result for message.
459
+ timestamp: When the event was created.
460
+ metadata: Additional event metadata.
461
+ input_channel: Which channel the user used to send message.
462
+ message_id: Unique ID for message.
463
+ use_text_for_featurization: `True` if the message's text was used to predict
464
+ next action. `False` if the message's intent was used.
465
+
466
+ """
467
+ self.text = text
468
+ self.intent = intent if intent else {}
469
+ self.entities = entities if entities else []
470
+ self.input_channel = input_channel
471
+ self.message_id = message_id
472
+
473
+ super().__init__(timestamp, metadata)
474
+
475
+ # The featurization is set by the policies during prediction time using a
476
+ # `DefinePrevUserUtteredFeaturization` event.
477
+ self.use_text_for_featurization = use_text_for_featurization
478
+ # define how this user utterance should be featurized
479
+ if self.text and not self.intent_name:
480
+ # happens during training
481
+ self.use_text_for_featurization = True
482
+ elif self.intent_name and not self.text:
483
+ # happens during training
484
+ self.use_text_for_featurization = False
485
+
486
+ self.parse_data: "NLUPredictionData" = {
487
+ INTENT: self.intent, # type: ignore[misc]
488
+ # Copy entities so that changes to `self.entities` don't affect
489
+ # `self.parse_data` and hence don't get persisted
490
+ ENTITIES: self.entities.copy(),
491
+ TEXT: self.text,
492
+ "message_id": self.message_id,
493
+ "metadata": self.metadata,
494
+ }
495
+ if parse_data:
496
+ self.parse_data.update(**parse_data)
497
+
498
+ @staticmethod
499
+ def _from_parse_data(
500
+ text: Text,
501
+ parse_data: "NLUPredictionData",
502
+ timestamp: Optional[float] = None,
503
+ input_channel: Optional[Text] = None,
504
+ message_id: Optional[Text] = None,
505
+ metadata: Optional[Dict] = None,
506
+ ) -> "UserUttered":
507
+ return UserUttered(
508
+ text,
509
+ parse_data.get(INTENT),
510
+ parse_data.get(ENTITIES, []),
511
+ parse_data,
512
+ timestamp,
513
+ input_channel,
514
+ message_id,
515
+ metadata,
516
+ )
517
+
518
+ def __hash__(self) -> int:
519
+ """Returns unique hash of object."""
520
+ return hash(json.dumps(self.as_sub_state()))
521
+
522
+ @property
523
+ def has_triggered_error(self) -> bool:
524
+ return len(self.error_commands) > 0
525
+
526
+ @property
527
+ def error_commands(self) -> List[Dict[Text, Any]]:
528
+ from rasa.dialogue_understanding.commands import ErrorCommand
529
+
530
+ return [c for c in self.commands if c["command"] == ErrorCommand.command()]
531
+
532
+ @property
533
+ def commands(self) -> List[Dict[str, Any]]:
534
+ """Returns commands included in the message."""
535
+ if COMMANDS in self.parse_data and isinstance(
536
+ self.parse_data[COMMANDS], list # type: ignore[literal-required]
537
+ ):
538
+ return self.parse_data[COMMANDS] # type: ignore[literal-required]
539
+ return []
540
+
541
+ @property
542
+ def intent_name(self) -> Optional[Text]:
543
+ """Returns intent name or `None` if no intent."""
544
+ return self.intent.get(INTENT_NAME_KEY)
545
+
546
+ @property
547
+ def full_retrieval_intent_name(self) -> Optional[Text]:
548
+ """Returns full retrieval intent name or `None` if no retrieval intent."""
549
+ return self.intent.get(FULL_RETRIEVAL_INTENT_NAME_KEY)
550
+
551
+ # Note that this means two UserUttered events with the same text, intent
552
+ # and entities but _different_ timestamps will be considered equal.
553
+ def __eq__(self, other: Any) -> bool:
554
+ """Compares object with other object."""
555
+ if not isinstance(other, UserUttered):
556
+ return NotImplemented
557
+
558
+ return (
559
+ self.text,
560
+ self.intent_name,
561
+ [
562
+ jsonpickle.encode(sorted(ent)) for ent in self.entities
563
+ ], # TODO: test? Or fix in regex_message_handler?
564
+ ) == (
565
+ other.text,
566
+ other.intent_name,
567
+ [jsonpickle.encode(sorted(ent)) for ent in other.entities],
568
+ )
569
+
570
+ def __str__(self) -> Text:
571
+ """Returns text representation of event."""
572
+ entities = ""
573
+ if self.entities:
574
+ entities_list = [
575
+ f"{entity[ENTITY_ATTRIBUTE_VALUE]} "
576
+ f"(Type: {entity[ENTITY_ATTRIBUTE_TYPE]}, "
577
+ f"Role: {entity.get(ENTITY_ATTRIBUTE_ROLE)}, "
578
+ f"Group: {entity.get(ENTITY_ATTRIBUTE_GROUP)})"
579
+ for entity in self.entities
580
+ ]
581
+ entities = f", entities: {', '.join(entities_list)}"
582
+
583
+ return (
584
+ f"UserUttered(text: {self.text}, intent: {self.intent_name}"
585
+ f"{entities}"
586
+ f", use_text_for_featurization: {self.use_text_for_featurization})"
587
+ )
588
+
589
+ def __repr__(self) -> Text:
590
+ """Returns text representation of event for debugging."""
591
+ return (
592
+ f"UserUttered('{self.text}', "
593
+ f"'{self.intent_name}', "
594
+ f"{json.dumps(self.entities)})"
595
+ )
596
+
597
+ @staticmethod
598
+ def empty() -> "UserUttered":
599
+ return UserUttered(None)
600
+
601
+ def is_empty(self) -> bool:
602
+ return not self.text and not self.intent_name and not self.entities
603
+
604
+ def as_dict(self) -> Dict[Text, Any]:
605
+ _dict = super().as_dict()
606
+ _dict.update(
607
+ {
608
+ "text": self.text,
609
+ "parse_data": self.parse_data,
610
+ "input_channel": getattr(self, "input_channel", None),
611
+ "message_id": getattr(self, "message_id", None),
612
+ "metadata": self.metadata,
613
+ }
614
+ )
615
+ return _dict
616
+
617
+ def as_sub_state(self) -> Dict[Text, Union[None, Text, List[Optional[Text]]]]:
618
+ """Turns a UserUttered event into features.
619
+
620
+ The substate contains information about entities, intent and text of the
621
+ `UserUttered` event.
622
+
623
+ Returns:
624
+ a dictionary with intent name, text and entities
625
+ """
626
+ entities = [entity.get(ENTITY_ATTRIBUTE_TYPE) for entity in self.entities]
627
+ entities.extend(
628
+ (
629
+ f"{entity.get(ENTITY_ATTRIBUTE_TYPE)}{ENTITY_LABEL_SEPARATOR}"
630
+ f"{entity.get(ENTITY_ATTRIBUTE_ROLE)}"
631
+ )
632
+ for entity in self.entities
633
+ if ENTITY_ATTRIBUTE_ROLE in entity
634
+ )
635
+ entities.extend(
636
+ (
637
+ f"{entity.get(ENTITY_ATTRIBUTE_TYPE)}{ENTITY_LABEL_SEPARATOR}"
638
+ f"{entity.get(ENTITY_ATTRIBUTE_GROUP)}"
639
+ )
640
+ for entity in self.entities
641
+ if ENTITY_ATTRIBUTE_GROUP in entity
642
+ )
643
+
644
+ out: Dict[Text, Union[None, Text, List[Optional[Text]]]] = {}
645
+ # During training we expect either intent_name or text to be set.
646
+ # During prediction both will be set.
647
+ if self.text and (
648
+ self.use_text_for_featurization or self.use_text_for_featurization is None
649
+ ):
650
+ out[TEXT] = self.text
651
+ if self.intent_name and not self.use_text_for_featurization:
652
+ out[INTENT] = self.intent_name
653
+ # don't add entities for e2e utterances
654
+ if entities and not self.use_text_for_featurization:
655
+ out[ENTITIES] = entities
656
+
657
+ return out
658
+
659
+ @classmethod
660
+ def _from_story_string(
661
+ cls, parameters: Dict[Text, Any]
662
+ ) -> Optional[List["UserUttered"]]:
663
+ try:
664
+ return [
665
+ cls._from_parse_data(
666
+ parameters.get("text"),
667
+ parameters.get("parse_data"),
668
+ parameters.get("timestamp"),
669
+ parameters.get("input_channel"),
670
+ parameters.get("message_id"),
671
+ parameters.get("metadata"),
672
+ )
673
+ ]
674
+ except KeyError as e:
675
+ raise ValueError(f"Failed to parse bot uttered event. {e}")
676
+
677
+ def _entity_string(self) -> Text:
678
+ if self.entities:
679
+ return json.dumps(
680
+ {
681
+ entity[ENTITY_ATTRIBUTE_TYPE]: entity[ENTITY_ATTRIBUTE_VALUE]
682
+ for entity in self.entities
683
+ },
684
+ ensure_ascii=False,
685
+ )
686
+ return ""
687
+
688
+ def as_story_string(self, e2e: bool = False) -> Text:
689
+ """Return event as string for Markdown training format.
690
+
691
+ Args:
692
+ e2e: `True` if the the event should be printed in the format for
693
+ end-to-end conversation tests.
694
+
695
+ Returns:
696
+ Event as string.
697
+ """
698
+ if self.use_text_for_featurization and not e2e:
699
+ raise UnsupportedFeatureException(
700
+ f"Printing end-to-end user utterances is not supported in the "
701
+ f"Markdown training format. Please use the YAML training data format "
702
+ f"instead. Please see {DOCS_URL_TRAINING_DATA} for more information."
703
+ )
704
+
705
+ if e2e:
706
+ text_with_entities = format_message(
707
+ self.text or "", self.intent_name, self.entities
708
+ )
709
+
710
+ intent_prefix = f"{self.intent_name}: " if self.intent_name else ""
711
+ return f"{intent_prefix}{text_with_entities}"
712
+
713
+ return f"{self.intent_name or ''}{self._entity_string()}"
714
+
715
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
716
+ """Applies event to tracker. See docstring of `Event`."""
717
+ tracker.latest_message = self
718
+ tracker.clear_followup_action()
719
+
720
+ @staticmethod
721
+ def create_external(
722
+ intent_name: Text,
723
+ entity_list: Optional[List[Dict[Text, Any]]] = None,
724
+ input_channel: Optional[Text] = None,
725
+ ) -> "UserUttered":
726
+ return UserUttered(
727
+ text=f"{EXTERNAL_MESSAGE_PREFIX}{intent_name}",
728
+ intent={INTENT_NAME_KEY: intent_name},
729
+ metadata={IS_EXTERNAL: True},
730
+ entities=entity_list or [],
731
+ input_channel=input_channel,
732
+ )
733
+
734
+
735
+ class DefinePrevUserUtteredFeaturization(SkipEventInMDStoryMixin):
736
+ """Stores information whether action was predicted based on text or intent."""
737
+
738
+ type_name = "user_featurization"
739
+
740
+ def __init__(
741
+ self,
742
+ use_text_for_featurization: bool,
743
+ timestamp: Optional[float] = None,
744
+ metadata: Optional[Dict[Text, Any]] = None,
745
+ ) -> None:
746
+ """Creates event.
747
+
748
+ Args:
749
+ use_text_for_featurization: `True` if message text was used to predict
750
+ action. `False` if intent was used.
751
+ timestamp: When the event was created.
752
+ metadata: Additional event metadata.
753
+ """
754
+ super().__init__(timestamp, metadata)
755
+ self.use_text_for_featurization = use_text_for_featurization
756
+
757
+ def __str__(self) -> Text:
758
+ """Returns text representation of event."""
759
+ return f"DefinePrevUserUtteredFeaturization({self.use_text_for_featurization})"
760
+
761
+ def __hash__(self) -> int:
762
+ """Returns unique hash for event."""
763
+ return hash(self.use_text_for_featurization)
764
+
765
+ @classmethod
766
+ def _from_parameters(
767
+ cls, parameters: Dict[Text, Any]
768
+ ) -> "DefinePrevUserUtteredFeaturization":
769
+ return DefinePrevUserUtteredFeaturization(
770
+ parameters.get(USE_TEXT_FOR_FEATURIZATION),
771
+ parameters.get("timestamp"),
772
+ parameters.get("metadata"),
773
+ )
774
+
775
+ def as_dict(self) -> Dict[Text, Any]:
776
+ """Returns serialized event."""
777
+ d = super().as_dict()
778
+ d.update({USE_TEXT_FOR_FEATURIZATION: self.use_text_for_featurization})
779
+ return d
780
+
781
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
782
+ """Applies event to current conversation state.
783
+
784
+ Args:
785
+ tracker: The current conversation state.
786
+ """
787
+ if tracker.latest_action_name != ACTION_LISTEN_NAME:
788
+ # featurization belong only to the last user message
789
+ # a user message is always followed by action listen
790
+ return
791
+
792
+ if not tracker.latest_message:
793
+ return
794
+
795
+ # update previous user message's featurization based on this event
796
+ tracker.latest_message.use_text_for_featurization = (
797
+ self.use_text_for_featurization
798
+ )
799
+
800
+ def __eq__(self, other: Any) -> bool:
801
+ """Compares object with other object."""
802
+ if not isinstance(other, DefinePrevUserUtteredFeaturization):
803
+ return NotImplemented
804
+
805
+ return self.use_text_for_featurization == other.use_text_for_featurization
806
+
807
+
808
+ class EntitiesAdded(SkipEventInMDStoryMixin):
809
+ """Event that is used to add extracted entities to the tracker state."""
810
+
811
+ type_name = "entities"
812
+
813
+ def __init__(
814
+ self,
815
+ entities: List[Dict[Text, Any]],
816
+ timestamp: Optional[float] = None,
817
+ metadata: Optional[Dict[Text, Any]] = None,
818
+ ) -> None:
819
+ """Initializes event.
820
+
821
+ Args:
822
+ entities: Entities extracted from previous user message. This can either
823
+ be done by NLU components or end-to-end policy predictions.
824
+ timestamp: the timestamp
825
+ metadata: some optional metadata
826
+ """
827
+ super().__init__(timestamp, metadata)
828
+ self.entities = entities
829
+
830
+ def __str__(self) -> Text:
831
+ """Returns the string representation of the event."""
832
+ entity_str = [e[ENTITY_ATTRIBUTE_TYPE] for e in self.entities]
833
+ return f"{self.__class__.__name__}({entity_str})"
834
+
835
+ def __hash__(self) -> int:
836
+ """Returns the hash value of the event."""
837
+ return hash(json.dumps(self.entities))
838
+
839
+ def __eq__(self, other: Any) -> bool:
840
+ """Compares this event with another event."""
841
+ if not isinstance(other, EntitiesAdded):
842
+ return NotImplemented
843
+
844
+ return self.entities == other.entities
845
+
846
+ @classmethod
847
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "EntitiesAdded":
848
+ return EntitiesAdded(
849
+ parameters.get(ENTITIES),
850
+ parameters.get("timestamp"),
851
+ parameters.get("metadata"),
852
+ )
853
+
854
+ def as_dict(self) -> Dict[Text, Any]:
855
+ """Converts the event into a dict.
856
+
857
+ Returns:
858
+ A dict that represents this event.
859
+ """
860
+ d = super().as_dict()
861
+ d.update({ENTITIES: self.entities})
862
+ return d
863
+
864
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
865
+ """Applies event to current conversation state.
866
+
867
+ Args:
868
+ tracker: The current conversation state.
869
+ """
870
+ if tracker.latest_action_name != ACTION_LISTEN_NAME:
871
+ # entities belong only to the last user message
872
+ # a user message always comes after action listen
873
+ return
874
+
875
+ if not tracker.latest_message:
876
+ return
877
+
878
+ for entity in self.entities:
879
+ if entity not in tracker.latest_message.entities:
880
+ tracker.latest_message.entities.append(entity)
881
+
882
+
883
+ class BotUttered(SkipEventInMDStoryMixin):
884
+ """The bot has said something to the user.
885
+
886
+ This class is not used in the story training as it is contained in the
887
+
888
+ ``ActionExecuted`` class. An entry is made in the ``Tracker``.
889
+ """
890
+
891
+ type_name = "bot"
892
+
893
+ def __init__(
894
+ self,
895
+ text: Optional[Text] = None,
896
+ data: Optional[Dict] = None,
897
+ metadata: Optional[Dict[Text, Any]] = None,
898
+ timestamp: Optional[float] = None,
899
+ ) -> None:
900
+ """Creates event for a bot response.
901
+
902
+ Args:
903
+ text: Plain text which bot responded with.
904
+ data: Additional data for more complex utterances (e.g. buttons).
905
+ timestamp: When the event was created.
906
+ metadata: Additional event metadata.
907
+ """
908
+ self.text = text
909
+ self.data = data or {}
910
+ super().__init__(timestamp, metadata)
911
+
912
+ def __members(self) -> Tuple[Optional[Text], Text, Text]:
913
+ data_no_nones = {k: v for k, v in self.data.items() if v is not None}
914
+ meta_no_nones = {k: v for k, v in self.metadata.items() if v is not None}
915
+ return (
916
+ self.text,
917
+ jsonpickle.encode(data_no_nones),
918
+ jsonpickle.encode(meta_no_nones),
919
+ )
920
+
921
+ def __hash__(self) -> int:
922
+ """Returns unique hash for event."""
923
+ return hash(self.__members())
924
+
925
+ def __eq__(self, other: Any) -> bool:
926
+ """Compares object with other object."""
927
+ if not isinstance(other, BotUttered):
928
+ return NotImplemented
929
+
930
+ return self.__members() == other.__members()
931
+
932
+ def __str__(self) -> Text:
933
+ """Returns text representation of event."""
934
+ return "BotUttered(text: {}, data: {}, metadata: {})".format(
935
+ self.text, json.dumps(self.data), json.dumps(self.metadata)
936
+ )
937
+
938
+ def __repr__(self) -> Text:
939
+ """Returns text representation of event for debugging."""
940
+ return "BotUttered('{}', {}, {}, {})".format(
941
+ self.text, json.dumps(self.data), json.dumps(self.metadata), self.timestamp
942
+ )
943
+
944
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
945
+ """Applies event to current conversation state."""
946
+ tracker.latest_bot_utterance = self
947
+
948
+ def message(self) -> Dict[Text, Any]:
949
+ """Return the complete message as a dictionary."""
950
+ m = self.data.copy()
951
+ m["text"] = self.text
952
+ m["timestamp"] = self.timestamp
953
+ m.update(self.metadata)
954
+
955
+ if m.get("image") == m.get("attachment"):
956
+ # we need this as there is an oddity we introduced a while ago where
957
+ # we automatically set the attachment to the image. to not break
958
+ # any persisted events we kept that, but we need to make sure that
959
+ # the message contains the image only once
960
+ m["attachment"] = None
961
+
962
+ return m
963
+
964
+ @staticmethod
965
+ def empty() -> "BotUttered":
966
+ """Creates an empty bot utterance."""
967
+ return BotUttered()
968
+
969
+ def as_dict(self) -> Dict[Text, Any]:
970
+ """Returns serialized event."""
971
+ d = super().as_dict()
972
+ d.update({"text": self.text, "data": self.data, "metadata": self.metadata})
973
+ return d
974
+
975
+ @classmethod
976
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "BotUttered":
977
+ try:
978
+ return BotUttered(
979
+ parameters.get("text"),
980
+ parameters.get("data"),
981
+ parameters.get("metadata"),
982
+ parameters.get("timestamp"),
983
+ )
984
+ except KeyError as e:
985
+ raise ValueError(f"Failed to parse bot uttered event. {e}")
986
+
987
+
988
+ class SlotSet(Event):
989
+ """The user has specified their preference for the value of a `slot`.
990
+
991
+ Every slot has a name and a value. This event can be used to set a
992
+ value for a slot on a conversation.
993
+
994
+ As a side effect the `Tracker`'s slots will be updated so
995
+ that `tracker.slots[key]=value`.
996
+ """
997
+
998
+ type_name = "slot"
999
+
1000
+ def __init__(
1001
+ self,
1002
+ key: Text,
1003
+ value: Optional[Any] = None,
1004
+ timestamp: Optional[float] = None,
1005
+ metadata: Optional[Dict[Text, Any]] = None,
1006
+ ) -> None:
1007
+ """Creates event to set slot.
1008
+
1009
+ Args:
1010
+ key: Name of the slot which is set.
1011
+ value: Value to which slot is set.
1012
+ timestamp: When the event was created.
1013
+ metadata: Additional event metadata.
1014
+ """
1015
+ self.key = key
1016
+ self.value = value
1017
+ super().__init__(timestamp, metadata)
1018
+
1019
+ def __repr__(self) -> Text:
1020
+ """Returns text representation of event."""
1021
+ return f"SlotSet(key: {self.key}, value: {self.value})"
1022
+
1023
+ def __hash__(self) -> int:
1024
+ """Returns unique hash for event."""
1025
+ return hash((self.key, jsonpickle.encode(self.value)))
1026
+
1027
+ def __eq__(self, other: Any) -> bool:
1028
+ """Compares object with other object."""
1029
+ if not isinstance(other, SlotSet):
1030
+ return NotImplemented
1031
+
1032
+ return (self.key, self.value) == (other.key, other.value)
1033
+
1034
+ def as_story_string(self) -> Text:
1035
+ """Returns text representation of event."""
1036
+ props = json.dumps({self.key: self.value}, ensure_ascii=False)
1037
+ return f"{self.type_name}{props}"
1038
+
1039
+ @classmethod
1040
+ def _from_story_string(
1041
+ cls, parameters: Dict[Text, Any]
1042
+ ) -> Optional[List["SlotSet"]]:
1043
+
1044
+ slots = []
1045
+ for slot_key, slot_val in parameters.items():
1046
+ slots.append(SlotSet(slot_key, slot_val))
1047
+
1048
+ if slots:
1049
+ return slots
1050
+ else:
1051
+ return None
1052
+
1053
+ def as_dict(self) -> Dict[Text, Any]:
1054
+ """Returns serialized event."""
1055
+ d = super().as_dict()
1056
+ d.update({"name": self.key, "value": self.value})
1057
+ return d
1058
+
1059
+ @classmethod
1060
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "SlotSet":
1061
+ try:
1062
+ return SlotSet(
1063
+ parameters.get("name"),
1064
+ parameters.get("value"),
1065
+ parameters.get("timestamp"),
1066
+ parameters.get("metadata"),
1067
+ )
1068
+ except KeyError as e:
1069
+ raise ValueError(f"Failed to parse set slot event. {e}")
1070
+
1071
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1072
+ """Applies event to current conversation state."""
1073
+ tracker._set_slot(self.key, self.value)
1074
+
1075
+
1076
+ class Restarted(AlwaysEqualEventMixin):
1077
+ """Conversation should start over & history wiped.
1078
+
1079
+ Instead of deleting all events, this event can be used to reset the
1080
+ trackers state (e.g. ignoring any past user messages & resetting all
1081
+ the slots).
1082
+ """
1083
+
1084
+ type_name = "restart"
1085
+
1086
+ def __hash__(self) -> int:
1087
+ """Returns unique hash for event."""
1088
+ return hash(32143124312)
1089
+
1090
+ def as_story_string(self) -> Text:
1091
+ """Returns text representation of event."""
1092
+ return self.type_name
1093
+
1094
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1095
+ """Resets the tracker and triggers a followup `ActionSessionStart`."""
1096
+ tracker._reset()
1097
+ tracker.trigger_followup_action(ACTION_SESSION_START_NAME)
1098
+
1099
+
1100
+ class UserUtteranceReverted(AlwaysEqualEventMixin):
1101
+ """Bot reverts everything until before the most recent user message.
1102
+
1103
+ The bot will revert all events after the latest `UserUttered`, this
1104
+ also means that the last event on the tracker is usually `action_listen`
1105
+ and the bot is waiting for a new user message.
1106
+ """
1107
+
1108
+ type_name = "rewind"
1109
+
1110
+ def __hash__(self) -> int:
1111
+ """Returns unique hash for event."""
1112
+ return hash(32143124315)
1113
+
1114
+ def as_story_string(self) -> Text:
1115
+ """Returns text representation of event."""
1116
+ return self.type_name
1117
+
1118
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1119
+ """Applies event to current conversation state."""
1120
+ tracker._reset()
1121
+ tracker.replay_events()
1122
+
1123
+
1124
+ class AllSlotsReset(AlwaysEqualEventMixin):
1125
+ """All Slots are reset to their initial values.
1126
+
1127
+ If you want to keep the dialogue history and only want to reset the
1128
+ slots, you can use this event to set all the slots to their initial
1129
+ values.
1130
+ """
1131
+
1132
+ type_name = "reset_slots"
1133
+
1134
+ def __hash__(self) -> int:
1135
+ """Returns unique hash for event."""
1136
+ return hash(32143124316)
1137
+
1138
+ def as_story_string(self) -> Text:
1139
+ """Returns text representation of event."""
1140
+ return self.type_name
1141
+
1142
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1143
+ """Applies event to current conversation state."""
1144
+ tracker._reset_slots()
1145
+
1146
+
1147
+ class DialogueStackUpdated(Event):
1148
+ """Update the stack of a conversation."""
1149
+
1150
+ type_name = "stack"
1151
+
1152
+ def __init__(
1153
+ self,
1154
+ update: str,
1155
+ timestamp: Optional[float] = None,
1156
+ metadata: Optional[Dict[Text, Any]] = None,
1157
+ ) -> None:
1158
+ """Creates an event which updates the stack on a tracker.
1159
+
1160
+ Args:
1161
+ update: The update to the stack.
1162
+ timestamp: When the event was created.
1163
+ metadata: Additional event metadata.
1164
+ """
1165
+ self.update = update
1166
+ super().__init__(timestamp, metadata)
1167
+
1168
+ def __hash__(self) -> int:
1169
+ """Returns unique hash for event."""
1170
+ return hash(self.update)
1171
+
1172
+ def __eq__(self, other: Any) -> bool:
1173
+ """Compares object with other object."""
1174
+ if not isinstance(other, DialogueStackUpdated):
1175
+ return NotImplemented
1176
+
1177
+ return self.update == other.update
1178
+
1179
+ def __str__(self) -> Text:
1180
+ """Returns text representation of event."""
1181
+ return f"DialogueStackUpdate(update: {self.update})"
1182
+
1183
+ def __repr__(self) -> Text:
1184
+ """Returns text representation of event for debugging."""
1185
+ return f'DialogueStackUpdate("""{self.update}""")'
1186
+
1187
+ def as_story_string(self) -> Text:
1188
+ """Returns text representation of event."""
1189
+ props = json.dumps({"update": self.update})
1190
+ return f"{self.type_name}{props}"
1191
+
1192
+ @classmethod
1193
+ def _from_story_string(
1194
+ cls, parameters: Dict[Text, Any]
1195
+ ) -> Optional[List["DialogueStackUpdated"]]:
1196
+
1197
+ return [
1198
+ DialogueStackUpdated(
1199
+ parameters.get("update"),
1200
+ parameters.get("timestamp"),
1201
+ parameters.get("metadata"),
1202
+ )
1203
+ ]
1204
+
1205
+ def as_dict(self) -> Dict[Text, Any]:
1206
+ """Returns serialized event."""
1207
+ d = super().as_dict()
1208
+ d.update({"update": self.update})
1209
+ return d
1210
+
1211
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1212
+ """Applies event to current conversation state."""
1213
+ tracker.apply_stack_update(self.update)
1214
+
1215
+ def update_as_json(self) -> List[Dict[Text, Any]]:
1216
+ """Return the update as a JsonPatch object."""
1217
+ return json.loads(self.update)
1218
+
1219
+
1220
+ class ReminderScheduled(Event):
1221
+ """Schedules the asynchronous triggering of a user intent at a given time.
1222
+
1223
+ The triggered intent can include entities if needed.
1224
+ """
1225
+
1226
+ type_name = "reminder"
1227
+
1228
+ def __init__(
1229
+ self,
1230
+ intent: Text,
1231
+ trigger_date_time: datetime,
1232
+ entities: Optional[List[Dict]] = None,
1233
+ name: Optional[Text] = None,
1234
+ kill_on_user_message: bool = True,
1235
+ timestamp: Optional[float] = None,
1236
+ metadata: Optional[Dict[Text, Any]] = None,
1237
+ ) -> None:
1238
+ """Creates the reminder.
1239
+
1240
+ Args:
1241
+ intent: Name of the intent to be triggered.
1242
+ trigger_date_time: Date at which the execution of the action
1243
+ should be triggered (either utc or with tz).
1244
+ name: ID of the reminder. If there are multiple reminders with
1245
+ the same id only the last will be run.
1246
+ entities: Entities that should be supplied together with the
1247
+ triggered intent.
1248
+ kill_on_user_message: ``True`` means a user message before the
1249
+ trigger date will abort the reminder.
1250
+ timestamp: Creation date of the event.
1251
+ metadata: Optional event metadata.
1252
+ """
1253
+ self.intent = intent
1254
+ self.entities = entities
1255
+ self.trigger_date_time = trigger_date_time
1256
+ self.kill_on_user_message = kill_on_user_message
1257
+ self.name = name if name is not None else str(uuid.uuid1())
1258
+ super().__init__(timestamp, metadata)
1259
+
1260
+ def __hash__(self) -> int:
1261
+ """Returns unique hash for event."""
1262
+ return hash(
1263
+ (
1264
+ self.intent,
1265
+ self.entities,
1266
+ self.trigger_date_time.isoformat(),
1267
+ self.kill_on_user_message,
1268
+ self.name,
1269
+ )
1270
+ )
1271
+
1272
+ def __eq__(self, other: Any) -> bool:
1273
+ """Compares object with other object."""
1274
+ if not isinstance(other, ReminderScheduled):
1275
+ return NotImplemented
1276
+
1277
+ return self.name == other.name
1278
+
1279
+ def __str__(self) -> Text:
1280
+ """Returns text representation of event."""
1281
+ return (
1282
+ f"ReminderScheduled(intent: {self.intent}, "
1283
+ f"trigger_date: {self.trigger_date_time}, "
1284
+ f"entities: {self.entities}, name: {self.name})"
1285
+ )
1286
+
1287
+ def scheduled_job_name(self, sender_id: Text) -> Text:
1288
+ return (
1289
+ f"[{hash(self.name)},{hash(self.intent)},{hash(str(self.entities))}]"
1290
+ f"{ACTION_NAME_SENDER_ID_CONNECTOR_STR}"
1291
+ f"{sender_id}"
1292
+ )
1293
+
1294
+ def _properties(self) -> Dict[Text, Any]:
1295
+ return {
1296
+ "intent": self.intent,
1297
+ "date_time": self.trigger_date_time.isoformat(),
1298
+ "entities": self.entities,
1299
+ "name": self.name,
1300
+ "kill_on_user_msg": self.kill_on_user_message,
1301
+ }
1302
+
1303
+ def as_story_string(self) -> Text:
1304
+ """Returns text representation of event."""
1305
+ props = json.dumps(self._properties())
1306
+ return f"{self.type_name}{props}"
1307
+
1308
+ def as_dict(self) -> Dict[Text, Any]:
1309
+ """Returns serialized event."""
1310
+ d = super().as_dict()
1311
+ d.update(self._properties())
1312
+ return d
1313
+
1314
+ @classmethod
1315
+ def _from_story_string(
1316
+ cls, parameters: Dict[Text, Any]
1317
+ ) -> Optional[List["ReminderScheduled"]]:
1318
+
1319
+ trigger_date_time = parser.parse(parameters.get("date_time"))
1320
+
1321
+ return [
1322
+ ReminderScheduled(
1323
+ parameters.get("intent"),
1324
+ trigger_date_time,
1325
+ parameters.get("entities"),
1326
+ name=parameters.get("name"),
1327
+ kill_on_user_message=parameters.get("kill_on_user_msg", True),
1328
+ timestamp=parameters.get("timestamp"),
1329
+ metadata=parameters.get("metadata"),
1330
+ )
1331
+ ]
1332
+
1333
+
1334
+ class ReminderCancelled(Event):
1335
+ """Cancel certain jobs."""
1336
+
1337
+ type_name = "cancel_reminder"
1338
+
1339
+ def __init__(
1340
+ self,
1341
+ name: Optional[Text] = None,
1342
+ intent: Optional[Text] = None,
1343
+ entities: Optional[List[Dict]] = None,
1344
+ timestamp: Optional[float] = None,
1345
+ metadata: Optional[Dict[Text, Any]] = None,
1346
+ ) -> None:
1347
+ """Creates a ReminderCancelled event.
1348
+
1349
+ If all arguments are `None`, this will cancel all reminders.
1350
+ are to be cancelled. If no arguments are supplied, this will cancel all
1351
+ reminders.
1352
+
1353
+ Args:
1354
+ name: Name of the reminder to be cancelled.
1355
+ intent: Intent name that is to be used to identify the reminders to be
1356
+ cancelled.
1357
+ entities: Entities that are to be used to identify the reminders to be
1358
+ cancelled.
1359
+ timestamp: Optional timestamp.
1360
+ metadata: Optional event metadata.
1361
+ """
1362
+ self.name = name
1363
+ self.intent = intent
1364
+ self.entities = entities
1365
+ super().__init__(timestamp, metadata)
1366
+
1367
+ def __hash__(self) -> int:
1368
+ """Returns unique hash for event."""
1369
+ return hash((self.name, self.intent, str(self.entities)))
1370
+
1371
+ def __eq__(self, other: Any) -> bool:
1372
+ """Compares object with other object."""
1373
+ if not isinstance(other, ReminderCancelled):
1374
+ return NotImplemented
1375
+
1376
+ return hash(self) == hash(other)
1377
+
1378
+ def __str__(self) -> Text:
1379
+ """Returns text representation of event."""
1380
+ return (
1381
+ f"ReminderCancelled(name: {self.name}, intent: {self.intent}, "
1382
+ f"entities: {self.entities})"
1383
+ )
1384
+
1385
+ def cancels_job_with_name(self, job_name: Text, sender_id: Text) -> bool:
1386
+ """Determines if this event should cancel the job with the given name.
1387
+
1388
+ Args:
1389
+ job_name: Name of the job to be tested.
1390
+ sender_id: The `sender_id` of the tracker.
1391
+
1392
+ Returns:
1393
+ `True`, if this `ReminderCancelled` event should cancel the job with the
1394
+ given name, and `False` otherwise.
1395
+ """
1396
+ match = re.match(
1397
+ rf"^\[([\d\-]*),([\d\-]*),([\d\-]*)\]"
1398
+ rf"({re.escape(ACTION_NAME_SENDER_ID_CONNECTOR_STR)}"
1399
+ rf"{re.escape(sender_id)})",
1400
+ job_name,
1401
+ )
1402
+ if not match:
1403
+ return False
1404
+ name_hash, intent_hash, entities_hash = match.group(1, 2, 3)
1405
+
1406
+ # Cancel everything unless names/intents/entities are given to
1407
+ # narrow it down.
1408
+ return (
1409
+ ((not self.name) or self._matches_name_hash(name_hash))
1410
+ and ((not self.intent) or self._matches_intent_hash(intent_hash))
1411
+ and ((not self.entities) or self._matches_entities_hash(entities_hash))
1412
+ )
1413
+
1414
+ def _matches_name_hash(self, name_hash: Text) -> bool:
1415
+ return str(hash(self.name)) == name_hash
1416
+
1417
+ def _matches_intent_hash(self, intent_hash: Text) -> bool:
1418
+ return str(hash(self.intent)) == intent_hash
1419
+
1420
+ def _matches_entities_hash(self, entities_hash: Text) -> bool:
1421
+ return str(hash(str(self.entities))) == entities_hash
1422
+
1423
+ def as_story_string(self) -> Text:
1424
+ """Returns text representation of event."""
1425
+ props = json.dumps(
1426
+ {"name": self.name, "intent": self.intent, "entities": self.entities}
1427
+ )
1428
+ return f"{self.type_name}{props}"
1429
+
1430
+ @classmethod
1431
+ def _from_story_string(
1432
+ cls, parameters: Dict[Text, Any]
1433
+ ) -> Optional[List["ReminderCancelled"]]:
1434
+ return [
1435
+ ReminderCancelled(
1436
+ parameters.get("name"),
1437
+ parameters.get("intent"),
1438
+ parameters.get("entities"),
1439
+ timestamp=parameters.get("timestamp"),
1440
+ metadata=parameters.get("metadata"),
1441
+ )
1442
+ ]
1443
+
1444
+
1445
+ class ActionReverted(AlwaysEqualEventMixin):
1446
+ """Bot undoes its last action.
1447
+
1448
+ The bot reverts everything until before the most recent action.
1449
+ This includes the action itself, as well as any events that
1450
+ action created, like set slot events - the bot will now
1451
+ predict a new action using the state before the most recent
1452
+ action.
1453
+ """
1454
+
1455
+ type_name = "undo"
1456
+
1457
+ def __hash__(self) -> int:
1458
+ """Returns unique hash for event."""
1459
+ return hash(32143124318)
1460
+
1461
+ def as_story_string(self) -> Text:
1462
+ """Returns text representation of event."""
1463
+ return self.type_name
1464
+
1465
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1466
+ """Applies event to current conversation state."""
1467
+ tracker._reset()
1468
+ tracker.replay_events()
1469
+
1470
+
1471
+ class StoryExported(Event):
1472
+ """Story should get dumped to a file."""
1473
+
1474
+ type_name = "export"
1475
+
1476
+ def __init__(
1477
+ self,
1478
+ path: Optional[Text] = None,
1479
+ timestamp: Optional[float] = None,
1480
+ metadata: Optional[Dict[Text, Any]] = None,
1481
+ ) -> None:
1482
+ """Creates event about story exporting.
1483
+
1484
+ Args:
1485
+ path: Path to which story was exported to.
1486
+ timestamp: When the event was created.
1487
+ metadata: Additional event metadata.
1488
+ """
1489
+ self.path = path
1490
+ super().__init__(timestamp, metadata)
1491
+
1492
+ def __hash__(self) -> int:
1493
+ """Returns unique hash for event."""
1494
+ return hash(32143124319)
1495
+
1496
+ @classmethod
1497
+ def _from_story_string(
1498
+ cls, parameters: Dict[Text, Any]
1499
+ ) -> Optional[List["StoryExported"]]:
1500
+ return [
1501
+ StoryExported(
1502
+ parameters.get("path"),
1503
+ parameters.get("timestamp"),
1504
+ parameters.get("metadata"),
1505
+ )
1506
+ ]
1507
+
1508
+ def as_story_string(self) -> Text:
1509
+ """Returns text representation of event."""
1510
+ return self.type_name
1511
+
1512
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1513
+ """Applies event to current conversation state."""
1514
+ if self.path:
1515
+ tracker.export_stories_to_file(self.path)
1516
+
1517
+ def __eq__(self, other: Any) -> bool:
1518
+ """Compares object with other object."""
1519
+ if not isinstance(other, StoryExported):
1520
+ return NotImplemented
1521
+
1522
+ return self.path == other.path
1523
+
1524
+
1525
+ class FollowupAction(Event):
1526
+ """Enqueue a followup action."""
1527
+
1528
+ type_name = "followup"
1529
+
1530
+ def __init__(
1531
+ self,
1532
+ name: Text,
1533
+ timestamp: Optional[float] = None,
1534
+ metadata: Optional[Dict[Text, Any]] = None,
1535
+ ) -> None:
1536
+ """Creates an event which forces the model to run a certain action next.
1537
+
1538
+ Args:
1539
+ name: Name of the action to run.
1540
+ timestamp: When the event was created.
1541
+ metadata: Additional event metadata.
1542
+ """
1543
+ self.action_name = name
1544
+ super().__init__(timestamp, metadata)
1545
+
1546
+ def __hash__(self) -> int:
1547
+ """Returns unique hash for event."""
1548
+ return hash(self.action_name)
1549
+
1550
+ def __eq__(self, other: Any) -> bool:
1551
+ """Compares object with other object."""
1552
+ if not isinstance(other, FollowupAction):
1553
+ return NotImplemented
1554
+
1555
+ return self.action_name == other.action_name
1556
+
1557
+ def __str__(self) -> Text:
1558
+ """Returns text representation of event."""
1559
+ return f"FollowupAction(action: {self.action_name})"
1560
+
1561
+ def as_story_string(self) -> Text:
1562
+ """Returns text representation of event."""
1563
+ props = json.dumps({"name": self.action_name})
1564
+ return f"{self.type_name}{props}"
1565
+
1566
+ @classmethod
1567
+ def _from_story_string(
1568
+ cls, parameters: Dict[Text, Any]
1569
+ ) -> Optional[List["FollowupAction"]]:
1570
+
1571
+ return [
1572
+ FollowupAction(
1573
+ parameters.get("name"),
1574
+ parameters.get("timestamp"),
1575
+ parameters.get("metadata"),
1576
+ )
1577
+ ]
1578
+
1579
+ def as_dict(self) -> Dict[Text, Any]:
1580
+ """Returns serialized event."""
1581
+ d = super().as_dict()
1582
+ d.update({"name": self.action_name})
1583
+ return d
1584
+
1585
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1586
+ """Applies event to current conversation state."""
1587
+ tracker.trigger_followup_action(self.action_name)
1588
+
1589
+
1590
+ class ConversationPaused(AlwaysEqualEventMixin):
1591
+ """Ignore messages from the user to let a human take over.
1592
+
1593
+ As a side effect the `Tracker`'s `paused` attribute will
1594
+ be set to `True`.
1595
+ """
1596
+
1597
+ type_name = "pause"
1598
+
1599
+ def __hash__(self) -> int:
1600
+ """Returns unique hash for event."""
1601
+ return hash(32143124313)
1602
+
1603
+ def as_story_string(self) -> Text:
1604
+ """Returns text representation of event."""
1605
+ return str(self)
1606
+
1607
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1608
+ """Applies event to current conversation state."""
1609
+ tracker._paused = True
1610
+
1611
+
1612
+ class ConversationResumed(AlwaysEqualEventMixin):
1613
+ """Bot takes over conversation.
1614
+
1615
+ Inverse of `PauseConversation`. As a side effect the `Tracker`'s
1616
+ `paused` attribute will be set to `False`.
1617
+ """
1618
+
1619
+ type_name = "resume"
1620
+
1621
+ def __hash__(self) -> int:
1622
+ """Returns unique hash for event."""
1623
+ return hash(32143124314)
1624
+
1625
+ def as_story_string(self) -> Text:
1626
+ """Returns text representation of event."""
1627
+ return self.type_name
1628
+
1629
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1630
+ """Applies event to current conversation state."""
1631
+ tracker._paused = False
1632
+
1633
+
1634
+ class ActionExecuted(Event):
1635
+ """An operation describes an action taken + its result.
1636
+
1637
+ It comprises an action and a list of events. operations will be appended
1638
+ to the latest `Turn`` in `Tracker.turns`.
1639
+ """
1640
+
1641
+ type_name = "action"
1642
+
1643
+ def __init__(
1644
+ self,
1645
+ action_name: Optional[Text] = None,
1646
+ policy: Optional[Text] = None,
1647
+ confidence: Optional[float] = None,
1648
+ timestamp: Optional[float] = None,
1649
+ metadata: Optional[Dict] = None,
1650
+ action_text: Optional[Text] = None,
1651
+ hide_rule_turn: bool = False,
1652
+ ) -> None:
1653
+ """Creates event for a successful event execution.
1654
+
1655
+ Args:
1656
+ action_name: Name of the action which was executed. `None` if it was an
1657
+ end-to-end prediction.
1658
+ policy: Policy which predicted action.
1659
+ confidence: Confidence with which policy predicted action.
1660
+ timestamp: When the event was created.
1661
+ metadata: Additional event metadata.
1662
+ action_text: In case it's an end-to-end action prediction, the text which
1663
+ was predicted.
1664
+ hide_rule_turn: If `True`, this action should be hidden in the dialogue
1665
+ history created for ML-based policies.
1666
+ """
1667
+ self.action_name = action_name
1668
+ self.policy = policy
1669
+ self.confidence = confidence
1670
+ self.unpredictable = False
1671
+ self.action_text = action_text
1672
+ self.hide_rule_turn = hide_rule_turn
1673
+
1674
+ if self.action_name is None and self.action_text is None:
1675
+ raise ValueError(
1676
+ "Both the name of the action and the end-to-end "
1677
+ "predicted text are missing. "
1678
+ "The `ActionExecuted` event cannot be initialised."
1679
+ )
1680
+
1681
+ super().__init__(timestamp, metadata)
1682
+
1683
+ def __members__(self) -> Tuple[Optional[Text], Optional[Text], Text]:
1684
+ meta_no_nones = {k: v for k, v in self.metadata.items() if v is not None}
1685
+ return (self.action_name, self.action_text, jsonpickle.encode(meta_no_nones))
1686
+
1687
+ def __repr__(self) -> Text:
1688
+ """Returns event as string for debugging."""
1689
+ return "ActionExecuted(action: {}, policy: {}, confidence: {})".format(
1690
+ self.action_name, self.policy, self.confidence
1691
+ )
1692
+
1693
+ def __str__(self) -> Text:
1694
+ """Returns event as human readable string."""
1695
+ return str(self.action_name) or str(self.action_text)
1696
+
1697
+ def __hash__(self) -> int:
1698
+ """Returns unique hash for event."""
1699
+ return hash(self.__members__())
1700
+
1701
+ def __eq__(self, other: Any) -> bool:
1702
+ """Compares object with other object."""
1703
+ if not isinstance(other, ActionExecuted):
1704
+ return NotImplemented
1705
+
1706
+ return self.__members__() == other.__members__()
1707
+
1708
+ def as_story_string(self) -> Optional[Text]:
1709
+ """Returns event in Markdown format."""
1710
+ if self.action_text:
1711
+ raise UnsupportedFeatureException(
1712
+ f"Printing end-to-end bot utterances is not supported in the "
1713
+ f"Markdown training format. Please use the YAML training data format "
1714
+ f"instead. Please see {DOCS_URL_TRAINING_DATA} for more information."
1715
+ )
1716
+
1717
+ return self.action_name
1718
+
1719
+ @classmethod
1720
+ def _from_story_string(
1721
+ cls, parameters: Dict[Text, Any]
1722
+ ) -> Optional[List["ActionExecuted"]]:
1723
+ return [
1724
+ ActionExecuted(
1725
+ parameters.get("name"),
1726
+ parameters.get("policy"),
1727
+ parameters.get("confidence"),
1728
+ parameters.get("timestamp"),
1729
+ parameters.get("metadata"),
1730
+ parameters.get("action_text"),
1731
+ parameters.get("hide_rule_turn", False),
1732
+ )
1733
+ ]
1734
+
1735
+ def as_dict(self) -> Dict[Text, Any]:
1736
+ """Returns serialized event."""
1737
+ d = super().as_dict()
1738
+ d.update(
1739
+ {
1740
+ "name": self.action_name,
1741
+ "policy": self.policy,
1742
+ "confidence": self.confidence,
1743
+ "action_text": self.action_text,
1744
+ "hide_rule_turn": self.hide_rule_turn,
1745
+ }
1746
+ )
1747
+ return d
1748
+
1749
+ def as_sub_state(self) -> Dict[Text, Text]:
1750
+ """Turns ActionExecuted into a dictionary containing action name or action text.
1751
+
1752
+ One action cannot have both set at the same time
1753
+
1754
+ Returns:
1755
+ a dictionary containing action name or action text with the corresponding
1756
+ key.
1757
+ """
1758
+ if self.action_name:
1759
+ return {ACTION_NAME: self.action_name}
1760
+ else:
1761
+ # FIXME: we should define the type better here, and require either
1762
+ # `action_name` or `action_text`
1763
+ return {ACTION_TEXT: cast(Text, self.action_text)}
1764
+
1765
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1766
+ """Applies event to current conversation state."""
1767
+ tracker.set_latest_action(self.as_sub_state())
1768
+ tracker.clear_followup_action()
1769
+
1770
+
1771
+ class AgentUttered(SkipEventInMDStoryMixin):
1772
+ """The agent has said something to the user.
1773
+
1774
+ This class is not used in the story training as it is contained in the
1775
+ ``ActionExecuted`` class. An entry is made in the ``Tracker``.
1776
+ """
1777
+
1778
+ type_name = "agent"
1779
+
1780
+ def __init__(
1781
+ self,
1782
+ text: Optional[Text] = None,
1783
+ data: Optional[Any] = None,
1784
+ timestamp: Optional[float] = None,
1785
+ metadata: Optional[Dict[Text, Any]] = None,
1786
+ ) -> None:
1787
+ """See docstring of `BotUttered`."""
1788
+ self.text = text
1789
+ self.data = data
1790
+ super().__init__(timestamp, metadata)
1791
+
1792
+ def __hash__(self) -> int:
1793
+ """Returns unique hash for event."""
1794
+ return hash((self.text, jsonpickle.encode(self.data)))
1795
+
1796
+ def __eq__(self, other: Any) -> bool:
1797
+ """Compares object with other object."""
1798
+ if not isinstance(other, AgentUttered):
1799
+ return NotImplemented
1800
+
1801
+ return (self.text, jsonpickle.encode(self.data)) == (
1802
+ other.text,
1803
+ jsonpickle.encode(other.data),
1804
+ )
1805
+
1806
+ def __str__(self) -> Text:
1807
+ """Returns text representation of event."""
1808
+ return "AgentUttered(text: {}, data: {})".format(
1809
+ self.text, json.dumps(self.data)
1810
+ )
1811
+
1812
+ def as_dict(self) -> Dict[Text, Any]:
1813
+ """Returns serialized event."""
1814
+ d = super().as_dict()
1815
+ d.update({"text": self.text, "data": self.data})
1816
+ return d
1817
+
1818
+ @classmethod
1819
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "AgentUttered":
1820
+ try:
1821
+ return AgentUttered(
1822
+ parameters.get("text"),
1823
+ parameters.get("data"),
1824
+ parameters.get("timestamp"),
1825
+ parameters.get("metadata"),
1826
+ )
1827
+ except KeyError as e:
1828
+ raise ValueError(f"Failed to parse agent uttered event. {e}")
1829
+
1830
+
1831
+ class ActiveLoop(Event):
1832
+ """If `name` is given: activates a loop with `name` else deactivates active loop."""
1833
+
1834
+ type_name = "active_loop"
1835
+
1836
+ def __init__(
1837
+ self,
1838
+ name: Optional[Text],
1839
+ timestamp: Optional[float] = None,
1840
+ metadata: Optional[Dict[Text, Any]] = None,
1841
+ ) -> None:
1842
+ """Creates event for active loop.
1843
+
1844
+ Args:
1845
+ name: Name of activated loop or `None` if current loop is deactivated.
1846
+ timestamp: When the event was created.
1847
+ metadata: Additional event metadata.
1848
+ """
1849
+ self.name = name
1850
+ super().__init__(timestamp, metadata)
1851
+
1852
+ def __str__(self) -> Text:
1853
+ """Returns text representation of event."""
1854
+ return f"Loop({self.name})"
1855
+
1856
+ def __repr__(self) -> Text:
1857
+ """Returns event as string for debugging."""
1858
+ return f"ActiveLoop({self.name}, {self.timestamp}, {self.metadata})"
1859
+
1860
+ def __hash__(self) -> int:
1861
+ """Returns unique hash for event."""
1862
+ return hash(self.name)
1863
+
1864
+ def __eq__(self, other: Any) -> bool:
1865
+ """Compares object with other object."""
1866
+ if not isinstance(other, ActiveLoop):
1867
+ return NotImplemented
1868
+
1869
+ return self.name == other.name
1870
+
1871
+ def as_story_string(self) -> Text:
1872
+ """Returns text representation of event."""
1873
+ props = json.dumps({LOOP_NAME: self.name})
1874
+ return f"{ActiveLoop.type_name}{props}"
1875
+
1876
+ @classmethod
1877
+ def _from_story_string(cls, parameters: Dict[Text, Any]) -> List["ActiveLoop"]:
1878
+ """Called to convert a parsed story line into an event."""
1879
+ return [
1880
+ ActiveLoop(
1881
+ parameters.get(LOOP_NAME),
1882
+ parameters.get("timestamp"),
1883
+ parameters.get("metadata"),
1884
+ )
1885
+ ]
1886
+
1887
+ def as_dict(self) -> Dict[Text, Any]:
1888
+ """Returns serialized event."""
1889
+ d = super().as_dict()
1890
+ d.update({LOOP_NAME: self.name})
1891
+ return d
1892
+
1893
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1894
+ """Applies event to current conversation state."""
1895
+ tracker.change_loop_to(self.name)
1896
+
1897
+
1898
+ class LegacyForm(ActiveLoop):
1899
+ """Legacy handler of old `Form` events.
1900
+
1901
+ The `ActiveLoop` event used to be called `Form`. This class is there to handle old
1902
+ legacy events which were stored with the old type name `form`.
1903
+ """
1904
+
1905
+ type_name = "form"
1906
+
1907
+ def as_dict(self) -> Dict[Text, Any]:
1908
+ """Returns serialized event."""
1909
+ d = super().as_dict()
1910
+ # Dump old `Form` events as `ActiveLoop` events instead of keeping the old
1911
+ # event type.
1912
+ d["event"] = ActiveLoop.type_name
1913
+ return d
1914
+
1915
+ def fingerprint(self) -> Text:
1916
+ """Returns the hash of the event."""
1917
+ d = self.as_dict()
1918
+ # Revert event name to legacy subclass name to avoid different event types
1919
+ # having the same fingerprint.
1920
+ d["event"] = self.type_name
1921
+ del d["timestamp"]
1922
+ return rasa.shared.utils.io.get_dictionary_fingerprint(d)
1923
+
1924
+
1925
+ class LoopInterrupted(SkipEventInMDStoryMixin):
1926
+ """Event added by FormPolicy and RulePolicy.
1927
+
1928
+ Notifies form action whether or not to validate the user input.
1929
+ """
1930
+
1931
+ type_name = "loop_interrupted"
1932
+
1933
+ def __init__(
1934
+ self,
1935
+ is_interrupted: bool,
1936
+ timestamp: Optional[float] = None,
1937
+ metadata: Optional[Dict[Text, Any]] = None,
1938
+ ) -> None:
1939
+ """Event to notify that loop was interrupted.
1940
+
1941
+ This e.g. happens when a user is within a form, and is de-railing the
1942
+ form-filling by asking FAQs.
1943
+
1944
+ Args:
1945
+ is_interrupted: `True` if the loop execution was interrupted, and ML
1946
+ policies had to take over the last prediction.
1947
+ timestamp: When the event was created.
1948
+ metadata: Additional event metadata.
1949
+ """
1950
+ super().__init__(timestamp, metadata)
1951
+ self.is_interrupted = is_interrupted
1952
+
1953
+ def __str__(self) -> Text:
1954
+ """Returns text representation of event."""
1955
+ return f"{LoopInterrupted.__name__}({self.is_interrupted})"
1956
+
1957
+ def __hash__(self) -> int:
1958
+ """Returns unique hash for event."""
1959
+ return hash(self.is_interrupted)
1960
+
1961
+ def __eq__(self, other: Any) -> bool:
1962
+ """Compares object with other object."""
1963
+ if not isinstance(other, LoopInterrupted):
1964
+ return NotImplemented
1965
+
1966
+ return self.is_interrupted == other.is_interrupted
1967
+
1968
+ @classmethod
1969
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "LoopInterrupted":
1970
+ return LoopInterrupted(
1971
+ parameters.get(LOOP_INTERRUPTED, False),
1972
+ parameters.get("timestamp"),
1973
+ parameters.get("metadata"),
1974
+ )
1975
+
1976
+ def as_dict(self) -> Dict[Text, Any]:
1977
+ """Returns serialized event."""
1978
+ d = super().as_dict()
1979
+ d.update({LOOP_INTERRUPTED: self.is_interrupted})
1980
+ return d
1981
+
1982
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
1983
+ """Applies event to current conversation state."""
1984
+ tracker.interrupt_loop(self.is_interrupted)
1985
+
1986
+
1987
+ class LegacyFormValidation(LoopInterrupted):
1988
+ """Legacy handler of old `FormValidation` events.
1989
+
1990
+ The `LoopInterrupted` event used to be called `FormValidation`. This class is there
1991
+ to handle old legacy events which were stored with the old type name
1992
+ `form_validation`.
1993
+ """
1994
+
1995
+ type_name = "form_validation"
1996
+
1997
+ def __init__(
1998
+ self,
1999
+ validate: bool,
2000
+ timestamp: Optional[float] = None,
2001
+ metadata: Optional[Dict[Text, Any]] = None,
2002
+ ) -> None:
2003
+ """See parent class docstring."""
2004
+ # `validate = True` is the same as `interrupted = False`
2005
+ super().__init__(not validate, timestamp, metadata)
2006
+
2007
+ @classmethod
2008
+ def _from_parameters(cls, parameters: Dict) -> "LoopInterrupted":
2009
+ return LoopInterrupted(
2010
+ # `validate = True` means `is_interrupted = False`
2011
+ not parameters.get("validate", True),
2012
+ parameters.get("timestamp"),
2013
+ parameters.get("metadata"),
2014
+ )
2015
+
2016
+ def as_dict(self) -> Dict[Text, Any]:
2017
+ """Returns serialized event."""
2018
+ d = super().as_dict()
2019
+ # Dump old `Form` events as `ActiveLoop` events instead of keeping the old
2020
+ # event type.
2021
+ d["event"] = LoopInterrupted.type_name
2022
+ return d
2023
+
2024
+ def fingerprint(self) -> Text:
2025
+ """Returns hash of the event."""
2026
+ d = self.as_dict()
2027
+ # Revert event name to legacy subclass name to avoid different event types
2028
+ # having the same fingerprint.
2029
+ d["event"] = self.type_name
2030
+ del d["timestamp"]
2031
+ return rasa.shared.utils.io.get_dictionary_fingerprint(d)
2032
+
2033
+
2034
+ class ActionExecutionRejected(SkipEventInMDStoryMixin):
2035
+ """Notify Core that the execution of the action has been rejected."""
2036
+
2037
+ type_name = "action_execution_rejected"
2038
+
2039
+ def __init__(
2040
+ self,
2041
+ action_name: Text,
2042
+ policy: Optional[Text] = None,
2043
+ confidence: Optional[float] = None,
2044
+ timestamp: Optional[float] = None,
2045
+ metadata: Optional[Dict[Text, Any]] = None,
2046
+ ) -> None:
2047
+ """Creates event.
2048
+
2049
+ Args:
2050
+ action_name: Action which was rejected.
2051
+ policy: Policy which predicted the rejected action.
2052
+ confidence: Confidence with which the reject action was predicted.
2053
+ timestamp: When the event was created.
2054
+ metadata: Additional event metadata.
2055
+ """
2056
+ self.action_name = action_name
2057
+ self.policy = policy
2058
+ self.confidence = confidence
2059
+ super().__init__(timestamp, metadata)
2060
+
2061
+ def __str__(self) -> Text:
2062
+ """Returns text representation of event."""
2063
+ return (
2064
+ "ActionExecutionRejected("
2065
+ "action: {}, policy: {}, confidence: {})"
2066
+ "".format(self.action_name, self.policy, self.confidence)
2067
+ )
2068
+
2069
+ def __hash__(self) -> int:
2070
+ """Returns unique hash for event."""
2071
+ return hash(self.action_name)
2072
+
2073
+ def __eq__(self, other: Any) -> bool:
2074
+ """Compares object with other object."""
2075
+ if not isinstance(other, ActionExecutionRejected):
2076
+ return NotImplemented
2077
+
2078
+ return self.action_name == other.action_name
2079
+
2080
+ @classmethod
2081
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "ActionExecutionRejected":
2082
+ return ActionExecutionRejected(
2083
+ parameters.get("name"),
2084
+ parameters.get("policy"),
2085
+ parameters.get("confidence"),
2086
+ parameters.get("timestamp"),
2087
+ parameters.get("metadata"),
2088
+ )
2089
+
2090
+ def as_dict(self) -> Dict[Text, Any]:
2091
+ """Returns serialized event."""
2092
+ d = super().as_dict()
2093
+ d.update(
2094
+ {
2095
+ "name": self.action_name,
2096
+ "policy": self.policy,
2097
+ "confidence": self.confidence,
2098
+ }
2099
+ )
2100
+ return d
2101
+
2102
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
2103
+ """Applies event to current conversation state."""
2104
+ tracker.reject_action(self.action_name)
2105
+
2106
+
2107
+ class SessionStarted(AlwaysEqualEventMixin):
2108
+ """Mark the beginning of a new conversation session."""
2109
+
2110
+ type_name = "session_started"
2111
+
2112
+ def __hash__(self) -> int:
2113
+ """Returns unique hash for event."""
2114
+ return hash(32143124320)
2115
+
2116
+ def __repr__(self) -> Text:
2117
+ """Returns event as string for debugging."""
2118
+ return f"SessionStarted(type_name: {self.type_name})"
2119
+
2120
+ def __str__(self) -> Text:
2121
+ """Returns event as human-readable string."""
2122
+ return f"{self.__class__.__name__}({self.type_name})"
2123
+
2124
+ def as_story_string(self) -> None:
2125
+ """Skips representing event in stories."""
2126
+ logger.warning(
2127
+ f"'{self.type_name}' events cannot be serialised as story strings."
2128
+ )
2129
+
2130
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
2131
+ """Applies event to current conversation state."""
2132
+ # noinspection PyProtectedMember
2133
+ tracker._reset()
2134
+
2135
+
2136
+ class RoutingSessionEnded(AlwaysEqualEventMixin):
2137
+ """Mark the end of a routing session for coexistence."""
2138
+
2139
+ type_name = "routing_session_ended"
2140
+
2141
+ def __hash__(self) -> int:
2142
+ """Returns unique hash for event."""
2143
+ return hash(32143124434)
2144
+
2145
+ def __repr__(self) -> Text:
2146
+ """Returns event as string for debugging."""
2147
+ return f"RoutingSessionEnded(type_name: {self.type_name})"
2148
+
2149
+ def __str__(self) -> Text:
2150
+ """Returns event as human-readable string."""
2151
+ return f"{self.__class__.__name__}({self.type_name})"
2152
+
2153
+ def as_story_string(self) -> None:
2154
+ """Skips representing event in stories."""
2155
+ logger.warning(
2156
+ f"'{self.type_name}' events cannot be serialised as story strings."
2157
+ )
2158
+
2159
+ def apply_to(self, tracker: "DialogueStateTracker") -> None:
2160
+ """Applies event to current conversation state."""
2161
+ # noinspection PyProtectedMember
2162
+ tracker._reset(is_coexistence_reset=True)
2163
+
2164
+
2165
+ class FlowStarted(SkipEventInMDStoryMixin):
2166
+ """Mark the beginning of a new flow."""
2167
+
2168
+ type_name = "flow_started"
2169
+
2170
+ def __init__(
2171
+ self,
2172
+ flow_id: Text,
2173
+ timestamp: Optional[float] = None,
2174
+ metadata: Optional[Dict[Text, Any]] = None,
2175
+ ) -> None:
2176
+ """Creates event to start flow.
2177
+
2178
+ Args:
2179
+ flow_id: ID of the flow to be started.
2180
+ timestamp: When the event was created.
2181
+ metadata: Additional event metadata.
2182
+ """
2183
+ self.flow_id = flow_id
2184
+ super().__init__(timestamp, metadata)
2185
+
2186
+ def __repr__(self) -> Text:
2187
+ """Returns event as string for debugging."""
2188
+ return f"FlowStarted(flow: {self.flow_id})"
2189
+
2190
+ def __str__(self) -> Text:
2191
+ """Returns event as human-readable string."""
2192
+ return f"{self.__class__.__name__}({self.flow_id})"
2193
+
2194
+ def __hash__(self) -> int:
2195
+ """Returns unique hash for event."""
2196
+ return hash(self.flow_id)
2197
+
2198
+ def __eq__(self, other: Any) -> bool:
2199
+ """Compares object with other object."""
2200
+ if not isinstance(other, FlowStarted):
2201
+ return NotImplemented
2202
+
2203
+ return self.flow_id == other.flow_id
2204
+
2205
+ def as_dict(self) -> Dict[Text, Any]:
2206
+ """Returns serialized event."""
2207
+ serialized = super().as_dict()
2208
+ serialized.update({"flow_id": self.flow_id})
2209
+ return serialized
2210
+
2211
+ @classmethod
2212
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "FlowStarted":
2213
+ try:
2214
+ return FlowStarted(
2215
+ parameters.get("flow_id"),
2216
+ parameters.get("timestamp"),
2217
+ parameters.get("metadata"),
2218
+ )
2219
+ except KeyError as e:
2220
+ raise ValueError(f"Failed to parse flow_started event. {e}")
2221
+
2222
+
2223
+ class FlowInterrupted(SkipEventInMDStoryMixin):
2224
+ """Mark the interruption of a flow."""
2225
+
2226
+ type_name = "flow_interrupted"
2227
+
2228
+ def __init__(
2229
+ self,
2230
+ flow_id: Text,
2231
+ step_id: Text,
2232
+ timestamp: Optional[float] = None,
2233
+ metadata: Optional[Dict[Text, Any]] = None,
2234
+ ) -> None:
2235
+ """Creates event to interrupt a flow.
2236
+
2237
+ Args:
2238
+ flow_id: Name of the flow to be interrupted.
2239
+ step_id: ID of the flow step where the flow was interrupted.
2240
+ timestamp: When the event was created.
2241
+ metadata: Additional event metadata.
2242
+ """
2243
+ self.flow_id = flow_id
2244
+ self.step_id = step_id
2245
+ self.is_interrupted = True
2246
+ super().__init__(timestamp, metadata)
2247
+
2248
+ def __repr__(self) -> Text:
2249
+ """Returns event as string for debugging."""
2250
+ return f"FlowInterrupted(flow: {self.flow_id}, step_id: {self.step_id})"
2251
+
2252
+ def __str__(self) -> Text:
2253
+ """Returns event as human-readable string."""
2254
+ return f"{self.__class__.__name__}({self.flow_id}, {self.step_id})"
2255
+
2256
+ def __hash__(self) -> int:
2257
+ """Returns unique hash for event."""
2258
+ return hash(
2259
+ (
2260
+ self.flow_id,
2261
+ self.step_id,
2262
+ json.dumps({"is_interrupted": self.is_interrupted}),
2263
+ )
2264
+ )
2265
+
2266
+ def __eq__(self, other: Any) -> bool:
2267
+ """Compares object with other object."""
2268
+ if not isinstance(other, FlowInterrupted):
2269
+ return NotImplemented
2270
+
2271
+ return (self.flow_id, self.step_id) == (other.flow_id, other.step_id)
2272
+
2273
+ def as_dict(self) -> Dict[Text, Any]:
2274
+ """Returns serialized event."""
2275
+ serialized = super().as_dict()
2276
+ serialized.update(
2277
+ {
2278
+ "flow_id": self.flow_id,
2279
+ "step_id": self.step_id,
2280
+ }
2281
+ )
2282
+ return serialized
2283
+
2284
+ @classmethod
2285
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "FlowInterrupted":
2286
+ try:
2287
+ return FlowInterrupted(
2288
+ parameters.get("flow_id"),
2289
+ parameters.get("step_id"),
2290
+ parameters.get("timestamp"),
2291
+ parameters.get("metadata"),
2292
+ )
2293
+ except KeyError as e:
2294
+ raise ValueError(f"Failed to parse flow_interrupted event. {e}")
2295
+
2296
+
2297
+ class FlowResumed(SkipEventInMDStoryMixin):
2298
+ """Mark the resuming of a flow."""
2299
+
2300
+ type_name = "flow_resumed"
2301
+
2302
+ def __init__(
2303
+ self,
2304
+ flow_id: Text,
2305
+ step_id: Text,
2306
+ timestamp: Optional[float] = None,
2307
+ metadata: Optional[Dict[Text, Any]] = None,
2308
+ ) -> None:
2309
+ """Creates event to resume a flow.
2310
+
2311
+ Args:
2312
+ flow_id: ID of the flow to be resumed.
2313
+ step_id: ID of the flow step where the flow was resumed.
2314
+ timestamp: When the event was created.
2315
+ metadata: Additional event metadata.
2316
+ """
2317
+ self.flow_id = flow_id
2318
+ self.step_id = step_id
2319
+ self.is_resumed = True
2320
+ super().__init__(timestamp, metadata)
2321
+
2322
+ def __repr__(self) -> Text:
2323
+ """Returns event as string for debugging."""
2324
+ return f"FlowResumed(flow: {self.flow_id}, step_id: {self.step_id})"
2325
+
2326
+ def __str__(self) -> Text:
2327
+ """Returns event as human-readable string."""
2328
+ return f"{self.__class__.__name__}({self.flow_id}, {self.step_id})"
2329
+
2330
+ def __hash__(self) -> int:
2331
+ """Returns unique hash for event."""
2332
+ return hash(
2333
+ (self.flow_id, self.step_id, json.dumps({"is_resumed": self.is_resumed}))
2334
+ )
2335
+
2336
+ def __eq__(self, other: Any) -> bool:
2337
+ """Compares object with other object."""
2338
+ if not isinstance(other, FlowResumed):
2339
+ return NotImplemented
2340
+
2341
+ return (self.flow_id, self.step_id) == (other.flow_id, other.step_id)
2342
+
2343
+ def as_dict(self) -> Dict[Text, Any]:
2344
+ """Returns serialized event."""
2345
+ serialized = super().as_dict()
2346
+ serialized.update({"flow_id": self.flow_id, "step_id": self.step_id})
2347
+ return serialized
2348
+
2349
+ @classmethod
2350
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "FlowResumed":
2351
+ try:
2352
+ return FlowResumed(
2353
+ parameters.get("flow_id"),
2354
+ parameters.get("step_id"),
2355
+ parameters.get("timestamp"),
2356
+ parameters.get("metadata"),
2357
+ )
2358
+ except KeyError as e:
2359
+ raise ValueError(f"Failed to parse flow_resumed event. {e}")
2360
+
2361
+
2362
+ class FlowCompleted(SkipEventInMDStoryMixin):
2363
+ """Mark the completion of a flow."""
2364
+
2365
+ type_name = "flow_completed"
2366
+
2367
+ def __init__(
2368
+ self,
2369
+ flow_id: Text,
2370
+ step_id: Text,
2371
+ timestamp: Optional[float] = None,
2372
+ metadata: Optional[Dict[Text, Any]] = None,
2373
+ ) -> None:
2374
+ """Creates event to complete a flow.
2375
+
2376
+ Args:
2377
+ flow_id: ID of the flow to be completed.
2378
+ step_id: ID of the flow step where the flow was completed.
2379
+ timestamp: When the event was created.
2380
+ metadata: Additional event metadata.
2381
+ """
2382
+ self.flow_id = flow_id
2383
+ self.step_id = step_id
2384
+ self.is_completed = True
2385
+ super().__init__(timestamp, metadata)
2386
+
2387
+ def __repr__(self) -> Text:
2388
+ """Returns event as string for debugging."""
2389
+ return f"FlowCompleted(flow: {self.flow_id}, step_id: {self.step_id})"
2390
+
2391
+ def __str__(self) -> Text:
2392
+ """Returns event as human-readable string."""
2393
+ return f"{self.__class__.__name__}({self.flow_id}, {self.step_id})"
2394
+
2395
+ def __hash__(self) -> int:
2396
+ """Returns unique hash for event."""
2397
+ return hash(
2398
+ (
2399
+ self.flow_id,
2400
+ self.step_id,
2401
+ json.dumps({"is_completed": self.is_completed}),
2402
+ )
2403
+ )
2404
+
2405
+ def __eq__(self, other: Any) -> bool:
2406
+ """Compares object with other object."""
2407
+ if not isinstance(other, FlowCompleted):
2408
+ return NotImplemented
2409
+
2410
+ return (self.flow_id, self.step_id) == (other.flow_id, other.step_id)
2411
+
2412
+ def as_dict(self) -> Dict[Text, Any]:
2413
+ """Returns serialized event."""
2414
+ serialized = super().as_dict()
2415
+ serialized.update(
2416
+ {
2417
+ "flow_id": self.flow_id,
2418
+ "step_id": self.step_id,
2419
+ }
2420
+ )
2421
+ return serialized
2422
+
2423
+ @classmethod
2424
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "FlowCompleted":
2425
+ try:
2426
+ return FlowCompleted(
2427
+ parameters.get("flow_id"),
2428
+ parameters.get("step_id"),
2429
+ parameters.get("timestamp"),
2430
+ parameters.get("metadata"),
2431
+ )
2432
+ except KeyError as e:
2433
+ raise ValueError(f"Failed to parse flow_completed event. {e}")
2434
+
2435
+
2436
+ class FlowCancelled(SkipEventInMDStoryMixin):
2437
+ """Mark the cancellation of a flow."""
2438
+
2439
+ type_name = "flow_cancelled"
2440
+
2441
+ def __init__(
2442
+ self,
2443
+ flow_id: Text,
2444
+ step_id: Text,
2445
+ timestamp: Optional[float] = None,
2446
+ metadata: Optional[Dict[Text, Any]] = None,
2447
+ ) -> None:
2448
+ """Creates event to cancel a flow.
2449
+
2450
+ Args:
2451
+ flow_id: ID of the flow which was cancelled.
2452
+ step_id: ID of the flow step where the flow was cancelled.
2453
+ timestamp: When the event was created.
2454
+ metadata: Additional event metadata.
2455
+ """
2456
+ self.flow_id = flow_id
2457
+ self.step_id = step_id
2458
+ self.is_cancelled = True
2459
+ super().__init__(timestamp, metadata)
2460
+
2461
+ def __repr__(self) -> Text:
2462
+ """Returns event as string for debugging."""
2463
+ return f"FlowCancelled(flow: {self.flow_id}, step_id: {self.step_id})"
2464
+
2465
+ def __str__(self) -> Text:
2466
+ """Returns event as human-readable string."""
2467
+ return f"{self.__class__.__name__}({self.flow_id}, {self.step_id})"
2468
+
2469
+ def __hash__(self) -> int:
2470
+ """Returns unique hash for event."""
2471
+ return hash(
2472
+ (
2473
+ self.flow_id,
2474
+ self.step_id,
2475
+ json.dumps({"is_cancelled": self.is_cancelled}),
2476
+ )
2477
+ )
2478
+
2479
+ def __eq__(self, other: Any) -> bool:
2480
+ """Compares object with other object."""
2481
+ if not isinstance(other, FlowCancelled):
2482
+ return NotImplemented
2483
+
2484
+ return (self.flow_id, self.step_id) == (other.flow_id, other.step_id)
2485
+
2486
+ def as_dict(self) -> Dict[Text, Any]:
2487
+ """Returns serialized event."""
2488
+ serialized = super().as_dict()
2489
+ serialized.update(
2490
+ {
2491
+ "flow_id": self.flow_id,
2492
+ "step_id": self.step_id,
2493
+ }
2494
+ )
2495
+ return serialized
2496
+
2497
+ @classmethod
2498
+ def _from_parameters(cls, parameters: Dict[Text, Any]) -> "FlowCancelled":
2499
+ try:
2500
+ return FlowCancelled(
2501
+ parameters.get("flow_id"),
2502
+ parameters.get("step_id"),
2503
+ parameters.get("timestamp"),
2504
+ parameters.get("metadata"),
2505
+ )
2506
+ except KeyError as e:
2507
+ raise ValueError(f"Failed to parse flow_cancelled event. {e}")