rasa-pro 3.8.16__py3-none-any.whl

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

Potentially problematic release.


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

Files changed (644) hide show
  1. README.md +380 -0
  2. rasa/__init__.py +10 -0
  3. rasa/__main__.py +151 -0
  4. rasa/anonymization/__init__.py +2 -0
  5. rasa/anonymization/anonymisation_rule_yaml_reader.py +91 -0
  6. rasa/anonymization/anonymization_pipeline.py +287 -0
  7. rasa/anonymization/anonymization_rule_executor.py +260 -0
  8. rasa/anonymization/anonymization_rule_orchestrator.py +120 -0
  9. rasa/anonymization/schemas/config.yml +47 -0
  10. rasa/anonymization/utils.py +117 -0
  11. rasa/api.py +146 -0
  12. rasa/cli/__init__.py +5 -0
  13. rasa/cli/arguments/__init__.py +0 -0
  14. rasa/cli/arguments/data.py +81 -0
  15. rasa/cli/arguments/default_arguments.py +165 -0
  16. rasa/cli/arguments/evaluate.py +65 -0
  17. rasa/cli/arguments/export.py +51 -0
  18. rasa/cli/arguments/interactive.py +74 -0
  19. rasa/cli/arguments/run.py +204 -0
  20. rasa/cli/arguments/shell.py +13 -0
  21. rasa/cli/arguments/test.py +211 -0
  22. rasa/cli/arguments/train.py +263 -0
  23. rasa/cli/arguments/visualize.py +34 -0
  24. rasa/cli/arguments/x.py +30 -0
  25. rasa/cli/data.py +292 -0
  26. rasa/cli/e2e_test.py +566 -0
  27. rasa/cli/evaluate.py +222 -0
  28. rasa/cli/export.py +251 -0
  29. rasa/cli/inspect.py +63 -0
  30. rasa/cli/interactive.py +164 -0
  31. rasa/cli/license.py +65 -0
  32. rasa/cli/markers.py +78 -0
  33. rasa/cli/project_templates/__init__.py +0 -0
  34. rasa/cli/project_templates/calm/actions/__init__.py +0 -0
  35. rasa/cli/project_templates/calm/actions/action_template.py +27 -0
  36. rasa/cli/project_templates/calm/actions/add_contact.py +30 -0
  37. rasa/cli/project_templates/calm/actions/db.py +57 -0
  38. rasa/cli/project_templates/calm/actions/list_contacts.py +22 -0
  39. rasa/cli/project_templates/calm/actions/remove_contact.py +35 -0
  40. rasa/cli/project_templates/calm/config.yml +12 -0
  41. rasa/cli/project_templates/calm/credentials.yml +33 -0
  42. rasa/cli/project_templates/calm/data/flows/add_contact.yml +31 -0
  43. rasa/cli/project_templates/calm/data/flows/list_contacts.yml +14 -0
  44. rasa/cli/project_templates/calm/data/flows/remove_contact.yml +29 -0
  45. rasa/cli/project_templates/calm/db/contacts.json +10 -0
  46. rasa/cli/project_templates/calm/domain/add_contact.yml +33 -0
  47. rasa/cli/project_templates/calm/domain/list_contacts.yml +14 -0
  48. rasa/cli/project_templates/calm/domain/remove_contact.yml +31 -0
  49. rasa/cli/project_templates/calm/domain/shared.yml +5 -0
  50. rasa/cli/project_templates/calm/e2e_tests/cancelations/user_cancels_during_a_correction.yml +16 -0
  51. rasa/cli/project_templates/calm/e2e_tests/cancelations/user_changes_mind_on_a_whim.yml +7 -0
  52. rasa/cli/project_templates/calm/e2e_tests/corrections/user_corrects_contact_handle.yml +20 -0
  53. rasa/cli/project_templates/calm/e2e_tests/corrections/user_corrects_contact_name.yml +19 -0
  54. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_adds_contact_to_their_list.yml +15 -0
  55. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_lists_contacts.yml +5 -0
  56. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_removes_contact.yml +11 -0
  57. rasa/cli/project_templates/calm/e2e_tests/happy_paths/user_removes_contact_from_list.yml +12 -0
  58. rasa/cli/project_templates/calm/endpoints.yml +45 -0
  59. rasa/cli/project_templates/default/actions/__init__.py +0 -0
  60. rasa/cli/project_templates/default/actions/actions.py +27 -0
  61. rasa/cli/project_templates/default/config.yml +44 -0
  62. rasa/cli/project_templates/default/credentials.yml +33 -0
  63. rasa/cli/project_templates/default/data/nlu.yml +91 -0
  64. rasa/cli/project_templates/default/data/rules.yml +13 -0
  65. rasa/cli/project_templates/default/data/stories.yml +30 -0
  66. rasa/cli/project_templates/default/domain.yml +34 -0
  67. rasa/cli/project_templates/default/endpoints.yml +42 -0
  68. rasa/cli/project_templates/default/tests/test_stories.yml +91 -0
  69. rasa/cli/project_templates/tutorial/actions.py +22 -0
  70. rasa/cli/project_templates/tutorial/config.yml +11 -0
  71. rasa/cli/project_templates/tutorial/credentials.yml +33 -0
  72. rasa/cli/project_templates/tutorial/data/flows.yml +8 -0
  73. rasa/cli/project_templates/tutorial/domain.yml +17 -0
  74. rasa/cli/project_templates/tutorial/endpoints.yml +45 -0
  75. rasa/cli/run.py +136 -0
  76. rasa/cli/scaffold.py +268 -0
  77. rasa/cli/shell.py +141 -0
  78. rasa/cli/studio/__init__.py +0 -0
  79. rasa/cli/studio/download.py +51 -0
  80. rasa/cli/studio/studio.py +110 -0
  81. rasa/cli/studio/train.py +59 -0
  82. rasa/cli/studio/upload.py +85 -0
  83. rasa/cli/telemetry.py +90 -0
  84. rasa/cli/test.py +280 -0
  85. rasa/cli/train.py +260 -0
  86. rasa/cli/utils.py +453 -0
  87. rasa/cli/visualize.py +40 -0
  88. rasa/cli/x.py +205 -0
  89. rasa/constants.py +37 -0
  90. rasa/core/__init__.py +17 -0
  91. rasa/core/actions/__init__.py +0 -0
  92. rasa/core/actions/action.py +1450 -0
  93. rasa/core/actions/action_clean_stack.py +59 -0
  94. rasa/core/actions/action_run_slot_rejections.py +207 -0
  95. rasa/core/actions/action_trigger_chitchat.py +31 -0
  96. rasa/core/actions/action_trigger_flow.py +109 -0
  97. rasa/core/actions/action_trigger_search.py +31 -0
  98. rasa/core/actions/constants.py +2 -0
  99. rasa/core/actions/forms.py +737 -0
  100. rasa/core/actions/loops.py +111 -0
  101. rasa/core/actions/two_stage_fallback.py +186 -0
  102. rasa/core/agent.py +557 -0
  103. rasa/core/auth_retry_tracker_store.py +122 -0
  104. rasa/core/brokers/__init__.py +0 -0
  105. rasa/core/brokers/broker.py +126 -0
  106. rasa/core/brokers/file.py +58 -0
  107. rasa/core/brokers/kafka.py +322 -0
  108. rasa/core/brokers/pika.py +387 -0
  109. rasa/core/brokers/sql.py +86 -0
  110. rasa/core/channels/__init__.py +55 -0
  111. rasa/core/channels/audiocodes.py +463 -0
  112. rasa/core/channels/botframework.py +339 -0
  113. rasa/core/channels/callback.py +85 -0
  114. rasa/core/channels/channel.py +419 -0
  115. rasa/core/channels/console.py +243 -0
  116. rasa/core/channels/development_inspector.py +93 -0
  117. rasa/core/channels/facebook.py +422 -0
  118. rasa/core/channels/hangouts.py +335 -0
  119. rasa/core/channels/inspector/.eslintrc.cjs +25 -0
  120. rasa/core/channels/inspector/.gitignore +23 -0
  121. rasa/core/channels/inspector/README.md +54 -0
  122. rasa/core/channels/inspector/assets/favicon.ico +0 -0
  123. rasa/core/channels/inspector/assets/rasa-chat.js +2 -0
  124. rasa/core/channels/inspector/custom.d.ts +3 -0
  125. rasa/core/channels/inspector/dist/assets/arc-5623b6dc.js +1 -0
  126. rasa/core/channels/inspector/dist/assets/array-9f3ba611.js +1 -0
  127. rasa/core/channels/inspector/dist/assets/c4Diagram-d0fbc5ce-685c106a.js +10 -0
  128. rasa/core/channels/inspector/dist/assets/classDiagram-936ed81e-8cbed007.js +2 -0
  129. rasa/core/channels/inspector/dist/assets/classDiagram-v2-c3cb15f1-5889cf12.js +2 -0
  130. rasa/core/channels/inspector/dist/assets/createText-62fc7601-24c249d7.js +7 -0
  131. rasa/core/channels/inspector/dist/assets/edges-f2ad444c-7dd06a75.js +4 -0
  132. rasa/core/channels/inspector/dist/assets/erDiagram-9d236eb7-62c1e54c.js +51 -0
  133. rasa/core/channels/inspector/dist/assets/flowDb-1972c806-ce49b86f.js +6 -0
  134. rasa/core/channels/inspector/dist/assets/flowDiagram-7ea5b25a-4067e48f.js +4 -0
  135. rasa/core/channels/inspector/dist/assets/flowDiagram-v2-855bc5b3-85583a23.js +1 -0
  136. rasa/core/channels/inspector/dist/assets/flowchart-elk-definition-abe16c3d-59fe4051.js +139 -0
  137. rasa/core/channels/inspector/dist/assets/ganttDiagram-9b5ea136-47e3a43b.js +266 -0
  138. rasa/core/channels/inspector/dist/assets/gitGraphDiagram-99d0ae7c-5a2ac0d9.js +70 -0
  139. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-128cfa44.ttf +0 -0
  140. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-21dbcb97.woff +0 -0
  141. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-222b5e26.svg +329 -0
  142. rasa/core/channels/inspector/dist/assets/ibm-plex-mono-v4-latin-regular-9ad89b2a.woff2 +0 -0
  143. rasa/core/channels/inspector/dist/assets/index-268a75c0.js +1040 -0
  144. rasa/core/channels/inspector/dist/assets/index-2c4b9a3b-dfb8efc4.js +1 -0
  145. rasa/core/channels/inspector/dist/assets/index-3ee28881.css +1 -0
  146. rasa/core/channels/inspector/dist/assets/infoDiagram-736b4530-b0c470f2.js +7 -0
  147. rasa/core/channels/inspector/dist/assets/init-77b53fdd.js +1 -0
  148. rasa/core/channels/inspector/dist/assets/journeyDiagram-df861f2b-2edb829a.js +139 -0
  149. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-60c05ee4.woff +0 -0
  150. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-8335d9b8.svg +438 -0
  151. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-9cc39c75.ttf +0 -0
  152. rasa/core/channels/inspector/dist/assets/lato-v14-latin-700-ead13ccf.woff2 +0 -0
  153. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-16705655.woff2 +0 -0
  154. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-5aeb07f9.woff +0 -0
  155. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-9c459044.ttf +0 -0
  156. rasa/core/channels/inspector/dist/assets/lato-v14-latin-regular-9e2898a4.svg +435 -0
  157. rasa/core/channels/inspector/dist/assets/layout-b6873d69.js +1 -0
  158. rasa/core/channels/inspector/dist/assets/line-1efc5781.js +1 -0
  159. rasa/core/channels/inspector/dist/assets/linear-661e9b94.js +1 -0
  160. rasa/core/channels/inspector/dist/assets/mindmap-definition-beec6740-2d2e727f.js +109 -0
  161. rasa/core/channels/inspector/dist/assets/ordinal-ba9b4969.js +1 -0
  162. rasa/core/channels/inspector/dist/assets/path-53f90ab3.js +1 -0
  163. rasa/core/channels/inspector/dist/assets/pieDiagram-dbbf0591-9d3ea93d.js +35 -0
  164. rasa/core/channels/inspector/dist/assets/quadrantDiagram-4d7f4fd6-06a178a2.js +7 -0
  165. rasa/core/channels/inspector/dist/assets/requirementDiagram-6fc4c22a-0bfedffc.js +52 -0
  166. rasa/core/channels/inspector/dist/assets/sankeyDiagram-8f13d901-d76d0a04.js +8 -0
  167. rasa/core/channels/inspector/dist/assets/sequenceDiagram-b655622a-37bb4341.js +122 -0
  168. rasa/core/channels/inspector/dist/assets/stateDiagram-59f0c015-f52f7f57.js +1 -0
  169. rasa/core/channels/inspector/dist/assets/stateDiagram-v2-2b26beab-4a986a20.js +1 -0
  170. rasa/core/channels/inspector/dist/assets/styles-080da4f6-7dd9ae12.js +110 -0
  171. rasa/core/channels/inspector/dist/assets/styles-3dcbcfbf-46e1ca14.js +159 -0
  172. rasa/core/channels/inspector/dist/assets/styles-9c745c82-4a97439a.js +207 -0
  173. rasa/core/channels/inspector/dist/assets/svgDrawCommon-4835440b-823917a3.js +1 -0
  174. rasa/core/channels/inspector/dist/assets/timeline-definition-5b62e21b-9ea72896.js +61 -0
  175. rasa/core/channels/inspector/dist/assets/xychartDiagram-2b33534f-b631a8b6.js +7 -0
  176. rasa/core/channels/inspector/dist/index.html +39 -0
  177. rasa/core/channels/inspector/index.html +37 -0
  178. rasa/core/channels/inspector/jest.config.ts +13 -0
  179. rasa/core/channels/inspector/package.json +48 -0
  180. rasa/core/channels/inspector/setupTests.ts +2 -0
  181. rasa/core/channels/inspector/src/App.tsx +170 -0
  182. rasa/core/channels/inspector/src/components/DiagramFlow.tsx +97 -0
  183. rasa/core/channels/inspector/src/components/DialogueInformation.tsx +187 -0
  184. rasa/core/channels/inspector/src/components/DialogueStack.tsx +151 -0
  185. rasa/core/channels/inspector/src/components/ExpandIcon.tsx +16 -0
  186. rasa/core/channels/inspector/src/components/FullscreenButton.tsx +45 -0
  187. rasa/core/channels/inspector/src/components/LoadingSpinner.tsx +19 -0
  188. rasa/core/channels/inspector/src/components/NoActiveFlow.tsx +21 -0
  189. rasa/core/channels/inspector/src/components/RasaLogo.tsx +32 -0
  190. rasa/core/channels/inspector/src/components/SaraDiagrams.tsx +39 -0
  191. rasa/core/channels/inspector/src/components/Slots.tsx +91 -0
  192. rasa/core/channels/inspector/src/components/Welcome.tsx +54 -0
  193. rasa/core/channels/inspector/src/helpers/formatters.test.ts +385 -0
  194. rasa/core/channels/inspector/src/helpers/formatters.ts +239 -0
  195. rasa/core/channels/inspector/src/helpers/utils.ts +42 -0
  196. rasa/core/channels/inspector/src/main.tsx +13 -0
  197. rasa/core/channels/inspector/src/theme/Button/Button.ts +29 -0
  198. rasa/core/channels/inspector/src/theme/Heading/Heading.ts +31 -0
  199. rasa/core/channels/inspector/src/theme/Input/Input.ts +27 -0
  200. rasa/core/channels/inspector/src/theme/Link/Link.ts +10 -0
  201. rasa/core/channels/inspector/src/theme/Modal/Modal.ts +47 -0
  202. rasa/core/channels/inspector/src/theme/Table/Table.tsx +38 -0
  203. rasa/core/channels/inspector/src/theme/Tooltip/Tooltip.ts +12 -0
  204. rasa/core/channels/inspector/src/theme/base/breakpoints.ts +8 -0
  205. rasa/core/channels/inspector/src/theme/base/colors.ts +88 -0
  206. rasa/core/channels/inspector/src/theme/base/fonts/fontFaces.css +29 -0
  207. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.eot +0 -0
  208. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.svg +329 -0
  209. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.ttf +0 -0
  210. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.woff +0 -0
  211. rasa/core/channels/inspector/src/theme/base/fonts/ibm-plex-mono-v4-latin/ibm-plex-mono-v4-latin-regular.woff2 +0 -0
  212. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.eot +0 -0
  213. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.svg +438 -0
  214. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.ttf +0 -0
  215. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.woff +0 -0
  216. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-700.woff2 +0 -0
  217. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.eot +0 -0
  218. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.svg +435 -0
  219. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.ttf +0 -0
  220. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.woff +0 -0
  221. rasa/core/channels/inspector/src/theme/base/fonts/lato-v14-latin/lato-v14-latin-regular.woff2 +0 -0
  222. rasa/core/channels/inspector/src/theme/base/radii.ts +9 -0
  223. rasa/core/channels/inspector/src/theme/base/shadows.ts +7 -0
  224. rasa/core/channels/inspector/src/theme/base/sizes.ts +7 -0
  225. rasa/core/channels/inspector/src/theme/base/space.ts +15 -0
  226. rasa/core/channels/inspector/src/theme/base/styles.ts +13 -0
  227. rasa/core/channels/inspector/src/theme/base/typography.ts +24 -0
  228. rasa/core/channels/inspector/src/theme/base/zIndices.ts +19 -0
  229. rasa/core/channels/inspector/src/theme/index.ts +101 -0
  230. rasa/core/channels/inspector/src/types.ts +64 -0
  231. rasa/core/channels/inspector/src/vite-env.d.ts +1 -0
  232. rasa/core/channels/inspector/tests/__mocks__/fileMock.ts +1 -0
  233. rasa/core/channels/inspector/tests/__mocks__/matchMedia.ts +16 -0
  234. rasa/core/channels/inspector/tests/__mocks__/styleMock.ts +1 -0
  235. rasa/core/channels/inspector/tests/renderWithProviders.tsx +14 -0
  236. rasa/core/channels/inspector/tsconfig.json +26 -0
  237. rasa/core/channels/inspector/tsconfig.node.json +10 -0
  238. rasa/core/channels/inspector/vite.config.ts +8 -0
  239. rasa/core/channels/inspector/yarn.lock +6156 -0
  240. rasa/core/channels/mattermost.py +229 -0
  241. rasa/core/channels/rasa_chat.py +126 -0
  242. rasa/core/channels/rest.py +210 -0
  243. rasa/core/channels/rocketchat.py +175 -0
  244. rasa/core/channels/slack.py +620 -0
  245. rasa/core/channels/socketio.py +274 -0
  246. rasa/core/channels/telegram.py +298 -0
  247. rasa/core/channels/twilio.py +169 -0
  248. rasa/core/channels/twilio_voice.py +367 -0
  249. rasa/core/channels/vier_cvg.py +374 -0
  250. rasa/core/channels/webexteams.py +135 -0
  251. rasa/core/concurrent_lock_store.py +210 -0
  252. rasa/core/constants.py +107 -0
  253. rasa/core/evaluation/__init__.py +0 -0
  254. rasa/core/evaluation/marker.py +267 -0
  255. rasa/core/evaluation/marker_base.py +925 -0
  256. rasa/core/evaluation/marker_stats.py +294 -0
  257. rasa/core/evaluation/marker_tracker_loader.py +103 -0
  258. rasa/core/exceptions.py +29 -0
  259. rasa/core/exporter.py +284 -0
  260. rasa/core/featurizers/__init__.py +0 -0
  261. rasa/core/featurizers/precomputation.py +410 -0
  262. rasa/core/featurizers/single_state_featurizer.py +402 -0
  263. rasa/core/featurizers/tracker_featurizers.py +1172 -0
  264. rasa/core/http_interpreter.py +89 -0
  265. rasa/core/information_retrieval/__init__.py +0 -0
  266. rasa/core/information_retrieval/faiss.py +116 -0
  267. rasa/core/information_retrieval/information_retrieval.py +72 -0
  268. rasa/core/information_retrieval/milvus.py +59 -0
  269. rasa/core/information_retrieval/qdrant.py +102 -0
  270. rasa/core/jobs.py +63 -0
  271. rasa/core/lock.py +139 -0
  272. rasa/core/lock_store.py +344 -0
  273. rasa/core/migrate.py +404 -0
  274. rasa/core/nlg/__init__.py +3 -0
  275. rasa/core/nlg/callback.py +147 -0
  276. rasa/core/nlg/contextual_response_rephraser.py +270 -0
  277. rasa/core/nlg/generator.py +230 -0
  278. rasa/core/nlg/interpolator.py +143 -0
  279. rasa/core/nlg/response.py +155 -0
  280. rasa/core/nlg/summarize.py +69 -0
  281. rasa/core/policies/__init__.py +0 -0
  282. rasa/core/policies/ensemble.py +329 -0
  283. rasa/core/policies/enterprise_search_policy.py +717 -0
  284. rasa/core/policies/enterprise_search_prompt_template.jinja2 +62 -0
  285. rasa/core/policies/flow_policy.py +205 -0
  286. rasa/core/policies/flows/__init__.py +0 -0
  287. rasa/core/policies/flows/flow_exceptions.py +44 -0
  288. rasa/core/policies/flows/flow_executor.py +582 -0
  289. rasa/core/policies/flows/flow_step_result.py +43 -0
  290. rasa/core/policies/intentless_policy.py +924 -0
  291. rasa/core/policies/intentless_prompt_template.jinja2 +22 -0
  292. rasa/core/policies/memoization.py +538 -0
  293. rasa/core/policies/policy.py +716 -0
  294. rasa/core/policies/rule_policy.py +1276 -0
  295. rasa/core/policies/ted_policy.py +2146 -0
  296. rasa/core/policies/unexpected_intent_policy.py +1015 -0
  297. rasa/core/processor.py +1331 -0
  298. rasa/core/run.py +315 -0
  299. rasa/core/secrets_manager/__init__.py +0 -0
  300. rasa/core/secrets_manager/constants.py +32 -0
  301. rasa/core/secrets_manager/endpoints.py +391 -0
  302. rasa/core/secrets_manager/factory.py +233 -0
  303. rasa/core/secrets_manager/secret_manager.py +262 -0
  304. rasa/core/secrets_manager/vault.py +576 -0
  305. rasa/core/test.py +1337 -0
  306. rasa/core/tracker_store.py +1664 -0
  307. rasa/core/train.py +107 -0
  308. rasa/core/training/__init__.py +89 -0
  309. rasa/core/training/converters/__init__.py +0 -0
  310. rasa/core/training/converters/responses_prefix_converter.py +119 -0
  311. rasa/core/training/interactive.py +1742 -0
  312. rasa/core/training/story_conflict.py +381 -0
  313. rasa/core/training/training.py +93 -0
  314. rasa/core/utils.py +344 -0
  315. rasa/core/visualize.py +70 -0
  316. rasa/dialogue_understanding/__init__.py +0 -0
  317. rasa/dialogue_understanding/coexistence/__init__.py +0 -0
  318. rasa/dialogue_understanding/coexistence/constants.py +4 -0
  319. rasa/dialogue_understanding/coexistence/intent_based_router.py +189 -0
  320. rasa/dialogue_understanding/coexistence/llm_based_router.py +261 -0
  321. rasa/dialogue_understanding/coexistence/router_template.jinja2 +12 -0
  322. rasa/dialogue_understanding/commands/__init__.py +45 -0
  323. rasa/dialogue_understanding/commands/can_not_handle_command.py +61 -0
  324. rasa/dialogue_understanding/commands/cancel_flow_command.py +116 -0
  325. rasa/dialogue_understanding/commands/chit_chat_answer_command.py +48 -0
  326. rasa/dialogue_understanding/commands/clarify_command.py +77 -0
  327. rasa/dialogue_understanding/commands/command.py +85 -0
  328. rasa/dialogue_understanding/commands/correct_slots_command.py +288 -0
  329. rasa/dialogue_understanding/commands/error_command.py +67 -0
  330. rasa/dialogue_understanding/commands/free_form_answer_command.py +9 -0
  331. rasa/dialogue_understanding/commands/handle_code_change_command.py +64 -0
  332. rasa/dialogue_understanding/commands/human_handoff_command.py +57 -0
  333. rasa/dialogue_understanding/commands/knowledge_answer_command.py +48 -0
  334. rasa/dialogue_understanding/commands/noop_command.py +45 -0
  335. rasa/dialogue_understanding/commands/set_slot_command.py +125 -0
  336. rasa/dialogue_understanding/commands/skip_question_command.py +66 -0
  337. rasa/dialogue_understanding/commands/start_flow_command.py +98 -0
  338. rasa/dialogue_understanding/generator/__init__.py +6 -0
  339. rasa/dialogue_understanding/generator/command_generator.py +257 -0
  340. rasa/dialogue_understanding/generator/command_prompt_template.jinja2 +57 -0
  341. rasa/dialogue_understanding/generator/flow_document_template.jinja2 +4 -0
  342. rasa/dialogue_understanding/generator/flow_retrieval.py +410 -0
  343. rasa/dialogue_understanding/generator/llm_command_generator.py +637 -0
  344. rasa/dialogue_understanding/generator/nlu_command_adapter.py +157 -0
  345. rasa/dialogue_understanding/patterns/__init__.py +0 -0
  346. rasa/dialogue_understanding/patterns/cancel.py +111 -0
  347. rasa/dialogue_understanding/patterns/cannot_handle.py +43 -0
  348. rasa/dialogue_understanding/patterns/chitchat.py +37 -0
  349. rasa/dialogue_understanding/patterns/clarify.py +97 -0
  350. rasa/dialogue_understanding/patterns/code_change.py +41 -0
  351. rasa/dialogue_understanding/patterns/collect_information.py +90 -0
  352. rasa/dialogue_understanding/patterns/completed.py +40 -0
  353. rasa/dialogue_understanding/patterns/continue_interrupted.py +42 -0
  354. rasa/dialogue_understanding/patterns/correction.py +278 -0
  355. rasa/dialogue_understanding/patterns/default_flows_for_patterns.yml +243 -0
  356. rasa/dialogue_understanding/patterns/human_handoff.py +37 -0
  357. rasa/dialogue_understanding/patterns/internal_error.py +47 -0
  358. rasa/dialogue_understanding/patterns/search.py +37 -0
  359. rasa/dialogue_understanding/patterns/skip_question.py +38 -0
  360. rasa/dialogue_understanding/processor/__init__.py +0 -0
  361. rasa/dialogue_understanding/processor/command_processor.py +578 -0
  362. rasa/dialogue_understanding/processor/command_processor_component.py +39 -0
  363. rasa/dialogue_understanding/stack/__init__.py +0 -0
  364. rasa/dialogue_understanding/stack/dialogue_stack.py +178 -0
  365. rasa/dialogue_understanding/stack/frames/__init__.py +19 -0
  366. rasa/dialogue_understanding/stack/frames/chit_chat_frame.py +27 -0
  367. rasa/dialogue_understanding/stack/frames/dialogue_stack_frame.py +137 -0
  368. rasa/dialogue_understanding/stack/frames/flow_stack_frame.py +157 -0
  369. rasa/dialogue_understanding/stack/frames/pattern_frame.py +10 -0
  370. rasa/dialogue_understanding/stack/frames/search_frame.py +27 -0
  371. rasa/dialogue_understanding/stack/utils.py +211 -0
  372. rasa/e2e_test/__init__.py +0 -0
  373. rasa/e2e_test/constants.py +10 -0
  374. rasa/e2e_test/e2e_test_case.py +322 -0
  375. rasa/e2e_test/e2e_test_result.py +34 -0
  376. rasa/e2e_test/e2e_test_runner.py +659 -0
  377. rasa/e2e_test/e2e_test_schema.yml +67 -0
  378. rasa/engine/__init__.py +0 -0
  379. rasa/engine/caching.py +464 -0
  380. rasa/engine/constants.py +17 -0
  381. rasa/engine/exceptions.py +14 -0
  382. rasa/engine/graph.py +625 -0
  383. rasa/engine/loader.py +36 -0
  384. rasa/engine/recipes/__init__.py +0 -0
  385. rasa/engine/recipes/config_files/default_config.yml +44 -0
  386. rasa/engine/recipes/default_components.py +99 -0
  387. rasa/engine/recipes/default_recipe.py +1252 -0
  388. rasa/engine/recipes/graph_recipe.py +79 -0
  389. rasa/engine/recipes/recipe.py +93 -0
  390. rasa/engine/runner/__init__.py +0 -0
  391. rasa/engine/runner/dask.py +256 -0
  392. rasa/engine/runner/interface.py +49 -0
  393. rasa/engine/storage/__init__.py +0 -0
  394. rasa/engine/storage/local_model_storage.py +248 -0
  395. rasa/engine/storage/resource.py +110 -0
  396. rasa/engine/storage/storage.py +203 -0
  397. rasa/engine/training/__init__.py +0 -0
  398. rasa/engine/training/components.py +176 -0
  399. rasa/engine/training/fingerprinting.py +64 -0
  400. rasa/engine/training/graph_trainer.py +256 -0
  401. rasa/engine/training/hooks.py +164 -0
  402. rasa/engine/validation.py +839 -0
  403. rasa/env.py +5 -0
  404. rasa/exceptions.py +69 -0
  405. rasa/graph_components/__init__.py +0 -0
  406. rasa/graph_components/converters/__init__.py +0 -0
  407. rasa/graph_components/converters/nlu_message_converter.py +48 -0
  408. rasa/graph_components/providers/__init__.py +0 -0
  409. rasa/graph_components/providers/domain_for_core_training_provider.py +87 -0
  410. rasa/graph_components/providers/domain_provider.py +71 -0
  411. rasa/graph_components/providers/flows_provider.py +74 -0
  412. rasa/graph_components/providers/forms_provider.py +44 -0
  413. rasa/graph_components/providers/nlu_training_data_provider.py +56 -0
  414. rasa/graph_components/providers/responses_provider.py +44 -0
  415. rasa/graph_components/providers/rule_only_provider.py +49 -0
  416. rasa/graph_components/providers/story_graph_provider.py +43 -0
  417. rasa/graph_components/providers/training_tracker_provider.py +55 -0
  418. rasa/graph_components/validators/__init__.py +0 -0
  419. rasa/graph_components/validators/default_recipe_validator.py +552 -0
  420. rasa/graph_components/validators/finetuning_validator.py +302 -0
  421. rasa/hooks.py +113 -0
  422. rasa/jupyter.py +63 -0
  423. rasa/keys +1 -0
  424. rasa/markers/__init__.py +0 -0
  425. rasa/markers/marker.py +269 -0
  426. rasa/markers/marker_base.py +828 -0
  427. rasa/markers/upload.py +74 -0
  428. rasa/markers/validate.py +21 -0
  429. rasa/model.py +118 -0
  430. rasa/model_testing.py +457 -0
  431. rasa/model_training.py +535 -0
  432. rasa/nlu/__init__.py +7 -0
  433. rasa/nlu/classifiers/__init__.py +3 -0
  434. rasa/nlu/classifiers/classifier.py +5 -0
  435. rasa/nlu/classifiers/diet_classifier.py +1874 -0
  436. rasa/nlu/classifiers/fallback_classifier.py +192 -0
  437. rasa/nlu/classifiers/keyword_intent_classifier.py +188 -0
  438. rasa/nlu/classifiers/llm_intent_classifier.py +519 -0
  439. rasa/nlu/classifiers/logistic_regression_classifier.py +240 -0
  440. rasa/nlu/classifiers/mitie_intent_classifier.py +156 -0
  441. rasa/nlu/classifiers/regex_message_handler.py +56 -0
  442. rasa/nlu/classifiers/sklearn_intent_classifier.py +309 -0
  443. rasa/nlu/constants.py +77 -0
  444. rasa/nlu/convert.py +40 -0
  445. rasa/nlu/emulators/__init__.py +0 -0
  446. rasa/nlu/emulators/dialogflow.py +55 -0
  447. rasa/nlu/emulators/emulator.py +49 -0
  448. rasa/nlu/emulators/luis.py +86 -0
  449. rasa/nlu/emulators/no_emulator.py +10 -0
  450. rasa/nlu/emulators/wit.py +56 -0
  451. rasa/nlu/extractors/__init__.py +0 -0
  452. rasa/nlu/extractors/crf_entity_extractor.py +672 -0
  453. rasa/nlu/extractors/duckling_entity_extractor.py +206 -0
  454. rasa/nlu/extractors/entity_synonyms.py +178 -0
  455. rasa/nlu/extractors/extractor.py +470 -0
  456. rasa/nlu/extractors/mitie_entity_extractor.py +293 -0
  457. rasa/nlu/extractors/regex_entity_extractor.py +220 -0
  458. rasa/nlu/extractors/spacy_entity_extractor.py +95 -0
  459. rasa/nlu/featurizers/__init__.py +0 -0
  460. rasa/nlu/featurizers/dense_featurizer/__init__.py +0 -0
  461. rasa/nlu/featurizers/dense_featurizer/convert_featurizer.py +449 -0
  462. rasa/nlu/featurizers/dense_featurizer/dense_featurizer.py +57 -0
  463. rasa/nlu/featurizers/dense_featurizer/lm_featurizer.py +772 -0
  464. rasa/nlu/featurizers/dense_featurizer/mitie_featurizer.py +170 -0
  465. rasa/nlu/featurizers/dense_featurizer/spacy_featurizer.py +132 -0
  466. rasa/nlu/featurizers/featurizer.py +89 -0
  467. rasa/nlu/featurizers/sparse_featurizer/__init__.py +0 -0
  468. rasa/nlu/featurizers/sparse_featurizer/count_vectors_featurizer.py +840 -0
  469. rasa/nlu/featurizers/sparse_featurizer/lexical_syntactic_featurizer.py +539 -0
  470. rasa/nlu/featurizers/sparse_featurizer/regex_featurizer.py +269 -0
  471. rasa/nlu/featurizers/sparse_featurizer/sparse_featurizer.py +9 -0
  472. rasa/nlu/model.py +24 -0
  473. rasa/nlu/persistor.py +240 -0
  474. rasa/nlu/run.py +27 -0
  475. rasa/nlu/selectors/__init__.py +0 -0
  476. rasa/nlu/selectors/response_selector.py +990 -0
  477. rasa/nlu/test.py +1943 -0
  478. rasa/nlu/tokenizers/__init__.py +0 -0
  479. rasa/nlu/tokenizers/jieba_tokenizer.py +148 -0
  480. rasa/nlu/tokenizers/mitie_tokenizer.py +75 -0
  481. rasa/nlu/tokenizers/spacy_tokenizer.py +72 -0
  482. rasa/nlu/tokenizers/tokenizer.py +239 -0
  483. rasa/nlu/tokenizers/whitespace_tokenizer.py +106 -0
  484. rasa/nlu/utils/__init__.py +35 -0
  485. rasa/nlu/utils/bilou_utils.py +462 -0
  486. rasa/nlu/utils/hugging_face/__init__.py +0 -0
  487. rasa/nlu/utils/hugging_face/registry.py +108 -0
  488. rasa/nlu/utils/hugging_face/transformers_pre_post_processors.py +311 -0
  489. rasa/nlu/utils/mitie_utils.py +113 -0
  490. rasa/nlu/utils/pattern_utils.py +168 -0
  491. rasa/nlu/utils/spacy_utils.py +312 -0
  492. rasa/plugin.py +90 -0
  493. rasa/server.py +1536 -0
  494. rasa/shared/__init__.py +0 -0
  495. rasa/shared/constants.py +181 -0
  496. rasa/shared/core/__init__.py +0 -0
  497. rasa/shared/core/constants.py +168 -0
  498. rasa/shared/core/conversation.py +46 -0
  499. rasa/shared/core/domain.py +2106 -0
  500. rasa/shared/core/events.py +2507 -0
  501. rasa/shared/core/flows/__init__.py +7 -0
  502. rasa/shared/core/flows/flow.py +353 -0
  503. rasa/shared/core/flows/flow_step.py +146 -0
  504. rasa/shared/core/flows/flow_step_links.py +319 -0
  505. rasa/shared/core/flows/flow_step_sequence.py +70 -0
  506. rasa/shared/core/flows/flows_list.py +211 -0
  507. rasa/shared/core/flows/flows_yaml_schema.json +217 -0
  508. rasa/shared/core/flows/nlu_trigger.py +117 -0
  509. rasa/shared/core/flows/steps/__init__.py +24 -0
  510. rasa/shared/core/flows/steps/action.py +51 -0
  511. rasa/shared/core/flows/steps/call.py +64 -0
  512. rasa/shared/core/flows/steps/collect.py +112 -0
  513. rasa/shared/core/flows/steps/constants.py +5 -0
  514. rasa/shared/core/flows/steps/continuation.py +36 -0
  515. rasa/shared/core/flows/steps/end.py +22 -0
  516. rasa/shared/core/flows/steps/internal.py +44 -0
  517. rasa/shared/core/flows/steps/link.py +51 -0
  518. rasa/shared/core/flows/steps/no_operation.py +48 -0
  519. rasa/shared/core/flows/steps/set_slots.py +50 -0
  520. rasa/shared/core/flows/steps/start.py +30 -0
  521. rasa/shared/core/flows/validation.py +527 -0
  522. rasa/shared/core/flows/yaml_flows_io.py +278 -0
  523. rasa/shared/core/generator.py +907 -0
  524. rasa/shared/core/slot_mappings.py +235 -0
  525. rasa/shared/core/slots.py +647 -0
  526. rasa/shared/core/trackers.py +1159 -0
  527. rasa/shared/core/training_data/__init__.py +0 -0
  528. rasa/shared/core/training_data/loading.py +90 -0
  529. rasa/shared/core/training_data/story_reader/__init__.py +0 -0
  530. rasa/shared/core/training_data/story_reader/story_reader.py +129 -0
  531. rasa/shared/core/training_data/story_reader/story_step_builder.py +168 -0
  532. rasa/shared/core/training_data/story_reader/yaml_story_reader.py +888 -0
  533. rasa/shared/core/training_data/story_writer/__init__.py +0 -0
  534. rasa/shared/core/training_data/story_writer/story_writer.py +76 -0
  535. rasa/shared/core/training_data/story_writer/yaml_story_writer.py +442 -0
  536. rasa/shared/core/training_data/structures.py +838 -0
  537. rasa/shared/core/training_data/visualization.html +146 -0
  538. rasa/shared/core/training_data/visualization.py +603 -0
  539. rasa/shared/data.py +192 -0
  540. rasa/shared/engine/__init__.py +0 -0
  541. rasa/shared/engine/caching.py +26 -0
  542. rasa/shared/exceptions.py +129 -0
  543. rasa/shared/importers/__init__.py +0 -0
  544. rasa/shared/importers/importer.py +705 -0
  545. rasa/shared/importers/multi_project.py +203 -0
  546. rasa/shared/importers/rasa.py +100 -0
  547. rasa/shared/importers/utils.py +34 -0
  548. rasa/shared/nlu/__init__.py +0 -0
  549. rasa/shared/nlu/constants.py +45 -0
  550. rasa/shared/nlu/interpreter.py +10 -0
  551. rasa/shared/nlu/training_data/__init__.py +0 -0
  552. rasa/shared/nlu/training_data/entities_parser.py +209 -0
  553. rasa/shared/nlu/training_data/features.py +374 -0
  554. rasa/shared/nlu/training_data/formats/__init__.py +10 -0
  555. rasa/shared/nlu/training_data/formats/dialogflow.py +162 -0
  556. rasa/shared/nlu/training_data/formats/luis.py +87 -0
  557. rasa/shared/nlu/training_data/formats/rasa.py +135 -0
  558. rasa/shared/nlu/training_data/formats/rasa_yaml.py +605 -0
  559. rasa/shared/nlu/training_data/formats/readerwriter.py +245 -0
  560. rasa/shared/nlu/training_data/formats/wit.py +52 -0
  561. rasa/shared/nlu/training_data/loading.py +137 -0
  562. rasa/shared/nlu/training_data/lookup_tables_parser.py +30 -0
  563. rasa/shared/nlu/training_data/message.py +477 -0
  564. rasa/shared/nlu/training_data/schemas/__init__.py +0 -0
  565. rasa/shared/nlu/training_data/schemas/data_schema.py +85 -0
  566. rasa/shared/nlu/training_data/schemas/nlu.yml +53 -0
  567. rasa/shared/nlu/training_data/schemas/responses.yml +70 -0
  568. rasa/shared/nlu/training_data/synonyms_parser.py +42 -0
  569. rasa/shared/nlu/training_data/training_data.py +732 -0
  570. rasa/shared/nlu/training_data/util.py +223 -0
  571. rasa/shared/providers/__init__.py +0 -0
  572. rasa/shared/providers/openai/__init__.py +0 -0
  573. rasa/shared/providers/openai/clients.py +43 -0
  574. rasa/shared/providers/openai/session_handler.py +110 -0
  575. rasa/shared/utils/__init__.py +0 -0
  576. rasa/shared/utils/cli.py +72 -0
  577. rasa/shared/utils/common.py +308 -0
  578. rasa/shared/utils/constants.py +1 -0
  579. rasa/shared/utils/io.py +403 -0
  580. rasa/shared/utils/llm.py +405 -0
  581. rasa/shared/utils/pykwalify_extensions.py +26 -0
  582. rasa/shared/utils/schemas/__init__.py +0 -0
  583. rasa/shared/utils/schemas/config.yml +2 -0
  584. rasa/shared/utils/schemas/domain.yml +142 -0
  585. rasa/shared/utils/schemas/events.py +212 -0
  586. rasa/shared/utils/schemas/model_config.yml +46 -0
  587. rasa/shared/utils/schemas/stories.yml +173 -0
  588. rasa/shared/utils/yaml.py +777 -0
  589. rasa/studio/__init__.py +0 -0
  590. rasa/studio/auth.py +252 -0
  591. rasa/studio/config.py +127 -0
  592. rasa/studio/constants.py +16 -0
  593. rasa/studio/data_handler.py +352 -0
  594. rasa/studio/download.py +350 -0
  595. rasa/studio/train.py +136 -0
  596. rasa/studio/upload.py +408 -0
  597. rasa/telemetry.py +1583 -0
  598. rasa/tracing/__init__.py +0 -0
  599. rasa/tracing/config.py +338 -0
  600. rasa/tracing/constants.py +38 -0
  601. rasa/tracing/instrumentation/__init__.py +0 -0
  602. rasa/tracing/instrumentation/attribute_extractors.py +663 -0
  603. rasa/tracing/instrumentation/instrumentation.py +939 -0
  604. rasa/tracing/instrumentation/intentless_policy_instrumentation.py +142 -0
  605. rasa/tracing/instrumentation/metrics.py +206 -0
  606. rasa/tracing/metric_instrument_provider.py +125 -0
  607. rasa/utils/__init__.py +0 -0
  608. rasa/utils/beta.py +83 -0
  609. rasa/utils/cli.py +27 -0
  610. rasa/utils/common.py +635 -0
  611. rasa/utils/converter.py +53 -0
  612. rasa/utils/endpoints.py +303 -0
  613. rasa/utils/io.py +326 -0
  614. rasa/utils/licensing.py +319 -0
  615. rasa/utils/log_utils.py +174 -0
  616. rasa/utils/mapper.py +210 -0
  617. rasa/utils/ml_utils.py +145 -0
  618. rasa/utils/plotting.py +362 -0
  619. rasa/utils/singleton.py +23 -0
  620. rasa/utils/tensorflow/__init__.py +0 -0
  621. rasa/utils/tensorflow/callback.py +112 -0
  622. rasa/utils/tensorflow/constants.py +116 -0
  623. rasa/utils/tensorflow/crf.py +492 -0
  624. rasa/utils/tensorflow/data_generator.py +440 -0
  625. rasa/utils/tensorflow/environment.py +161 -0
  626. rasa/utils/tensorflow/exceptions.py +5 -0
  627. rasa/utils/tensorflow/layers.py +1565 -0
  628. rasa/utils/tensorflow/layers_utils.py +113 -0
  629. rasa/utils/tensorflow/metrics.py +281 -0
  630. rasa/utils/tensorflow/model_data.py +991 -0
  631. rasa/utils/tensorflow/model_data_utils.py +500 -0
  632. rasa/utils/tensorflow/models.py +936 -0
  633. rasa/utils/tensorflow/rasa_layers.py +1094 -0
  634. rasa/utils/tensorflow/transformer.py +640 -0
  635. rasa/utils/tensorflow/types.py +6 -0
  636. rasa/utils/train_utils.py +572 -0
  637. rasa/utils/yaml.py +54 -0
  638. rasa/validator.py +1035 -0
  639. rasa/version.py +3 -0
  640. rasa_pro-3.8.16.dist-info/METADATA +528 -0
  641. rasa_pro-3.8.16.dist-info/NOTICE +5 -0
  642. rasa_pro-3.8.16.dist-info/RECORD +644 -0
  643. rasa_pro-3.8.16.dist-info/WHEEL +4 -0
  644. rasa_pro-3.8.16.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,1450 @@
