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,2106 @@
1
+ import copy
2
+ import collections
3
+ import json
4
+ import structlog
5
+ import os
6
+ from pathlib import Path
7
+ from typing import (
8
+ Any,
9
+ Dict,
10
+ List,
11
+ NoReturn,
12
+ Optional,
13
+ Set,
14
+ Text,
15
+ Tuple,
16
+ Union,
17
+ TYPE_CHECKING,
18
+ Iterable,
19
+ MutableMapping,
20
+ NamedTuple,
21
+ Callable,
22
+ cast,
23
+ )
24
+ from dataclasses import dataclass
25
+
26
+ from ruamel.yaml.scalarstring import DoubleQuotedScalarString
27
+
28
+ from rasa.shared.constants import (
29
+ DEFAULT_SESSION_EXPIRATION_TIME_IN_MINUTES,
30
+ DEFAULT_CARRY_OVER_SLOTS_TO_NEW_SESSION,
31
+ DOMAIN_SCHEMA_FILE,
32
+ DOCS_URL_DOMAINS,
33
+ DOCS_URL_FORMS,
34
+ LATEST_TRAINING_DATA_FORMAT_VERSION,
35
+ DOCS_URL_RESPONSES,
36
+ REQUIRED_SLOTS_KEY,
37
+ IGNORED_INTENTS,
38
+ RESPONSE_CONDITION,
39
+ )
40
+ from rasa.shared.core.constants import (
41
+ ACTION_SHOULD_SEND_DOMAIN,
42
+ SLOT_MAPPINGS,
43
+ SlotMappingType,
44
+ MAPPING_TYPE,
45
+ MAPPING_CONDITIONS,
46
+ KNOWLEDGE_BASE_SLOT_NAMES,
47
+ ACTIVE_LOOP,
48
+ )
49
+ from rasa.shared.exceptions import (
50
+ RasaException,
51
+ YamlException,
52
+ YamlSyntaxException,
53
+ )
54
+ from rasa.shared.utils.cli import print_error_and_exit
55
+ import rasa.shared.utils.io
56
+ import rasa.shared.utils.common
57
+ import rasa.shared.core.slot_mappings
58
+ from rasa.shared.core.events import SlotSet, UserUttered
59
+ from rasa.shared.core.slots import (
60
+ Slot,
61
+ CategoricalSlot,
62
+ TextSlot,
63
+ AnySlot,
64
+ ListSlot,
65
+ )
66
+ from rasa.shared.utils.yaml import (
67
+ KEY_TRAINING_DATA_FORMAT_VERSION,
68
+ read_yaml,
69
+ validate_training_data_format_version,
70
+ read_yaml_file,
71
+ dump_obj_as_yaml_to_string,
72
+ validate_raw_yaml_using_schema_file_with_responses,
73
+ )
74
+ from rasa.shared.nlu.constants import (
75
+ ENTITY_ATTRIBUTE_TYPE,
76
+ ENTITY_ATTRIBUTE_ROLE,
77
+ ENTITY_ATTRIBUTE_GROUP,
78
+ RESPONSE_IDENTIFIER_DELIMITER,
79
+ INTENT_NAME_KEY,
80
+ ENTITIES,
81
+ )
82
+
83
+
84
+ if TYPE_CHECKING:
85
+ from rasa.shared.core.trackers import DialogueStateTracker
86
+
87
+ CARRY_OVER_SLOTS_KEY = "carry_over_slots_to_new_session"
88
+ SESSION_EXPIRATION_TIME_KEY = "session_expiration_time"
89
+ SESSION_CONFIG_KEY = "session_config"
90
+ USED_ENTITIES_KEY = "used_entities"
91
+ USE_ENTITIES_KEY = "use_entities"
92
+ IGNORE_ENTITIES_KEY = "ignore_entities"
93
+ IS_RETRIEVAL_INTENT_KEY = "is_retrieval_intent"
94
+ ENTITY_ROLES_KEY = "roles"
95
+ ENTITY_GROUPS_KEY = "groups"
96
+ ENTITY_FEATURIZATION_KEY = "influence_conversation"
97
+
98
+ KEY_SLOTS = "slots"
99
+ KEY_INTENTS = "intents"
100
+ KEY_ENTITIES = "entities"
101
+ KEY_RESPONSES = "responses"
102
+ KEY_ACTIONS = "actions"
103
+ KEY_FORMS = "forms"
104
+ KEY_E2E_ACTIONS = "e2e_actions"
105
+ KEY_RESPONSES_TEXT = "text"
106
+ KEY_RESPONSES_IMAGE = "image"
107
+ KEY_RESPONSES_CUSTOM = "custom"
108
+ KEY_RESPONSES_BUTTONS = "buttons"
109
+ KEY_RESPONSES_ATTACHMENT = "attachment"
110
+ KEY_RESPONSES_QUICK_REPLIES = "quick_replies"
111
+
112
+ RESPONSE_KEYS_TO_INTERPOLATE = [
113
+ KEY_RESPONSES_TEXT,
114
+ KEY_RESPONSES_IMAGE,
115
+ KEY_RESPONSES_CUSTOM,
116
+ KEY_RESPONSES_BUTTONS,
117
+ KEY_RESPONSES_ATTACHMENT,
118
+ KEY_RESPONSES_QUICK_REPLIES,
119
+ ]
120
+
121
+ ALL_DOMAIN_KEYS = [
122
+ KEY_SLOTS,
123
+ KEY_FORMS,
124
+ KEY_ACTIONS,
125
+ KEY_ENTITIES,
126
+ KEY_INTENTS,
127
+ KEY_RESPONSES,
128
+ KEY_E2E_ACTIONS,
129
+ SESSION_CONFIG_KEY,
130
+ ]
131
+
132
+ PREV_PREFIX = "prev_"
133
+
134
+ # State is a dictionary with keys (USER, PREVIOUS_ACTION, SLOTS, ACTIVE_LOOP)
135
+ # representing the origin of a SubState;
136
+ # the values are SubStates, that contain the information needed for featurization
137
+ SubStateValue = Union[Text, Tuple[Union[float, Text], ...]]
138
+ SubState = MutableMapping[Text, SubStateValue]
139
+ State = Dict[Text, SubState]
140
+
141
+ structlogger = structlog.get_logger(__name__)
142
+
143
+
144
+ class InvalidDomain(RasaException):
145
+ """Exception that can be raised when domain is not valid."""
146
+
147
+
148
+ class ActionNotFoundException(ValueError, RasaException):
149
+ """Raised when an action name could not be found."""
150
+
151
+
152
+ class SessionConfig(NamedTuple):
153
+ """The Session Configuration."""
154
+
155
+ session_expiration_time: float # in minutes
156
+ carry_over_slots: bool
157
+
158
+ @staticmethod
159
+ def default() -> "SessionConfig":
160
+ """Returns the SessionConfig with the default values."""
161
+ return SessionConfig(
162
+ DEFAULT_SESSION_EXPIRATION_TIME_IN_MINUTES,
163
+ DEFAULT_CARRY_OVER_SLOTS_TO_NEW_SESSION,
164
+ )
165
+
166
+ def are_sessions_enabled(self) -> bool:
167
+ """Returns a boolean value depending on the value of session_expiration_time."""
168
+ return self.session_expiration_time > 0
169
+
170
+ def as_dict(self) -> Dict:
171
+ """Return serialized `SessionConfig`."""
172
+ return {
173
+ "session_expiration_time": self.session_expiration_time,
174
+ "carry_over_slots_to_new_session": self.carry_over_slots,
175
+ }
176
+
177
+
178
+ @dataclass
179
+ class EntityProperties:
180
+ """Class for keeping track of the properties of entities in the domain."""
181
+
182
+ entities: List[Text]
183
+ roles: Dict[Text, List[Text]]
184
+ groups: Dict[Text, List[Text]]
185
+ default_ignored_entities: List[Text]
186
+
187
+
188
+ class Domain:
189
+ """The domain specifies the universe in which the bot's policy acts.
190
+
191
+ A Domain subclass provides the actions the bot can take, the intents
192
+ and entities it can recognise.
193
+ """
194
+
195
+ @classmethod
196
+ def empty(cls) -> "Domain":
197
+ """Returns empty Domain."""
198
+ return Domain.from_dict({})
199
+
200
+ @classmethod
201
+ def load(cls, paths: Union[List[Union[Path, Text]], Text, Path]) -> "Domain":
202
+ """Returns loaded Domain after merging all domain files."""
203
+ if not paths:
204
+ raise InvalidDomain(
205
+ "No domain file was specified. Please specify a path "
206
+ "to a valid domain file."
207
+ )
208
+ elif not isinstance(paths, list) and not isinstance(paths, set):
209
+ paths = [paths]
210
+
211
+ domain = Domain.empty()
212
+ for path in paths:
213
+ other = cls.from_path(path)
214
+ domain = domain.merge(other)
215
+
216
+ return domain
217
+
218
+ @classmethod
219
+ def from_path(cls, path: Union[Text, Path]) -> "Domain":
220
+ """Loads the `Domain` from a path."""
221
+ path = os.path.abspath(path)
222
+
223
+ if os.path.isfile(path):
224
+ domain = cls.from_file(path)
225
+ elif os.path.isdir(path):
226
+ domain = cls.from_directory(path)
227
+ else:
228
+ raise InvalidDomain(
229
+ "Failed to load domain specification from '{}'. "
230
+ "File not found!".format(os.path.abspath(path))
231
+ )
232
+
233
+ return domain
234
+
235
+ @classmethod
236
+ def from_file(cls, path: Text) -> "Domain":
237
+ """Loads the `Domain` from a YAML file."""
238
+ return cls.from_yaml(rasa.shared.utils.io.read_file(path), path)
239
+
240
+ @classmethod
241
+ def from_yaml(cls, yaml: Text, original_filename: Text = "") -> "Domain":
242
+ """Loads the `Domain` from YAML text after validating it."""
243
+ try:
244
+ validate_raw_yaml_using_schema_file_with_responses(yaml, DOMAIN_SCHEMA_FILE)
245
+
246
+ data = read_yaml(yaml)
247
+ if not validate_training_data_format_version(data, original_filename):
248
+ return Domain.empty()
249
+ return cls.from_dict(data)
250
+ except YamlException as e:
251
+ e.filename = original_filename
252
+ raise e
253
+
254
+ @classmethod
255
+ def from_dict(cls, data: Dict) -> "Domain":
256
+ """Deserializes and creates domain.
257
+
258
+ Args:
259
+ data: The serialized domain.
260
+
261
+ Returns:
262
+ The instantiated `Domain` object.
263
+ """
264
+ responses = data.get(KEY_RESPONSES, {})
265
+
266
+ domain_slots = data.get(KEY_SLOTS, {})
267
+ if domain_slots:
268
+ rasa.shared.core.slot_mappings.validate_slot_mappings(domain_slots)
269
+ slots = cls.collect_slots(domain_slots)
270
+ domain_actions = data.get(KEY_ACTIONS, [])
271
+ actions = cls._collect_action_names(domain_actions)
272
+
273
+ additional_arguments = {
274
+ **data.get("config", {}),
275
+ "actions_which_explicitly_need_domain": cls._collect_actions_which_explicitly_need_domain( # noqa: E501
276
+ domain_actions
277
+ ),
278
+ }
279
+ session_config = cls._get_session_config(data.get(SESSION_CONFIG_KEY, {}))
280
+ intents = data.get(KEY_INTENTS, {})
281
+
282
+ forms = data.get(KEY_FORMS, {})
283
+ _validate_forms(forms)
284
+
285
+ return cls(
286
+ intents=intents,
287
+ entities=data.get(KEY_ENTITIES, {}),
288
+ slots=slots,
289
+ responses=responses,
290
+ action_names=actions,
291
+ forms=data.get(KEY_FORMS, {}),
292
+ data=Domain._cleaned_data(data),
293
+ action_texts=data.get(KEY_E2E_ACTIONS, []),
294
+ session_config=session_config,
295
+ **additional_arguments,
296
+ )
297
+
298
+ @staticmethod
299
+ def _get_session_config(session_config: Dict) -> SessionConfig:
300
+ session_expiration_time_min = session_config.get(SESSION_EXPIRATION_TIME_KEY)
301
+
302
+ if session_expiration_time_min is None:
303
+ session_expiration_time_min = DEFAULT_SESSION_EXPIRATION_TIME_IN_MINUTES
304
+
305
+ carry_over_slots = session_config.get(
306
+ CARRY_OVER_SLOTS_KEY, DEFAULT_CARRY_OVER_SLOTS_TO_NEW_SESSION
307
+ )
308
+
309
+ return SessionConfig(session_expiration_time_min, carry_over_slots)
310
+
311
+ @classmethod
312
+ def from_directory(cls, path: Text) -> "Domain":
313
+ """Loads and merges multiple domain files recursively from a directory tree."""
314
+ combined: Dict[Text, Any] = {}
315
+ duplicates: List[Dict[Text, List[Text]]] = []
316
+
317
+ for root, _, files in os.walk(path, followlinks=True):
318
+ for file in files:
319
+
320
+ full_path = os.path.join(root, file)
321
+ if not Domain.is_domain_file(full_path):
322
+ continue
323
+
324
+ # does the validation here only
325
+ _ = Domain.from_file(full_path)
326
+
327
+ other_dict = read_yaml(rasa.shared.utils.io.read_file(full_path))
328
+ combined = Domain.merge_domain_dicts(other_dict, combined)
329
+ duplicates.append(combined.pop("duplicates", {}))
330
+
331
+ Domain._handle_duplicates_from_multiple_files(duplicates)
332
+
333
+ domain = Domain.from_dict(combined)
334
+ return domain
335
+
336
+ @staticmethod
337
+ def _handle_duplicates_from_multiple_files(
338
+ duplicates_from_multiple_files: List[Dict[Text, List[Text]]]
339
+ ) -> None:
340
+ combined_duplicates: Dict[Text, List[Text]] = collections.defaultdict(list)
341
+
342
+ for duplicates in duplicates_from_multiple_files:
343
+ duplicates = rasa.shared.utils.common.clean_duplicates(duplicates)
344
+
345
+ for key in duplicates.keys():
346
+ combined_duplicates[key].extend(duplicates[key])
347
+
348
+ # handle duplicated responses by raising an error
349
+ duplicated_responses = combined_duplicates.pop(KEY_RESPONSES, [])
350
+ Domain._handle_duplicate_responses(duplicated_responses)
351
+
352
+ # warn about other duplicates
353
+ warn_about_duplicates_found_during_domain_merging(combined_duplicates)
354
+
355
+ @staticmethod
356
+ def _handle_duplicate_responses(response_duplicates: List[Text]) -> None:
357
+ if response_duplicates:
358
+ for response in response_duplicates:
359
+ structlogger.error(
360
+ "domain.duplicate_response",
361
+ response=response,
362
+ event_info=(
363
+ f"Response '{response}' is defined in multiple domains. "
364
+ f"Please make sure this response is only defined in one domain."
365
+ ),
366
+ )
367
+ print_error_and_exit(
368
+ "Unable to merge domains due to duplicate responses in domain."
369
+ )
370
+
371
+ def merge(
372
+ self,
373
+ domain: Optional["Domain"],
374
+ override: bool = False,
375
+ ignore_warnings_about_duplicates: bool = False,
376
+ ) -> "Domain":
377
+ """Merges this domain dict with another one, combining their attributes.
378
+
379
+ This method merges domain dicts, and ensures all attributes (like ``intents``,
380
+ ``entities``, and ``actions``) are known to the Domain when the
381
+ object is created.
382
+
383
+ List attributes like ``intents`` and ``actions`` are deduped
384
+ and merged. Single attributes are taken from `domain1` unless
385
+ override is `True`, in which case they are taken from `domain2`.
386
+ """
387
+ if not domain or domain.is_empty():
388
+ return self
389
+
390
+ if self.is_empty():
391
+ return domain
392
+
393
+ combined = self.__class__.merge_domain_dicts(
394
+ domain.as_dict(), self.as_dict(), override
395
+ )
396
+
397
+ duplicates = combined.pop("duplicates", {})
398
+
399
+ if not ignore_warnings_about_duplicates:
400
+ warn_about_duplicates_found_during_domain_merging(duplicates)
401
+
402
+ return Domain.from_dict(combined)
403
+
404
+ @staticmethod
405
+ def merge_domain_dicts(
406
+ domain_dict: Dict,
407
+ combined: Dict,
408
+ override: bool = False,
409
+ ) -> Dict:
410
+ """Combines two domain dictionaries."""
411
+ if not domain_dict:
412
+ return combined
413
+
414
+ if not combined:
415
+ return domain_dict
416
+
417
+ if override:
418
+ config = domain_dict.get("config", {})
419
+ for key, val in config.items():
420
+ combined["config"][key] = val
421
+
422
+ if (
423
+ override
424
+ or combined.get(SESSION_CONFIG_KEY) == SessionConfig.default().as_dict()
425
+ or combined.get(SESSION_CONFIG_KEY) is None
426
+ ) and domain_dict.get(SESSION_CONFIG_KEY) not in [
427
+ None,
428
+ SessionConfig.default().as_dict(),
429
+ ]:
430
+ combined[SESSION_CONFIG_KEY] = domain_dict[SESSION_CONFIG_KEY]
431
+
432
+ # remove existing forms from new actions
433
+ for form in combined.get(KEY_FORMS, []):
434
+ if form in domain_dict.get(KEY_ACTIONS, []):
435
+ domain_dict[KEY_ACTIONS].remove(form)
436
+
437
+ duplicates: Dict[Text, List[Text]] = {}
438
+
439
+ merge_func_mappings: Dict[Text, Callable[..., Any]] = {
440
+ KEY_INTENTS: rasa.shared.utils.common.merge_lists_of_dicts,
441
+ KEY_ENTITIES: rasa.shared.utils.common.merge_lists_of_dicts,
442
+ KEY_ACTIONS: rasa.shared.utils.common.merge_lists_of_dicts,
443
+ KEY_E2E_ACTIONS: rasa.shared.utils.common.merge_lists,
444
+ KEY_FORMS: rasa.shared.utils.common.merge_dicts,
445
+ KEY_RESPONSES: rasa.shared.utils.common.merge_dicts,
446
+ KEY_SLOTS: rasa.shared.utils.common.merge_dicts,
447
+ }
448
+
449
+ for key, merge_func in merge_func_mappings.items():
450
+ duplicates[key] = rasa.shared.utils.common.extract_duplicates(
451
+ combined.get(key, []), domain_dict.get(key, [])
452
+ )
453
+
454
+ default: Union[List[Any], Dict[Text, Any]] = (
455
+ {} if merge_func == rasa.shared.utils.common.merge_dicts else []
456
+ )
457
+
458
+ combined[key] = merge_func(
459
+ combined.get(key, default), domain_dict.get(key, default), override
460
+ )
461
+
462
+ if duplicates:
463
+ combined.update({"duplicates": duplicates})
464
+
465
+ return combined
466
+
467
+ def _preprocess_domain_dict(
468
+ self,
469
+ data: Dict,
470
+ store_entities_as_slots: bool,
471
+ session_config: SessionConfig,
472
+ ) -> Dict:
473
+ data = self._add_default_keys_to_domain_dict(
474
+ data,
475
+ store_entities_as_slots,
476
+ session_config,
477
+ )
478
+ data = self._sanitize_intents_in_domain_dict(data)
479
+
480
+ return data
481
+
482
+ @staticmethod
483
+ def _add_default_keys_to_domain_dict(
484
+ data: Dict,
485
+ store_entities_as_slots: bool,
486
+ session_config: SessionConfig,
487
+ ) -> Dict:
488
+ # add the config, session_config and training data version defaults
489
+ # if not included in the original domain dict
490
+ if "config" not in data and not store_entities_as_slots:
491
+ data.update(
492
+ {"config": {"store_entities_as_slots": store_entities_as_slots}}
493
+ )
494
+
495
+ if SESSION_CONFIG_KEY not in data:
496
+ data.update(
497
+ {
498
+ SESSION_CONFIG_KEY: {
499
+ SESSION_EXPIRATION_TIME_KEY: (
500
+ session_config.session_expiration_time
501
+ ),
502
+ CARRY_OVER_SLOTS_KEY: session_config.carry_over_slots,
503
+ }
504
+ }
505
+ )
506
+
507
+ if KEY_TRAINING_DATA_FORMAT_VERSION not in data:
508
+ data.update(
509
+ {
510
+ KEY_TRAINING_DATA_FORMAT_VERSION: DoubleQuotedScalarString(
511
+ LATEST_TRAINING_DATA_FORMAT_VERSION
512
+ )
513
+ }
514
+ )
515
+
516
+ return data
517
+
518
+ @staticmethod
519
+ def _reset_intent_flags(intent: Dict[Text, Any]) -> None:
520
+ for intent_property in intent.values():
521
+ if (
522
+ USE_ENTITIES_KEY in intent_property.keys()
523
+ and not intent_property[USE_ENTITIES_KEY]
524
+ ):
525
+ intent_property[USE_ENTITIES_KEY] = []
526
+ if (
527
+ IGNORE_ENTITIES_KEY in intent_property.keys()
528
+ and not intent_property[IGNORE_ENTITIES_KEY]
529
+ ):
530
+ intent_property[IGNORE_ENTITIES_KEY] = []
531
+
532
+ @staticmethod
533
+ def _sanitize_intents_in_domain_dict(data: Dict[Text, Any]) -> Dict[Text, Any]:
534
+ if not data.get(KEY_INTENTS):
535
+ return data
536
+
537
+ for intent in data.get(KEY_INTENTS, []):
538
+ if isinstance(intent, dict):
539
+ Domain._reset_intent_flags(intent)
540
+
541
+ data[KEY_INTENTS] = Domain._sort_intent_names_alphabetical_order(
542
+ intents=data.get(KEY_INTENTS)
543
+ )
544
+
545
+ return data
546
+
547
+ @staticmethod
548
+ def collect_slots(slot_dict: Dict[Text, Any]) -> List[Slot]:
549
+ """Collects a list of slots from a dictionary."""
550
+ slots = []
551
+ # make a copy to not alter the input dictionary
552
+ slot_dict = copy.deepcopy(slot_dict)
553
+ # Don't sort the slots, see https://github.com/RasaHQ/rasa-x/issues/3900
554
+ for slot_name in slot_dict:
555
+ slot_type = slot_dict[slot_name].pop("type", None)
556
+ slot_class = Slot.resolve_by_type(slot_type)
557
+
558
+ if SLOT_MAPPINGS not in slot_dict[slot_name]:
559
+ structlogger.debug(
560
+ "domain.collect_slots.no_mappings_defined",
561
+ event_info=(
562
+ f"Slot '{slot_name}' has no mappings defined. "
563
+ f"We will continue with an empty list of mappings."
564
+ ),
565
+ )
566
+ slot_dict[slot_name][SLOT_MAPPINGS] = []
567
+
568
+ slot = slot_class(slot_name, **slot_dict[slot_name])
569
+ slots.append(slot)
570
+ return slots
571
+
572
+ @staticmethod
573
+ def _transform_intent_properties_for_internal_use(
574
+ intent: Dict[Text, Any], entity_properties: EntityProperties
575
+ ) -> Dict[Text, Any]:
576
+ """Transforms the intent's parameters in a format suitable for internal use.
577
+
578
+ When an intent is retrieved from the `domain.yml` file, it contains two
579
+ parameters, the `use_entities` and the `ignore_entities` parameter.
580
+ With the values of these two parameters the Domain class is updated, a new
581
+ parameter is added to the intent called `used_entities` and the two
582
+ previous parameters are deleted. This happens because internally only the
583
+ parameter `used_entities` is needed to list all the entities that should be
584
+ used for this intent.
585
+
586
+ Args:
587
+ intent: The intent as retrieved from the `domain.yml` file thus having two
588
+ parameters, the `use_entities` and the `ignore_entities` parameter.
589
+ entity_properties: Entity properties as provided by the domain file.
590
+
591
+ Returns:
592
+ The intent with the new format thus having only one parameter called
593
+ `used_entities` since this is the expected format of the intent
594
+ when used internally.
595
+ """
596
+ name, properties = next(iter(intent.items()))
597
+
598
+ if properties:
599
+ properties.setdefault(USE_ENTITIES_KEY, True)
600
+ else:
601
+ raise InvalidDomain(
602
+ f"In the `domain.yml` file, the intent '{name}' cannot have value of"
603
+ f" `{type(properties)}`. If you have placed a ':' character after the"
604
+ f" intent's name without adding any additional parameters to this"
605
+ f" intent then you would need to remove the ':' character. Please see"
606
+ f" {rasa.shared.constants.DOCS_URL_DOMAINS} for more information on how"
607
+ f" to correctly add `intents` in the `domain` and"
608
+ f" {rasa.shared.constants.DOCS_URL_INTENTS} for examples on"
609
+ f" when to use the ':' character after an intent's name."
610
+ )
611
+
612
+ properties.setdefault(
613
+ IGNORE_ENTITIES_KEY, entity_properties.default_ignored_entities
614
+ )
615
+ if not properties[USE_ENTITIES_KEY]: # this covers False, None and []
616
+ properties[USE_ENTITIES_KEY] = []
617
+
618
+ # `use_entities` is either a list of explicitly included entities
619
+ # or `True` if all should be included
620
+ # if the listed entities have a role or group label, concatenate the entity
621
+ # label with the corresponding role or group label to make sure roles and
622
+ # groups can also influence the dialogue predictions
623
+ if properties[USE_ENTITIES_KEY] is True:
624
+ included_entities = set(entity_properties.entities) - set(
625
+ entity_properties.default_ignored_entities
626
+ )
627
+ included_entities.update(
628
+ Domain.concatenate_entity_labels(entity_properties.roles)
629
+ )
630
+ included_entities.update(
631
+ Domain.concatenate_entity_labels(entity_properties.groups)
632
+ )
633
+ else:
634
+ included_entities = set(properties[USE_ENTITIES_KEY])
635
+ for entity in list(included_entities):
636
+ included_entities.update(
637
+ Domain.concatenate_entity_labels(entity_properties.roles, entity)
638
+ )
639
+ included_entities.update(
640
+ Domain.concatenate_entity_labels(entity_properties.groups, entity)
641
+ )
642
+ excluded_entities = set(properties[IGNORE_ENTITIES_KEY])
643
+ for entity in list(excluded_entities):
644
+ excluded_entities.update(
645
+ Domain.concatenate_entity_labels(entity_properties.roles, entity)
646
+ )
647
+ excluded_entities.update(
648
+ Domain.concatenate_entity_labels(entity_properties.groups, entity)
649
+ )
650
+ used_entities = list(included_entities - excluded_entities)
651
+ used_entities.sort()
652
+
653
+ # Only print warning for ambiguous configurations if entities were included
654
+ # explicitly.
655
+ explicitly_included = isinstance(properties[USE_ENTITIES_KEY], list)
656
+ ambiguous_entities = included_entities.intersection(excluded_entities)
657
+ if explicitly_included and ambiguous_entities:
658
+ structlogger.warning(
659
+ "domain.ambiguous_entities",
660
+ intent=name,
661
+ entities=ambiguous_entities,
662
+ event_info=(
663
+ f"Entities: '{ambiguous_entities}' are "
664
+ f"explicitly included and excluded for "
665
+ f"intent '{name}'. Excluding takes precedence "
666
+ f"in this case. Please resolve that ambiguity."
667
+ ),
668
+ docs=f"{DOCS_URL_DOMAINS}",
669
+ )
670
+
671
+ properties[USED_ENTITIES_KEY] = used_entities
672
+ del properties[USE_ENTITIES_KEY]
673
+ del properties[IGNORE_ENTITIES_KEY]
674
+
675
+ return intent
676
+
677
+ @rasa.shared.utils.common.lazy_property
678
+ def retrieval_intents(self) -> List[Text]:
679
+ """List retrieval intents present in the domain."""
680
+ return [
681
+ intent
682
+ for intent in self.intent_properties
683
+ if self.intent_properties[intent].get(IS_RETRIEVAL_INTENT_KEY)
684
+ ]
685
+
686
+ @classmethod
687
+ def collect_entity_properties(
688
+ cls, domain_entities: List[Union[Text, Dict[Text, Any]]]
689
+ ) -> EntityProperties:
690
+ """Get entity properties for a domain from what is provided by a domain file.
691
+
692
+ Args:
693
+ domain_entities: The entities as provided by a domain file.
694
+
695
+ Returns:
696
+ An instance of EntityProperties.
697
+ """
698
+ entity_properties = EntityProperties([], {}, {}, [])
699
+ for entity in domain_entities:
700
+ if isinstance(entity, str):
701
+ entity_properties.entities.append(entity)
702
+ elif isinstance(entity, dict):
703
+ for _entity, sub_labels in entity.items():
704
+ entity_properties.entities.append(_entity)
705
+ if sub_labels:
706
+ if ENTITY_ROLES_KEY in sub_labels:
707
+ entity_properties.roles[_entity] = sub_labels[
708
+ ENTITY_ROLES_KEY
709
+ ]
710
+ if ENTITY_GROUPS_KEY in sub_labels:
711
+ entity_properties.groups[_entity] = sub_labels[
712
+ ENTITY_GROUPS_KEY
713
+ ]
714
+ if (
715
+ ENTITY_FEATURIZATION_KEY in sub_labels
716
+ and sub_labels[ENTITY_FEATURIZATION_KEY] is False
717
+ ):
718
+ entity_properties.default_ignored_entities.append(_entity)
719
+ else:
720
+ raise InvalidDomain(
721
+ f"In the `domain.yml` file, the entity '{_entity}' cannot"
722
+ f" have value of `{type(sub_labels)}`. If you have placed a"
723
+ f" ':' character after the entity `{_entity}` without"
724
+ f" adding any additional parameters to this entity then you"
725
+ f" would need to remove the ':' character. Please see"
726
+ f" {rasa.shared.constants.DOCS_URL_DOMAINS} for more"
727
+ f" information on how to correctly add `entities` in the"
728
+ f" `domain` and {rasa.shared.constants.DOCS_URL_ENTITIES}"
729
+ f" for examples on when to use the ':' character after an"
730
+ f" entity's name."
731
+ )
732
+ else:
733
+ raise InvalidDomain(
734
+ f"Invalid domain. Entity is invalid, type of entity '{entity}' "
735
+ f"not supported: '{type(entity).__name__}'"
736
+ )
737
+
738
+ return entity_properties
739
+
740
+ @classmethod
741
+ def collect_intent_properties(
742
+ cls,
743
+ intents: List[Union[Text, Dict[Text, Any]]],
744
+ entity_properties: EntityProperties,
745
+ ) -> Dict[Text, Dict[Text, Union[bool, List]]]:
746
+ """Get intent properties for a domain from what is provided by a domain file.
747
+
748
+ Args:
749
+ intents: The intents as provided by a domain file.
750
+ entity_properties: Entity properties as provided by the domain file.
751
+
752
+ Returns:
753
+ The intent properties to be stored in the domain.
754
+ """
755
+ # make a copy to not alter the input argument
756
+ intents = copy.deepcopy(intents)
757
+ intent_properties: Dict[Text, Any] = {}
758
+ duplicates = set()
759
+
760
+ for intent in intents:
761
+ intent_name, properties = cls._intent_properties(intent, entity_properties)
762
+
763
+ if intent_name in intent_properties.keys():
764
+ duplicates.add(intent_name)
765
+
766
+ intent_properties.update(properties)
767
+
768
+ if duplicates:
769
+ raise InvalidDomain(
770
+ f"Intents are not unique! Found multiple intents "
771
+ f"with name(s) {sorted(duplicates)}. "
772
+ f"Either rename or remove the duplicate ones."
773
+ )
774
+
775
+ cls._add_default_intents(intent_properties, entity_properties)
776
+
777
+ return intent_properties
778
+
779
+ @classmethod
780
+ def _intent_properties(
781
+ cls, intent: Union[Text, Dict[Text, Any]], entity_properties: EntityProperties
782
+ ) -> Tuple[Text, Dict[Text, Any]]:
783
+ if not isinstance(intent, dict):
784
+ intent_name = intent
785
+ intent = {
786
+ intent_name: {
787
+ USE_ENTITIES_KEY: True,
788
+ IGNORE_ENTITIES_KEY: entity_properties.default_ignored_entities,
789
+ }
790
+ }
791
+ else:
792
+ intent_name = next(iter(intent.keys()))
793
+
794
+ return (
795
+ intent_name,
796
+ cls._transform_intent_properties_for_internal_use(
797
+ intent, entity_properties
798
+ ),
799
+ )
800
+
801
+ @classmethod
802
+ def _add_default_intents(
803
+ cls,
804
+ intent_properties: Dict[Text, Dict[Text, Union[bool, List]]],
805
+ entity_properties: EntityProperties,
806
+ ) -> None:
807
+ for intent_name in rasa.shared.core.constants.DEFAULT_INTENTS:
808
+ if intent_name not in intent_properties:
809
+ _, properties = cls._intent_properties(intent_name, entity_properties)
810
+ intent_properties.update(properties)
811
+
812
+ def __init__(
813
+ self,
814
+ intents: Union[Set[Text], List[Text], List[Dict[Text, Any]]],
815
+ entities: List[Union[Text, Dict[Text, Any]]],
816
+ slots: List[Slot],
817
+ responses: Dict[Text, List[Dict[Text, Any]]],
818
+ action_names: List[Text],
819
+ forms: Union[Dict[Text, Any], List[Text]],
820
+ data: Dict,
821
+ action_texts: Optional[List[Text]] = None,
822
+ store_entities_as_slots: bool = True,
823
+ session_config: SessionConfig = SessionConfig.default(),
824
+ **kwargs: Any,
825
+ ) -> None:
826
+ """Create a `Domain`.
827
+
828
+ Args:
829
+ intents: Intent labels.
830
+ entities: The names of entities which might be present in user messages.
831
+ slots: Slots to store information during the conversation.
832
+ responses: Bot responses. If an action with the same name is executed, it
833
+ will send the matching response to the user.
834
+ action_names: Names of custom actions.
835
+ forms: Form names and their slot mappings.
836
+ data: original domain dict representation.
837
+ action_texts: End-to-End bot utterances from end-to-end stories.
838
+ store_entities_as_slots: If `True` Rasa will automatically create `SlotSet`
839
+ events for entities if there are slots with the same name as the entity.
840
+ session_config: Configuration for conversation sessions. Conversations are
841
+ restarted at the end of a session.
842
+ kwargs: Additional arguments.
843
+ """
844
+ self.entity_properties = self.collect_entity_properties(entities)
845
+ self.intent_properties = self.collect_intent_properties(
846
+ intents, self.entity_properties
847
+ )
848
+ self.overridden_default_intents = self._collect_overridden_default_intents(
849
+ intents
850
+ )
851
+
852
+ self.form_names, self.forms, overridden_form_actions = self._initialize_forms(
853
+ forms
854
+ )
855
+
856
+ action_names += overridden_form_actions
857
+
858
+ self.responses = responses
859
+
860
+ self.action_texts = action_texts if action_texts is not None else []
861
+
862
+ data_copy = copy.deepcopy(data)
863
+ self._data = self._preprocess_domain_dict(
864
+ data_copy,
865
+ store_entities_as_slots,
866
+ session_config,
867
+ )
868
+
869
+ self.session_config = session_config
870
+
871
+ self._custom_actions = action_names
872
+ self._actions_which_explicitly_need_domain = (
873
+ kwargs.get("actions_which_explicitly_need_domain") or []
874
+ )
875
+
876
+ # only includes custom actions and utterance actions
877
+ self.user_actions = self._combine_with_responses(action_names, responses)
878
+
879
+ # includes all action names (custom, utterance, default actions and forms)
880
+ # and action texts from end-to-end bot utterances
881
+ self.action_names_or_texts = (
882
+ self._combine_user_with_default_actions(self.user_actions)
883
+ + [
884
+ form_name
885
+ for form_name in self.form_names
886
+ if form_name not in self._custom_actions
887
+ ]
888
+ + self.action_texts
889
+ )
890
+
891
+ self._user_slots = copy.copy(slots)
892
+ self.slots = slots
893
+ self._add_default_slots()
894
+ self.store_entities_as_slots = store_entities_as_slots
895
+ self._check_domain_sanity()
896
+
897
+ def __deepcopy__(self, memo: Optional[Dict[int, Any]]) -> "Domain":
898
+ """Enables making a deep copy of the `Domain` using `copy.deepcopy`.
899
+
900
+ See https://docs.python.org/3/library/copy.html#copy.deepcopy
901
+ for more implementation.
902
+
903
+ Args:
904
+ memo: Optional dictionary of objects already copied during the current
905
+ copying pass.
906
+
907
+ Returns:
908
+ A deep copy of the current domain.
909
+ """
910
+ domain_dict = self.as_dict()
911
+ return self.__class__.from_dict(copy.deepcopy(domain_dict, memo))
912
+
913
+ def count_conditional_response_variations(self) -> int:
914
+ """Returns count of conditional response variations."""
915
+ count = 0
916
+ for response_variations in self.responses.values():
917
+ for variation in response_variations:
918
+ if RESPONSE_CONDITION in variation:
919
+ count += 1
920
+
921
+ return count
922
+
923
+ @staticmethod
924
+ def _collect_overridden_default_intents(
925
+ intents: Union[Set[Text], List[Text], List[Dict[Text, Any]]]
926
+ ) -> List[Text]:
927
+ """Collects the default intents overridden by the user.
928
+
929
+ Args:
930
+ intents: User-provided intents.
931
+
932
+ Returns:
933
+ User-defined intents that are default intents.
934
+ """
935
+ intent_names: Set[Text] = {
936
+ next(iter(intent.keys())) if isinstance(intent, dict) else intent
937
+ for intent in intents
938
+ }
939
+ return sorted(
940
+ intent_names.intersection(set(rasa.shared.core.constants.DEFAULT_INTENTS))
941
+ )
942
+
943
+ @staticmethod
944
+ def _initialize_forms(
945
+ forms: Dict[Text, Any]
946
+ ) -> Tuple[List[Text], Dict[Text, Any], List[Text]]:
947
+ """Retrieves the initial values for the Domain's form fields.
948
+
949
+ Args:
950
+ forms: Parsed content of the `forms` section in the domain.
951
+
952
+ Returns:
953
+ The form names, a mapping of form names and required slots, and custom
954
+ actions.
955
+ Returning custom actions for each forms means that Rasa Open Source should
956
+ not use the default `FormAction` for the forms, but rather a custom action
957
+ for it. This can e.g. be used to run the deprecated Rasa Open Source 1
958
+ `FormAction` which is implemented in the Rasa SDK.
959
+ """
960
+ for form_name, form_data in forms.items():
961
+ if form_data is not None and REQUIRED_SLOTS_KEY not in form_data:
962
+ forms[form_name] = {REQUIRED_SLOTS_KEY: form_data}
963
+ return list(forms.keys()), forms, []
964
+
965
+ def __hash__(self) -> int:
966
+ """Returns a unique hash for the domain."""
967
+ return int(self.fingerprint(), 16)
968
+
969
+ def fingerprint(self) -> Text:
970
+ """Returns a unique hash for the domain which is stable across python runs.
971
+
972
+ Returns:
973
+ fingerprint of the domain
974
+ """
975
+ self_as_dict = self.as_dict()
976
+ transformed_intents: List[Text] = []
977
+ for intent in self_as_dict.get(KEY_INTENTS, []):
978
+ if isinstance(intent, dict):
979
+ transformed_intents.append(*intent.keys())
980
+ elif isinstance(intent, str):
981
+ transformed_intents.append(intent)
982
+
983
+ self_as_dict[KEY_INTENTS] = sorted(transformed_intents)
984
+ self_as_dict[KEY_ACTIONS] = self.action_names_or_texts
985
+ return rasa.shared.utils.io.get_dictionary_fingerprint(self_as_dict)
986
+
987
+ @staticmethod
988
+ def _sort_intent_names_alphabetical_order(
989
+ intents: List[Union[Text, Dict]]
990
+ ) -> List[Union[Text, Dict]]:
991
+ def sort(elem: Union[Text, Dict]) -> Union[Text, Dict]:
992
+ if isinstance(elem, dict):
993
+ return next(iter(elem.keys()))
994
+ elif isinstance(elem, str):
995
+ return elem
996
+
997
+ sorted_intents = sorted(intents, key=sort)
998
+ return sorted_intents
999
+
1000
+ @rasa.shared.utils.common.lazy_property
1001
+ def user_actions_and_forms(self) -> List[Text]:
1002
+ """Returns combination of user actions and forms."""
1003
+ return self.user_actions + self.form_names
1004
+
1005
+ @rasa.shared.utils.common.lazy_property
1006
+ def num_actions(self) -> int:
1007
+ """Returns the number of available actions."""
1008
+ # noinspection PyTypeChecker
1009
+ return len(self.action_names_or_texts)
1010
+
1011
+ @rasa.shared.utils.common.lazy_property
1012
+ def num_states(self) -> int:
1013
+ """Number of used input states for the action prediction."""
1014
+ return len(self.input_states)
1015
+
1016
+ @rasa.shared.utils.common.lazy_property
1017
+ def retrieval_intent_responses(self) -> Dict[Text, List[Dict[Text, Any]]]:
1018
+ """Return only the responses which are defined for retrieval intents."""
1019
+ return dict(
1020
+ filter(
1021
+ lambda intent_response: self.is_retrieval_intent_response(
1022
+ intent_response
1023
+ ),
1024
+ self.responses.items(),
1025
+ )
1026
+ )
1027
+
1028
+ @staticmethod
1029
+ def is_retrieval_intent_response(
1030
+ response: Tuple[Text, List[Dict[Text, Any]]]
1031
+ ) -> bool:
1032
+ """Check if the response is for a retrieval intent.
1033
+
1034
+ These responses have a `/` symbol in their name. Use that to filter them from
1035
+ the rest.
1036
+ """
1037
+ return RESPONSE_IDENTIFIER_DELIMITER in response[0]
1038
+
1039
+ def _add_default_slots(self) -> None:
1040
+ """Sets up the default slots and slot values for the domain."""
1041
+ self._add_requested_slot()
1042
+ self._add_flow_slots()
1043
+ self._add_knowledge_base_slots()
1044
+ self._add_categorical_slot_default_value()
1045
+ self._add_session_metadata_slot()
1046
+
1047
+ def _add_categorical_slot_default_value(self) -> None:
1048
+ """Add a default value to all categorical slots.
1049
+
1050
+ All unseen values found for the slot will be mapped to this default value
1051
+ for featurization.
1052
+ """
1053
+ for slot in [s for s in self.slots if isinstance(s, CategoricalSlot)]:
1054
+ slot.add_default_value()
1055
+
1056
+ def _add_flow_slots(self) -> None:
1057
+ """Adds the slots needed for the conversation flows."""
1058
+ from rasa.shared.core.constants import FLOW_SLOT_NAMES
1059
+
1060
+ slot_names = [slot.name for slot in self.slots]
1061
+
1062
+ for flow_slot in FLOW_SLOT_NAMES:
1063
+ if flow_slot not in slot_names:
1064
+ self.slots.append(
1065
+ AnySlot(
1066
+ flow_slot,
1067
+ mappings=[],
1068
+ influence_conversation=False,
1069
+ is_builtin=True,
1070
+ )
1071
+ )
1072
+ else:
1073
+ # TODO: in the future we need to prevent this entirely.
1074
+ structlogger.error(
1075
+ "domain.add_flow_slots.slot_reserved_for_internal_usage",
1076
+ event_info=(
1077
+ f"Slot {flow_slot} is reserved for Rasa internal usage, "
1078
+ f"but it already exists. This might lead to bad outcomes."
1079
+ ),
1080
+ )
1081
+
1082
+ def _add_requested_slot(self) -> None:
1083
+ """Add a slot called `requested_slot` to the list of slots.
1084
+
1085
+ The value of this slot will hold the name of the slot which the user
1086
+ needs to fill in next (either explicitly or implicitly) as part of a form.
1087
+ """
1088
+ if self.form_names and rasa.shared.core.constants.REQUESTED_SLOT not in [
1089
+ slot.name for slot in self.slots
1090
+ ]:
1091
+ self.slots.append(
1092
+ TextSlot(
1093
+ rasa.shared.core.constants.REQUESTED_SLOT,
1094
+ mappings=[],
1095
+ influence_conversation=False,
1096
+ is_builtin=True,
1097
+ )
1098
+ )
1099
+
1100
+ def _add_knowledge_base_slots(self) -> None:
1101
+ """Add slots for the knowledge base action to slots.
1102
+
1103
+ Slots are only added if the default knowledge base action name is present.
1104
+
1105
+ As soon as the knowledge base action is not experimental anymore, we should
1106
+ consider creating a new section in the domain file dedicated to knowledge
1107
+ base slots.
1108
+ """
1109
+ if (
1110
+ rasa.shared.core.constants.DEFAULT_KNOWLEDGE_BASE_ACTION
1111
+ in self.action_names_or_texts
1112
+ ):
1113
+ structlogger.warning(
1114
+ "domain.add_knowledge_base_slots.use_of_experimental_feature",
1115
+ event_info=(
1116
+ "You are using an experimental feature: Action '{}'!".format(
1117
+ rasa.shared.core.constants.DEFAULT_KNOWLEDGE_BASE_ACTION
1118
+ )
1119
+ ),
1120
+ )
1121
+ slot_names = [slot.name for slot in self.slots]
1122
+ for slot in KNOWLEDGE_BASE_SLOT_NAMES:
1123
+ if slot not in slot_names:
1124
+ self.slots.append(
1125
+ TextSlot(
1126
+ slot,
1127
+ mappings=[],
1128
+ influence_conversation=False,
1129
+ is_builtin=True,
1130
+ )
1131
+ )
1132
+
1133
+ def _add_session_metadata_slot(self) -> None:
1134
+ self.slots.append(
1135
+ AnySlot(
1136
+ rasa.shared.core.constants.SESSION_START_METADATA_SLOT,
1137
+ mappings=[],
1138
+ is_builtin=True,
1139
+ )
1140
+ )
1141
+
1142
+ def index_for_action(self, action_name: Text) -> int:
1143
+ """Looks up which action index corresponds to this action name."""
1144
+ try:
1145
+ return self.action_names_or_texts.index(action_name)
1146
+ except ValueError:
1147
+ self.raise_action_not_found_exception(action_name)
1148
+
1149
+ def raise_action_not_found_exception(self, action_name_or_text: Text) -> NoReturn:
1150
+ """Raises exception if action name or text not part of the domain or stories.
1151
+
1152
+ Args:
1153
+ action_name_or_text: Name of an action or its text in case it's an
1154
+ end-to-end bot utterance.
1155
+
1156
+ Raises:
1157
+ ActionNotFoundException: If `action_name_or_text` are not part of this
1158
+ domain.
1159
+ """
1160
+ action_names = "\n".join([f"\t - {a}" for a in self.action_names_or_texts])
1161
+ raise ActionNotFoundException(
1162
+ f"Cannot access action '{action_name_or_text}', "
1163
+ f"as that name is not a registered "
1164
+ f"action for this domain. "
1165
+ f"Available actions are: \n{action_names}"
1166
+ )
1167
+
1168
+ # noinspection PyTypeChecker
1169
+ @rasa.shared.utils.common.lazy_property
1170
+ def slot_states(self) -> List[Text]:
1171
+ """Returns all available slot state strings."""
1172
+ return [
1173
+ f"{slot.name}_{feature_index}"
1174
+ for slot in self.slots
1175
+ for feature_index in range(0, slot.feature_dimensionality())
1176
+ ]
1177
+
1178
+ # noinspection PyTypeChecker
1179
+ @rasa.shared.utils.common.lazy_property
1180
+ def entity_states(self) -> List[Text]:
1181
+ """Returns all available entity state strings."""
1182
+ entity_states = copy.deepcopy(self.entities)
1183
+ entity_states.extend(
1184
+ Domain.concatenate_entity_labels(self.entity_properties.roles)
1185
+ )
1186
+ entity_states.extend(
1187
+ Domain.concatenate_entity_labels(self.entity_properties.groups)
1188
+ )
1189
+
1190
+ return entity_states
1191
+
1192
+ @staticmethod
1193
+ def concatenate_entity_labels(
1194
+ entity_labels: Dict[Text, List[Text]], entity: Optional[Text] = None
1195
+ ) -> List[Text]:
1196
+ """Concatenates the given entity labels with their corresponding sub-labels.
1197
+
1198
+ If a specific entity label is given, only this entity label will be
1199
+ concatenated with its corresponding sub-labels.
1200
+
1201
+ Args:
1202
+ entity_labels: A map of an entity label to its sub-label list.
1203
+ entity: If present, only this entity will be considered.
1204
+
1205
+ Returns:
1206
+ A list of labels.
1207
+ """
1208
+ if entity is not None and entity not in entity_labels:
1209
+ return []
1210
+
1211
+ if entity:
1212
+ return [
1213
+ f"{entity}"
1214
+ f"{rasa.shared.core.constants.ENTITY_LABEL_SEPARATOR}"
1215
+ f"{sub_label}"
1216
+ for sub_label in entity_labels[entity]
1217
+ ]
1218
+
1219
+ return [
1220
+ f"{entity_label}"
1221
+ f"{rasa.shared.core.constants.ENTITY_LABEL_SEPARATOR}"
1222
+ f"{entity_sub_label}"
1223
+ for entity_label, entity_sub_labels in entity_labels.items()
1224
+ for entity_sub_label in entity_sub_labels
1225
+ ]
1226
+
1227
+ @rasa.shared.utils.common.lazy_property
1228
+ def input_state_map(self) -> Dict[Text, int]:
1229
+ """Provide a mapping from state names to indices."""
1230
+ return {f: i for i, f in enumerate(self.input_states)}
1231
+
1232
+ @rasa.shared.utils.common.lazy_property
1233
+ def input_states(self) -> List[Text]:
1234
+ """Returns all available states."""
1235
+ return (
1236
+ self.intents
1237
+ + self.entity_states
1238
+ + self.slot_states
1239
+ + self.action_names_or_texts
1240
+ + self.form_names
1241
+ )
1242
+
1243
+ def _get_featurized_entities(self, latest_message: UserUttered) -> Set[Text]:
1244
+ """Gets the names of all entities that are present and wanted in the message.
1245
+
1246
+ Wherever an entity has a role or group specified as well, an additional role-
1247
+ or group-specific entity name is added.
1248
+ """
1249
+ intent_name = latest_message.intent.get(INTENT_NAME_KEY)
1250
+ intent_config = self.intent_config(intent_name)
1251
+ entities = latest_message.entities
1252
+
1253
+ # If Entity Roles and Groups is used, we also need to make sure the roles and
1254
+ # groups get featurized. We concatenate the entity label with the role/group
1255
+ # label using a special separator to make sure that the resulting label is
1256
+ # unique (as you can have the same role/group label for different entities).
1257
+ entity_names_basic = set(
1258
+ entity["entity"] for entity in entities if "entity" in entity.keys()
1259
+ )
1260
+ entity_names_roles = set(
1261
+ f"{entity['entity']}"
1262
+ f"{rasa.shared.core.constants.ENTITY_LABEL_SEPARATOR}{entity['role']}"
1263
+ for entity in entities
1264
+ if "entity" in entity.keys() and "role" in entity.keys()
1265
+ )
1266
+ entity_names_groups = set(
1267
+ f"{entity['entity']}"
1268
+ f"{rasa.shared.core.constants.ENTITY_LABEL_SEPARATOR}{entity['group']}"
1269
+ for entity in entities
1270
+ if "entity" in entity.keys() and "group" in entity.keys()
1271
+ )
1272
+ entity_names = entity_names_basic.union(entity_names_roles, entity_names_groups)
1273
+
1274
+ # the USED_ENTITIES_KEY of an intent also contains the entity labels and the
1275
+ # concatenated entity labels with their corresponding roles and groups labels
1276
+ wanted_entities = set(intent_config.get(USED_ENTITIES_KEY, entity_names))
1277
+
1278
+ return entity_names.intersection(wanted_entities)
1279
+
1280
+ def _get_user_sub_state(self, tracker: "DialogueStateTracker") -> SubState:
1281
+ """Turns latest UserUttered event into a substate.
1282
+
1283
+ The substate will contain intent, text, and entities (if any are present).
1284
+
1285
+ Args:
1286
+ tracker: dialog state tracker containing the dialog so far
1287
+ Returns:
1288
+ a dictionary containing intent, text and set entities
1289
+ """
1290
+ # proceed with values only if the user of a bot have done something
1291
+ # at the previous step i.e., when the state is not empty.
1292
+ latest_message = tracker.latest_message
1293
+ if not latest_message or latest_message.is_empty():
1294
+ return {}
1295
+
1296
+ sub_state = cast(SubState, latest_message.as_sub_state())
1297
+
1298
+ # Filter entities based on intent config. We need to convert the set into a
1299
+ # tuple because sub_state will be later transformed into a frozenset (so it can
1300
+ # be hashed for deduplication).
1301
+ entities = tuple(
1302
+ self._get_featurized_entities(latest_message).intersection(
1303
+ set(sub_state.get(ENTITIES, ()))
1304
+ )
1305
+ )
1306
+ # Sort entities so that any derived state representation is consistent across
1307
+ # runs and invariant to the order in which the entities for an utterance are
1308
+ # listed in data files.
1309
+ entities = tuple(sorted(entities))
1310
+
1311
+ if entities:
1312
+ sub_state[ENTITIES] = entities
1313
+ else:
1314
+ sub_state.pop(ENTITIES, None)
1315
+
1316
+ return sub_state
1317
+
1318
+ @staticmethod
1319
+ def _get_slots_sub_state(
1320
+ tracker: "DialogueStateTracker", omit_unset_slots: bool = False
1321
+ ) -> SubState:
1322
+ """Sets all set slots with the featurization of the stored value.
1323
+
1324
+ Args:
1325
+ tracker: dialog state tracker containing the dialog so far
1326
+ omit_unset_slots: If `True` do not include the initial values of slots.
1327
+
1328
+ Returns:
1329
+ a mapping of slot names to their featurization
1330
+ """
1331
+ slots: SubState = {}
1332
+ for slot_name, slot in tracker.slots.items():
1333
+ # If the slot doesn't influence conversations, slot.as_feature() will return
1334
+ # a result that evaluates to False, meaning that the slot shouldn't be
1335
+ # included in featurised sub-states.
1336
+ # Note that this condition checks if the slot itself is None. An unset slot
1337
+ # will be a Slot object and its `value` attribute will be None.
1338
+ if slot is not None and slot.as_feature():
1339
+ if omit_unset_slots and not slot.has_been_set:
1340
+ continue
1341
+ if slot.value == rasa.shared.core.constants.SHOULD_NOT_BE_SET:
1342
+ slots[slot_name] = rasa.shared.core.constants.SHOULD_NOT_BE_SET
1343
+ elif any(slot.as_feature()):
1344
+ # Only include slot in featurised sub-state if the slot is not
1345
+ # unset, i.e. is set to some actual value and has been successfully
1346
+ # featurized, and hence has at least one non-zero feature.
1347
+ slots[slot_name] = tuple(slot.as_feature())
1348
+ return slots
1349
+
1350
+ @staticmethod
1351
+ def _get_prev_action_sub_state(
1352
+ tracker: "DialogueStateTracker",
1353
+ ) -> Optional[Dict[Text, Text]]:
1354
+ """Turn the previous taken action into a state name.
1355
+
1356
+ Args:
1357
+ tracker: dialog state tracker containing the dialog so far
1358
+ Returns:
1359
+ a dictionary with the information on latest action
1360
+ """
1361
+ return tracker.latest_action
1362
+
1363
+ @staticmethod
1364
+ def _get_active_loop_sub_state(
1365
+ tracker: "DialogueStateTracker",
1366
+ ) -> Dict[Text, Optional[Text]]:
1367
+ """Turn tracker's active loop into a state name.
1368
+
1369
+ Args:
1370
+ tracker: dialog state tracker containing the dialog so far
1371
+ Returns:
1372
+ a dictionary mapping "name" to active loop name if present
1373
+ """
1374
+ # we don't use tracker.active_loop_name
1375
+ # because we need to keep should_not_be_set
1376
+ if tracker.active_loop:
1377
+ return {rasa.shared.core.constants.LOOP_NAME: tracker.active_loop.name}
1378
+ else:
1379
+ return {}
1380
+
1381
+ @staticmethod
1382
+ def _clean_state(state: State) -> State:
1383
+ return {
1384
+ state_type: sub_state
1385
+ for state_type, sub_state in state.items()
1386
+ if sub_state
1387
+ }
1388
+
1389
+ def get_active_state(
1390
+ self, tracker: "DialogueStateTracker", omit_unset_slots: bool = False
1391
+ ) -> State:
1392
+ """Given a dialogue tracker, makes a representation of current dialogue state.
1393
+
1394
+ Args:
1395
+ tracker: dialog state tracker containing the dialog so far
1396
+ omit_unset_slots: If `True` do not include the initial values of slots.
1397
+
1398
+ Returns:
1399
+ A representation of the dialogue's current state.
1400
+ """
1401
+ state = {
1402
+ rasa.shared.core.constants.USER: self._get_user_sub_state(tracker),
1403
+ rasa.shared.core.constants.SLOTS: self._get_slots_sub_state(
1404
+ tracker, omit_unset_slots=omit_unset_slots
1405
+ ),
1406
+ rasa.shared.core.constants.PREVIOUS_ACTION: self._get_prev_action_sub_state(
1407
+ tracker
1408
+ ),
1409
+ rasa.shared.core.constants.ACTIVE_LOOP: self._get_active_loop_sub_state(
1410
+ tracker
1411
+ ),
1412
+ }
1413
+ return self._clean_state(state)
1414
+
1415
+ @staticmethod
1416
+ def _remove_rule_only_features(
1417
+ state: State, rule_only_data: Optional[Dict[Text, Any]]
1418
+ ) -> None:
1419
+ if not rule_only_data:
1420
+ return
1421
+
1422
+ rule_only_slots = rule_only_data.get(
1423
+ rasa.shared.core.constants.RULE_ONLY_SLOTS, []
1424
+ )
1425
+ rule_only_loops = rule_only_data.get(
1426
+ rasa.shared.core.constants.RULE_ONLY_LOOPS, []
1427
+ )
1428
+
1429
+ # remove slots which only occur in rules but not in stories
1430
+ if rule_only_slots:
1431
+ for slot in rule_only_slots:
1432
+ state.get(rasa.shared.core.constants.SLOTS, {}).pop(slot, None)
1433
+ # remove active loop which only occur in rules but not in stories
1434
+ if (
1435
+ rule_only_loops
1436
+ and state.get(rasa.shared.core.constants.ACTIVE_LOOP, {}).get(
1437
+ rasa.shared.core.constants.LOOP_NAME
1438
+ )
1439
+ in rule_only_loops
1440
+ ):
1441
+ del state[rasa.shared.core.constants.ACTIVE_LOOP]
1442
+
1443
+ @staticmethod
1444
+ def _substitute_rule_only_user_input(state: State, last_ml_state: State) -> None:
1445
+ if not rasa.shared.core.trackers.is_prev_action_listen_in_state(state):
1446
+ if not last_ml_state.get(rasa.shared.core.constants.USER) and state.get(
1447
+ rasa.shared.core.constants.USER
1448
+ ):
1449
+ del state[rasa.shared.core.constants.USER]
1450
+ elif last_ml_state.get(rasa.shared.core.constants.USER):
1451
+ state[rasa.shared.core.constants.USER] = last_ml_state[
1452
+ rasa.shared.core.constants.USER
1453
+ ]
1454
+
1455
+ def states_for_tracker_history(
1456
+ self,
1457
+ tracker: "DialogueStateTracker",
1458
+ omit_unset_slots: bool = False,
1459
+ ignore_rule_only_turns: bool = False,
1460
+ rule_only_data: Optional[Dict[Text, Any]] = None,
1461
+ ) -> List[State]:
1462
+ """List of states for each state of the trackers history.
1463
+
1464
+ Args:
1465
+ tracker: Dialogue state tracker containing the dialogue so far.
1466
+ omit_unset_slots: If `True` do not include the initial values of slots.
1467
+ ignore_rule_only_turns: If True ignore dialogue turns that are present
1468
+ only in rules.
1469
+ rule_only_data: Slots and loops,
1470
+ which only occur in rules but not in stories.
1471
+
1472
+ Return:
1473
+ A list of states.
1474
+ """
1475
+ states: List[State] = []
1476
+ last_ml_action_sub_state = None
1477
+ turn_was_hidden = False
1478
+ for tr, hide_rule_turn in tracker.generate_all_prior_trackers():
1479
+ if ignore_rule_only_turns:
1480
+ # remember previous ml action based on the last non hidden turn
1481
+ # we need this to override previous action in the ml state
1482
+ if not turn_was_hidden:
1483
+ last_ml_action_sub_state = self._get_prev_action_sub_state(tr)
1484
+
1485
+ # followup action or happy path loop prediction
1486
+ # don't change the fact whether dialogue turn should be hidden
1487
+ if (
1488
+ not tr.followup_action
1489
+ and not tr.latest_action_name == tr.active_loop_name
1490
+ ):
1491
+ turn_was_hidden = hide_rule_turn
1492
+
1493
+ if turn_was_hidden:
1494
+ continue
1495
+
1496
+ state = self.get_active_state(tr, omit_unset_slots=omit_unset_slots)
1497
+
1498
+ if ignore_rule_only_turns:
1499
+ # clean state from only rule features
1500
+ self._remove_rule_only_features(state, rule_only_data)
1501
+ # make sure user input is the same as for previous state
1502
+ # for non action_listen turns
1503
+ if states:
1504
+ self._substitute_rule_only_user_input(state, states[-1])
1505
+ # substitute previous rule action with last_ml_action_sub_state
1506
+ if last_ml_action_sub_state:
1507
+ # FIXME: better type annotation for `State` would require
1508
+ # a larger refactoring (e.g. switch to dataclass)
1509
+ state[rasa.shared.core.constants.PREVIOUS_ACTION] = cast(
1510
+ SubState,
1511
+ last_ml_action_sub_state,
1512
+ )
1513
+
1514
+ states.append(self._clean_state(state))
1515
+
1516
+ return states
1517
+
1518
+ def slots_for_entities(self, entities: List[Dict[Text, Any]]) -> List[SlotSet]:
1519
+ """Creates slot events for entities if from_entity mapping matches.
1520
+
1521
+ Args:
1522
+ entities: The list of entities.
1523
+
1524
+ Returns:
1525
+ A list of `SlotSet` events.
1526
+ """
1527
+ if self.store_entities_as_slots:
1528
+ slot_events = []
1529
+
1530
+ for slot in self.slots:
1531
+ matching_entities = []
1532
+
1533
+ for mapping in slot.mappings:
1534
+ mapping_conditions = mapping.get(MAPPING_CONDITIONS)
1535
+ if mapping[MAPPING_TYPE] != str(SlotMappingType.FROM_ENTITY) or (
1536
+ mapping_conditions
1537
+ and mapping_conditions[0].get(ACTIVE_LOOP) is not None
1538
+ ):
1539
+ continue
1540
+
1541
+ for entity in entities:
1542
+ if (
1543
+ entity.get(ENTITY_ATTRIBUTE_TYPE)
1544
+ == mapping.get(ENTITY_ATTRIBUTE_TYPE)
1545
+ and entity.get(ENTITY_ATTRIBUTE_ROLE)
1546
+ == mapping.get(ENTITY_ATTRIBUTE_ROLE)
1547
+ and entity.get(ENTITY_ATTRIBUTE_GROUP)
1548
+ == mapping.get(ENTITY_ATTRIBUTE_GROUP)
1549
+ ):
1550
+ matching_entities.append(entity.get("value"))
1551
+
1552
+ if matching_entities:
1553
+ if isinstance(slot, ListSlot):
1554
+ slot_events.append(SlotSet(slot.name, matching_entities))
1555
+ else:
1556
+ slot_events.append(SlotSet(slot.name, matching_entities[-1]))
1557
+
1558
+ return slot_events
1559
+ else:
1560
+ return []
1561
+
1562
+ def persist_specification(self, model_path: Text) -> None:
1563
+ """Persist the domain specification to storage."""
1564
+ domain_spec_path = os.path.join(model_path, "domain.json")
1565
+ rasa.shared.utils.io.create_directory_for_file(domain_spec_path)
1566
+
1567
+ metadata = {"states": self.input_states}
1568
+ rasa.shared.utils.io.dump_obj_as_json_to_file(domain_spec_path, metadata)
1569
+
1570
+ @classmethod
1571
+ def load_specification(cls, path: Text) -> Dict[Text, Any]:
1572
+ """Load a domains specification from a dumped model directory."""
1573
+ metadata_path = os.path.join(path, "domain.json")
1574
+
1575
+ return json.loads(rasa.shared.utils.io.read_file(metadata_path))
1576
+
1577
+ def compare_with_specification(self, path: Text) -> bool:
1578
+ """Compare the domain spec of the current and the loaded domain.
1579
+
1580
+ Throws exception if the loaded domain specification is different
1581
+ to the current domain are different.
1582
+ """
1583
+ loaded_domain_spec = self.load_specification(path)
1584
+ states = loaded_domain_spec["states"]
1585
+
1586
+ if set(states) != set(self.input_states):
1587
+ missing = ",".join(set(states) - set(self.input_states))
1588
+ additional = ",".join(set(self.input_states) - set(states))
1589
+ raise InvalidDomain(
1590
+ f"Domain specification has changed. "
1591
+ f"You MUST retrain the policy. "
1592
+ f"Detected mismatch in domain specification. "
1593
+ f"The following states have been \n"
1594
+ f"\t - removed: {missing} \n"
1595
+ f"\t - added: {additional} "
1596
+ )
1597
+ else:
1598
+ return True
1599
+
1600
+ def as_dict(self) -> Dict[Text, Any]:
1601
+ """Return serialized `Domain`."""
1602
+ return self._data
1603
+
1604
+ @staticmethod
1605
+ def get_responses_with_multilines(
1606
+ responses: Dict[Text, List[Dict[Text, Any]]]
1607
+ ) -> Dict[Text, List[Dict[Text, Any]]]:
1608
+ """Returns `responses` with preserved multilines in the `text` key.
1609
+
1610
+ Args:
1611
+ responses: Original `responses`.
1612
+
1613
+ Returns:
1614
+ `responses` with preserved multilines in the `text` key.
1615
+ """
1616
+ from ruamel.yaml.scalarstring import LiteralScalarString
1617
+
1618
+ final_responses = responses.copy()
1619
+ for utter_action, examples in final_responses.items():
1620
+ for i, example in enumerate(examples):
1621
+ response_text = example.get(KEY_RESPONSES_TEXT, "")
1622
+ if not response_text or "\n" not in response_text:
1623
+ continue
1624
+ # Has new lines, use `LiteralScalarString`
1625
+ final_responses[utter_action][i][
1626
+ KEY_RESPONSES_TEXT
1627
+ ] = LiteralScalarString(response_text)
1628
+
1629
+ return final_responses
1630
+
1631
+ @staticmethod
1632
+ def _cleaned_data(data: Dict[Text, Any]) -> Dict[Text, Any]:
1633
+ """Remove empty and redundant keys from merged domain dict.
1634
+
1635
+ Returns:
1636
+ A cleaned dictionary version of the domain.
1637
+ """
1638
+ return {
1639
+ key: val
1640
+ for key, val in data.items()
1641
+ if val != {} and val != [] and val is not None
1642
+ }
1643
+
1644
+ def persist(self, filename: Union[Text, Path]) -> None:
1645
+ """Write domain to a file."""
1646
+ as_yaml = self.as_yaml()
1647
+ rasa.shared.utils.io.write_text_file(as_yaml, filename)
1648
+
1649
+ def as_yaml(self) -> Text:
1650
+ """Dump the `Domain` object as a YAML string.
1651
+
1652
+ This function preserves the orders of the keys in the domain.
1653
+
1654
+ Returns:
1655
+ A string in YAML format representing the domain.
1656
+ """
1657
+ # setting the `version` key first so that it appears at the top of YAML files
1658
+ # thanks to the `should_preserve_key_order` argument
1659
+ # of `dump_obj_as_yaml_to_string`
1660
+ domain_data: Dict[Text, Any] = {
1661
+ KEY_TRAINING_DATA_FORMAT_VERSION: DoubleQuotedScalarString(
1662
+ LATEST_TRAINING_DATA_FORMAT_VERSION
1663
+ )
1664
+ }
1665
+
1666
+ domain_data.update(self.as_dict())
1667
+
1668
+ if domain_data.get(KEY_RESPONSES, {}):
1669
+ domain_data[KEY_RESPONSES] = self.get_responses_with_multilines(
1670
+ domain_data[KEY_RESPONSES]
1671
+ )
1672
+
1673
+ return dump_obj_as_yaml_to_string(domain_data, should_preserve_key_order=True)
1674
+
1675
+ def intent_config(self, intent_name: Text) -> Dict[Text, Any]:
1676
+ """Return the configuration for an intent."""
1677
+ return self.intent_properties.get(intent_name, {})
1678
+
1679
+ @rasa.shared.utils.common.lazy_property
1680
+ def intents(self) -> List[Text]:
1681
+ """Returns sorted list of intents."""
1682
+ return sorted(self.intent_properties.keys())
1683
+
1684
+ @rasa.shared.utils.common.lazy_property
1685
+ def entities(self) -> List[Text]:
1686
+ """Returns sorted list of entities."""
1687
+ return sorted(self.entity_properties.entities)
1688
+
1689
+ @property
1690
+ def _slots_for_domain_warnings(self) -> List[Text]:
1691
+ """Fetch names of slots that are used in domain warnings.
1692
+
1693
+ Excludes slots which aren't featurized.
1694
+ """
1695
+ return [slot.name for slot in self._user_slots if slot.influence_conversation]
1696
+
1697
+ @property
1698
+ def _actions_for_domain_warnings(self) -> List[Text]:
1699
+ """Fetch names of actions that are used in domain warnings.
1700
+
1701
+ Includes user and form actions, but excludes those that are default actions.
1702
+ """
1703
+ return [
1704
+ action
1705
+ for action in self.user_actions_and_forms
1706
+ if action not in rasa.shared.core.constants.DEFAULT_ACTION_NAMES
1707
+ ]
1708
+
1709
+ @staticmethod
1710
+ def _get_symmetric_difference(
1711
+ domain_elements: Union[List[Text], Set[Text]],
1712
+ training_data_elements: Optional[Union[List[Text], Set[Text]]],
1713
+ ) -> Dict[Text, Set[Text]]:
1714
+ """Gets the symmetric difference between two sets.
1715
+
1716
+ One set represents domain elements and the other one is a set of training
1717
+ data elements.
1718
+
1719
+ Returns a dictionary containing a list of items found in the `domain_elements`
1720
+ but not in `training_data_elements` at key `in_domain`, and a list of items
1721
+ found in `training_data_elements` but not in `domain_elements` at key
1722
+ `in_training_data_set`.
1723
+ """
1724
+ if training_data_elements is None:
1725
+ training_data_elements = set()
1726
+
1727
+ in_domain_diff = set(domain_elements) - set(training_data_elements)
1728
+ in_training_data_diff = set(training_data_elements) - set(domain_elements)
1729
+
1730
+ return {"in_domain": in_domain_diff, "in_training_data": in_training_data_diff}
1731
+
1732
+ @staticmethod
1733
+ def _combine_with_responses(
1734
+ actions: List[Text], responses: Dict[Text, Any]
1735
+ ) -> List[Text]:
1736
+ """Combines actions with utter actions listed in responses section."""
1737
+ unique_utter_actions = [
1738
+ action for action in sorted(list(responses.keys())) if action not in actions
1739
+ ]
1740
+ return actions + unique_utter_actions
1741
+
1742
+ @staticmethod
1743
+ def _combine_user_with_default_actions(user_actions: List[Text]) -> List[Text]:
1744
+ # remove all user actions that overwrite default actions
1745
+ # this logic is a bit reversed, you'd think that we should remove
1746
+ # the action name from the default action names if the user overwrites
1747
+ # the action, but there are some locations in the code where we
1748
+ # implicitly assume that e.g. "action_listen" is always at location
1749
+ # 0 in this array. to keep it that way, we remove the duplicate
1750
+ # action names from the users list instead of the defaults
1751
+ unique_user_actions = [
1752
+ action
1753
+ for action in user_actions
1754
+ if action not in rasa.shared.core.constants.DEFAULT_ACTION_NAMES
1755
+ ]
1756
+ return rasa.shared.core.constants.DEFAULT_ACTION_NAMES + unique_user_actions
1757
+
1758
+ def domain_warnings(
1759
+ self,
1760
+ intents: Optional[Union[List[Text], Set[Text]]] = None,
1761
+ entities: Optional[Union[List[Text], Set[Text]]] = None,
1762
+ actions: Optional[Union[List[Text], Set[Text]]] = None,
1763
+ slots: Optional[Union[List[Text], Set[Text]]] = None,
1764
+ ) -> Dict[Text, Dict[Text, Set[Text]]]:
1765
+ """Generate domain warnings from intents, entities, actions and slots.
1766
+
1767
+ Returns a dictionary with entries for `intent_warnings`,
1768
+ `entity_warnings`, `action_warnings` and `slot_warnings`. Excludes domain slots
1769
+ from domain warnings in case they are not featurized.
1770
+ """
1771
+ intent_warnings = self._get_symmetric_difference(self.intents, intents)
1772
+ entity_warnings = self._get_symmetric_difference(self.entities, entities)
1773
+ action_warnings = self._get_symmetric_difference(
1774
+ self._actions_for_domain_warnings, actions
1775
+ )
1776
+ slot_warnings = self._get_symmetric_difference(
1777
+ self._slots_for_domain_warnings, slots
1778
+ )
1779
+
1780
+ return {
1781
+ "intent_warnings": intent_warnings,
1782
+ "entity_warnings": entity_warnings,
1783
+ "action_warnings": action_warnings,
1784
+ "slot_warnings": slot_warnings,
1785
+ }
1786
+
1787
+ def _check_domain_sanity(self) -> None:
1788
+ """Make sure the domain is properly configured.
1789
+
1790
+ If the domain contains any duplicate slots, intents, actions
1791
+ or entities, an InvalidDomain error is raised. This error
1792
+ is also raised when intent-action mappings are incorrectly
1793
+ named or a response is missing.
1794
+ """
1795
+
1796
+ def get_duplicates(my_items: Iterable[Any]) -> List[Any]:
1797
+ """Returns a list of duplicate items in my_items."""
1798
+ return [
1799
+ item
1800
+ for item, count in collections.Counter(my_items).items()
1801
+ if count > 1
1802
+ ]
1803
+
1804
+ def check_mappings(
1805
+ intent_properties: Dict[Text, Dict[Text, Union[bool, List]]]
1806
+ ) -> List[Tuple[Text, Text]]:
1807
+ """Checks whether intent-action mappings use valid action names or texts."""
1808
+ incorrect = []
1809
+ for intent, properties in intent_properties.items():
1810
+ if "triggers" in properties:
1811
+ triggered_action = properties.get("triggers")
1812
+ if triggered_action not in self.action_names_or_texts:
1813
+ incorrect.append((intent, str(triggered_action)))
1814
+ return incorrect
1815
+
1816
+ def get_exception_message(
1817
+ duplicates: Optional[List[Tuple[List[Text], Text]]] = None,
1818
+ mappings: Optional[List[Tuple[Text, Text]]] = None,
1819
+ ) -> Text:
1820
+ """Return a message given a list of error locations."""
1821
+ message = ""
1822
+ if duplicates:
1823
+ message += get_duplicate_exception_message(duplicates)
1824
+ if mappings:
1825
+ if message:
1826
+ message += "\n"
1827
+ message += get_mapping_exception_message(mappings)
1828
+ return message
1829
+
1830
+ def get_mapping_exception_message(mappings: List[Tuple[Text, Text]]) -> Text:
1831
+ """Return a message given a list of duplicates."""
1832
+ message = ""
1833
+ for name, action_name in mappings:
1834
+ if message:
1835
+ message += "\n"
1836
+ message += (
1837
+ "Intent '{}' is set to trigger action '{}', which is "
1838
+ "not defined in the domain.".format(name, action_name)
1839
+ )
1840
+ return message
1841
+
1842
+ def get_duplicate_exception_message(
1843
+ duplicates: List[Tuple[List[Text], Text]]
1844
+ ) -> Text:
1845
+ """Return a message given a list of duplicates."""
1846
+ message = ""
1847
+ for d, name in duplicates:
1848
+ if d:
1849
+ if message:
1850
+ message += "\n"
1851
+ message += (
1852
+ f"Duplicate {name} in domain. "
1853
+ f"These {name} occur more than once in "
1854
+ f"the domain: '{', '.join(d)}'."
1855
+ )
1856
+ return message
1857
+
1858
+ duplicate_actions = get_duplicates(self.action_names_or_texts)
1859
+ duplicate_slots = get_duplicates([s.name for s in self.slots])
1860
+ duplicate_entities = get_duplicates(self.entities)
1861
+ incorrect_mappings = check_mappings(self.intent_properties)
1862
+
1863
+ if (
1864
+ duplicate_actions
1865
+ or duplicate_slots
1866
+ or duplicate_entities
1867
+ or incorrect_mappings
1868
+ ):
1869
+ raise InvalidDomain(
1870
+ get_exception_message(
1871
+ [
1872
+ (duplicate_actions, KEY_ACTIONS),
1873
+ (duplicate_slots, KEY_SLOTS),
1874
+ (duplicate_entities, KEY_ENTITIES),
1875
+ ],
1876
+ incorrect_mappings,
1877
+ )
1878
+ )
1879
+
1880
+ @property
1881
+ def utterances_for_response(self) -> Set[Text]:
1882
+ """Returns utterance set which should have a response.
1883
+
1884
+ Will filter out utterances which are subintent (retrieval intent) types.
1885
+ eg. if actions have ['utter_chitchat', 'utter_chitchat/greet'], this
1886
+ will only return ['utter_chitchat/greet'] as only that will need a
1887
+ response.
1888
+ """
1889
+ utterances = set()
1890
+ subintent_parents = set()
1891
+ for action in self.action_names_or_texts:
1892
+ if not action.startswith(rasa.shared.constants.UTTER_PREFIX):
1893
+ continue
1894
+ action_parent_split = action.split(RESPONSE_IDENTIFIER_DELIMITER)
1895
+ if len(action_parent_split) == 2:
1896
+ action_parent = action_parent_split[0]
1897
+ subintent_parents.add(action_parent)
1898
+ utterances.add(action)
1899
+ return utterances - subintent_parents
1900
+
1901
+ def check_missing_responses(self) -> None:
1902
+ """Warn user of utterance names which have no specified response."""
1903
+ missing_responses = self.utterances_for_response - set(self.responses)
1904
+
1905
+ for response in missing_responses:
1906
+ structlogger.warning(
1907
+ "domain.check_missing_response",
1908
+ response=response,
1909
+ event_info=(
1910
+ f"Action '{response}' is listed as a "
1911
+ f"response action in the domain file, but there is "
1912
+ f"no matching response defined. Please check your domain."
1913
+ ),
1914
+ docs=DOCS_URL_RESPONSES,
1915
+ )
1916
+
1917
+ def is_empty(self) -> bool:
1918
+ """Check whether the domain is empty."""
1919
+ return self.as_dict() == Domain.empty().as_dict()
1920
+
1921
+ @staticmethod
1922
+ def is_domain_file(filename: Union[Text, Path]) -> bool:
1923
+ """Checks whether the given file path is a Rasa domain file.
1924
+
1925
+ Args:
1926
+ filename: Path of the file which should be checked.
1927
+
1928
+ Returns:
1929
+ `True` if it's a domain file, otherwise `False`.
1930
+
1931
+ Raises:
1932
+ YamlException: if the file seems to be a YAML file (extension) but
1933
+ can not be read / parsed.
1934
+ """
1935
+ from rasa.shared.data import is_likely_yaml_file
1936
+
1937
+ if not is_likely_yaml_file(filename):
1938
+ return False
1939
+
1940
+ try:
1941
+ content = read_yaml_file(filename)
1942
+ except (RasaException, YamlSyntaxException):
1943
+ structlogger.warning(
1944
+ "domain.cannot_load_domain_file",
1945
+ file=filename,
1946
+ event_info=(
1947
+ f"The file {filename} could not be loaded as domain file. "
1948
+ f"You can use https://yamlchecker.com/ to validate "
1949
+ f"the YAML syntax of your file."
1950
+ ),
1951
+ )
1952
+ return False
1953
+
1954
+ return any(key in content for key in ALL_DOMAIN_KEYS)
1955
+
1956
+ def required_slots_for_form(self, form_name: Text) -> List[Text]:
1957
+ """Retrieve the list of required slot names for a form defined in the domain.
1958
+
1959
+ Args:
1960
+ form_name: The name of the form.
1961
+
1962
+ Returns:
1963
+ The list of slot names or an empty list if no form was found.
1964
+ """
1965
+ form = self.forms.get(form_name)
1966
+ if form:
1967
+ return form[REQUIRED_SLOTS_KEY]
1968
+
1969
+ return []
1970
+
1971
+ def count_slot_mapping_statistics(self) -> Tuple[int, int, int]:
1972
+ """Counts the total number of slot mappings and custom slot mappings.
1973
+
1974
+ Returns:
1975
+ A triple of integers where the first entry is the total number of mappings,
1976
+ the second entry is the total number of custom mappings, and the third entry
1977
+ is the total number of mappings which have conditions attached.
1978
+ """
1979
+ total_mappings = 0
1980
+ custom_mappings = 0
1981
+ conditional_mappings = 0
1982
+
1983
+ for slot in self.slots:
1984
+ total_mappings += len(slot.mappings)
1985
+ for mapping in slot.mappings:
1986
+ if mapping[MAPPING_TYPE] == str(SlotMappingType.CUSTOM):
1987
+ custom_mappings += 1
1988
+
1989
+ if MAPPING_CONDITIONS in mapping:
1990
+ conditional_mappings += 1
1991
+
1992
+ return (total_mappings, custom_mappings, conditional_mappings)
1993
+
1994
+ def does_custom_action_explicitly_need_domain(self, action_name: Text) -> bool:
1995
+ """Assert if action has explicitly stated that it needs domain.
1996
+
1997
+ Args:
1998
+ action_name: Name of the action to be checked
1999
+
2000
+ Returns:
2001
+ True if action has explicitly stated that it needs domain.
2002
+ Otherwise, it returns false.
2003
+ """
2004
+ return action_name in self._actions_which_explicitly_need_domain
2005
+
2006
+ def __repr__(self) -> Text:
2007
+ """Returns text representation of object."""
2008
+ return (
2009
+ f"{self.__class__.__name__}: {len(self.action_names_or_texts)} actions, "
2010
+ f"{len(self.intent_properties)} intents, {len(self.responses)} responses, "
2011
+ f"{len(self.slots)} slots, "
2012
+ f"{len(self.entities)} entities, {len(self.form_names)} forms"
2013
+ )
2014
+
2015
+ @staticmethod
2016
+ def _collect_action_names(
2017
+ actions: List[Union[Text, Dict[Text, Any]]]
2018
+ ) -> List[Text]:
2019
+ action_names: List[Text] = []
2020
+
2021
+ for action in actions:
2022
+ if isinstance(action, dict):
2023
+ action_names.extend(list(action.keys()))
2024
+ elif isinstance(action, str):
2025
+ action_names += [action]
2026
+
2027
+ return action_names
2028
+
2029
+ @staticmethod
2030
+ def _collect_actions_which_explicitly_need_domain(
2031
+ actions: List[Union[Text, Dict[Text, Any]]]
2032
+ ) -> List[Text]:
2033
+ action_names: List[Text] = []
2034
+
2035
+ for action in actions:
2036
+ if isinstance(action, dict):
2037
+ for action_name, action_config in action.items():
2038
+ should_send_domain = action_config.get(
2039
+ ACTION_SHOULD_SEND_DOMAIN, False
2040
+ )
2041
+ if should_send_domain:
2042
+ action_names += [action_name]
2043
+
2044
+ elif action.startswith("validate_"):
2045
+ action_names += [action]
2046
+
2047
+ return action_names
2048
+
2049
+
2050
+ def warn_about_duplicates_found_during_domain_merging(
2051
+ duplicates: Dict[str, List[str]]
2052
+ ) -> None:
2053
+ """Emits a warning about found duplicates while loading multiple domain paths."""
2054
+ domain_keys = [
2055
+ KEY_INTENTS,
2056
+ KEY_FORMS,
2057
+ KEY_ACTIONS,
2058
+ KEY_E2E_ACTIONS,
2059
+ KEY_RESPONSES,
2060
+ KEY_SLOTS,
2061
+ KEY_ENTITIES,
2062
+ ]
2063
+
2064
+ # Build the message if there are duplicates
2065
+ message = []
2066
+ for key in domain_keys:
2067
+ duplicates_per_key = duplicates.get(key)
2068
+ if duplicates_per_key:
2069
+ message.append(
2070
+ f"The following duplicated {key} have been found "
2071
+ f"across multiple domain files: "
2072
+ f"{', '.join(duplicates_per_key)}"
2073
+ )
2074
+
2075
+ # send the warning with the constructed message
2076
+ if message:
2077
+ full_message = " \n".join(message)
2078
+ structlogger.warning(
2079
+ "domain.duplicates_found", event_info=full_message, docs=DOCS_URL_DOMAINS
2080
+ )
2081
+
2082
+ return None
2083
+
2084
+
2085
+ def _validate_forms(forms: Union[Dict, List]) -> None:
2086
+ if not isinstance(forms, dict):
2087
+ raise InvalidDomain("Forms have to be specified as dictionary.")
2088
+
2089
+ for form_name, form_data in forms.items():
2090
+ if form_data is None:
2091
+ continue
2092
+
2093
+ if not isinstance(form_data, Dict):
2094
+ raise InvalidDomain(
2095
+ f"The contents of form '{form_name}' were specified "
2096
+ f"as '{type(form_data)}'. They need to be specified "
2097
+ f"as dictionary. Please see {DOCS_URL_FORMS} "
2098
+ f"for more information."
2099
+ )
2100
+
2101
+ if IGNORED_INTENTS in form_data and REQUIRED_SLOTS_KEY not in form_data:
2102
+ raise InvalidDomain(
2103
+ f"If you use the `{IGNORED_INTENTS}` parameter in your form, then "
2104
+ f"the keyword `{REQUIRED_SLOTS_KEY}` is required. "
2105
+ f"Please see {DOCS_URL_FORMS} for more information."
2106
+ )