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