rasa-pro 3.12.0.dev13__py3-none-any.whl → 3.12.0rc2__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 (139) hide show
  1. README.md +10 -13
  2. rasa/anonymization/anonymization_rule_executor.py +16 -10
  3. rasa/cli/data.py +16 -0
  4. rasa/cli/project_templates/calm/config.yml +2 -2
  5. rasa/cli/project_templates/calm/domain/list_contacts.yml +1 -2
  6. rasa/cli/project_templates/calm/domain/remove_contact.yml +1 -2
  7. rasa/cli/project_templates/calm/domain/shared.yml +1 -4
  8. rasa/cli/project_templates/calm/endpoints.yml +2 -2
  9. rasa/cli/utils.py +12 -0
  10. rasa/core/actions/action.py +84 -191
  11. rasa/core/actions/action_handle_digressions.py +35 -13
  12. rasa/core/actions/action_run_slot_rejections.py +16 -4
  13. rasa/core/channels/__init__.py +2 -0
  14. rasa/core/channels/studio_chat.py +19 -0
  15. rasa/core/channels/telegram.py +42 -24
  16. rasa/core/channels/voice_ready/utils.py +1 -1
  17. rasa/core/channels/voice_stream/asr/asr_engine.py +10 -4
  18. rasa/core/channels/voice_stream/asr/azure.py +14 -1
  19. rasa/core/channels/voice_stream/asr/deepgram.py +20 -4
  20. rasa/core/channels/voice_stream/audiocodes.py +264 -0
  21. rasa/core/channels/voice_stream/browser_audio.py +4 -1
  22. rasa/core/channels/voice_stream/call_state.py +3 -0
  23. rasa/core/channels/voice_stream/genesys.py +6 -2
  24. rasa/core/channels/voice_stream/tts/azure.py +9 -1
  25. rasa/core/channels/voice_stream/tts/cartesia.py +14 -8
  26. rasa/core/channels/voice_stream/voice_channel.py +23 -2
  27. rasa/core/constants.py +2 -0
  28. rasa/core/nlg/contextual_response_rephraser.py +18 -1
  29. rasa/core/nlg/generator.py +83 -15
  30. rasa/core/nlg/response.py +6 -3
  31. rasa/core/nlg/translate.py +55 -0
  32. rasa/core/policies/enterprise_search_prompt_with_citation_template.jinja2 +1 -1
  33. rasa/core/policies/flows/flow_executor.py +19 -7
  34. rasa/core/processor.py +71 -9
  35. rasa/dialogue_understanding/commands/can_not_handle_command.py +20 -2
  36. rasa/dialogue_understanding/commands/cancel_flow_command.py +24 -6
  37. rasa/dialogue_understanding/commands/change_flow_command.py +20 -2
  38. rasa/dialogue_understanding/commands/chit_chat_answer_command.py +20 -2
  39. rasa/dialogue_understanding/commands/clarify_command.py +29 -3
  40. rasa/dialogue_understanding/commands/command.py +1 -16
  41. rasa/dialogue_understanding/commands/command_syntax_manager.py +55 -0
  42. rasa/dialogue_understanding/commands/handle_digressions_command.py +1 -7
  43. rasa/dialogue_understanding/commands/human_handoff_command.py +20 -2
  44. rasa/dialogue_understanding/commands/knowledge_answer_command.py +20 -2
  45. rasa/dialogue_understanding/commands/prompt_command.py +94 -0
  46. rasa/dialogue_understanding/commands/repeat_bot_messages_command.py +20 -2
  47. rasa/dialogue_understanding/commands/set_slot_command.py +24 -2
  48. rasa/dialogue_understanding/commands/skip_question_command.py +20 -2
  49. rasa/dialogue_understanding/commands/start_flow_command.py +22 -2
  50. rasa/dialogue_understanding/commands/utils.py +71 -4
  51. rasa/dialogue_understanding/generator/__init__.py +2 -0
  52. rasa/dialogue_understanding/generator/command_parser.py +15 -12
  53. rasa/dialogue_understanding/generator/constants.py +3 -0
  54. rasa/dialogue_understanding/generator/llm_based_command_generator.py +12 -5
  55. rasa/dialogue_understanding/generator/llm_command_generator.py +5 -3
  56. rasa/dialogue_understanding/generator/multi_step/multi_step_llm_command_generator.py +17 -3
  57. rasa/dialogue_understanding/generator/prompt_templates/__init__.py +0 -0
  58. rasa/dialogue_understanding/generator/{single_step → prompt_templates}/command_prompt_template.jinja2 +2 -0
  59. rasa/dialogue_understanding/generator/prompt_templates/command_prompt_v2_claude_3_5_sonnet_20240620_template.jinja2 +77 -0
  60. rasa/dialogue_understanding/generator/prompt_templates/command_prompt_v2_default.jinja2 +68 -0
  61. rasa/dialogue_understanding/generator/prompt_templates/command_prompt_v2_gpt_4o_2024_11_20_template.jinja2 +84 -0
  62. rasa/dialogue_understanding/generator/single_step/compact_llm_command_generator.py +522 -0
  63. rasa/dialogue_understanding/generator/single_step/single_step_llm_command_generator.py +12 -310
  64. rasa/dialogue_understanding/patterns/collect_information.py +1 -1
  65. rasa/dialogue_understanding/patterns/default_flows_for_patterns.yml +16 -0
  66. rasa/dialogue_understanding/patterns/validate_slot.py +65 -0
  67. rasa/dialogue_understanding/processor/command_processor.py +39 -0
  68. rasa/dialogue_understanding/stack/utils.py +38 -0
  69. rasa/dialogue_understanding_test/du_test_case.py +58 -18
  70. rasa/dialogue_understanding_test/du_test_result.py +14 -10
  71. rasa/dialogue_understanding_test/io.py +14 -0
  72. rasa/e2e_test/assertions.py +6 -8
  73. rasa/e2e_test/llm_judge_prompts/answer_relevance_prompt_template.jinja2 +5 -1
  74. rasa/e2e_test/llm_judge_prompts/groundedness_prompt_template.jinja2 +4 -0
  75. rasa/e2e_test/utils/io.py +0 -37
  76. rasa/engine/graph.py +1 -0
  77. rasa/engine/language.py +140 -0
  78. rasa/engine/recipes/config_files/default_config.yml +4 -0
  79. rasa/engine/recipes/default_recipe.py +2 -0
  80. rasa/engine/recipes/graph_recipe.py +2 -0
  81. rasa/engine/storage/local_model_storage.py +1 -0
  82. rasa/engine/storage/storage.py +4 -1
  83. rasa/llm_fine_tuning/conversations.py +1 -1
  84. rasa/model_manager/runner_service.py +7 -4
  85. rasa/model_manager/socket_bridge.py +7 -6
  86. rasa/shared/constants.py +15 -13
  87. rasa/shared/core/constants.py +2 -0
  88. rasa/shared/core/flows/constants.py +11 -0
  89. rasa/shared/core/flows/flow.py +83 -19
  90. rasa/shared/core/flows/flows_yaml_schema.json +31 -3
  91. rasa/shared/core/flows/steps/collect.py +1 -36
  92. rasa/shared/core/flows/utils.py +28 -4
  93. rasa/shared/core/flows/validation.py +1 -1
  94. rasa/shared/core/slot_mappings.py +208 -5
  95. rasa/shared/core/slots.py +137 -1
  96. rasa/shared/core/trackers.py +74 -1
  97. rasa/shared/importers/importer.py +50 -2
  98. rasa/shared/nlu/training_data/schemas/responses.yml +19 -12
  99. rasa/shared/providers/_configs/azure_entra_id_config.py +541 -0
  100. rasa/shared/providers/_configs/azure_openai_client_config.py +138 -3
  101. rasa/shared/providers/_configs/client_config.py +3 -1
  102. rasa/shared/providers/_configs/default_litellm_client_config.py +3 -1
  103. rasa/shared/providers/_configs/huggingface_local_embedding_client_config.py +3 -1
  104. rasa/shared/providers/_configs/litellm_router_client_config.py +3 -1
  105. rasa/shared/providers/_configs/model_group_config.py +4 -2
  106. rasa/shared/providers/_configs/oauth_config.py +33 -0
  107. rasa/shared/providers/_configs/openai_client_config.py +3 -1
  108. rasa/shared/providers/_configs/rasa_llm_client_config.py +3 -1
  109. rasa/shared/providers/_configs/self_hosted_llm_client_config.py +3 -1
  110. rasa/shared/providers/constants.py +6 -0
  111. rasa/shared/providers/embedding/azure_openai_embedding_client.py +28 -3
  112. rasa/shared/providers/embedding/litellm_router_embedding_client.py +3 -1
  113. rasa/shared/providers/llm/_base_litellm_client.py +42 -17
  114. rasa/shared/providers/llm/azure_openai_llm_client.py +81 -25
  115. rasa/shared/providers/llm/default_litellm_llm_client.py +3 -1
  116. rasa/shared/providers/llm/litellm_router_llm_client.py +29 -8
  117. rasa/shared/providers/llm/llm_client.py +23 -7
  118. rasa/shared/providers/llm/openai_llm_client.py +9 -3
  119. rasa/shared/providers/llm/rasa_llm_client.py +11 -2
  120. rasa/shared/providers/llm/self_hosted_llm_client.py +30 -11
  121. rasa/shared/providers/router/_base_litellm_router_client.py +3 -1
  122. rasa/shared/providers/router/router_client.py +3 -1
  123. rasa/shared/utils/constants.py +3 -0
  124. rasa/shared/utils/llm.py +33 -7
  125. rasa/shared/utils/pykwalify_extensions.py +24 -0
  126. rasa/shared/utils/schemas/domain.yml +26 -0
  127. rasa/telemetry.py +2 -1
  128. rasa/tracing/config.py +2 -0
  129. rasa/tracing/constants.py +12 -0
  130. rasa/tracing/instrumentation/instrumentation.py +36 -0
  131. rasa/tracing/instrumentation/metrics.py +41 -0
  132. rasa/tracing/metric_instrument_provider.py +40 -0
  133. rasa/validator.py +372 -7
  134. rasa/version.py +1 -1
  135. {rasa_pro-3.12.0.dev13.dist-info → rasa_pro-3.12.0rc2.dist-info}/METADATA +13 -14
  136. {rasa_pro-3.12.0.dev13.dist-info → rasa_pro-3.12.0rc2.dist-info}/RECORD +139 -124
  137. {rasa_pro-3.12.0.dev13.dist-info → rasa_pro-3.12.0rc2.dist-info}/NOTICE +0 -0
  138. {rasa_pro-3.12.0.dev13.dist-info → rasa_pro-3.12.0rc2.dist-info}/WHEEL +0 -0
  139. {rasa_pro-3.12.0.dev13.dist-info → rasa_pro-3.12.0rc2.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,522 @@
1
+ from typing import Any, Dict, List, Optional, Text
2
+
3
+ import structlog
4
+
5
+ import rasa.shared.utils.io
6
+ from rasa.dialogue_understanding.commands import (
7
+ CannotHandleCommand,
8
+ Command,
9
+ ErrorCommand,
10
+ SetSlotCommand,
11
+ )
12
+ from rasa.dialogue_understanding.commands.command_syntax_manager import (
13
+ CommandSyntaxManager,
14
+ CommandSyntaxVersion,
15
+ )
16
+ from rasa.dialogue_understanding.generator import LLMBasedCommandGenerator
17
+ from rasa.dialogue_understanding.generator.command_parser import (
18
+ parse_commands as parse_commands_using_command_parsers,
19
+ )
20
+ from rasa.dialogue_understanding.generator.constants import (
21
+ COMMAND_PROMPT_FILE_NAME,
22
+ DEFAULT_LLM_CONFIG,
23
+ FLOW_RETRIEVAL_KEY,
24
+ LLM_BASED_COMMAND_GENERATOR_CONFIG_FILE,
25
+ LLM_CONFIG_KEY,
26
+ USER_INPUT_CONFIG_KEY,
27
+ )
28
+ from rasa.dialogue_understanding.generator.flow_retrieval import FlowRetrieval
29
+ from rasa.dialogue_understanding.stack.utils import top_flow_frame
30
+ from rasa.dialogue_understanding.utils import (
31
+ add_commands_to_message_parse_data,
32
+ add_prompt_to_message_parse_data,
33
+ )
34
+ from rasa.engine.graph import ExecutionContext
35
+ from rasa.engine.recipes.default_recipe import DefaultV1Recipe
36
+ from rasa.engine.storage.resource import Resource
37
+ from rasa.engine.storage.storage import ModelStorage
38
+ from rasa.shared.constants import (
39
+ EMBEDDINGS_CONFIG_KEY,
40
+ PROMPT_TEMPLATE_CONFIG_KEY,
41
+ ROUTE_TO_CALM_SLOT,
42
+ )
43
+ from rasa.shared.core.flows import FlowsList
44
+ from rasa.shared.core.trackers import DialogueStateTracker
45
+ from rasa.shared.exceptions import ProviderClientAPIException
46
+ from rasa.shared.nlu.constants import LLM_COMMANDS, LLM_PROMPT, TEXT
47
+ from rasa.shared.nlu.training_data.message import Message
48
+ from rasa.shared.providers.llm.llm_response import LLMResponse
49
+ from rasa.shared.utils.io import deep_container_fingerprint
50
+ from rasa.shared.utils.llm import (
51
+ allowed_values_for_slot,
52
+ get_default_prompt_template_based_on_model,
53
+ get_prompt_template,
54
+ resolve_model_client_config,
55
+ sanitize_message_for_prompt,
56
+ tracker_as_readable_transcript,
57
+ )
58
+ from rasa.utils.beta import BetaNotEnabledException, ensure_beta_feature_is_enabled
59
+ from rasa.utils.log_utils import log_llm
60
+
61
+ structlogger = structlog.get_logger()
62
+
63
+
64
+ MODEL_PROMPT_MAPPER = {
65
+ "openai/gpt-4o-2024-11-20": "command_prompt_v2_gpt_4o_2024_11_20_template.jinja2",
66
+ "azure/gpt-4o-2024-11-20": "command_prompt_v2_gpt_4o_2024_11_20_template.jinja2",
67
+ "bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0": (
68
+ "command_prompt_v2_claude_3_5_sonnet_20240620_template.jinja2"
69
+ ),
70
+ "anthropic/claude-3-5-sonnet-20240620": (
71
+ "command_prompt_v2_claude_3_5_sonnet_20240620_template.jinja2"
72
+ ),
73
+ }
74
+
75
+ DEFAULT_COMMAND_PROMPT_TEMPLATE_FILE_NAME = "command_prompt_v2_default.jinja2"
76
+
77
+
78
+ class CommandParserValidatorSingleton:
79
+ """Singleton class to validate the command parser.
80
+
81
+ This class is used to validate the command parser. It keeps track of the number of
82
+ consecutive turns where no commands are parsed by the command parser. If the
83
+ number of consecutive turns exceeds a certain threshold, a warning is logged.
84
+ The prompt can use a DSL syntax that can be incompatible with the command syntax
85
+ used by the command parser. This class helps to detect such incompatibilities.
86
+ """
87
+
88
+ MAX_CONSECUTIVE_TURNS_NO_COMMAND_PREDICTED = 5
89
+ _NO_COMMAND_PREDICTED_TURN_COUNTER = 0
90
+ _command_parser_validated = False
91
+
92
+ @classmethod
93
+ def get_no_command_predicted_turn_counter(cls) -> int:
94
+ return cls._NO_COMMAND_PREDICTED_TURN_COUNTER
95
+
96
+ @classmethod
97
+ def should_validate_command_parser(cls) -> bool:
98
+ return not cls._command_parser_validated
99
+
100
+ @classmethod
101
+ def reset_command_parser_validation(cls) -> None:
102
+ cls._NO_COMMAND_PREDICTED_TURN_COUNTER = 0
103
+ cls._command_parser_validated = False
104
+
105
+ @classmethod
106
+ def validate_if_commands_are_parsed_from_llm_response(
107
+ cls, commands: List[Command], llm_response: str
108
+ ) -> None:
109
+ if llm_response and not commands:
110
+ cls._NO_COMMAND_PREDICTED_TURN_COUNTER += 1
111
+ else:
112
+ # Reset the counter if commands are generated, and mark
113
+ # the command parser as validated.
114
+ cls._NO_COMMAND_PREDICTED_TURN_COUNTER = 0
115
+ cls._command_parser_validated = True
116
+ return
117
+
118
+ if (
119
+ cls._NO_COMMAND_PREDICTED_TURN_COUNTER
120
+ >= cls.MAX_CONSECUTIVE_TURNS_NO_COMMAND_PREDICTED
121
+ ):
122
+ structlogger.warning(
123
+ "llm_command_generator.predict_commands.command_parser_not_working",
124
+ event_info=(
125
+ f"No commands were generated by the command parser for the last "
126
+ f"{cls._NO_COMMAND_PREDICTED_TURN_COUNTER} times. Check if you "
127
+ "are running incompatible prompt and LLM command generator."
128
+ ),
129
+ )
130
+
131
+
132
+ @DefaultV1Recipe.register(
133
+ [
134
+ DefaultV1Recipe.ComponentType.COMMAND_GENERATOR,
135
+ ],
136
+ is_trainable=True,
137
+ )
138
+ class CompactLLMCommandGenerator(LLMBasedCommandGenerator):
139
+ """A single step LLM-based command generator."""
140
+
141
+ def __init__(
142
+ self,
143
+ config: Dict[str, Any],
144
+ model_storage: ModelStorage,
145
+ resource: Resource,
146
+ prompt_template: Optional[Text] = None,
147
+ **kwargs: Any,
148
+ ) -> None:
149
+ super().__init__(
150
+ config,
151
+ model_storage,
152
+ resource,
153
+ prompt_template=prompt_template,
154
+ **kwargs,
155
+ )
156
+
157
+ # Get the default prompt template based on the model name
158
+ default_command_prompt_template = get_default_prompt_template_based_on_model(
159
+ self.config.get(LLM_CONFIG_KEY, {}) or {},
160
+ MODEL_PROMPT_MAPPER,
161
+ DEFAULT_COMMAND_PROMPT_TEMPLATE_FILE_NAME,
162
+ )
163
+
164
+ # Set the prompt template either from the config or the default prompt template.
165
+ self.prompt_template = prompt_template or get_prompt_template(
166
+ self.config.get(PROMPT_TEMPLATE_CONFIG_KEY),
167
+ default_command_prompt_template,
168
+ )
169
+
170
+ self.trace_prompt_tokens = self.config.get("trace_prompt_tokens", False)
171
+ self.repeat_command_enabled = self.is_repeat_command_enabled()
172
+
173
+ # Set the command syntax version to v2
174
+ CommandSyntaxManager.set_syntax_version(CommandSyntaxVersion.v2)
175
+
176
+ ### Implementations of LLMBasedCommandGenerator parent
177
+ @staticmethod
178
+ def get_default_config() -> Dict[str, Any]:
179
+ """The component's default config (see parent class for full docstring)."""
180
+ return {
181
+ PROMPT_TEMPLATE_CONFIG_KEY: None,
182
+ USER_INPUT_CONFIG_KEY: None,
183
+ LLM_CONFIG_KEY: None,
184
+ FLOW_RETRIEVAL_KEY: FlowRetrieval.get_default_config(),
185
+ }
186
+
187
+ def persist(self) -> None:
188
+ """Persist this component to disk for future loading."""
189
+ self._persist_prompt_template()
190
+ self._persist_config()
191
+ if self.flow_retrieval is not None:
192
+ self.flow_retrieval.persist()
193
+
194
+ def _persist_prompt_template(self) -> None:
195
+ """Persist prompt template for future loading."""
196
+ with self._model_storage.write_to(self._resource) as path:
197
+ rasa.shared.utils.io.write_text_file(
198
+ self.prompt_template, path / COMMAND_PROMPT_FILE_NAME
199
+ )
200
+
201
+ def _persist_config(self) -> None:
202
+ """Persist config as a source of truth for resolved clients."""
203
+ with self._model_storage.write_to(self._resource) as path:
204
+ rasa.shared.utils.io.dump_obj_as_json_to_file(
205
+ path / LLM_BASED_COMMAND_GENERATOR_CONFIG_FILE, self.config
206
+ )
207
+
208
+ @classmethod
209
+ def load(
210
+ cls: Any,
211
+ config: Dict[str, Any],
212
+ model_storage: ModelStorage,
213
+ resource: Resource,
214
+ execution_context: ExecutionContext,
215
+ **kwargs: Any,
216
+ ) -> "CompactLLMCommandGenerator":
217
+ """Loads trained component (see parent class for full docstring)."""
218
+ # Perform health check of the LLM API endpoint
219
+ llm_config = resolve_model_client_config(config.get(LLM_CONFIG_KEY, {}))
220
+ cls.perform_llm_health_check(
221
+ llm_config,
222
+ DEFAULT_LLM_CONFIG,
223
+ "compact_llm_command_generator.load",
224
+ cls.__name__,
225
+ )
226
+
227
+ # load prompt template from the model storage.
228
+ prompt_template = cls.load_prompt_template_from_model_storage(
229
+ model_storage, resource, COMMAND_PROMPT_FILE_NAME
230
+ )
231
+
232
+ # init base command generator
233
+ command_generator = cls(config, model_storage, resource, prompt_template)
234
+ # load flow retrieval if enabled
235
+ if command_generator.enabled_flow_retrieval:
236
+ command_generator.flow_retrieval = cls.load_flow_retrival(
237
+ command_generator.config, model_storage, resource
238
+ )
239
+
240
+ return command_generator
241
+
242
+ async def predict_commands(
243
+ self,
244
+ message: Message,
245
+ flows: FlowsList,
246
+ tracker: Optional[DialogueStateTracker] = None,
247
+ **kwargs: Any,
248
+ ) -> List[Command]:
249
+ """Predict commands using the LLM.
250
+
251
+ Args:
252
+ message: The message from the user.
253
+ flows: The flows available to the user.
254
+ tracker: The tracker containing the current state of the conversation.
255
+ **kwargs: Keyword arguments for forward compatibility.
256
+
257
+ Returns:
258
+ The commands generated by the llm.
259
+ """
260
+ prior_commands = self._get_prior_commands(message)
261
+
262
+ if tracker is None or flows.is_empty():
263
+ # cannot do anything if there are no flows or no tracker
264
+ return prior_commands
265
+
266
+ if self._should_skip_llm_call(prior_commands, flows, tracker):
267
+ return prior_commands
268
+
269
+ try:
270
+ commands = await self._predict_commands(message, flows, tracker)
271
+ except ProviderClientAPIException:
272
+ # if command predictions resulted in API exception
273
+ # "predict" the ErrorCommand
274
+ commands = [ErrorCommand()]
275
+ structlogger.warning(
276
+ "llm_command_generator.predict_commands.api_exception",
277
+ event_info=(
278
+ "ProviderClientAPIException occurred while predicting commands."
279
+ ),
280
+ commands=commands,
281
+ )
282
+
283
+ if not commands and not prior_commands:
284
+ # no commands are parsed or there's an invalid command
285
+ structlogger.warning(
286
+ "llm_command_generator.predict_commands",
287
+ message="No commands were predicted as the LLM response could "
288
+ "not be parsed or the LLM responded with an invalid command. "
289
+ "Returning a CannotHandleCommand instead.",
290
+ )
291
+ commands = [CannotHandleCommand()]
292
+
293
+ if tracker.has_coexistence_routing_slot:
294
+ # if coexistence feature is used, set the routing slot
295
+ commands += [SetSlotCommand(ROUTE_TO_CALM_SLOT, True)]
296
+
297
+ log_llm(
298
+ logger=structlogger,
299
+ log_module=self.__class__.__name__,
300
+ log_event="llm_command_generator.predict_commands.finished",
301
+ commands=commands,
302
+ )
303
+
304
+ domain = kwargs.get("domain")
305
+ commands = self._check_commands_against_slot_mappings(commands, tracker, domain)
306
+
307
+ return self._check_commands_overlap(prior_commands, commands)
308
+
309
+ async def _predict_commands(
310
+ self,
311
+ message: Message,
312
+ flows: FlowsList,
313
+ tracker: Optional[DialogueStateTracker] = None,
314
+ ) -> List[Command]:
315
+ """Predict commands using the LLM.
316
+
317
+ Args:
318
+ message: The message from the user.
319
+ flows: The flows available to the user.
320
+ tracker: The tracker containing the current state of the conversation.
321
+
322
+ Returns:
323
+ The commands generated by the llm.
324
+
325
+ Raises:
326
+ ProviderClientAPIException: If API calls raised an error.
327
+ """
328
+ # retrieve flows
329
+ filtered_flows = await self.filter_flows(message, flows, tracker)
330
+
331
+ flow_prompt = self.render_template(message, tracker, filtered_flows, flows)
332
+ log_llm(
333
+ logger=structlogger,
334
+ log_module=self.__class__.__name__,
335
+ log_event="llm_command_generator.predict_commands.prompt_rendered",
336
+ prompt=flow_prompt,
337
+ )
338
+
339
+ response = await self.invoke_llm(flow_prompt)
340
+ llm_response = LLMResponse.ensure_llm_response(response)
341
+ # The check for 'None' maintains compatibility with older versions
342
+ # of LLMCommandGenerator. In previous implementations, 'invoke_llm'
343
+ # might return 'None' to indicate a failure to generate actions.
344
+ if llm_response is None or not llm_response.choices:
345
+ structlogger.warning(
346
+ "llm_command_generator.predict_commands.no_actions_generated",
347
+ event_info=(
348
+ "No actions were generated by the LLM. Returning an ErrorCommand."
349
+ ),
350
+ )
351
+ return [ErrorCommand()]
352
+
353
+ action_list = llm_response.choices[0]
354
+
355
+ log_llm(
356
+ logger=structlogger,
357
+ log_module=self.__class__.__name__,
358
+ log_event="llm_command_generator.predict_commands.actions_generated",
359
+ action_list=action_list,
360
+ )
361
+
362
+ commands = self.parse_commands(action_list, tracker, flows)
363
+
364
+ if CommandParserValidatorSingleton.should_validate_command_parser():
365
+ CommandParserValidatorSingleton.validate_if_commands_are_parsed_from_llm_response(
366
+ commands, action_list
367
+ )
368
+
369
+ self._update_message_parse_data_for_fine_tuning(message, commands, flow_prompt)
370
+ add_commands_to_message_parse_data(message, self.__class__.__name__, commands)
371
+ add_prompt_to_message_parse_data(
372
+ message=message,
373
+ component_name=self.__class__.__name__,
374
+ prompt_name="command_generator_prompt",
375
+ user_prompt=flow_prompt,
376
+ llm_response=llm_response,
377
+ )
378
+
379
+ return commands
380
+
381
+ @staticmethod
382
+ def _update_message_parse_data_for_fine_tuning(
383
+ message: Message, commands: List[Command], prompt: str
384
+ ) -> None:
385
+ from rasa.llm_fine_tuning.annotation_module import preparing_fine_tuning_data
386
+
387
+ if preparing_fine_tuning_data:
388
+ # Add commands and prompt to the message object in order to create
389
+ # prompt -> commands pairs for fine-tuning
390
+ message.set(
391
+ LLM_COMMANDS,
392
+ [command.as_dict() for command in commands],
393
+ add_to_output=True,
394
+ )
395
+ message.set(LLM_PROMPT, prompt, add_to_output=True)
396
+
397
+ @classmethod
398
+ def parse_commands(
399
+ cls, actions: Optional[str], tracker: DialogueStateTracker, flows: FlowsList
400
+ ) -> List[Command]:
401
+ """Parse the actions returned by the llm into intent and entities.
402
+
403
+ Args:
404
+ actions: The actions returned by the llm.
405
+ tracker: The tracker containing the current state of the conversation.
406
+ flows: the list of flows
407
+
408
+ Returns:
409
+ The parsed commands.
410
+ """
411
+ commands = parse_commands_using_command_parsers(actions, flows)
412
+ if not commands:
413
+ structlogger.warning(
414
+ f"{cls.__name__}.parse_commands",
415
+ message="No commands were parsed from the LLM actions.",
416
+ actions=actions,
417
+ )
418
+
419
+ return commands
420
+
421
+ ### Helper methods
422
+ def render_template(
423
+ self,
424
+ message: Message,
425
+ tracker: DialogueStateTracker,
426
+ startable_flows: FlowsList,
427
+ all_flows: FlowsList,
428
+ ) -> str:
429
+ """Render the jinja template to create the prompt for the LLM.
430
+
431
+ Args:
432
+ message: The current message from the user.
433
+ tracker: The tracker containing the current state of the conversation.
434
+ startable_flows: The flows startable at this point in time by the user.
435
+ all_flows: all flows present in the assistant
436
+
437
+ Returns:
438
+ The rendered prompt template.
439
+ """
440
+ # need to make this distinction here because current step of the
441
+ # top_calling_frame would be the call step, but we need the collect step from
442
+ # the called frame. If no call is active calling and called frame are the same.
443
+ top_calling_frame = top_flow_frame(tracker.stack)
444
+ top_called_frame = top_flow_frame(tracker.stack, ignore_call_frames=False)
445
+
446
+ top_flow = top_calling_frame.flow(all_flows) if top_calling_frame else None
447
+ current_step = top_called_frame.step(all_flows) if top_called_frame else None
448
+
449
+ flow_slots = self.prepare_current_flow_slots_for_template(
450
+ top_flow, current_step, tracker
451
+ )
452
+ current_slot, current_slot_description = self.prepare_current_slot_for_template(
453
+ current_step
454
+ )
455
+ current_slot_type = None
456
+ current_slot_allowed_values = None
457
+ if current_slot:
458
+ current_slot_type = (
459
+ slot.type_name
460
+ if (slot := tracker.slots.get(current_slot)) is not None
461
+ else None
462
+ )
463
+ current_slot_allowed_values = allowed_values_for_slot(
464
+ tracker.slots.get(current_slot)
465
+ )
466
+ current_conversation = tracker_as_readable_transcript(tracker)
467
+ latest_user_message = sanitize_message_for_prompt(message.get(TEXT))
468
+ current_conversation += f"\nUSER: {latest_user_message}"
469
+
470
+ inputs = {
471
+ "available_flows": self.prepare_flows_for_template(
472
+ startable_flows, tracker
473
+ ),
474
+ "current_conversation": current_conversation,
475
+ "flow_slots": flow_slots,
476
+ "current_flow": top_flow.id if top_flow is not None else None,
477
+ "current_slot": current_slot,
478
+ "current_slot_description": current_slot_description,
479
+ "current_slot_type": current_slot_type,
480
+ "current_slot_allowed_values": current_slot_allowed_values,
481
+ "user_message": latest_user_message,
482
+ "is_repeat_command_enabled": self.repeat_command_enabled,
483
+ }
484
+
485
+ return self.compile_template(self.prompt_template).render(**inputs)
486
+
487
+ def is_repeat_command_enabled(self) -> bool:
488
+ """Check for feature flag"""
489
+ RASA_PRO_BETA_REPEAT_COMMAND_ENV_VAR_NAME = "RASA_PRO_BETA_REPEAT_COMMAND"
490
+ try:
491
+ ensure_beta_feature_is_enabled(
492
+ "Repeat Command",
493
+ env_flag=RASA_PRO_BETA_REPEAT_COMMAND_ENV_VAR_NAME,
494
+ )
495
+ except BetaNotEnabledException:
496
+ return False
497
+
498
+ return True
499
+
500
+ @classmethod
501
+ def fingerprint_addon(cls: Any, config: Dict[str, Any]) -> Optional[str]:
502
+ """Add a fingerprint for the graph."""
503
+ # Get the default prompt template based on the model name
504
+ llm_config = resolve_model_client_config(
505
+ config.get(LLM_CONFIG_KEY), CompactLLMCommandGenerator.__name__
506
+ )
507
+ embedding_config = resolve_model_client_config(
508
+ config.get(FLOW_RETRIEVAL_KEY, {}).get(EMBEDDINGS_CONFIG_KEY),
509
+ FlowRetrieval.__name__,
510
+ )
511
+ default_command_prompt_template = get_default_prompt_template_based_on_model(
512
+ llm_config or {},
513
+ MODEL_PROMPT_MAPPER,
514
+ DEFAULT_COMMAND_PROMPT_TEMPLATE_FILE_NAME,
515
+ )
516
+ prompt_template = get_prompt_template(
517
+ config.get(PROMPT_TEMPLATE_CONFIG_KEY),
518
+ default_command_prompt_template,
519
+ )
520
+ return deep_container_fingerprint(
521
+ [prompt_template, llm_config, embedding_config]
522
+ )