1
+ import copy
2
+ import json
3
+ import logging
4
+ from typing import (
5
+ List,
6
+ Text,
7
+ Optional,
8
+ Dict,
9
+ Any,
10
+ TYPE_CHECKING,
11
+ Tuple,
12
+ Set,
13
+ cast,
14
+ )
15
+
16
+ import aiohttp
17
+ import rasa.core
18
+ from rasa.core.actions.constants import DEFAULT_SELECTIVE_DOMAIN, SELECTIVE_DOMAIN
19
+ from rasa.core.constants import (
20
+ DEFAULT_REQUEST_TIMEOUT,
21
+ COMPRESS_ACTION_SERVER_REQUEST_ENV_NAME,
22
+ DEFAULT_COMPRESS_ACTION_SERVER_REQUEST,
23
+ )
24
+ from rasa.core.policies.policy import PolicyPrediction
25
+ from rasa.nlu.constants import (
26
+ RESPONSE_SELECTOR_DEFAULT_INTENT,
27
+ RESPONSE_SELECTOR_PROPERTY_NAME,
28
+ RESPONSE_SELECTOR_PREDICTION_KEY,
29
+ RESPONSE_SELECTOR_UTTER_ACTION_KEY,
30
+ )
31
+ from rasa.shared.constants import (
32
+ DOCS_BASE_URL,
33
+ DEFAULT_NLU_FALLBACK_INTENT_NAME,
34
+ ROUTE_TO_CALM_SLOT,
35
+ UTTER_PREFIX,
36
+ FLOW_PREFIX,
37
+ )
38
+ from rasa.shared.core.constants import (
39
+ ACTION_RESET_ROUTING,
40
+ USER_INTENT_OUT_OF_SCOPE,
41
+ ACTION_LISTEN_NAME,
42
+ ACTION_RESTART_NAME,
43
+ ACTION_SEND_TEXT_NAME,
44
+ ACTION_SESSION_START_NAME,
45
+ ACTION_DEFAULT_FALLBACK_NAME,
46
+ ACTION_DEACTIVATE_LOOP_NAME,
47
+ ACTION_REVERT_FALLBACK_EVENTS_NAME,
48
+ ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
49
+ ACTION_DEFAULT_ASK_REPHRASE_NAME,
50
+ ACTION_UNLIKELY_INTENT_NAME,
51
+ ACTION_BACK_NAME,
52
+ REQUESTED_SLOT,
53
+ ACTION_EXTRACT_SLOTS,
54
+ DEFAULT_SLOT_NAMES,
55
+ MAPPING_CONDITIONS,
56
+ ACTIVE_LOOP,
57
+ ACTION_VALIDATE_SLOT_MAPPINGS,
58
+ MAPPING_TYPE,
59
+ SlotMappingType,
60
+ KNOWLEDGE_BASE_SLOT_NAMES,
61
+ )
62
+ from rasa.shared.core.domain import Domain
63
+ from rasa.shared.core.events import (
64
+ RoutingSessionEnded,
65
+ UserUtteranceReverted,
66
+ UserUttered,
67
+ ActionExecuted,
68
+ Event,
69
+ BotUttered,
70
+ SlotSet,
71
+ ActiveLoop,
72
+ Restarted,
73
+ SessionStarted,
74
+ )
75
+ from rasa.shared.core.slot_mappings import SlotMapping
76
+ from rasa.shared.core.slots import ListSlot
77
+ from rasa.shared.core.trackers import DialogueStateTracker
78
+ from rasa.shared.exceptions import RasaException
79
+ from rasa.shared.nlu.constants import (
80
+ INTENT_NAME_KEY,
81
+ INTENT_RANKING_KEY,
82
+ ENTITY_ATTRIBUTE_TYPE,
83
+ ENTITY_ATTRIBUTE_ROLE,
84
+ ENTITY_ATTRIBUTE_GROUP,
85
+ )
86
+ from rasa.shared.utils.schemas.events import EVENTS_SCHEMA
87
+ import rasa.shared.utils.io
88
+ from rasa.utils.common import get_bool_env_variable
89
+ from rasa.utils.endpoints import EndpointConfig, ClientResponseError
90
+
91
+ if TYPE_CHECKING:
92
+ from rasa.core.nlg import NaturalLanguageGenerator
93
+ from rasa.core.channels.channel import OutputChannel
94
+ from rasa.shared.core.events import IntentPrediction
95
+
96
+ logger = logging.getLogger(__name__)
97
+
98
+
99
+ def default_actions(action_endpoint: Optional[EndpointConfig] = None) -> List["Action"]:
100
+ """List default actions."""
101
+ from rasa.core.actions.two_stage_fallback import TwoStageFallbackAction
102
+ from rasa.dialogue_understanding.patterns.correction import ActionCorrectFlowSlot
103
+ from rasa.dialogue_understanding.patterns.cancel import ActionCancelFlow
104
+ from rasa.dialogue_understanding.patterns.clarify import ActionClarifyFlows
105
+ from rasa.core.actions.action_run_slot_rejections import ActionRunSlotRejections
106
+ from rasa.core.actions.action_trigger_chitchat import ActionTriggerChitchat
107
+ from rasa.core.actions.action_trigger_search import ActionTriggerSearch
108
+ from rasa.core.actions.action_clean_stack import ActionCleanStack
109
+
110
+ return [
111
+ ActionListen(),
112
+ ActionRestart(),
113
+ ActionSessionStart(),
114
+ ActionDefaultFallback(),
115
+ ActionDeactivateLoop(),
116
+ ActionRevertFallbackEvents(),
117
+ ActionDefaultAskAffirmation(),
118
+ ActionDefaultAskRephrase(),
119
+ TwoStageFallbackAction(action_endpoint),
120
+ ActionUnlikelyIntent(),
121
+ ActionSendText(),
122
+ ActionBack(),
123
+ ActionExtractSlots(action_endpoint),
124
+ ActionCancelFlow(),
125
+ ActionCorrectFlowSlot(),
126
+ ActionClarifyFlows(),
127
+ ActionRunSlotRejections(),
128
+ ActionCleanStack(),
129
+ ActionTriggerSearch(),
130
+ ActionTriggerChitchat(),
131
+ ActionResetRouting(),
132
+ ]
133
+
134
+
135
+ def action_for_index(
136
+ index: int, domain: Domain, action_endpoint: Optional[EndpointConfig]
137
+ ) -> "Action":
138
+ """Get an action based on its index in the list of available actions.
139
+
140
+ Args:
141
+ index: The index of the action. This is usually used by `Policy`s as they
142
+ predict the action index instead of the name.
143
+ domain: The `Domain` of the current model. The domain contains the actions
144
+ provided by the user + the default actions.
145
+ action_endpoint: Can be used to run `custom_actions`
146
+ (e.g. using the `rasa-sdk`).
147
+
148
+ Returns:
149
+ The instantiated `Action` or `None` if no `Action` was found for the given
150
+ index.
151
+ """
152
+ if domain.num_actions <= index or index < 0:
153
+ raise IndexError(
154
+ f"Cannot access action at index {index}. "
155
+ f"Domain has {domain.num_actions} actions."
156
+ )
157
+
158
+ return action_for_name_or_text(
159
+ domain.action_names_or_texts[index], domain, action_endpoint
160
+ )
161
+
162
+
163
+ def is_retrieval_action(action_name: Text, retrieval_intents: List[Text]) -> bool:
164
+ """Check if an action name is a retrieval action.
165
+
166
+ The name for a retrieval action has an extra `utter_` prefix added to
167
+ the corresponding retrieval intent name.
168
+
169
+ Args:
170
+ action_name: Name of the action.
171
+ retrieval_intents: List of retrieval intents defined in the NLU training data.
172
+
173
+ Returns:
174
+ `True` if the resolved intent name is present in the list of retrieval
175
+ intents, `False` otherwise.
176
+ """
177
+ return (
178
+ ActionRetrieveResponse.intent_name_from_action(action_name) in retrieval_intents
179
+ )
180
+
181
+
182
+ def action_for_name_or_text(
183
+ action_name_or_text: Text, domain: Domain, action_endpoint: Optional[EndpointConfig]
184
+ ) -> "Action":
185
+ """Retrieves an action by its name or by its text in case it's an end-to-end action.
186
+
187
+ Args:
188
+ action_name_or_text: The name of the action.
189
+ domain: The current model domain.
190
+ action_endpoint: The endpoint to execute custom actions.
191
+
192
+ Raises:
193
+ ActionNotFoundException: If action not in current domain.
194
+
195
+ Returns:
196
+ The instantiated action.
197
+ """
198
+ if action_name_or_text not in domain.action_names_or_texts:
199
+ domain.raise_action_not_found_exception(action_name_or_text)
200
+
201
+ defaults = {a.name(): a for a in default_actions(action_endpoint)}
202
+
203
+ if (
204
+ action_name_or_text in defaults
205
+ and action_name_or_text not in domain.user_actions_and_forms
206
+ ):
207
+ return defaults[action_name_or_text]
208
+
209
+ if action_name_or_text.startswith(UTTER_PREFIX) and is_retrieval_action(
210
+ action_name_or_text, domain.retrieval_intents
211
+ ):
212
+ return ActionRetrieveResponse(action_name_or_text)
213
+
214
+ if action_name_or_text in domain.action_texts:
215
+ return ActionEndToEndResponse(action_name_or_text)
216
+
217
+ if action_name_or_text.startswith(UTTER_PREFIX):
218
+ return ActionBotResponse(action_name_or_text)
219
+
220
+ is_form = action_name_or_text in domain.form_names
221
+ # Users can override the form by defining an action with the same name as the form
222
+ user_overrode_form_action = is_form and action_name_or_text in domain.user_actions
223
+ if is_form and not user_overrode_form_action:
224
+ from rasa.core.actions.forms import FormAction
225
+
226
+ return FormAction(action_name_or_text, action_endpoint)
227
+
228
+ if action_name_or_text.startswith(FLOW_PREFIX):
229
+ from rasa.core.actions.action_trigger_flow import ActionTriggerFlow
230
+
231
+ return ActionTriggerFlow(action_name_or_text)
232
+ return RemoteAction(action_name_or_text, action_endpoint)
233
+
234
+
235
+ def create_bot_utterance(message: Dict[Text, Any]) -> BotUttered:
236
+ """Create BotUttered event from message."""
237
+ bot_message = BotUttered(
238
+ text=message.pop("text", None),
239
+ data={
240
+ "elements": message.pop("elements", None),
241
+ "quick_replies": message.pop("quick_replies", None),
242
+ "buttons": message.pop("buttons", None),
243
+ # for legacy / compatibility reasons we need to set the image
244
+ # to be the attachment if there is no other attachment (the
245
+ # `.get` is intentional - no `pop` as we still need the image`
246
+ # property to set it in the following line)
247
+ "attachment": message.pop("attachment", None) or message.get("image", None),
248
+ "image": message.pop("image", None),
249
+ "custom": message.pop("custom", None),
250
+ },
251
+ metadata=message,
252
+ )
253
+ return bot_message
254
+
255
+
256
+ class Action:
257
+ """Next action to be taken in response to a dialogue state."""
258
+
259
+ def name(self) -> Text:
260
+ """Unique identifier of this simple action."""
261
+ raise NotImplementedError
262
+
263
+ async def run(
264
+ self,
265
+ output_channel: "OutputChannel",
266
+ nlg: "NaturalLanguageGenerator",
267
+ tracker: "DialogueStateTracker",
268
+ domain: "Domain",
269
+ metadata: Optional[Dict[Text, Any]] = None,
270
+ ) -> List[Event]:
271
+ """Execute the side effects of this action.
272
+
273
+ Args:
274
+ nlg: which ``nlg`` to use for response generation
275
+ output_channel: ``output_channel`` to which to send the resulting message.
276
+ tracker (DialogueStateTracker): the state tracker for the current
277
+ user. You can access slot values using
278
+ ``tracker.get_slot(slot_name)`` and the most recent user
279
+ message is ``tracker.latest_message.text``.
280
+ domain (Domain): the bot's domain
281
+ metadata: Additional information for the action.
282
+
283
+ Returns:
284
+ A list of :class:`rasa.core.events.Event` instances
285
+ """
286
+ raise NotImplementedError
287
+
288
+ def __str__(self) -> Text:
289
+ """Returns text representation of form."""
290
+ return f"{self.__class__.__name__}('{self.name()}')"
291
+
292
+ def event_for_successful_execution(
293
+ self, prediction: PolicyPrediction
294
+ ) -> ActionExecuted:
295
+ """Event which should be logged for the successful execution of this action.
296
+
297
+ Args:
298
+ prediction: Prediction which led to the execution of this event.
299
+
300
+ Returns:
301
+ Event which should be logged onto the tracker.
302
+ """
303
+ return ActionExecuted(
304
+ self.name(),
305
+ prediction.policy_name,
306
+ prediction.max_confidence,
307
+ hide_rule_turn=prediction.hide_rule_turn,
308
+ metadata=prediction.action_metadata,
309
+ )
310
+
311
+
312
+ class ActionBotResponse(Action):
313
+ """An action which only effect is to utter a response when it is run."""
314
+
315
+ def __init__(self, name: Text, silent_fail: Optional[bool] = False) -> None:
316
+ """Creates action.
317
+
318
+ Args:
319
+ name: Name of the action.
320
+ silent_fail: `True` if the action should fail silently in case no response
321
+ was defined for this action.
322
+ """
323
+ self.utter_action = name
324
+ self.silent_fail = silent_fail
325
+
326
+ async def run(
327
+ self,
328
+ output_channel: "OutputChannel",
329
+ nlg: "NaturalLanguageGenerator",
330
+ tracker: "DialogueStateTracker",
331
+ domain: "Domain",
332
+ metadata: Optional[Dict[Text, Any]] = None,
333
+ ) -> List[Event]:
334
+ """Simple run implementation uttering a (hopefully defined) response."""
335
+ kwargs = {
336
+ "domain_responses": domain.responses,
337
+ }
338
+
339
+ message = await nlg.generate(
340
+ self.utter_action,
341
+ tracker,
342
+ output_channel.name(),
343
+ **kwargs,
344
+ )
345
+ if message is None:
346
+ if not self.silent_fail:
347
+ logger.error(
348
+ "Couldn't create message for response '{}'."
349
+ "".format(self.utter_action)
350
+ )
351
+ return []
352
+ message["utter_action"] = self.utter_action
353
+
354
+ return [create_bot_utterance(message)]
355
+
356
+ def name(self) -> Text:
357
+ """Returns action name."""
358
+ return self.utter_action
359
+
360
+
361
+ class ActionEndToEndResponse(Action):
362
+ """Action to utter end-to-end responses to the user."""
363
+
364
+ def __init__(self, action_text: Text) -> None:
365
+ """Creates action.
366
+
367
+ Args:
368
+ action_text: Text of end-to-end bot response.
369
+ """
370
+ self.action_text = action_text
371
+
372
+ def name(self) -> Text:
373
+ """Returns action name."""
374
+ # In case of an end-to-end action there is no label (aka name) for the action.
375
+ # We fake a name by returning the text which the bot sends back to the user.
376
+ return self.action_text
377
+
378
+ async def run(
379
+ self,
380
+ output_channel: "OutputChannel",
381
+ nlg: "NaturalLanguageGenerator",
382
+ tracker: "DialogueStateTracker",
383
+ domain: "Domain",
384
+ metadata: Optional[Dict[Text, Any]] = None,
385
+ ) -> List[Event]:
386
+ """Runs action (see parent class for full docstring)."""
387
+ message = {"text": self.action_text}
388
+ return [create_bot_utterance(message)]
389
+
390
+ def event_for_successful_execution(
391
+ self, prediction: PolicyPrediction
392
+ ) -> ActionExecuted:
393
+ """Event which should be logged for the successful execution of this action.
394
+
395
+ Args:
396
+ prediction: Prediction which led to the execution of this event.
397
+
398
+ Returns:
399
+ Event which should be logged onto the tracker.
400
+ """
401
+ return ActionExecuted(
402
+ policy=prediction.policy_name,
403
+ confidence=prediction.max_confidence,
404
+ action_text=self.action_text,
405
+ hide_rule_turn=prediction.hide_rule_turn,
406
+ metadata=prediction.action_metadata,
407
+ )
408
+
409
+
410
+ class ActionRetrieveResponse(ActionBotResponse):
411
+ """An action which queries the Response Selector for the appropriate response."""
412
+
413
+ def __init__(self, name: Text, silent_fail: Optional[bool] = False) -> None:
414
+ """Creates action. See docstring of parent class."""
415
+ super().__init__(name, silent_fail)
416
+ self.action_name = name
417
+ self.silent_fail = silent_fail
418
+
419
+ @staticmethod
420
+ def intent_name_from_action(action_name: Text) -> Text:
421
+ """Resolve the name of the intent from the action name."""
422
+ return action_name.split(UTTER_PREFIX)[1]
423
+
424
+ def get_full_retrieval_name(
425
+ self, tracker: "DialogueStateTracker"
426
+ ) -> Optional[Text]:
427
+ """Returns full retrieval name for the action.
428
+
429
+ Extracts retrieval intent from response selector and
430
+ returns complete action utterance name.
431
+
432
+ Args:
433
+ tracker: Tracker containing past conversation events.
434
+
435
+ Returns:
436
+ Full retrieval name of the action if the last user utterance
437
+ contains a response selector output, `None` otherwise.
438
+ """
439
+ latest_message = tracker.latest_message
440
+
441
+ if latest_message is None:
442
+ return None
443
+
444
+ if RESPONSE_SELECTOR_PROPERTY_NAME not in latest_message.parse_data:
445
+ return None
446
+
447
+ response_selector_properties = latest_message.parse_data[
448
+ RESPONSE_SELECTOR_PROPERTY_NAME # type: ignore[literal-required]
449
+ ]
450
+
451
+ if (
452
+ self.intent_name_from_action(self.action_name)
453
+ in response_selector_properties
454
+ ):
455
+ query_key = self.intent_name_from_action(self.action_name)
456
+ elif RESPONSE_SELECTOR_DEFAULT_INTENT in response_selector_properties:
457
+ query_key = RESPONSE_SELECTOR_DEFAULT_INTENT
458
+ else:
459
+ return None
460
+
461
+ selected = response_selector_properties[query_key]
462
+ full_retrieval_utter_action = selected[RESPONSE_SELECTOR_PREDICTION_KEY][
463
+ RESPONSE_SELECTOR_UTTER_ACTION_KEY
464
+ ]
465
+ return full_retrieval_utter_action
466
+
467
+ async def run(
468
+ self,
469
+ output_channel: "OutputChannel",
470
+ nlg: "NaturalLanguageGenerator",
471
+ tracker: "DialogueStateTracker",
472
+ domain: "Domain",
473
+ metadata: Optional[Dict[Text, Any]] = None,
474
+ ) -> List[Event]:
475
+ """Query the appropriate response and create a bot utterance with that."""
476
+ latest_message = tracker.latest_message
477
+
478
+ if latest_message is None:
479
+ return []
480
+
481
+ response_selector_properties = latest_message.parse_data[
482
+ RESPONSE_SELECTOR_PROPERTY_NAME # type: ignore[literal-required]
483
+ ]
484
+
485
+ if (
486
+ self.intent_name_from_action(self.action_name)
487
+ in response_selector_properties
488
+ ):
489
+ query_key = self.intent_name_from_action(self.action_name)
490
+ elif RESPONSE_SELECTOR_DEFAULT_INTENT in response_selector_properties:
491
+ query_key = RESPONSE_SELECTOR_DEFAULT_INTENT
492
+ else:
493
+ if not self.silent_fail:
494
+ logger.error(
495
+ "Couldn't create message for response action '{}'."
496
+ "".format(self.action_name)
497
+ )
498
+ return []
499
+
500
+ logger.debug(f"Picking response from selector of type {query_key}")
501
+ selected = response_selector_properties[query_key]
502
+
503
+ # Override utter action of ActionBotResponse
504
+ # with the complete utter action retrieved from
505
+ # the output of response selector.
506
+ self.utter_action = selected[RESPONSE_SELECTOR_PREDICTION_KEY][
507
+ RESPONSE_SELECTOR_UTTER_ACTION_KEY
508
+ ]
509
+
510
+ return await super().run(output_channel, nlg, tracker, domain)
511
+
512
+ def name(self) -> Text:
513
+ """Returns action name."""
514
+ return self.action_name
515
+
516
+
517
+ class ActionBack(ActionBotResponse):
518
+ """Revert the tracker state by two user utterances."""
519
+
520
+ def name(self) -> Text:
521
+ """Returns action back name."""
522
+ return ACTION_BACK_NAME
523
+
524
+ def __init__(self) -> None:
525
+ """Initializes action back."""
526
+ super().__init__("utter_back", silent_fail=True)
527
+
528
+ async def run(
529
+ self,
530
+ output_channel: "OutputChannel",
531
+ nlg: "NaturalLanguageGenerator",
532
+ tracker: "DialogueStateTracker",
533
+ domain: "Domain",
534
+ metadata: Optional[Dict[Text, Any]] = None,
535
+ ) -> List[Event]:
536
+ """Runs action. Please see parent class for the full docstring."""
537
+ # only utter the response if it is available
538
+ evts = await super().run(output_channel, nlg, tracker, domain)
539
+
540
+ return evts + [UserUtteranceReverted(), UserUtteranceReverted()]
541
+
542
+
543
+ class ActionListen(Action):
544
+ """The first action in any turn - bot waits for a user message.
545
+
546
+ The bot should stop taking further actions and wait for the user to say
547
+ something.
548
+ """
549
+
550
+ def name(self) -> Text:
551
+ """Returns action listen name."""
552
+ return ACTION_LISTEN_NAME
553
+
554
+ async def run(
555
+ self,
556
+ output_channel: "OutputChannel",
557
+ nlg: "NaturalLanguageGenerator",
558
+ tracker: "DialogueStateTracker",
559
+ domain: "Domain",
560
+ metadata: Optional[Dict[Text, Any]] = None,
561
+ ) -> List[Event]:
562
+ """Runs action. Please see parent class for the full docstring."""
563
+ return []
564
+
565
+
566
+ class ActionResetRouting(Action):
567
+ """Resets the tracker to its initial state.
568
+
569
+ Utters the restart response if available.
570
+ """
571
+
572
+ def name(self) -> Text:
573
+ """Returns action restart name."""
574
+ return ACTION_RESET_ROUTING
575
+
576
+ async def run(
577
+ self,
578
+ output_channel: "OutputChannel",
579
+ nlg: "NaturalLanguageGenerator",
580
+ tracker: "DialogueStateTracker",
581
+ domain: "Domain",
582
+ metadata: Optional[Dict[Text, Any]] = None,
583
+ ) -> List[Event]:
584
+ """Runs action. Please see parent class for the full docstring."""
585
+ # SlotSet(ROUTE_TO_CALM_SLOT, None) is needed to ensure the routing slot
586
+ # is really reset to None and not just reset to an initial value
587
+ return [RoutingSessionEnded(), SlotSet(ROUTE_TO_CALM_SLOT, None)]
588
+
589
+
590
+ class ActionRestart(ActionBotResponse):
591
+ """Resets the tracker to its initial state.
592
+
593
+ Utters the restart response if available.
594
+ """
595
+
596
+ def name(self) -> Text:
597
+ """Returns action restart name."""
598
+ return ACTION_RESTART_NAME
599
+
600
+ def __init__(self) -> None:
601
+ """Initializes action restart."""
602
+ super().__init__("utter_restart", silent_fail=True)
603
+
604
+ async def run(
605
+ self,
606
+ output_channel: "OutputChannel",
607
+ nlg: "NaturalLanguageGenerator",
608
+ tracker: "DialogueStateTracker",
609
+ domain: "Domain",
610
+ metadata: Optional[Dict[Text, Any]] = None,
611
+ ) -> List[Event]:
612
+ """Runs action. Please see parent class for the full docstring."""
613
+ # only utter the response if it is available
614
+ evts = await super().run(output_channel, nlg, tracker, domain)
615
+
616
+ return evts + [Restarted()]
617
+
618
+
619
+ class ActionSessionStart(Action):
620
+ """Applies a conversation session start.
621
+
622
+ Takes all `SlotSet` events from the previous session and applies them to the new
623
+ session.
624
+ """
625
+
626
+ def name(self) -> Text:
627
+ """Returns action start name."""
628
+ return ACTION_SESSION_START_NAME
629
+
630
+ @staticmethod
631
+ def _slot_set_events_from_tracker(
632
+ tracker: "DialogueStateTracker",
633
+ ) -> List["SlotSet"]:
634
+ """Fetch SlotSet events from tracker and carry over key, value and metadata."""
635
+ return [
636
+ SlotSet(key=event.key, value=event.value, metadata=event.metadata)
637
+ for event in tracker.applied_events()
638
+ if isinstance(event, SlotSet)
639
+ ]
640
+
641
+ async def run(
642
+ self,
643
+ output_channel: "OutputChannel",
644
+ nlg: "NaturalLanguageGenerator",
645
+ tracker: "DialogueStateTracker",
646
+ domain: "Domain",
647
+ metadata: Optional[Dict[Text, Any]] = None,
648
+ ) -> List[Event]:
649
+ """Runs action. Please see parent class for the full docstring."""
650
+ _events: List[Event] = [SessionStarted()]
651
+
652
+ if domain.session_config.carry_over_slots:
653
+ _events.extend(self._slot_set_events_from_tracker(tracker))
654
+
655
+ _events.append(ActionExecuted(ACTION_LISTEN_NAME))
656
+
657
+ return _events
658
+
659
+
660
+ class ActionDefaultFallback(ActionBotResponse):
661
+ """Executes the fallback action and goes back to the prev state of the dialogue."""
662
+
663
+ def name(self) -> Text:
664
+ """Returns action default fallback name."""
665
+ return ACTION_DEFAULT_FALLBACK_NAME
666
+
667
+ def __init__(self) -> None:
668
+ """Initializes action default fallback."""
669
+ super().__init__("utter_default", silent_fail=True)
670
+
671
+ async def run(
672
+ self,
673
+ output_channel: "OutputChannel",
674
+ nlg: "NaturalLanguageGenerator",
675
+ tracker: "DialogueStateTracker",
676
+ domain: "Domain",
677
+ metadata: Optional[Dict[Text, Any]] = None,
678
+ ) -> List[Event]:
679
+ """Runs action. Please see parent class for the full docstring."""
680
+ # only utter the response if it is available
681
+ evts = await super().run(output_channel, nlg, tracker, domain)
682
+
683
+ return evts + [UserUtteranceReverted()]
684
+
685
+
686
+ class ActionDeactivateLoop(Action):
687
+ """Deactivates an active loop."""
688
+
689
+ def name(self) -> Text:
690
+ return ACTION_DEACTIVATE_LOOP_NAME
691
+
692
+ async def run(
693
+ self,
694
+ output_channel: "OutputChannel",
695
+ nlg: "NaturalLanguageGenerator",
696
+ tracker: "DialogueStateTracker",
697
+ domain: "Domain",
698
+ metadata: Optional[Dict[Text, Any]] = None,
699
+ ) -> List[Event]:
700
+ """Runs action. Please see parent class for the full docstring."""
701
+ return [ActiveLoop(None), SlotSet(REQUESTED_SLOT, None)]
702
+
703
+
704
+ class RemoteAction(Action):
705
+ def __init__(self, name: Text, action_endpoint: Optional[EndpointConfig]) -> None:
706
+
707
+ self._name = name
708
+ self.action_endpoint = action_endpoint
709
+
710
+ def _action_call_format(
711
+ self,
712
+ tracker: "DialogueStateTracker",
713
+ domain: "Domain",
714
+ ) -> Dict[Text, Any]:
715
+ """Create the request json send to the action server."""
716
+ from rasa.shared.core.trackers import EventVerbosity
717
+
718
+ tracker_state = tracker.current_state(EventVerbosity.ALL)
719
+
720
+ result = {
721
+ "next_action": self._name,
722
+ "sender_id": tracker.sender_id,
723
+ "tracker": tracker_state,
724
+ "version": rasa.__version__,
725
+ }
726
+
727
+ if (
728
+ not self._is_selective_domain_enabled()
729
+ or domain.does_custom_action_explicitly_need_domain(self.name())
730
+ ):
731
+ result["domain"] = domain.as_dict()
732
+
733
+ return result
734
+
735
+ def _is_selective_domain_enabled(self) -> bool:
736
+ if self.action_endpoint is None:
737
+ return False
738
+ return bool(
739
+ self.action_endpoint.kwargs.get(SELECTIVE_DOMAIN, DEFAULT_SELECTIVE_DOMAIN)
740
+ )
741
+
742
+ @staticmethod
743
+ def action_response_format_spec() -> Dict[Text, Any]:
744
+ """Expected response schema for an Action endpoint.
745
+
746
+ Used for validation of the response returned from the
747
+ Action endpoint.
748
+ """
749
+ schema = {
750
+ "type": "object",
751
+ "properties": {
752
+ "events": EVENTS_SCHEMA,
753
+ "responses": {"type": "array", "items": {"type": "object"}},
754
+ },
755
+ }
756
+ return schema
757
+
758
+ def _validate_action_result(self, result: Dict[Text, Any]) -> bool:
759
+ from jsonschema import validate
760
+ from jsonschema import ValidationError
761
+
762
+ try:
763
+ validate(result, self.action_response_format_spec())
764
+ return True
765
+ except ValidationError as e:
766
+ e.message += (
767
+ f". Failed to validate Action server response from API, "
768
+ f"make sure your response from the Action endpoint is valid. "
769
+ f"For more information about the format visit "
770
+ f"{DOCS_BASE_URL}/custom-actions"
771
+ )
772
+ raise e
773
+
774
+ @staticmethod
775
+ async def _utter_responses(
776
+ responses: List[Dict[Text, Any]],
777
+ output_channel: "OutputChannel",
778
+ nlg: "NaturalLanguageGenerator",
779
+ tracker: "DialogueStateTracker",
780
+ ) -> List[BotUttered]:
781
+ """Use the responses generated by the action endpoint and utter them."""
782
+ bot_messages = []
783
+ for response in responses:
784
+ generated_response = response.pop("response", None)
785
+ if generated_response:
786
+ draft = await nlg.generate(
787
+ generated_response, tracker, output_channel.name(), **response
788
+ )
789
+ if not draft:
790
+ continue
791
+ draft["utter_action"] = generated_response
792
+ else:
793
+ draft = {}
794
+
795
+ buttons = response.pop("buttons", []) or []
796
+ if buttons:
797
+ draft.setdefault("buttons", [])
798
+ draft["buttons"].extend(buttons)
799
+
800
+ # Avoid overwriting `draft` values with empty values
801
+ response = {k: v for k, v in response.items() if v}
802
+ draft.update(response)
803
+ bot_messages.append(create_bot_utterance(draft))
804
+
805
+ return bot_messages
806
+
807
+ async def run(
808
+ self,
809
+ output_channel: "OutputChannel",
810
+ nlg: "NaturalLanguageGenerator",
811
+ tracker: "DialogueStateTracker",
812
+ domain: "Domain",
813
+ metadata: Optional[Dict[Text, Any]] = None,
814
+ ) -> List[Event]:
815
+ """Runs action. Please see parent class for the full docstring."""
816
+ json_body = self._action_call_format(tracker, domain)
817
+ if not self.action_endpoint:
818
+ raise RasaException(
819
+ f"Failed to execute custom action '{self.name()}' "
820
+ f"because no endpoint is configured to run this "
821
+ f"custom action. Please take a look at "
822
+ f"the docs and set an endpoint configuration via the "
823
+ f"--endpoints flag. "
824
+ f"{DOCS_BASE_URL}/custom-actions"
825
+ )
826
+
827
+ try:
828
+ logger.debug(
829
+ "Calling action endpoint to run action '{}'.".format(self.name())
830
+ )
831
+
832
+ should_compress = get_bool_env_variable(
833
+ COMPRESS_ACTION_SERVER_REQUEST_ENV_NAME,
834
+ DEFAULT_COMPRESS_ACTION_SERVER_REQUEST,
835
+ )
836
+
837
+ response: Any = await self.action_endpoint.request(
838
+ json=json_body,
839
+ method="post",
840
+ timeout=DEFAULT_REQUEST_TIMEOUT,
841
+ compress=should_compress,
842
+ )
843
+ self._validate_action_result(response)
844
+
845
+ events_json = response.get("events", [])
846
+ responses = response.get("responses", [])
847
+ bot_messages = await self._utter_responses(
848
+ responses, output_channel, nlg, tracker
849
+ )
850
+
851
+ events = rasa.shared.core.events.deserialise_events(events_json)
852
+ return cast(List[Event], bot_messages) + events
853
+
854
+ except ClientResponseError as e:
855
+ if e.status == 400:
856
+ response_data = json.loads(e.text)
857
+ exception = ActionExecutionRejection(
858
+ response_data["action_name"], response_data.get("error")
859
+ )
860
+ logger.error(exception.message)
861
+ raise exception
862
+ else:
863
+ raise RasaException(
864
+ f"Failed to execute custom action '{self.name()}'"
865
+ ) from e
866
+
867
+ except aiohttp.ClientConnectionError as e:
868
+ logger.error(
869
+ f"Failed to run custom action '{self.name()}'. Couldn't connect "
870
+ f"to the server at '{self.action_endpoint.url}'. "
871
+ f"Is the server running? "
872
+ f"Error: {e}"
873
+ )
874
+ raise RasaException(
875
+ f"Failed to execute custom action '{self.name()}'. Couldn't connect "
876
+ f"to the server at '{self.action_endpoint.url}."
877
+ )
878
+
879
+ except aiohttp.ClientError as e:
880
+ # not all errors have a status attribute, but
881
+ # helpful to log if they got it
882
+
883
+ # noinspection PyUnresolvedReferences
884
+ status = getattr(e, "status", None)
885
+ raise RasaException(
886
+ "Failed to run custom action '{}'. Action server "
887
+ "responded with a non 200 status code of {}. "
888
+ "Make sure your action server properly runs actions "
889
+ "and returns a 200 once the action is executed. "
890
+ "Error: {}".format(self.name(), status, e)
891
+ )
892
+
893
+ def name(self) -> Text:
894
+ return self._name
895
+
896
+
897
+ class ActionExecutionRejection(RasaException):
898
+ """Raising this exception allows other policies to predict a different action."""
899
+
900
+ def __init__(self, action_name: Text, message: Optional[Text] = None) -> None:
901
+ """Create a new ActionExecutionRejection exception."""
902
+ self.action_name = action_name
903
+ self.message = message or "Custom action '{}' rejected to run".format(
904
+ action_name
905
+ )
906
+ super(ActionExecutionRejection, self).__init__()
907
+
908
+ def __str__(self) -> Text:
909
+ return self.message
910
+
911
+
912
+ class ActionRevertFallbackEvents(Action):
913
+ """Reverts events which were done during the `TwoStageFallbackPolicy`.
914
+
915
+ This reverts user messages and bot utterances done during a fallback
916
+ of the `TwoStageFallbackPolicy`. By doing so it is not necessary to
917
+ write custom stories for the different paths, but only of the happy
918
+ path. This is deprecated and can be removed once the
919
+ `TwoStageFallbackPolicy` is removed.
920
+ """
921
+
922
+ def name(self) -> Text:
923
+ return ACTION_REVERT_FALLBACK_EVENTS_NAME
924
+
925
+ async def run(
926
+ self,
927
+ output_channel: "OutputChannel",
928
+ nlg: "NaturalLanguageGenerator",
929
+ tracker: "DialogueStateTracker",
930
+ domain: "Domain",
931
+ metadata: Optional[Dict[Text, Any]] = None,
932
+ ) -> List[Event]:
933
+ """Runs action. Please see parent class for the full docstring."""
934
+ from rasa.core.policies.two_stage_fallback import has_user_rephrased
935
+
936
+ # User rephrased
937
+ if has_user_rephrased(tracker):
938
+ return _revert_successful_rephrasing(tracker)
939
+ # User affirmed
940
+ elif has_user_affirmed(tracker):
941
+ return _revert_affirmation_events(tracker)
942
+ else:
943
+ return []
944
+
945
+
946
+ class ActionUnlikelyIntent(Action):
947
+ """An action that indicates that the intent predicted by NLU is unexpected.
948
+
949
+ This action can be predicted by `UnexpecTEDIntentPolicy`.
950
+ """
951
+
952
+ def name(self) -> Text:
953
+ """Returns the name of the action."""
954
+ return ACTION_UNLIKELY_INTENT_NAME
955
+
956
+ async def run(
957
+ self,
958
+ output_channel: "OutputChannel",
959
+ nlg: "NaturalLanguageGenerator",
960
+ tracker: "DialogueStateTracker",
961
+ domain: "Domain",
962
+ metadata: Optional[Dict[Text, Any]] = None,
963
+ ) -> List[Event]:
964
+ """Runs action. Please see parent class for the full docstring."""
965
+ return []
966
+
967
+
968
+ def has_user_affirmed(tracker: "DialogueStateTracker") -> bool:
969
+ """Indicates if the last executed action is `action_default_ask_affirmation`."""
970
+ return tracker.last_executed_action_has(ACTION_DEFAULT_ASK_AFFIRMATION_NAME)
971
+
972
+
973
+ def _revert_affirmation_events(tracker: "DialogueStateTracker") -> List[Event]:
974
+ revert_events = _revert_single_affirmation_events()
975
+
976
+ # User affirms the rephrased intent
977
+ rephrased_intent = tracker.last_executed_action_has(
978
+ name=ACTION_DEFAULT_ASK_REPHRASE_NAME, skip=1
979
+ )
980
+ if rephrased_intent:
981
+ revert_events += _revert_rephrasing_events()
982
+
983
+ last_user_event = tracker.get_last_event_for(UserUttered)
984
+ if not last_user_event:
985
+ raise TypeError("Cannot find last event to revert to.")
986
+
987
+ last_user_event = copy.deepcopy(last_user_event)
988
+ # FIXME: better type annotation for `parse_data` would require
989
+ # a larger refactoring (e.g. switch to dataclass)
990
+ last_user_event.parse_data["intent"]["confidence"] = 1.0 # type: ignore[typeddict-item] # noqa: E501
991
+
992
+ return revert_events + [last_user_event]
993
+
994
+
995
+ def _revert_single_affirmation_events() -> List[Event]:
996
+ return [
997
+ UserUtteranceReverted(), # revert affirmation and request
998
+ # revert original intent (has to be re-added later)
999
+ UserUtteranceReverted(),
1000
+ # add action listen intent
1001
+ ActionExecuted(action_name=ACTION_LISTEN_NAME),
1002
+ ]
1003
+
1004
+
1005
+ def _revert_successful_rephrasing(tracker: "DialogueStateTracker") -> List[Event]:
1006
+ last_user_event = tracker.get_last_event_for(UserUttered)
1007
+ if not last_user_event:
1008
+ raise TypeError("Cannot find last event to revert to.")
1009
+
1010
+ last_user_event = copy.deepcopy(last_user_event)
1011
+ return _revert_rephrasing_events() + [last_user_event]
1012
+
1013
+
1014
+ def _revert_rephrasing_events() -> List[Event]:
1015
+ return [
1016
+ UserUtteranceReverted(), # remove rephrasing
1017
+ # remove feedback and rephrase request
1018
+ UserUtteranceReverted(),
1019
+ # remove affirmation request and false intent
1020
+ UserUtteranceReverted(),
1021
+ # replace action with action listen
1022
+ ActionExecuted(action_name=ACTION_LISTEN_NAME),
1023
+ ]
1024
+
1025
+
1026
+ class ActionDefaultAskAffirmation(Action):
1027
+ """Default implementation which asks the user to affirm his intent.
1028
+
1029
+ It is suggested to overwrite this default action with a custom action
1030
+ to have more meaningful prompts for the affirmations. E.g. have a
1031
+ description of the intent instead of its identifier name.
1032
+ """
1033
+
1034
+ def name(self) -> Text:
1035
+ return ACTION_DEFAULT_ASK_AFFIRMATION_NAME
1036
+
1037
+ async def run(
1038
+ self,
1039
+ output_channel: "OutputChannel",
1040
+ nlg: "NaturalLanguageGenerator",
1041
+ tracker: "DialogueStateTracker",
1042
+ domain: "Domain",
1043
+ metadata: Optional[Dict[Text, Any]] = None,
1044
+ ) -> List[Event]:
1045
+ """Runs action. Please see parent class for the full docstring."""
1046
+ latest_message = tracker.latest_message
1047
+ if latest_message is None:
1048
+ raise TypeError(
1049
+ "Cannot find last user message for detecting fallback affirmation."
1050
+ )
1051
+
1052
+ intent_to_affirm = latest_message.intent.get(INTENT_NAME_KEY)
1053
+
1054
+ # FIXME: better type annotation for `parse_data` would require
1055
+ # a larger refactoring (e.g. switch to dataclass)
1056
+ intent_ranking = cast(
1057
+ List["IntentPrediction"],
1058
+ latest_message.parse_data.get(INTENT_RANKING_KEY) or [],
1059
+ )
1060
+ if (
1061
+ intent_to_affirm == DEFAULT_NLU_FALLBACK_INTENT_NAME
1062
+ and len(intent_ranking) > 1
1063
+ ):
1064
+ intent_to_affirm = intent_ranking[1][INTENT_NAME_KEY] # type: ignore[literal-required] # noqa: E501
1065
+
1066
+ affirmation_message = f"Did you mean '{intent_to_affirm}'?"
1067
+
1068
+ message = {
1069
+ "text": affirmation_message,
1070
+ "buttons": [
1071
+ {"title": "Yes", "payload": f"/{intent_to_affirm}"},
1072
+ {"title": "No", "payload": f"/{USER_INTENT_OUT_OF_SCOPE}"},
1073
+ ],
1074
+ "utter_action": self.name(),
1075
+ }
1076
+
1077
+ return [create_bot_utterance(message)]
1078
+
1079
+
1080
+ class ActionDefaultAskRephrase(ActionBotResponse):
1081
+ """Default implementation which asks the user to rephrase his intent."""
1082
+
1083
+ def name(self) -> Text:
1084
+ """Returns action default ask rephrase name."""
1085
+ return ACTION_DEFAULT_ASK_REPHRASE_NAME
1086
+
1087
+ def __init__(self) -> None:
1088
+ """Initializes action default ask rephrase."""
1089
+ super().__init__("utter_ask_rephrase", silent_fail=True)
1090
+
1091
+
1092
+ class ActionSendText(Action):
1093
+ """Sends a text message to the output channel."""
1094
+
1095
+ def name(self) -> Text:
1096
+ return ACTION_SEND_TEXT_NAME
1097
+
1098
+ async def run(
1099
+ self,
1100
+ output_channel: "OutputChannel",
1101
+ nlg: "NaturalLanguageGenerator",
1102
+ tracker: "DialogueStateTracker",
1103
+ domain: "Domain",
1104
+ metadata: Optional[Dict[Text, Any]] = None,
1105
+ ) -> List[Event]:
1106
+ """Runs action. Please see parent class for the full docstring."""
1107
+ fallback = {"text": ""}
1108
+ message = metadata.get("message", fallback) if metadata else fallback
1109
+ return [create_bot_utterance(message)]
1110
+
1111
+
1112
+ class ActionExtractSlots(Action):
1113
+ """Default action that runs after each user turn.
1114
+
1115
+ Action is executed automatically in MessageProcessor.handle_message(...)
1116
+ before the next predicted action is run.
1117
+
1118
+ Set slots to extracted values from user message
1119
+ according to assigned slot mappings.
1120
+ """
1121
+
1122
+ def __init__(self, action_endpoint: Optional[EndpointConfig]) -> None:
1123
+ """Initializes default action extract slots."""
1124
+ self._action_endpoint = action_endpoint
1125
+
1126
+ def name(self) -> Text:
1127
+ """Returns action_extract_slots name."""
1128
+ return ACTION_EXTRACT_SLOTS
1129
+
1130
+ @staticmethod
1131
+ def _matches_mapping_conditions(
1132
+ mapping: Dict[Text, Any], tracker: "DialogueStateTracker", slot_name: Text
1133
+ ) -> bool:
1134
+ slot_mapping_conditions = mapping.get(MAPPING_CONDITIONS)
1135
+
1136
+ if not slot_mapping_conditions:
1137
+ return True
1138
+
1139
+ if (
1140
+ tracker.is_active_loop_rejected
1141
+ and tracker.get_slot(REQUESTED_SLOT) == slot_name
1142
+ ):
1143
+ return False
1144
+
1145
+ # check if found mapping conditions matches form
1146
+ for condition in slot_mapping_conditions:
1147
+ active_loop = condition.get(ACTIVE_LOOP)
1148
+
1149
+ if active_loop and active_loop == tracker.active_loop_name:
1150
+ condition_requested_slot = condition.get(REQUESTED_SLOT)
1151
+ if not condition_requested_slot:
1152
+ return True
1153
+ if condition_requested_slot == tracker.get_slot(REQUESTED_SLOT):
1154
+ return True
1155
+
1156
+ if active_loop is None and tracker.active_loop_name is None:
1157
+ return True
1158
+
1159
+ return False
1160
+
1161
+ @staticmethod
1162
+ def _verify_mapping_conditions(
1163
+ mapping: Dict[Text, Any], tracker: "DialogueStateTracker", slot_name: Text
1164
+ ) -> bool:
1165
+ if mapping.get(MAPPING_CONDITIONS) and mapping[MAPPING_TYPE] != str(
1166
+ SlotMappingType.FROM_TRIGGER_INTENT
1167
+ ):
1168
+ if not ActionExtractSlots._matches_mapping_conditions(
1169
+ mapping, tracker, slot_name
1170
+ ):
1171
+ return False
1172
+
1173
+ return True
1174
+
1175
+ async def _run_custom_action(
1176
+ self,
1177
+ custom_action: Text,
1178
+ output_channel: "OutputChannel",
1179
+ nlg: "NaturalLanguageGenerator",
1180
+ tracker: "DialogueStateTracker",
1181
+ domain: "Domain",
1182
+ ) -> List[Event]:
1183
+ slot_events: List[Event] = []
1184
+ remote_action = RemoteAction(custom_action, self._action_endpoint)
1185
+ disallowed_types = set()
1186
+
1187
+ try:
1188
+ custom_events = await remote_action.run(
1189
+ output_channel, nlg, tracker, domain
1190
+ )
1191
+ for event in custom_events:
1192
+ if isinstance(event, SlotSet):
1193
+ slot_events.append(event)
1194
+ elif isinstance(event, BotUttered):
1195
+ slot_events.append(event)
1196
+ else:
1197
+ disallowed_types.add(event.type_name)
1198
+ except (RasaException, ClientResponseError) as e:
1199
+ logger.warning(
1200
+ f"Failed to execute custom action '{custom_action}' "
1201
+ f"as a result of error '{e!s}'. The default action "
1202
+ f"'{self.name()}' failed to fill slots with custom "
1203
+ f"mappings."
1204
+ )
1205
+
1206
+ for type_name in disallowed_types:
1207
+ logger.info(
1208
+ f"Running custom action '{custom_action}' has resulted "
1209
+ f"in an event of type '{type_name}'. This is "
1210
+ f"disallowed and the tracker will not be "
1211
+ f"updated with this event."
1212
+ )
1213
+
1214
+ return slot_events
1215
+
1216
+ async def _execute_custom_action(
1217
+ self,
1218
+ mapping: Dict[Text, Any],
1219
+ executed_custom_actions: Set[Text],
1220
+ output_channel: "OutputChannel",
1221
+ nlg: "NaturalLanguageGenerator",
1222
+ tracker: "DialogueStateTracker",
1223
+ domain: "Domain",
1224
+ ) -> Tuple[List[Event], Set[Text]]:
1225
+ custom_action = mapping.get("action")
1226
+
1227
+ if not custom_action or custom_action in executed_custom_actions:
1228
+ return [], executed_custom_actions
1229
+
1230
+ slot_events = await self._run_custom_action(
1231
+ custom_action, output_channel, nlg, tracker, domain
1232
+ )
1233
+
1234
+ executed_custom_actions.add(custom_action)
1235
+
1236
+ return slot_events, executed_custom_actions
1237
+
1238
+ async def _execute_validation_action(
1239
+ self,
1240
+ extraction_events: List[Event],
1241
+ output_channel: "OutputChannel",
1242
+ nlg: "NaturalLanguageGenerator",
1243
+ tracker: "DialogueStateTracker",
1244
+ domain: "Domain",
1245
+ ) -> List[Event]:
1246
+ slot_events: List[SlotSet] = [
1247
+ event for event in extraction_events if isinstance(event, SlotSet)
1248
+ ]
1249
+
1250
+ slot_candidates = "\n".join([e.key for e in slot_events])
1251
+ logger.debug(f"Validating extracted slots: {slot_candidates}")
1252
+
1253
+ if ACTION_VALIDATE_SLOT_MAPPINGS not in domain.user_actions:
1254
+ return cast(List[Event], slot_events)
1255
+
1256
+ _tracker = DialogueStateTracker.from_events(
1257
+ tracker.sender_id,
1258
+ tracker.events_after_latest_restart() + cast(List[Event], slot_events),
1259
+ slots=domain.slots,
1260
+ )
1261
+ validate_events = await self._run_custom_action(
1262
+ ACTION_VALIDATE_SLOT_MAPPINGS, output_channel, nlg, _tracker, domain
1263
+ )
1264
+ validated_slot_names = [
1265
+ event.key for event in validate_events if isinstance(event, SlotSet)
1266
+ ]
1267
+
1268
+ # If the custom action doesn't return a SlotSet event for an extracted slot
1269
+ # candidate we assume that it was valid. The custom action has to return a
1270
+ # SlotSet(slot_name, None) event to mark a Slot as invalid.
1271
+ return validate_events + [
1272
+ event for event in slot_events if event.key not in validated_slot_names
1273
+ ]
1274
+
1275
+ def _fails_unique_entity_mapping_check(
1276
+ self,
1277
+ slot_name: Text,
1278
+ mapping: Dict[Text, Any],
1279
+ tracker: "DialogueStateTracker",
1280
+ domain: "Domain",
1281
+ ) -> bool:
1282
+ from rasa.core.actions.forms import FormAction
1283
+
1284
+ if mapping[MAPPING_TYPE] != str(SlotMappingType.FROM_ENTITY):
1285
+ return False
1286
+
1287
+ form_name = tracker.active_loop_name
1288
+
1289
+ if not form_name:
1290
+ return False
1291
+
1292
+ if tracker.get_slot(REQUESTED_SLOT) == slot_name:
1293
+ return False
1294
+
1295
+ form = FormAction(form_name, self._action_endpoint)
1296
+
1297
+ if slot_name not in form.required_slots(domain):
1298
+ return False
1299
+
1300
+ if form.entity_mapping_is_unique(mapping, domain):
1301
+ return False
1302
+
1303
+ return True
1304
+
1305
+ async def run(
1306
+ self,
1307
+ output_channel: "OutputChannel",
1308
+ nlg: "NaturalLanguageGenerator",
1309
+ tracker: "DialogueStateTracker",
1310
+ domain: "Domain",
1311
+ metadata: Optional[Dict[Text, Any]] = None,
1312
+ ) -> List[Event]:
1313
+ """Runs action. Please see parent class for the full docstring."""
1314
+ slot_events: List[Event] = []
1315
+ executed_custom_actions: Set[Text] = set()
1316
+
1317
+ user_slots = [
1318
+ slot
1319
+ for slot in domain.slots
1320
+ if slot.name not in DEFAULT_SLOT_NAMES | KNOWLEDGE_BASE_SLOT_NAMES
1321
+ ]
1322
+
1323
+ for slot in user_slots:
1324
+ for mapping in slot.mappings:
1325
+ mapping_type = SlotMappingType(mapping.get(MAPPING_TYPE))
1326
+
1327
+ if not SlotMapping.check_mapping_validity(
1328
+ slot_name=slot.name,
1329
+ mapping_type=mapping_type,
1330
+ mapping=mapping,
1331
+ domain=domain,
1332
+ ):
1333
+ continue
1334
+
1335
+ intent_is_desired = SlotMapping.intent_is_desired(
1336
+ mapping, tracker, domain
1337
+ )
1338
+
1339
+ if not intent_is_desired:
1340
+ continue
1341
+
1342
+ if not ActionExtractSlots._verify_mapping_conditions(
1343
+ mapping, tracker, slot.name
1344
+ ):
1345
+ continue
1346
+
1347
+ if self._fails_unique_entity_mapping_check(
1348
+ slot.name, mapping, tracker, domain
1349
+ ):
1350
+ continue
1351
+
1352
+ if mapping_type.is_predefined_type():
1353
+ value = extract_slot_value_from_predefined_mapping(
1354
+ mapping_type, mapping, tracker
1355
+ )
1356
+ else:
1357
+ value = None
1358
+
1359
+ if value:
1360
+ if not isinstance(slot, ListSlot):
1361
+ value = value[-1]
1362
+
1363
+ if value is not None or tracker.get_slot(slot.name) is not None:
1364
+ slot_events.append(SlotSet(slot.name, value))
1365
+ break
1366
+
1367
+ should_fill_custom_slot = mapping_type == SlotMappingType.CUSTOM
1368
+
1369
+ if should_fill_custom_slot:
1370
+ (
1371
+ custom_evts,
1372
+ executed_custom_actions,
1373
+ ) = await self._execute_custom_action(
1374
+ mapping,
1375
+ executed_custom_actions,
1376
+ output_channel,
1377
+ nlg,
1378
+ tracker,
1379
+ domain,
1380
+ )
1381
+ slot_events.extend(custom_evts)
1382
+
1383
+ validated_events = await self._execute_validation_action(
1384
+ slot_events, output_channel, nlg, tracker, domain
1385
+ )
1386
+ return validated_events
1387
+
1388
+
1389
+ def extract_slot_value_from_predefined_mapping(
1390
+ mapping_type: SlotMappingType,
1391
+ mapping: Dict[Text, Any],
1392
+ tracker: "DialogueStateTracker",
1393
+ ) -> List[Any]:
1394
+ """Extracts slot value if slot has an applicable predefined mapping."""
1395
+ if tracker.has_bot_message_after_latest_user_message():
1396
+ # TODO: this needs further validation - not sure if this breaks something!!!
1397
+
1398
+ # If the bot sent a message after the user sent a message, we can't
1399
+ # extract any slots from the user message. We assume that the user
1400
+ # message was already processed by the bot and the slot value was
1401
+ # already extracted (e.g. for a prior form slot).
1402
+ return []
1403
+
1404
+ should_fill_entity_slot = (
1405
+ mapping_type == SlotMappingType.FROM_ENTITY
1406
+ and SlotMapping.entity_is_desired(mapping, tracker)
1407
+ )
1408
+
1409
+ should_fill_intent_slot = mapping_type == SlotMappingType.FROM_INTENT
1410
+
1411
+ should_fill_text_slot = mapping_type == SlotMappingType.FROM_TEXT
1412
+
1413
+ active_loops_in_mapping_conditions = [
1414
+ active_loop.get(ACTIVE_LOOP)
1415
+ for active_loop in mapping.get(MAPPING_CONDITIONS, [])
1416
+ ]
1417
+
1418
+ trigger_mapping_condition_met = True
1419
+
1420
+ if tracker.active_loop_name is None:
1421
+ trigger_mapping_condition_met = False
1422
+ elif (
1423
+ active_loops_in_mapping_conditions
1424
+ and tracker.active_loop_name is not None
1425
+ and (tracker.active_loop_name not in active_loops_in_mapping_conditions)
1426
+ ):
1427
+ trigger_mapping_condition_met = False
1428
+
1429
+ should_fill_trigger_slot = (
1430
+ mapping_type == SlotMappingType.FROM_TRIGGER_INTENT
1431
+ and trigger_mapping_condition_met
1432
+ )
1433
+
1434
+ value: List[Any] = []
1435
+ if should_fill_entity_slot:
1436
+ value = list(
1437
+ tracker.get_latest_entity_values(
1438
+ mapping.get(ENTITY_ATTRIBUTE_TYPE),
1439
+ mapping.get(ENTITY_ATTRIBUTE_ROLE),
1440
+ mapping.get(ENTITY_ATTRIBUTE_GROUP),
1441
+ )
1442
+ )
1443
+ elif should_fill_intent_slot or should_fill_trigger_slot:
1444
+ value = [mapping.get("value")]
1445
+ elif should_fill_text_slot:
1446
+ value = [
1447
+ tracker.latest_message.text if tracker.latest_message is not None else None
1448
+ ]
1449
+
1450
+ return value