rasa-pro 3.12.0.dev12__py3-none-any.whl → 3.12.0rc1__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.
- rasa/anonymization/anonymization_rule_executor.py +16 -10
- rasa/cli/data.py +16 -0
- rasa/cli/inspect.py +20 -1
- rasa/cli/project_templates/calm/config.yml +2 -2
- rasa/cli/project_templates/calm/endpoints.yml +2 -2
- rasa/cli/shell.py +3 -3
- rasa/cli/utils.py +12 -0
- rasa/core/actions/action.py +99 -193
- rasa/core/actions/action_handle_digressions.py +142 -0
- rasa/core/actions/action_run_slot_rejections.py +16 -4
- rasa/core/actions/forms.py +10 -5
- rasa/core/channels/__init__.py +4 -0
- rasa/core/channels/studio_chat.py +19 -0
- rasa/core/channels/telegram.py +42 -24
- rasa/core/channels/voice_ready/audiocodes.py +42 -23
- rasa/core/channels/voice_ready/utils.py +1 -1
- rasa/core/channels/voice_stream/asr/asr_engine.py +10 -4
- rasa/core/channels/voice_stream/asr/azure.py +14 -1
- rasa/core/channels/voice_stream/asr/deepgram.py +20 -4
- rasa/core/channels/voice_stream/audiocodes.py +264 -0
- rasa/core/channels/voice_stream/browser_audio.py +5 -1
- rasa/core/channels/voice_stream/call_state.py +10 -1
- rasa/core/channels/voice_stream/genesys.py +335 -0
- rasa/core/channels/voice_stream/tts/azure.py +11 -2
- rasa/core/channels/voice_stream/tts/cartesia.py +29 -10
- rasa/core/channels/voice_stream/twilio_media_streams.py +2 -1
- rasa/core/channels/voice_stream/voice_channel.py +25 -3
- rasa/core/constants.py +2 -0
- rasa/core/migrate.py +2 -2
- rasa/core/nlg/contextual_response_rephraser.py +18 -1
- rasa/core/nlg/generator.py +83 -15
- rasa/core/nlg/response.py +6 -3
- rasa/core/nlg/translate.py +55 -0
- rasa/core/policies/enterprise_search_prompt_with_citation_template.jinja2 +1 -1
- rasa/core/policies/flows/flow_executor.py +47 -46
- rasa/core/processor.py +72 -9
- rasa/core/run.py +4 -3
- rasa/dialogue_understanding/commands/can_not_handle_command.py +20 -2
- rasa/dialogue_understanding/commands/cancel_flow_command.py +80 -4
- rasa/dialogue_understanding/commands/change_flow_command.py +20 -2
- rasa/dialogue_understanding/commands/chit_chat_answer_command.py +20 -2
- rasa/dialogue_understanding/commands/clarify_command.py +29 -3
- rasa/dialogue_understanding/commands/command.py +1 -16
- rasa/dialogue_understanding/commands/command_syntax_manager.py +55 -0
- rasa/dialogue_understanding/commands/correct_slots_command.py +11 -2
- rasa/dialogue_understanding/commands/handle_digressions_command.py +150 -0
- rasa/dialogue_understanding/commands/human_handoff_command.py +20 -2
- rasa/dialogue_understanding/commands/knowledge_answer_command.py +20 -2
- rasa/dialogue_understanding/commands/prompt_command.py +94 -0
- rasa/dialogue_understanding/commands/repeat_bot_messages_command.py +20 -2
- rasa/dialogue_understanding/commands/set_slot_command.py +29 -15
- rasa/dialogue_understanding/commands/skip_question_command.py +20 -2
- rasa/dialogue_understanding/commands/start_flow_command.py +61 -2
- rasa/dialogue_understanding/commands/utils.py +98 -4
- rasa/dialogue_understanding/constants.py +1 -0
- rasa/dialogue_understanding/generator/__init__.py +2 -0
- rasa/dialogue_understanding/generator/command_generator.py +110 -73
- rasa/dialogue_understanding/generator/command_parser.py +16 -13
- rasa/dialogue_understanding/generator/constants.py +3 -0
- rasa/dialogue_understanding/generator/llm_based_command_generator.py +170 -5
- rasa/dialogue_understanding/generator/llm_command_generator.py +5 -3
- rasa/dialogue_understanding/generator/multi_step/multi_step_llm_command_generator.py +26 -4
- rasa/dialogue_understanding/generator/nlu_command_adapter.py +44 -3
- rasa/dialogue_understanding/generator/prompt_templates/__init__.py +0 -0
- rasa/dialogue_understanding/generator/prompt_templates/command_prompt_template.jinja2 +60 -0
- rasa/dialogue_understanding/generator/prompt_templates/command_prompt_v2_claude_3_5_sonnet_20240620_template.jinja2 +77 -0
- rasa/dialogue_understanding/generator/prompt_templates/command_prompt_v2_default.jinja2 +68 -0
- rasa/dialogue_understanding/generator/{single_step/command_prompt_template.jinja2 → prompt_templates/command_prompt_v2_gpt_4o_2024_11_20_template.jinja2} +1 -1
- rasa/dialogue_understanding/generator/single_step/compact_llm_command_generator.py +460 -0
- rasa/dialogue_understanding/generator/single_step/single_step_llm_command_generator.py +12 -318
- rasa/dialogue_understanding/generator/utils.py +32 -1
- rasa/dialogue_understanding/patterns/collect_information.py +1 -1
- rasa/dialogue_understanding/patterns/correction.py +13 -1
- rasa/dialogue_understanding/patterns/default_flows_for_patterns.yml +78 -2
- rasa/dialogue_understanding/patterns/handle_digressions.py +81 -0
- rasa/dialogue_understanding/patterns/validate_slot.py +65 -0
- rasa/dialogue_understanding/processor/command_processor.py +154 -28
- rasa/dialogue_understanding/utils.py +31 -0
- rasa/dialogue_understanding_test/README.md +50 -0
- rasa/dialogue_understanding_test/du_test_case.py +28 -8
- rasa/dialogue_understanding_test/du_test_result.py +13 -9
- rasa/dialogue_understanding_test/io.py +14 -0
- rasa/dialogue_understanding_test/test_case_simulation/test_case_tracker_simulator.py +3 -3
- rasa/e2e_test/utils/io.py +0 -37
- rasa/engine/graph.py +1 -0
- rasa/engine/language.py +140 -0
- rasa/engine/recipes/config_files/default_config.yml +4 -0
- rasa/engine/recipes/default_recipe.py +2 -0
- rasa/engine/recipes/graph_recipe.py +2 -0
- rasa/engine/storage/local_model_storage.py +1 -0
- rasa/engine/storage/storage.py +4 -1
- rasa/model_manager/runner_service.py +7 -4
- rasa/model_manager/socket_bridge.py +7 -6
- rasa/model_manager/warm_rasa_process.py +0 -1
- rasa/model_training.py +24 -27
- rasa/shared/constants.py +15 -13
- rasa/shared/core/constants.py +30 -3
- rasa/shared/core/domain.py +13 -20
- rasa/shared/core/events.py +13 -2
- rasa/shared/core/flows/constants.py +11 -0
- rasa/shared/core/flows/flow.py +100 -19
- rasa/shared/core/flows/flows_yaml_schema.json +69 -3
- rasa/shared/core/flows/steps/collect.py +19 -37
- rasa/shared/core/flows/utils.py +43 -4
- rasa/shared/core/flows/validation.py +1 -1
- rasa/shared/core/slot_mappings.py +350 -111
- rasa/shared/core/slots.py +154 -3
- rasa/shared/core/trackers.py +77 -2
- rasa/shared/importers/importer.py +50 -2
- rasa/shared/nlu/constants.py +1 -0
- rasa/shared/nlu/training_data/schemas/responses.yml +19 -12
- rasa/shared/providers/_configs/azure_entra_id_config.py +541 -0
- rasa/shared/providers/_configs/azure_openai_client_config.py +138 -3
- rasa/shared/providers/_configs/client_config.py +3 -1
- rasa/shared/providers/_configs/default_litellm_client_config.py +3 -1
- rasa/shared/providers/_configs/huggingface_local_embedding_client_config.py +3 -1
- rasa/shared/providers/_configs/litellm_router_client_config.py +3 -1
- rasa/shared/providers/_configs/model_group_config.py +4 -2
- rasa/shared/providers/_configs/oauth_config.py +33 -0
- rasa/shared/providers/_configs/openai_client_config.py +3 -1
- rasa/shared/providers/_configs/rasa_llm_client_config.py +3 -1
- rasa/shared/providers/_configs/self_hosted_llm_client_config.py +3 -1
- rasa/shared/providers/constants.py +6 -0
- rasa/shared/providers/embedding/azure_openai_embedding_client.py +28 -3
- rasa/shared/providers/embedding/litellm_router_embedding_client.py +3 -1
- rasa/shared/providers/llm/_base_litellm_client.py +42 -17
- rasa/shared/providers/llm/azure_openai_llm_client.py +81 -25
- rasa/shared/providers/llm/default_litellm_llm_client.py +3 -1
- rasa/shared/providers/llm/litellm_router_llm_client.py +29 -8
- rasa/shared/providers/llm/llm_client.py +23 -7
- rasa/shared/providers/llm/openai_llm_client.py +9 -3
- rasa/shared/providers/llm/rasa_llm_client.py +11 -2
- rasa/shared/providers/llm/self_hosted_llm_client.py +30 -11
- rasa/shared/providers/router/_base_litellm_router_client.py +3 -1
- rasa/shared/providers/router/router_client.py +3 -1
- rasa/shared/utils/constants.py +3 -0
- rasa/shared/utils/llm.py +31 -8
- rasa/shared/utils/pykwalify_extensions.py +24 -0
- rasa/shared/utils/schemas/domain.yml +26 -1
- rasa/telemetry.py +45 -14
- rasa/tracing/config.py +2 -0
- rasa/tracing/constants.py +12 -0
- rasa/tracing/instrumentation/instrumentation.py +36 -0
- rasa/tracing/instrumentation/metrics.py +41 -0
- rasa/tracing/metric_instrument_provider.py +40 -0
- rasa/utils/common.py +0 -1
- rasa/validator.py +561 -89
- rasa/version.py +1 -1
- {rasa_pro-3.12.0.dev12.dist-info → rasa_pro-3.12.0rc1.dist-info}/METADATA +2 -1
- {rasa_pro-3.12.0.dev12.dist-info → rasa_pro-3.12.0rc1.dist-info}/RECORD +153 -134
- {rasa_pro-3.12.0.dev12.dist-info → rasa_pro-3.12.0rc1.dist-info}/NOTICE +0 -0
- {rasa_pro-3.12.0.dev12.dist-info → rasa_pro-3.12.0rc1.dist-info}/WHEEL +0 -0
- {rasa_pro-3.12.0.dev12.dist-info → rasa_pro-3.12.0rc1.dist-info}/entry_points.txt +0 -0
rasa/core/processor.py
CHANGED
|
@@ -25,6 +25,7 @@ from rasa.core.channels.channel import (
|
|
|
25
25
|
OutputChannel,
|
|
26
26
|
UserMessage,
|
|
27
27
|
)
|
|
28
|
+
from rasa.core.constants import KEY_IS_CALM_SYSTEM, KEY_IS_COEXISTENCE_ASSISTANT
|
|
28
29
|
from rasa.core.http_interpreter import RasaNLUHttpInterpreter
|
|
29
30
|
from rasa.core.lock_store import LockStore
|
|
30
31
|
from rasa.core.nlg import NaturalLanguageGenerator
|
|
@@ -35,6 +36,12 @@ from rasa.dialogue_understanding.commands import (
|
|
|
35
36
|
NoopCommand,
|
|
36
37
|
SetSlotCommand,
|
|
37
38
|
)
|
|
39
|
+
from rasa.dialogue_understanding.commands.utils import (
|
|
40
|
+
create_validate_frames_from_slot_set_events,
|
|
41
|
+
)
|
|
42
|
+
from rasa.dialogue_understanding.patterns.validate_slot import (
|
|
43
|
+
ValidateSlotPatternFlowStackFrame,
|
|
44
|
+
)
|
|
38
45
|
from rasa.dialogue_understanding.utils import add_commands_to_message_parse_data
|
|
39
46
|
from rasa.engine import loader
|
|
40
47
|
from rasa.engine.constants import (
|
|
@@ -201,10 +208,7 @@ class MessageProcessor:
|
|
|
201
208
|
)
|
|
202
209
|
return None
|
|
203
210
|
|
|
204
|
-
|
|
205
|
-
tracker = await self.run_action_extract_slots(
|
|
206
|
-
message.output_channel, tracker
|
|
207
|
-
)
|
|
211
|
+
tracker = await self.run_action_extract_slots(message.output_channel, tracker)
|
|
208
212
|
|
|
209
213
|
await self._run_prediction_loop(message.output_channel, tracker)
|
|
210
214
|
|
|
@@ -218,7 +222,9 @@ class MessageProcessor:
|
|
|
218
222
|
return None
|
|
219
223
|
|
|
220
224
|
async def run_action_extract_slots(
|
|
221
|
-
self,
|
|
225
|
+
self,
|
|
226
|
+
output_channel: OutputChannel,
|
|
227
|
+
tracker: DialogueStateTracker,
|
|
222
228
|
) -> DialogueStateTracker:
|
|
223
229
|
"""Run action to extract slots and update the tracker accordingly.
|
|
224
230
|
|
|
@@ -233,6 +239,10 @@ class MessageProcessor:
|
|
|
233
239
|
ACTION_EXTRACT_SLOTS, self.domain, self.action_endpoint
|
|
234
240
|
)
|
|
235
241
|
metadata = await self._add_flows_to_metadata()
|
|
242
|
+
metadata[KEY_IS_CALM_SYSTEM] = self.message_contains_commands(
|
|
243
|
+
tracker.latest_message
|
|
244
|
+
)
|
|
245
|
+
metadata[KEY_IS_COEXISTENCE_ASSISTANT] = self._is_coexistence_assistant(tracker)
|
|
236
246
|
|
|
237
247
|
extraction_events = await action_extract_slots.run(
|
|
238
248
|
output_channel, self.nlg, tracker, self.domain, metadata
|
|
@@ -1251,6 +1261,12 @@ class MessageProcessor:
|
|
|
1251
1261
|
# events and return values are used to update
|
|
1252
1262
|
# the tracker state after an action has been taken
|
|
1253
1263
|
try:
|
|
1264
|
+
validate_frames: List[ValidateSlotPatternFlowStackFrame] = []
|
|
1265
|
+
# check if the last action was a correction action
|
|
1266
|
+
# before validating the corrected slots
|
|
1267
|
+
if tracker.latest_action_name == ACTION_CORRECT_FLOW_SLOT:
|
|
1268
|
+
tracker, validate_frames = self.validate_corrected_slots(tracker)
|
|
1269
|
+
|
|
1254
1270
|
# Use temporary tracker as we might need to discard the policy events in
|
|
1255
1271
|
# case of a rejection.
|
|
1256
1272
|
temporary_tracker = tracker.copy()
|
|
@@ -1262,6 +1278,12 @@ class MessageProcessor:
|
|
|
1262
1278
|
|
|
1263
1279
|
if isinstance(action, FormAction):
|
|
1264
1280
|
flows_metadata = await self._add_flows_to_metadata()
|
|
1281
|
+
flows_metadata[KEY_IS_CALM_SYSTEM] = self.message_contains_commands(
|
|
1282
|
+
temporary_tracker.latest_message
|
|
1283
|
+
)
|
|
1284
|
+
flows_metadata[KEY_IS_COEXISTENCE_ASSISTANT] = (
|
|
1285
|
+
self._is_coexistence_assistant(temporary_tracker)
|
|
1286
|
+
)
|
|
1265
1287
|
metadata = prediction.action_metadata or {}
|
|
1266
1288
|
metadata.update(flows_metadata)
|
|
1267
1289
|
|
|
@@ -1276,6 +1298,14 @@ class MessageProcessor:
|
|
|
1276
1298
|
events = await action.run(
|
|
1277
1299
|
output_channel, nlg, temporary_tracker, self.domain
|
|
1278
1300
|
)
|
|
1301
|
+
|
|
1302
|
+
if validate_frames:
|
|
1303
|
+
stack = tracker.stack
|
|
1304
|
+
for frame in validate_frames:
|
|
1305
|
+
stack.push(frame)
|
|
1306
|
+
new_events = tracker.create_stack_updated_events(stack)
|
|
1307
|
+
tracker.update_with_events(new_events)
|
|
1308
|
+
|
|
1279
1309
|
self._log_action_and_events_on_tracker(tracker, action, events, prediction)
|
|
1280
1310
|
except ActionExecutionRejection:
|
|
1281
1311
|
events = [
|
|
@@ -1479,18 +1509,51 @@ class MessageProcessor:
|
|
|
1479
1509
|
Returns:
|
|
1480
1510
|
bool: True if any node in the graph schema uses `FlowPolicy`.
|
|
1481
1511
|
"""
|
|
1512
|
+
flow_policy_class_path = "rasa.core.policies.flow_policy.FlowPolicy"
|
|
1513
|
+
return self._is_component_present_in_graph_nodes(flow_policy_class_path)
|
|
1514
|
+
|
|
1515
|
+
@staticmethod
|
|
1516
|
+
def _is_coexistence_assistant(tracker: DialogueStateTracker) -> bool:
|
|
1517
|
+
"""Inspect the tracker to decide if we are in coexistence.
|
|
1518
|
+
|
|
1519
|
+
Returns:
|
|
1520
|
+
bool: True if the tracker contains the routine slot.
|
|
1521
|
+
"""
|
|
1522
|
+
return tracker.slots.get(ROUTE_TO_CALM_SLOT) is not None
|
|
1523
|
+
|
|
1524
|
+
def _is_component_present_in_graph_nodes(self, component_path: Text) -> bool:
|
|
1525
|
+
"""Check if a component is present in the graph nodes.
|
|
1526
|
+
|
|
1527
|
+
Args:
|
|
1528
|
+
component_path: The path of the component to check for.
|
|
1529
|
+
|
|
1530
|
+
Returns:
|
|
1531
|
+
`True` if the component is present in the graph nodes, `False` otherwise.
|
|
1532
|
+
"""
|
|
1482
1533
|
# Get the graph schema's nodes from the graph runner.
|
|
1483
1534
|
nodes: dict[str, Any] = self.graph_runner._graph_schema.nodes # type: ignore[attr-defined]
|
|
1484
1535
|
|
|
1485
|
-
flow_policy_class_path = "rasa.core.policies.flow_policy.FlowPolicy"
|
|
1486
|
-
# Iterate over the nodes and check if any node uses `FlowPolicy`.
|
|
1487
1536
|
for node_name, schema_node in nodes.items():
|
|
1488
1537
|
if (
|
|
1489
1538
|
schema_node.uses is not None
|
|
1490
1539
|
and f"{schema_node.uses.__module__}.{schema_node.uses.__name__}"
|
|
1491
|
-
==
|
|
1540
|
+
== component_path
|
|
1492
1541
|
):
|
|
1493
1542
|
return True
|
|
1494
1543
|
|
|
1495
|
-
# Return False if no node is found using `FlowPolicy`.
|
|
1496
1544
|
return False
|
|
1545
|
+
|
|
1546
|
+
def validate_corrected_slots(
|
|
1547
|
+
self,
|
|
1548
|
+
tracker: DialogueStateTracker,
|
|
1549
|
+
) -> Tuple[DialogueStateTracker, List[ValidateSlotPatternFlowStackFrame]]:
|
|
1550
|
+
"""Validate the slots that were corrected in the tracker."""
|
|
1551
|
+
prior_tracker_events = list(reversed(tracker.events))
|
|
1552
|
+
tracker, validate_frames = create_validate_frames_from_slot_set_events(
|
|
1553
|
+
tracker,
|
|
1554
|
+
prior_tracker_events,
|
|
1555
|
+
should_break=True,
|
|
1556
|
+
update_corrected_slots=True,
|
|
1557
|
+
)
|
|
1558
|
+
|
|
1559
|
+
return tracker, validate_frames
|
rasa/core/run.py
CHANGED
|
@@ -283,9 +283,10 @@ def serve_application(
|
|
|
283
283
|
endpoints.lock_store if endpoints else None
|
|
284
284
|
)
|
|
285
285
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
286
|
+
if not inspect:
|
|
287
|
+
telemetry.track_server_start(
|
|
288
|
+
input_channels, endpoints, model_path, number_of_workers, enable_api
|
|
289
|
+
)
|
|
289
290
|
|
|
290
291
|
rasa.utils.common.update_sanic_log_level(
|
|
291
292
|
log_file, use_syslog, syslog_address, syslog_port, syslog_protocol
|
|
@@ -5,6 +5,10 @@ from dataclasses import dataclass
|
|
|
5
5
|
from typing import Any, Dict, List, Optional, Text
|
|
6
6
|
|
|
7
7
|
from rasa.dialogue_understanding.commands.command import Command
|
|
8
|
+
from rasa.dialogue_understanding.commands.command_syntax_manager import (
|
|
9
|
+
CommandSyntaxManager,
|
|
10
|
+
CommandSyntaxVersion,
|
|
11
|
+
)
|
|
8
12
|
from rasa.dialogue_understanding.patterns.cannot_handle import (
|
|
9
13
|
CannotHandlePatternFlowStackFrame,
|
|
10
14
|
)
|
|
@@ -74,7 +78,14 @@ class CannotHandleCommand(Command):
|
|
|
74
78
|
|
|
75
79
|
def to_dsl(self) -> str:
|
|
76
80
|
"""Converts the command to a DSL string."""
|
|
77
|
-
|
|
81
|
+
mapper = {
|
|
82
|
+
CommandSyntaxVersion.v1: "CannotHandle()",
|
|
83
|
+
CommandSyntaxVersion.v2: "cannot handle",
|
|
84
|
+
}
|
|
85
|
+
return mapper.get(
|
|
86
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
87
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
88
|
+
)
|
|
78
89
|
|
|
79
90
|
@classmethod
|
|
80
91
|
def from_dsl(cls, match: re.Match, **kwargs: Any) -> CannotHandleCommand:
|
|
@@ -86,4 +97,11 @@ class CannotHandleCommand(Command):
|
|
|
86
97
|
|
|
87
98
|
@staticmethod
|
|
88
99
|
def regex_pattern() -> str:
|
|
89
|
-
|
|
100
|
+
mapper = {
|
|
101
|
+
CommandSyntaxVersion.v1: r"CannotHandle\(\)",
|
|
102
|
+
CommandSyntaxVersion.v2: r"^[^\w]*cannot handle$",
|
|
103
|
+
}
|
|
104
|
+
return mapper.get(
|
|
105
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
106
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
107
|
+
)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import copy
|
|
3
4
|
import re
|
|
4
5
|
from dataclasses import dataclass
|
|
5
6
|
from typing import Any, Dict, List
|
|
@@ -7,7 +8,12 @@ from typing import Any, Dict, List
|
|
|
7
8
|
import structlog
|
|
8
9
|
|
|
9
10
|
from rasa.dialogue_understanding.commands.command import Command
|
|
11
|
+
from rasa.dialogue_understanding.commands.command_syntax_manager import (
|
|
12
|
+
CommandSyntaxManager,
|
|
13
|
+
CommandSyntaxVersion,
|
|
14
|
+
)
|
|
10
15
|
from rasa.dialogue_understanding.patterns.cancel import CancelPatternFlowStackFrame
|
|
16
|
+
from rasa.dialogue_understanding.patterns.clarify import ClarifyPatternFlowStackFrame
|
|
11
17
|
from rasa.dialogue_understanding.stack.dialogue_stack import DialogueStack
|
|
12
18
|
from rasa.dialogue_understanding.stack.frames import UserFlowStackFrame
|
|
13
19
|
from rasa.dialogue_understanding.stack.frames.flow_stack_frame import FlowStackFrameType
|
|
@@ -89,7 +95,8 @@ class CancelFlowCommand(Command):
|
|
|
89
95
|
original_stack = original_tracker.stack
|
|
90
96
|
|
|
91
97
|
applied_events: List[Event] = []
|
|
92
|
-
|
|
98
|
+
# capture the top frame before we push new frames onto the stack
|
|
99
|
+
initial_top_frame = stack.top()
|
|
93
100
|
user_frame = top_user_flow_frame(original_stack)
|
|
94
101
|
current_flow = user_frame.flow(all_flows) if user_frame else None
|
|
95
102
|
|
|
@@ -106,7 +113,9 @@ class CancelFlowCommand(Command):
|
|
|
106
113
|
|
|
107
114
|
stack.push(
|
|
108
115
|
CancelPatternFlowStackFrame(
|
|
109
|
-
canceled_name=current_flow.readable_name(
|
|
116
|
+
canceled_name=current_flow.readable_name(
|
|
117
|
+
language=tracker.current_language
|
|
118
|
+
),
|
|
110
119
|
canceled_frames=canceled_frames,
|
|
111
120
|
)
|
|
112
121
|
)
|
|
@@ -114,6 +123,21 @@ class CancelFlowCommand(Command):
|
|
|
114
123
|
if user_frame:
|
|
115
124
|
applied_events.append(FlowCancelled(user_frame.flow_id, user_frame.step_id))
|
|
116
125
|
|
|
126
|
+
if initial_top_frame and isinstance(
|
|
127
|
+
initial_top_frame, ClarifyPatternFlowStackFrame
|
|
128
|
+
):
|
|
129
|
+
structlogger.debug(
|
|
130
|
+
"command_executor.cancel_flow.cancel_clarification_options",
|
|
131
|
+
clarification_options=initial_top_frame.clarification_options,
|
|
132
|
+
)
|
|
133
|
+
applied_events += cancel_all_pending_clarification_options(
|
|
134
|
+
initial_top_frame,
|
|
135
|
+
original_stack,
|
|
136
|
+
canceled_frames,
|
|
137
|
+
all_flows,
|
|
138
|
+
stack,
|
|
139
|
+
)
|
|
140
|
+
|
|
117
141
|
return applied_events + tracker.create_stack_updated_events(stack)
|
|
118
142
|
|
|
119
143
|
def __hash__(self) -> int:
|
|
@@ -124,7 +148,14 @@ class CancelFlowCommand(Command):
|
|
|
124
148
|
|
|
125
149
|
def to_dsl(self) -> str:
|
|
126
150
|
"""Converts the command to a DSL string."""
|
|
127
|
-
|
|
151
|
+
mapper = {
|
|
152
|
+
CommandSyntaxVersion.v1: "CancelFlow()",
|
|
153
|
+
CommandSyntaxVersion.v2: "cancel flow",
|
|
154
|
+
}
|
|
155
|
+
return mapper.get(
|
|
156
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
157
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
158
|
+
)
|
|
128
159
|
|
|
129
160
|
@classmethod
|
|
130
161
|
def from_dsl(cls, match: re.Match, **kwargs: Any) -> CancelFlowCommand:
|
|
@@ -133,4 +164,49 @@ class CancelFlowCommand(Command):
|
|
|
133
164
|
|
|
134
165
|
@staticmethod
|
|
135
166
|
def regex_pattern() -> str:
|
|
136
|
-
|
|
167
|
+
mapper = {
|
|
168
|
+
CommandSyntaxVersion.v1: r"CancelFlow\(\)",
|
|
169
|
+
CommandSyntaxVersion.v2: r"^[^\w]*cancel flow$",
|
|
170
|
+
}
|
|
171
|
+
return mapper.get(
|
|
172
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
173
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def cancel_all_pending_clarification_options(
|
|
178
|
+
initial_top_frame: ClarifyPatternFlowStackFrame,
|
|
179
|
+
original_stack: DialogueStack,
|
|
180
|
+
canceled_frames: List[str],
|
|
181
|
+
all_flows: FlowsList,
|
|
182
|
+
stack: DialogueStack,
|
|
183
|
+
) -> List[FlowCancelled]:
|
|
184
|
+
"""Cancel all pending clarification options.
|
|
185
|
+
|
|
186
|
+
This is a special case when the assistant asks the user to clarify
|
|
187
|
+
which pending digression flow to start after the completion of an active flow.
|
|
188
|
+
If the user chooses to cancel all options, this function takes care of
|
|
189
|
+
updating the stack by removing all pending flow stack frames
|
|
190
|
+
listed as clarification options.
|
|
191
|
+
"""
|
|
192
|
+
clarification_names = set(initial_top_frame.names)
|
|
193
|
+
to_be_canceled_frames = []
|
|
194
|
+
applied_events = []
|
|
195
|
+
for frame in reversed(original_stack.frames):
|
|
196
|
+
if frame.frame_id in canceled_frames:
|
|
197
|
+
continue
|
|
198
|
+
|
|
199
|
+
to_be_canceled_frames.append(frame.frame_id)
|
|
200
|
+
if isinstance(frame, UserFlowStackFrame):
|
|
201
|
+
readable_flow_name = frame.flow(all_flows).readable_name()
|
|
202
|
+
if readable_flow_name in clarification_names:
|
|
203
|
+
stack.push(
|
|
204
|
+
CancelPatternFlowStackFrame(
|
|
205
|
+
canceled_name=readable_flow_name,
|
|
206
|
+
canceled_frames=copy.deepcopy(to_be_canceled_frames),
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
applied_events.append(FlowCancelled(frame.flow_id, frame.step_id))
|
|
210
|
+
to_be_canceled_frames.clear()
|
|
211
|
+
|
|
212
|
+
return applied_events
|
|
@@ -5,6 +5,10 @@ from dataclasses import dataclass
|
|
|
5
5
|
from typing import Any, Dict, List
|
|
6
6
|
|
|
7
7
|
from rasa.dialogue_understanding.commands.command import Command
|
|
8
|
+
from rasa.dialogue_understanding.commands.command_syntax_manager import (
|
|
9
|
+
CommandSyntaxManager,
|
|
10
|
+
CommandSyntaxVersion,
|
|
11
|
+
)
|
|
8
12
|
from rasa.shared.core.events import Event
|
|
9
13
|
from rasa.shared.core.flows import FlowsList
|
|
10
14
|
from rasa.shared.core.trackers import DialogueStateTracker
|
|
@@ -48,7 +52,14 @@ class ChangeFlowCommand(Command):
|
|
|
48
52
|
|
|
49
53
|
def to_dsl(self) -> str:
|
|
50
54
|
"""Converts the command to a DSL string."""
|
|
51
|
-
|
|
55
|
+
mapper = {
|
|
56
|
+
CommandSyntaxVersion.v1: "ChangeFlow()",
|
|
57
|
+
CommandSyntaxVersion.v2: "change",
|
|
58
|
+
}
|
|
59
|
+
return mapper.get(
|
|
60
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
61
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
62
|
+
)
|
|
52
63
|
|
|
53
64
|
@staticmethod
|
|
54
65
|
def from_dsl(match: re.Match, **kwargs: Any) -> ChangeFlowCommand:
|
|
@@ -57,4 +68,11 @@ class ChangeFlowCommand(Command):
|
|
|
57
68
|
|
|
58
69
|
@staticmethod
|
|
59
70
|
def regex_pattern() -> str:
|
|
60
|
-
|
|
71
|
+
mapper = {
|
|
72
|
+
CommandSyntaxVersion.v1: r"ChangeFlow\(\)",
|
|
73
|
+
CommandSyntaxVersion.v2: r"^[^\w]*change$",
|
|
74
|
+
}
|
|
75
|
+
return mapper.get(
|
|
76
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
77
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
78
|
+
)
|
|
@@ -4,6 +4,10 @@ import re
|
|
|
4
4
|
from dataclasses import dataclass
|
|
5
5
|
from typing import Any, Dict, List
|
|
6
6
|
|
|
7
|
+
from rasa.dialogue_understanding.commands.command_syntax_manager import (
|
|
8
|
+
CommandSyntaxManager,
|
|
9
|
+
CommandSyntaxVersion,
|
|
10
|
+
)
|
|
7
11
|
from rasa.dialogue_understanding.commands.free_form_answer_command import (
|
|
8
12
|
FreeFormAnswerCommand,
|
|
9
13
|
)
|
|
@@ -59,7 +63,14 @@ class ChitChatAnswerCommand(FreeFormAnswerCommand):
|
|
|
59
63
|
|
|
60
64
|
def to_dsl(self) -> str:
|
|
61
65
|
"""Converts the command to a DSL string."""
|
|
62
|
-
|
|
66
|
+
mapper = {
|
|
67
|
+
CommandSyntaxVersion.v1: "ChitChat()",
|
|
68
|
+
CommandSyntaxVersion.v2: "offtopic reply",
|
|
69
|
+
}
|
|
70
|
+
return mapper.get(
|
|
71
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
72
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
73
|
+
)
|
|
63
74
|
|
|
64
75
|
@classmethod
|
|
65
76
|
def from_dsl(cls, match: re.Match, **kwargs: Any) -> ChitChatAnswerCommand:
|
|
@@ -68,4 +79,11 @@ class ChitChatAnswerCommand(FreeFormAnswerCommand):
|
|
|
68
79
|
|
|
69
80
|
@staticmethod
|
|
70
81
|
def regex_pattern() -> str:
|
|
71
|
-
|
|
82
|
+
mapper = {
|
|
83
|
+
CommandSyntaxVersion.v1: r"ChitChat\(\)",
|
|
84
|
+
CommandSyntaxVersion.v2: r"^[^\w]*offtopic reply$",
|
|
85
|
+
}
|
|
86
|
+
return mapper.get(
|
|
87
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
88
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
89
|
+
)
|
|
@@ -7,6 +7,10 @@ from typing import Any, Dict, List, Optional
|
|
|
7
7
|
import structlog
|
|
8
8
|
|
|
9
9
|
from rasa.dialogue_understanding.commands.command import Command
|
|
10
|
+
from rasa.dialogue_understanding.commands.command_syntax_manager import (
|
|
11
|
+
CommandSyntaxManager,
|
|
12
|
+
CommandSyntaxVersion,
|
|
13
|
+
)
|
|
10
14
|
from rasa.dialogue_understanding.commands.utils import extract_cleaned_options
|
|
11
15
|
from rasa.dialogue_understanding.patterns.clarify import ClarifyPatternFlowStackFrame
|
|
12
16
|
from rasa.shared.core.events import Event
|
|
@@ -74,7 +78,13 @@ class ClarifyCommand(Command):
|
|
|
74
78
|
|
|
75
79
|
stack = tracker.stack
|
|
76
80
|
relevant_flows = [all_flows.flow_by_id(opt) for opt in clean_options]
|
|
77
|
-
|
|
81
|
+
|
|
82
|
+
names = [
|
|
83
|
+
flow.readable_name(language=tracker.current_language)
|
|
84
|
+
for flow in relevant_flows
|
|
85
|
+
if flow is not None
|
|
86
|
+
]
|
|
87
|
+
|
|
78
88
|
stack.push(ClarifyPatternFlowStackFrame(names=names))
|
|
79
89
|
return tracker.create_stack_updated_events(stack)
|
|
80
90
|
|
|
@@ -89,7 +99,14 @@ class ClarifyCommand(Command):
|
|
|
89
99
|
|
|
90
100
|
def to_dsl(self) -> str:
|
|
91
101
|
"""Converts the command to a DSL string."""
|
|
92
|
-
|
|
102
|
+
mapper = {
|
|
103
|
+
CommandSyntaxVersion.v1: f"Clarify({', '.join(self.options)})",
|
|
104
|
+
CommandSyntaxVersion.v2: f"disambiguate flows {' '.join(self.options)}",
|
|
105
|
+
}
|
|
106
|
+
return mapper.get(
|
|
107
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
108
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
109
|
+
)
|
|
93
110
|
|
|
94
111
|
@classmethod
|
|
95
112
|
def from_dsl(cls, match: re.Match, **kwargs: Any) -> Optional[ClarifyCommand]:
|
|
@@ -99,4 +116,13 @@ class ClarifyCommand(Command):
|
|
|
99
116
|
|
|
100
117
|
@staticmethod
|
|
101
118
|
def regex_pattern() -> str:
|
|
102
|
-
|
|
119
|
+
mapper = {
|
|
120
|
+
CommandSyntaxVersion.v1: r"Clarify\(([\"\'a-zA-Z0-9_, ]*)\)",
|
|
121
|
+
CommandSyntaxVersion.v2: (
|
|
122
|
+
r"^[^\w]*disambiguate flows ([\"\'a-zA-Z0-9_, ]*)$"
|
|
123
|
+
),
|
|
124
|
+
}
|
|
125
|
+
return mapper.get(
|
|
126
|
+
CommandSyntaxManager.get_syntax_version(),
|
|
127
|
+
mapper[CommandSyntaxManager.get_default_syntax_version()],
|
|
128
|
+
)
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import dataclasses
|
|
4
|
-
import re
|
|
5
4
|
from dataclasses import dataclass
|
|
6
|
-
from typing import Any, Dict, List
|
|
5
|
+
from typing import Any, Dict, List
|
|
7
6
|
|
|
8
7
|
import rasa.shared.utils.common
|
|
9
8
|
from rasa.shared.core.events import Event
|
|
@@ -85,17 +84,3 @@ class Command:
|
|
|
85
84
|
The events to apply to the tracker.
|
|
86
85
|
"""
|
|
87
86
|
raise NotImplementedError()
|
|
88
|
-
|
|
89
|
-
def to_dsl(self) -> str:
|
|
90
|
-
"""Converts the command to a DSL string."""
|
|
91
|
-
raise NotImplementedError()
|
|
92
|
-
|
|
93
|
-
@classmethod
|
|
94
|
-
def from_dsl(cls, match: re.Match, **kwargs: Any) -> Optional[Command]:
|
|
95
|
-
"""Converts the DSL string to a command."""
|
|
96
|
-
raise NotImplementedError()
|
|
97
|
-
|
|
98
|
-
@staticmethod
|
|
99
|
-
def regex_pattern() -> str:
|
|
100
|
-
"""Returns the regex pattern for the command."""
|
|
101
|
-
raise NotImplementedError()
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import structlog
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CommandSyntaxVersion(Enum):
|
|
8
|
+
"""Defines different syntax versions for commands."""
|
|
9
|
+
|
|
10
|
+
v1 = "v1"
|
|
11
|
+
v2 = "v2"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
structlogger = structlog.get_logger()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CommandSyntaxManager:
|
|
18
|
+
"""A class to manage the command syntax version. It is used to set and get the
|
|
19
|
+
command syntax version. This class provides a way to introduce new syntax versions
|
|
20
|
+
for commands in the future. Hence, it is for internal use only.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
_version = None # Directly store the version as a class attribute
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def set_syntax_version(cls, version: CommandSyntaxVersion) -> None:
|
|
27
|
+
"""Sets the command syntax version on the class itself.
|
|
28
|
+
This method is called only once at the time of LLMCommandGenerator
|
|
29
|
+
initialization to set the command syntax version, which ensures that the command
|
|
30
|
+
syntax version remains consistent throughout the lifetime of the generator.
|
|
31
|
+
"""
|
|
32
|
+
if cls._version:
|
|
33
|
+
structlogger.warn(
|
|
34
|
+
"command_syntax_manager.syntax_version_already_set",
|
|
35
|
+
event_info=(
|
|
36
|
+
f"The command syntax version has already been set. Overwriting "
|
|
37
|
+
f"the existing version with the new version - {version}."
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
cls._version = version
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def get_syntax_version(cls) -> Optional[CommandSyntaxVersion]:
|
|
44
|
+
"""Fetches the stored command syntax version."""
|
|
45
|
+
return cls._version
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def get_default_syntax_version() -> CommandSyntaxVersion:
|
|
49
|
+
"""Returns the default command syntax version."""
|
|
50
|
+
return CommandSyntaxVersion.v1
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def reset_syntax_version(cls) -> None:
|
|
54
|
+
"""Resets the command syntax version. Implemented for use in testing."""
|
|
55
|
+
cls._version = None
|
|
@@ -31,6 +31,7 @@ class CorrectedSlot:
|
|
|
31
31
|
|
|
32
32
|
name: str
|
|
33
33
|
value: Any
|
|
34
|
+
filled_by: Optional[str] = None
|
|
34
35
|
|
|
35
36
|
|
|
36
37
|
@dataclass
|
|
@@ -54,7 +55,9 @@ class CorrectSlotsCommand(Command):
|
|
|
54
55
|
try:
|
|
55
56
|
return CorrectSlotsCommand(
|
|
56
57
|
corrected_slots=[
|
|
57
|
-
CorrectedSlot(
|
|
58
|
+
CorrectedSlot(
|
|
59
|
+
s["name"], value=s["value"], filled_by=s.get("filled_by", None)
|
|
60
|
+
)
|
|
58
61
|
for s in data["corrected_slots"]
|
|
59
62
|
]
|
|
60
63
|
)
|
|
@@ -135,7 +138,10 @@ class CorrectSlotsCommand(Command):
|
|
|
135
138
|
proposed_slots = {}
|
|
136
139
|
for corrected_slot in self.corrected_slots:
|
|
137
140
|
if tracker.get_slot(corrected_slot.name) != corrected_slot.value:
|
|
138
|
-
proposed_slots[corrected_slot.name] =
|
|
141
|
+
proposed_slots[corrected_slot.name] = {
|
|
142
|
+
"value": corrected_slot.value,
|
|
143
|
+
"filled_by": corrected_slot.filled_by,
|
|
144
|
+
}
|
|
139
145
|
else:
|
|
140
146
|
structlogger.debug(
|
|
141
147
|
"command_executor.skip_correction.slot_already_set", command=self
|
|
@@ -240,6 +246,9 @@ class CorrectSlotsCommand(Command):
|
|
|
240
246
|
corrected_slots=proposed_slots,
|
|
241
247
|
reset_flow_id=earliest_collect.flow_id if earliest_collect else None,
|
|
242
248
|
reset_step_id=earliest_collect.step.id if earliest_collect else None,
|
|
249
|
+
new_slot_values=[
|
|
250
|
+
value.get("value") for slot, value in proposed_slots.items()
|
|
251
|
+
],
|
|
243
252
|
)
|
|
244
253
|
|
|
245
254
|
def run_command_on_tracker(
|