From 4c087cf82f24163a6b89cefd5fffa803204eeba9 Mon Sep 17 00:00:00 2001 From: ZergLev <64711614+ZergLev@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:01:21 +0500 Subject: [PATCH] Make functions accept str along with Message (#337) # Description - Made `dff.script.conditions.exact_match` and `dff.utils.testing.common.check_happy_path` accept `str` along with `Message` - Tests added for `exact_match()`, but not for `check_happy_path()`. - Changed all instances of `exact_match(Message("some text"))` to `exact_match("some text")` # Checklist - [x] I have performed a self-review of the changes # To Consider - Update tutorials / guides --------- Co-authored-by: Roman Zlobin --- README.md | 2 +- dff/script/conditions/std_conditions.py | 10 ++- dff/utils/testing/common.py | 10 ++- dff/utils/testing/toy_script.py | 74 ++++++++-------- docs/source/user_guides/basic_conceptions.rst | 16 ++-- tests/pipeline/test_messenger_interface.py | 6 +- tests/script/conditions/test_conditions.py | 22 ++--- tests/tutorials/test_utils.py | 3 +- tutorials/messengers/telegram/1_basic.py | 4 +- tutorials/script/core/1_basics.py | 52 +++++------ tutorials/script/core/2_conditions.py | 46 +++++----- tutorials/script/core/3_responses.py | 46 +++++----- tutorials/script/core/4_transitions.py | 87 ++++++++----------- tutorials/script/core/5_global_transitions.py | 87 ++++++++----------- .../script/core/6_context_serialization.py | 8 +- .../script/core/7_pre_response_processing.py | 15 ++-- tutorials/script/core/8_misc.py | 60 +++++-------- .../core/9_pre_transitions_processing.py | 10 +-- tutorials/script/responses/1_basics.py | 16 ++-- tutorials/script/responses/4_multi_message.py | 16 ++-- tutorials/utils/1_cache.py | 6 +- tutorials/utils/2_lru_cache.py | 6 +- 22 files changed, 277 insertions(+), 325 deletions(-) diff --git a/README.md b/README.md index 2afa8b3ae..6b54cf43f 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ import dff.script.conditions.std_conditions as cnd script = { GLOBAL: { TRANSITIONS: { - ("flow", "node_hi"): cnd.exact_match(Message("Hi")), + ("flow", "node_hi"): cnd.exact_match("Hi"), ("flow", "node_ok"): cnd.true() } }, diff --git a/dff/script/conditions/std_conditions.py b/dff/script/conditions/std_conditions.py index 88c086ab4..56cbc1ef9 100644 --- a/dff/script/conditions/std_conditions.py +++ b/dff/script/conditions/std_conditions.py @@ -22,18 +22,22 @@ @validate_call -def exact_match(match: Message, skip_none: bool = True) -> Callable[[Context, Pipeline], bool]: +def exact_match(match: Union[str, Message], skip_none: bool = True) -> Callable[[Context, Pipeline], bool]: """ Return function handler. This handler returns `True` only if the last user phrase - is the same Message as the `match`. + is the same `Message` as the `match`. If `skip_none` the handler will not compare `None` fields of `match`. - :param match: A Message variable to compare user request with. + :param match: A `Message` variable to compare user request with. + Can also accept `str`, which will be converted into a `Message` with its text field equal to `match`. :param skip_none: Whether fields should be compared if they are `None` in :py:const:`match`. """ def exact_match_condition_handler(ctx: Context, pipeline: Pipeline) -> bool: request = ctx.last_request + nonlocal match + if isinstance(match, str): + match = Message(text=match) if request is None: return False for field in match.model_fields: diff --git a/dff/utils/testing/common.py b/dff/utils/testing/common.py index b89621044..77310b2b6 100644 --- a/dff/utils/testing/common.py +++ b/dff/utils/testing/common.py @@ -5,7 +5,7 @@ """ from os import getenv -from typing import Callable, Tuple, Optional +from typing import Callable, Tuple, Optional, Union from uuid import uuid4 from dff.script import Context, Message @@ -32,7 +32,7 @@ def is_interactive_mode() -> bool: # pragma: no cover def check_happy_path( pipeline: Pipeline, - happy_path: Tuple[Tuple[Message, Message], ...], + happy_path: Tuple[Tuple[Union[str, Message], Union[str, Message]], ...], # This optional argument is used for additional processing of candidate responses and reference responses response_comparer: Callable[[Message, Message, Context], Optional[str]] = default_comparer, printout_enable: bool = True, @@ -51,7 +51,11 @@ def check_happy_path( """ ctx_id = uuid4() # get random ID for current context - for step_id, (request, reference_response) in enumerate(happy_path): + for step_id, (request_raw, reference_response_raw) in enumerate(happy_path): + request = Message(text=request_raw) if isinstance(request_raw, str) else request_raw + reference_response = ( + Message(text=reference_response_raw) if isinstance(reference_response_raw, str) else reference_response_raw + ) ctx = pipeline(request, ctx_id) candidate_response = ctx.last_response if printout_enable: diff --git a/dff/utils/testing/toy_script.py b/dff/utils/testing/toy_script.py index dfcd7594e..ee56a7ecf 100644 --- a/dff/utils/testing/toy_script.py +++ b/dff/utils/testing/toy_script.py @@ -12,24 +12,24 @@ "greeting_flow": { "start_node": { RESPONSE: Message(), - TRANSITIONS: {"node1": exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": exact_match("Hi")}, }, "node1": { RESPONSE: Message("Hi, how are you?"), - TRANSITIONS: {"node2": exact_match(Message("i'm fine, how are you?"))}, + TRANSITIONS: {"node2": exact_match("i'm fine, how are you?")}, }, "node2": { RESPONSE: Message("Good. What do you want to talk about?"), - TRANSITIONS: {"node3": exact_match(Message("Let's talk about music."))}, + TRANSITIONS: {"node3": exact_match("Let's talk about music.")}, }, "node3": { RESPONSE: Message("Sorry, I can not talk about music now."), - TRANSITIONS: {"node4": exact_match(Message("Ok, goodbye."))}, + TRANSITIONS: {"node4": exact_match("Ok, goodbye.")}, }, - "node4": {RESPONSE: Message("bye"), TRANSITIONS: {"node1": exact_match(Message("Hi"))}}, + "node4": {RESPONSE: Message("bye"), TRANSITIONS: {"node1": exact_match("Hi")}}, "fallback_node": { RESPONSE: Message("Ooops"), - TRANSITIONS: {"node1": exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": exact_match("Hi")}, }, } } @@ -52,11 +52,11 @@ """ HAPPY_PATH = ( - (Message("Hi"), Message("Hi, how are you?")), - (Message("i'm fine, how are you?"), Message("Good. What do you want to talk about?")), - (Message("Let's talk about music."), Message("Sorry, I can not talk about music now.")), - (Message("Ok, goodbye."), Message("bye")), - (Message("Hi"), Message("Hi, how are you?")), + ("Hi", "Hi, how are you?"), + ("i'm fine, how are you?", "Good. What do you want to talk about?"), + ("Let's talk about music.", "Sorry, I can not talk about music now."), + ("Ok, goodbye.", "bye"), + ("Hi", "Hi, how are you?"), ) """ An example of a simple dialog. @@ -69,10 +69,10 @@ "start": { RESPONSE: Message("Hi"), TRANSITIONS: { - ("small_talk", "ask_some_questions"): exact_match(Message("hi")), - ("animals", "have_pets"): exact_match(Message("i like animals")), - ("animals", "like_animals"): exact_match(Message("let's talk about animals")), - ("news", "what_news"): exact_match(Message("let's talk about news")), + ("small_talk", "ask_some_questions"): exact_match("hi"), + ("animals", "have_pets"): exact_match("i like animals"), + ("animals", "like_animals"): exact_match("let's talk about animals"), + ("news", "what_news"): exact_match("let's talk about news"), }, }, "fallback": {RESPONSE: Message("Oops")}, @@ -80,26 +80,26 @@ "animals": { "have_pets": { RESPONSE: Message("do you have pets?"), - TRANSITIONS: {"what_animal": exact_match(Message("yes"))}, + TRANSITIONS: {"what_animal": exact_match("yes")}, }, "like_animals": { RESPONSE: Message("do you like it?"), - TRANSITIONS: {"what_animal": exact_match(Message("yes"))}, + TRANSITIONS: {"what_animal": exact_match("yes")}, }, "what_animal": { RESPONSE: Message("what animals do you have?"), TRANSITIONS: { - "ask_about_color": exact_match(Message("bird")), - "ask_about_breed": exact_match(Message("dog")), + "ask_about_color": exact_match("bird"), + "ask_about_breed": exact_match("dog"), }, }, "ask_about_color": {RESPONSE: Message("what color is it")}, "ask_about_breed": { RESPONSE: Message("what is this breed?"), TRANSITIONS: { - "ask_about_breed": exact_match(Message("pereat")), - "tell_fact_about_breed": exact_match(Message("bulldog")), - "ask_about_training": exact_match(Message("I don't know")), + "ask_about_breed": exact_match("pereat"), + "tell_fact_about_breed": exact_match("bulldog"), + "ask_about_training": exact_match("I don't know"), }, }, "tell_fact_about_breed": { @@ -111,36 +111,36 @@ "what_news": { RESPONSE: Message("what kind of news do you prefer?"), TRANSITIONS: { - "ask_about_science": exact_match(Message("science")), - "ask_about_sport": exact_match(Message("sport")), + "ask_about_science": exact_match("science"), + "ask_about_sport": exact_match("sport"), }, }, "ask_about_science": { RESPONSE: Message("i got news about science, do you want to hear?"), TRANSITIONS: { - "science_news": exact_match(Message("yes")), - ("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")), + "science_news": exact_match("yes"), + ("small_talk", "ask_some_questions"): exact_match("let's change the topic"), }, }, "science_news": { RESPONSE: Message("This is science news"), TRANSITIONS: { - "what_news": exact_match(Message("ok")), - ("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")), + "what_news": exact_match("ok"), + ("small_talk", "ask_some_questions"): exact_match("let's change the topic"), }, }, "ask_about_sport": { RESPONSE: Message("i got news about sport, do you want to hear?"), TRANSITIONS: { - "sport_news": exact_match(Message("yes")), - ("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")), + "sport_news": exact_match("yes"), + ("small_talk", "ask_some_questions"): exact_match("let's change the topic"), }, }, "sport_news": { RESPONSE: Message("This is sport news"), TRANSITIONS: { - "what_news": exact_match(Message("ok")), - ("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")), + "what_news": exact_match("ok"), + ("small_talk", "ask_some_questions"): exact_match("let's change the topic"), }, }, }, @@ -148,16 +148,16 @@ "ask_some_questions": { RESPONSE: Message("how are you"), TRANSITIONS: { - "ask_talk_about": exact_match(Message("fine")), - ("animals", "like_animals"): exact_match(Message("let's talk about animals")), - ("news", "what_news"): exact_match(Message("let's talk about news")), + "ask_talk_about": exact_match("fine"), + ("animals", "like_animals"): exact_match("let's talk about animals"), + ("news", "what_news"): exact_match("let's talk about news"), }, }, "ask_talk_about": { RESPONSE: Message("what do you want to talk about"), TRANSITIONS: { - ("animals", "like_animals"): exact_match(Message("dog")), - ("news", "what_news"): exact_match(Message("let's talk about news")), + ("animals", "like_animals"): exact_match("dog"), + ("news", "what_news"): exact_match("let's talk about news"), }, }, }, diff --git a/docs/source/user_guides/basic_conceptions.rst b/docs/source/user_guides/basic_conceptions.rst index 515b4ff49..f1a819f6e 100644 --- a/docs/source/user_guides/basic_conceptions.rst +++ b/docs/source/user_guides/basic_conceptions.rst @@ -89,14 +89,14 @@ Example flow & script RESPONSE: Message(), # the response of the initial node is skipped TRANSITIONS: { ("greeting_flow", "greeting_node"): - cnd.exact_match(Message("/start")), + cnd.exact_match("/start"), }, }, "greeting_node": { RESPONSE: Message("Hi!"), TRANSITIONS: { ("ping_pong_flow", "game_start_node"): - cnd.exact_match(Message("Hello!")) + cnd.exact_match("Hello!") } }, "fallback_node": { @@ -111,14 +111,14 @@ Example flow & script RESPONSE: Message("Let's play ping-pong!"), TRANSITIONS: { ("ping_pong_flow", "response_node"): - cnd.exact_match(Message("Ping!")), + cnd.exact_match("Ping!"), }, }, "response_node": { RESPONSE: Message("Pong!"), TRANSITIONS: { ("ping_pong_flow", "response_node"): - cnd.exact_match(Message("Ping!")), + cnd.exact_match("Ping!"), }, }, }, @@ -289,9 +289,9 @@ conversational service. .. code-block:: python happy_path = ( - (Message("/start"), Message("Hi!")), - (Message("Hello!"), Message("Let's play ping-pong!")), - (Message("Ping!"), Message("Pong!")) + ("/start", "Hi!"), + ("Hello!", "Let's play ping-pong!"), + ("Ping!", "Pong!") ) A special function is then used to ascertain complete identity of the messages taken from @@ -384,4 +384,4 @@ Further reading * `Guide on Context <../user_guides/context_guide.html>`_ * `Tutorial on global transitions <../tutorials/tutorials.script.core.5_global_transitions.html>`_ * `Tutorial on context serialization <../tutorials/tutorials.script.core.6_context_serialization.html>`_ -* `Tutorial on script MISC <../tutorials/tutorials.script.core.8_misc.html>`_ \ No newline at end of file +* `Tutorial on script MISC <../tutorials/tutorials.script.core.8_misc.html>`_ diff --git a/tests/pipeline/test_messenger_interface.py b/tests/pipeline/test_messenger_interface.py index 855efeb96..c85384b09 100644 --- a/tests/pipeline/test_messenger_interface.py +++ b/tests/pipeline/test_messenger_interface.py @@ -13,19 +13,19 @@ RESPONSE: { "text": "", }, - TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))}, + TRANSITIONS: {"node1": cnd.exact_match("Ping")}, }, "node1": { RESPONSE: { "text": "Pong", }, - TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))}, + TRANSITIONS: {"node1": cnd.exact_match("Ping")}, }, "fallback_node": { RESPONSE: { "text": "Ooops", }, - TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))}, + TRANSITIONS: {"node1": cnd.exact_match("Ping")}, }, } } diff --git a/tests/script/conditions/test_conditions.py b/tests/script/conditions/test_conditions.py index ab85df16a..60035c728 100644 --- a/tests/script/conditions/test_conditions.py +++ b/tests/script/conditions/test_conditions.py @@ -14,12 +14,14 @@ def test_conditions(): failed_ctx.add_label(label) pipeline = Pipeline.from_script(script={"flow": {"node": {}}}, start_label=("flow", "node")) - assert cnd.exact_match(Message("text"))(ctx, pipeline) + assert cnd.exact_match("text")(ctx, pipeline) assert cnd.exact_match(Message("text", misc={}))(ctx, pipeline) assert not cnd.exact_match(Message("text", misc={1: 1}))(ctx, pipeline) - assert not cnd.exact_match(Message("text1"))(ctx, pipeline) + assert not cnd.exact_match("text1")(ctx, pipeline) assert cnd.exact_match(Message())(ctx, pipeline) assert not cnd.exact_match(Message(), skip_none=False)(ctx, pipeline) + assert cnd.exact_match("text")(ctx, pipeline) + assert not cnd.exact_match("text1")(ctx, pipeline) assert cnd.has_text("text")(ctx, pipeline) assert cnd.has_text("te")(ctx, pipeline) @@ -30,17 +32,17 @@ def test_conditions(): assert not cnd.regexp("t.*t1")(ctx, pipeline) assert not cnd.regexp("t.*t1")(failed_ctx, pipeline) - assert cnd.agg([cnd.regexp("t.*t"), cnd.exact_match(Message("text"))], aggregate_func=all)(ctx, pipeline) - assert not cnd.agg([cnd.regexp("t.*t1"), cnd.exact_match(Message("text"))], aggregate_func=all)(ctx, pipeline) + assert cnd.agg([cnd.regexp("t.*t"), cnd.exact_match("text")], aggregate_func=all)(ctx, pipeline) + assert not cnd.agg([cnd.regexp("t.*t1"), cnd.exact_match("text")], aggregate_func=all)(ctx, pipeline) - assert cnd.any([cnd.regexp("t.*t1"), cnd.exact_match(Message("text"))])(ctx, pipeline) - assert not cnd.any([cnd.regexp("t.*t1"), cnd.exact_match(Message("text1"))])(ctx, pipeline) + assert cnd.any([cnd.regexp("t.*t1"), cnd.exact_match("text")])(ctx, pipeline) + assert not cnd.any([cnd.regexp("t.*t1"), cnd.exact_match("text1")])(ctx, pipeline) - assert cnd.all([cnd.regexp("t.*t"), cnd.exact_match(Message("text"))])(ctx, pipeline) - assert not cnd.all([cnd.regexp("t.*t1"), cnd.exact_match(Message("text"))])(ctx, pipeline) + assert cnd.all([cnd.regexp("t.*t"), cnd.exact_match("text")])(ctx, pipeline) + assert not cnd.all([cnd.regexp("t.*t1"), cnd.exact_match("text")])(ctx, pipeline) - assert cnd.neg(cnd.exact_match(Message("text1")))(ctx, pipeline) - assert not cnd.neg(cnd.exact_match(Message("text")))(ctx, pipeline) + assert cnd.neg(cnd.exact_match("text1"))(ctx, pipeline) + assert not cnd.neg(cnd.exact_match("text"))(ctx, pipeline) assert cnd.has_last_labels(flow_labels=["flow"])(ctx, pipeline) assert not cnd.has_last_labels(flow_labels=["flow1"])(ctx, pipeline) diff --git a/tests/tutorials/test_utils.py b/tests/tutorials/test_utils.py index 2ed14aa6b..55fd5aae4 100644 --- a/tests/tutorials/test_utils.py +++ b/tests/tutorials/test_utils.py @@ -2,14 +2,13 @@ import re import pytest -from dff.script import Message from dff.utils.testing.common import check_happy_path, is_interactive_mode from tests.pipeline.test_messenger_interface import pipeline def test_unhappy_path(): with pytest.raises(Exception) as e: - check_happy_path(pipeline, ((Message("Hi"), Message("false_response")),)) + check_happy_path(pipeline, (("Hi", "false_response"),)) assert e msg = str(e) assert msg diff --git a/tutorials/messengers/telegram/1_basic.py b/tutorials/messengers/telegram/1_basic.py index 5f633c500..7d804eafe 100644 --- a/tutorials/messengers/telegram/1_basic.py +++ b/tutorials/messengers/telegram/1_basic.py @@ -44,7 +44,7 @@ class and [telebot](https://pytba.readthedocs.io/en/latest/index.html) script = { "greeting_flow": { "start_node": { - TRANSITIONS: {"greeting_node": cnd.exact_match(Message("/start"))}, + TRANSITIONS: {"greeting_node": cnd.exact_match("/start")}, }, "greeting_node": { RESPONSE: Message("Hi"), @@ -52,7 +52,7 @@ class and [telebot](https://pytba.readthedocs.io/en/latest/index.html) }, "fallback_node": { RESPONSE: Message("Please, repeat the request"), - TRANSITIONS: {"greeting_node": cnd.exact_match(Message("/start"))}, + TRANSITIONS: {"greeting_node": cnd.exact_match("/start")}, }, } } diff --git a/tutorials/script/core/1_basics.py b/tutorials/script/core/1_basics.py index b53c3b352..dc9314686 100644 --- a/tutorials/script/core/1_basics.py +++ b/tutorials/script/core/1_basics.py @@ -57,7 +57,7 @@ "start_node": { # This is the initial node, # it doesn't contain a `RESPONSE`. RESPONSE: Message(), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, # If "Hi" == request of the user then we make the transition. }, "node1": { @@ -65,29 +65,25 @@ text="Hi, how are you?" ), # When the agent enters node1, # return "Hi, how are you?". - TRANSITIONS: { - "node2": cnd.exact_match(Message("I'm fine, how are you?")) - }, + TRANSITIONS: {"node2": cnd.exact_match("I'm fine, how are you?")}, }, "node2": { RESPONSE: Message("Good. What do you want to talk about?"), - TRANSITIONS: { - "node3": cnd.exact_match(Message("Let's talk about music.")) - }, + TRANSITIONS: {"node3": cnd.exact_match("Let's talk about music.")}, }, "node3": { RESPONSE: Message("Sorry, I can not talk about music now."), - TRANSITIONS: {"node4": cnd.exact_match(Message("Ok, goodbye."))}, + TRANSITIONS: {"node4": cnd.exact_match("Ok, goodbye.")}, }, "node4": { RESPONSE: Message("Bye"), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, "fallback_node": { # We get to this node if the conditions # for switching to other nodes are not performed. RESPONSE: Message("Ooops"), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, } } @@ -95,37 +91,37 @@ happy_path = ( ( - Message("Hi"), - Message("Hi, how are you?"), + "Hi", + "Hi, how are you?", ), # start_node -> node1 ( - Message("I'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "I'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("Bye")), # node3 -> node4 - (Message("Hi"), Message("Hi, how are you?")), # node4 -> node1 - (Message("stop"), Message("Ooops")), # node1 -> fallback_node + ("Ok, goodbye.", "Bye"), # node3 -> node4 + ("Hi", "Hi, how are you?"), # node4 -> node1 + ("stop", "Ooops"), # node1 -> fallback_node ( - Message("stop"), - Message("Ooops"), + "stop", + "Ooops", ), # fallback_node -> fallback_node ( - Message("Hi"), - Message("Hi, how are you?"), + "Hi", + "Hi, how are you?", ), # fallback_node -> node1 ( - Message("I'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "I'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("Bye")), # node3 -> node4 + ("Ok, goodbye.", "Bye"), # node3 -> node4 ) diff --git a/tutorials/script/core/2_conditions.py b/tutorials/script/core/2_conditions.py index 95e96c14f..d4ecc0bcd 100644 --- a/tutorials/script/core/2_conditions.py +++ b/tutorials/script/core/2_conditions.py @@ -107,7 +107,7 @@ def internal_condition_function(ctx: Context, _: Pipeline) -> bool: "start_node": { # This is the initial node, # it doesn't contain a `RESPONSE`. RESPONSE: Message(), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, # If "Hi" == request of user then we make the transition }, "node1": { @@ -137,7 +137,7 @@ def internal_condition_function(ctx: Context, _: Pipeline) -> bool: "node1": cnd.any( [ hi_lower_case_condition, - cnd.exact_match(Message("hello")), + cnd.exact_match("hello"), ] ) }, @@ -170,45 +170,45 @@ def internal_condition_function(ctx: Context, _: Pipeline) -> bool: # testing happy_path = ( ( - Message("Hi"), - Message("Hi, how are you?"), + "Hi", + "Hi, how are you?", ), # start_node -> node1 ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("bye")), # node3 -> node4 - (Message("Hi"), Message("Hi, how are you?")), # node4 -> node1 - (Message("stop"), Message("Ooops")), # node1 -> fallback_node + ("Ok, goodbye.", "bye"), # node3 -> node4 + ("Hi", "Hi, how are you?"), # node4 -> node1 + ("stop", "Ooops"), # node1 -> fallback_node ( - Message("one"), - Message("Ooops"), + "one", + "Ooops", ), # fallback_node -> fallback_node ( - Message("help"), - Message("Ooops"), + "help", + "Ooops", ), # fallback_node -> fallback_node ( - Message("nope"), - Message("Ooops"), + "nope", + "Ooops", ), # fallback_node -> fallback_node ( Message(misc={"some_key": "some_value"}), - Message("Hi, how are you?"), + "Hi, how are you?", ), # fallback_node -> node1 ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("bye")), # node3 -> node4 + ("Ok, goodbye.", "bye"), # node3 -> node4 ) # %% diff --git a/tutorials/script/core/3_responses.py b/tutorials/script/core/3_responses.py index 0eef02ecc..f6e879e5e 100644 --- a/tutorials/script/core/3_responses.py +++ b/tutorials/script/core/3_responses.py @@ -84,7 +84,7 @@ def fallback_trace_response(ctx: Context, _: Pipeline) -> Message: "start_node": { # This is an initial node, # it doesn't need a `RESPONSE`. RESPONSE: Message(), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, # If "Hi" == request of user then we make the transition }, "node1": { @@ -95,28 +95,24 @@ def fallback_trace_response(ctx: Context, _: Pipeline) -> Message: ] ), # Random choice from candidate list. - TRANSITIONS: { - "node2": cnd.exact_match(Message("I'm fine, how are you?")) - }, + TRANSITIONS: {"node2": cnd.exact_match("I'm fine, how are you?")}, }, "node2": { RESPONSE: Message("Good. What do you want to talk about?"), - TRANSITIONS: { - "node3": cnd.exact_match(Message("Let's talk about music.")) - }, + TRANSITIONS: {"node3": cnd.exact_match("Let's talk about music.")}, }, "node3": { RESPONSE: cannot_talk_about_topic_response, - TRANSITIONS: {"node4": cnd.exact_match(Message("Ok, goodbye."))}, + TRANSITIONS: {"node4": cnd.exact_match("Ok, goodbye.")}, }, "node4": { RESPONSE: upper_case_response(Message("bye")), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, "fallback_node": { # We get to this node # if an error occurred while the agent was running. RESPONSE: fallback_trace_response, - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, } } @@ -124,19 +120,19 @@ def fallback_trace_response(ctx: Context, _: Pipeline) -> Message: # testing happy_path = ( ( - Message("Hi"), - Message("Hello, how are you?"), + "Hi", + "Hello, how are you?", ), # start_node -> node1 ( - Message("I'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "I'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("BYE")), # node3 -> node4 - (Message("Hi"), Message("Hello, how are you?")), # node4 -> node1 + ("Ok, goodbye.", "BYE"), # node3 -> node4 + ("Hi", "Hello, how are you?"), # node4 -> node1 ( Message("stop"), Message( @@ -175,18 +171,18 @@ def fallback_trace_response(ctx: Context, _: Pipeline) -> Message: ), ), # f_n->f_n ( - Message("Hi"), - Message("Hi, what is up?"), + "Hi", + "Hi, what is up?", ), # fallback_node -> node1 ( - Message("I'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "I'm fine, how are you?", + "Good. What do you want to talk about?", ), # node1 -> node2 ( - Message("Let's talk about music."), - Message("Sorry, I can not talk about music now."), + "Let's talk about music.", + "Sorry, I can not talk about music now.", ), # node2 -> node3 - (Message("Ok, goodbye."), Message("BYE")), # node3 -> node4 + ("Ok, goodbye.", "BYE"), # node3 -> node4 ) # %% diff --git a/tutorials/script/core/4_transitions.py b/tutorials/script/core/4_transitions.py index 8acf16143..84805c93f 100644 --- a/tutorials/script/core/4_transitions.py +++ b/tutorials/script/core/4_transitions.py @@ -235,76 +235,63 @@ def transition(_: Context, __: Pipeline) -> ConstLabel: # testing happy_path = ( - (Message("hi"), Message("Hi, how are you?")), + ("hi", "Hi, how are you?"), ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), ( - Message("talk about music."), - Message( - text="I love `System of a Down` group, " - "would you like to talk about it?" - ), + "talk about music.", + "I love `System of a Down` group, " "would you like to talk about it?", ), ( - Message("yes"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "yes", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("next"), - Message( - text="The band achieved commercial success " - "with the release of five studio albums." - ), + "next", + "The band achieved commercial success " + "with the release of five studio albums.", ), ( - Message("back"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "back", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("repeat"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "repeat", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("next"), - Message( - text="The band achieved commercial success " - "with the release of five studio albums." - ), + "next", + "The band achieved commercial success " + "with the release of five studio albums.", ), - (Message("next"), Message("That's all what I know.")), + ("next", "That's all what I know."), ( - Message("next"), - Message("Good. What do you want to talk about?"), + "next", + "Good. What do you want to talk about?", ), - (Message("previous"), Message("That's all what I know.")), - (Message("next time"), Message("Bye")), - (Message("stop"), Message("Ooops")), - (Message("previous"), Message("Bye")), - (Message("stop"), Message("Ooops")), - (Message("nope"), Message("Ooops")), - (Message("hi"), Message("Hi, how are you?")), - (Message("stop"), Message("Ooops")), - (Message("previous"), Message("Hi, how are you?")), + ("previous", "That's all what I know."), + ("next time", "Bye"), + ("stop", "Ooops"), + ("previous", "Bye"), + ("stop", "Ooops"), + ("nope", "Ooops"), + ("hi", "Hi, how are you?"), + ("stop", "Ooops"), + ("previous", "Hi, how are you?"), ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), ( - Message("let's talk about something."), - Message("Sorry, I can not talk about that now."), + "let's talk about something.", + "Sorry, I can not talk about that now.", ), - (Message("Ok, goodbye."), Message("Bye")), + ("Ok, goodbye.", "Bye"), ) # %% diff --git a/tutorials/script/core/5_global_transitions.py b/tutorials/script/core/5_global_transitions.py index 474bf068f..563fb5c59 100644 --- a/tutorials/script/core/5_global_transitions.py +++ b/tutorials/script/core/5_global_transitions.py @@ -136,76 +136,63 @@ # testing happy_path = ( - (Message("hi"), Message("Hi, how are you?")), + ("hi", "Hi, how are you?"), ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), ( - Message("talk about music."), - Message( - text="I love `System of a Down` group, " - "would you like to talk about it?" - ), + "talk about music.", + "I love `System of a Down` group, " "would you like to talk about it?", ), ( - Message("yes"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "yes", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("next"), - Message( - text="The band achieved commercial success " - "with the release of five studio albums." - ), + "next", + "The band achieved commercial success " + "with the release of five studio albums.", ), ( - Message("back"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "back", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("repeat"), - Message( - text="System of a Down is " - "an Armenian-American heavy metal band formed in 1994." - ), + "repeat", + "System of a Down is " + "an Armenian-American heavy metal band formed in 1994.", ), ( - Message("next"), - Message( - text="The band achieved commercial success " - "with the release of five studio albums." - ), + "next", + "The band achieved commercial success " + "with the release of five studio albums.", ), - (Message("next"), Message("That's all what I know.")), + ("next", "That's all what I know."), ( - Message("next"), - Message("Good. What do you want to talk about?"), + "next", + "Good. What do you want to talk about?", ), - (Message("previous"), Message("That's all what I know.")), - (Message("next time"), Message("bye")), - (Message("stop"), Message("Ooops")), - (Message("previous"), Message("bye")), - (Message("stop"), Message("Ooops")), - (Message("nope"), Message("Ooops")), - (Message("hi"), Message("Hi, how are you?")), - (Message("stop"), Message("Ooops")), - (Message("previous"), Message("Hi, how are you?")), + ("previous", "That's all what I know."), + ("next time", "bye"), + ("stop", "Ooops"), + ("previous", "bye"), + ("stop", "Ooops"), + ("nope", "Ooops"), + ("hi", "Hi, how are you?"), + ("stop", "Ooops"), + ("previous", "Hi, how are you?"), ( - Message("i'm fine, how are you?"), - Message("Good. What do you want to talk about?"), + "i'm fine, how are you?", + "Good. What do you want to talk about?", ), ( - Message("let's talk about something."), - Message("Sorry, I can not talk about that now."), + "let's talk about something.", + "Sorry, I can not talk about that now.", ), - (Message("Ok, goodbye."), Message("bye")), + ("Ok, goodbye.", "bye"), ) # %% diff --git a/tutorials/script/core/6_context_serialization.py b/tutorials/script/core/6_context_serialization.py index 1af38c619..51d9749d8 100644 --- a/tutorials/script/core/6_context_serialization.py +++ b/tutorials/script/core/6_context_serialization.py @@ -46,10 +46,10 @@ def response_handler(ctx: Context, _: Pipeline) -> Message: # testing happy_path = ( - (Message("hi"), Message("answer 1")), - (Message("how are you?"), Message("answer 2")), - (Message("ok"), Message("answer 3")), - (Message("good"), Message("answer 4")), + ("hi", "answer 1"), + ("how are you?", "answer 2"), + ("ok", "answer 3"), + ("good", "answer 4"), ) # %% [markdown] diff --git a/tutorials/script/core/7_pre_response_processing.py b/tutorials/script/core/7_pre_response_processing.py index 9697b8675..5d7f5e584 100644 --- a/tutorials/script/core/7_pre_response_processing.py +++ b/tutorials/script/core/7_pre_response_processing.py @@ -107,15 +107,12 @@ def add_prefix_processing(ctx: Context, _: Pipeline): # testing happy_path = ( - (Message(), Message("l3_local: l2_local: l1_global: first")), - (Message(), Message("l3_local: l2_local: l1_step_1: second")), - (Message(), Message("l3_local: l2_step_2: l1_global: third")), - (Message(), Message("l3_step_3: l2_local: l1_global: fourth")), - ( - Message(), - Message("l4_step_4: l3_local: l2_local: l1_global: fifth"), - ), - (Message(), Message("l3_local: l2_local: l1_global: first")), + (Message(), "l3_local: l2_local: l1_global: first"), + (Message(), "l3_local: l2_local: l1_step_1: second"), + (Message(), "l3_local: l2_step_2: l1_global: third"), + (Message(), "l3_step_3: l2_local: l1_global: fourth"), + (Message(), "l4_step_4: l3_local: l2_local: l1_global: fifth"), + (Message(), "l3_local: l2_local: l1_global: first"), ) diff --git a/tutorials/script/core/8_misc.py b/tutorials/script/core/8_misc.py index 6379fede4..c42da6baa 100644 --- a/tutorials/script/core/8_misc.py +++ b/tutorials/script/core/8_misc.py @@ -98,57 +98,45 @@ def custom_response(ctx: Context, _: Pipeline) -> Message: happy_path = ( ( Message(), - Message( - text="ctx.last_label=('flow', 'step_0'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_0'}" - ), + "ctx.last_label=('flow', 'step_0'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_0'}", ), ( Message(), - Message( - text="ctx.last_label=('flow', 'step_1'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_1'}" - ), + "ctx.last_label=('flow', 'step_1'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_1'}", ), ( Message(), - Message( - text="ctx.last_label=('flow', 'step_2'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_2'}" - ), + "ctx.last_label=('flow', 'step_2'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_2'}", ), ( Message(), - Message( - text="ctx.last_label=('flow', 'step_3'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_3'}" - ), + "ctx.last_label=('flow', 'step_3'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_3'}", ), ( Message(), - Message( - text="ctx.last_label=('flow', 'step_4'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_4'}" - ), + "ctx.last_label=('flow', 'step_4'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_4'}", ), ( Message(), - Message( - text="ctx.last_label=('flow', 'step_0'): current_node.misc=" - "{'var1': 'global_data', " - "'var2': 'rewrite_by_local', " - "'var3': 'info_of_step_0'}" - ), + "ctx.last_label=('flow', 'step_0'): current_node.misc=" + "{'var1': 'global_data', " + "'var2': 'rewrite_by_local', " + "'var3': 'info_of_step_0'}", ), ) diff --git a/tutorials/script/core/9_pre_transitions_processing.py b/tutorials/script/core/9_pre_transitions_processing.py index 9f2eddc01..c0f6e1f24 100644 --- a/tutorials/script/core/9_pre_transitions_processing.py +++ b/tutorials/script/core/9_pre_transitions_processing.py @@ -78,11 +78,11 @@ def prepend_previous_node_response(ctx: Context, _: Pipeline): # testing happy_path = ( - (Message("1"), Message("previous=None: current=first")), - (Message("2"), Message("previous=first: current=second")), - (Message("3"), Message("previous=second: current=third")), - (Message("4"), Message("previous=third: current=fourth")), - (Message("5"), Message("previous=fourth: current=fifth")), + ("1", "previous=None: current=first"), + ("2", "previous=first: current=second"), + ("3", "previous=second: current=third"), + ("4", "previous=third: current=fourth"), + ("5", "previous=fourth: current=fifth"), ) diff --git a/tutorials/script/responses/1_basics.py b/tutorials/script/responses/1_basics.py index 2b1332d1d..debf55bec 100644 --- a/tutorials/script/responses/1_basics.py +++ b/tutorials/script/responses/1_basics.py @@ -29,31 +29,27 @@ "greeting_flow": { "start_node": { RESPONSE: Message(""), - TRANSITIONS: {"node1": exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": exact_match("Hi")}, }, "node1": { RESPONSE: Message("Hi, how are you?"), - TRANSITIONS: { - "node2": exact_match(Message("i'm fine, how are you?")) - }, + TRANSITIONS: {"node2": exact_match("i'm fine, how are you?")}, }, "node2": { RESPONSE: Message("Good. What do you want to talk about?"), - TRANSITIONS: { - "node3": exact_match(Message("Let's talk about music.")) - }, + TRANSITIONS: {"node3": exact_match("Let's talk about music.")}, }, "node3": { RESPONSE: Message("Sorry, I can not talk about music now."), - TRANSITIONS: {"node4": exact_match(Message("Ok, goodbye."))}, + TRANSITIONS: {"node4": exact_match("Ok, goodbye.")}, }, "node4": { RESPONSE: Message("bye"), - TRANSITIONS: {"node1": exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": exact_match("Hi")}, }, "fallback_node": { RESPONSE: Message("Ooops"), - TRANSITIONS: {"node1": exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": exact_match("Hi")}, }, } } diff --git a/tutorials/script/responses/4_multi_message.py b/tutorials/script/responses/4_multi_message.py index 323ccffec..bfed3313b 100644 --- a/tutorials/script/responses/4_multi_message.py +++ b/tutorials/script/responses/4_multi_message.py @@ -27,7 +27,7 @@ toy_script = { "greeting_flow": { "start_node": { # This is an initial node, - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, # If "Hi" == request of user then we make the transition }, "node1": { @@ -42,28 +42,24 @@ ] } ), - TRANSITIONS: { - "node2": cnd.exact_match(Message("I'm fine, how are you?")) - }, + TRANSITIONS: {"node2": cnd.exact_match("I'm fine, how are you?")}, }, "node2": { RESPONSE: Message("Good. What do you want to talk about?"), - TRANSITIONS: { - "node3": cnd.exact_match(Message("Let's talk about music.")) - }, + TRANSITIONS: {"node3": cnd.exact_match("Let's talk about music.")}, }, "node3": { RESPONSE: Message("Sorry, I can not talk about that now."), - TRANSITIONS: {"node4": cnd.exact_match(Message("Ok, goodbye."))}, + TRANSITIONS: {"node4": cnd.exact_match("Ok, goodbye.")}, }, "node4": { RESPONSE: Message("bye"), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, "fallback_node": { # We get to this node # if an error occurred while the agent was running. RESPONSE: Message("Ooops"), - TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))}, + TRANSITIONS: {"node1": cnd.exact_match("Hi")}, }, } } diff --git a/tutorials/utils/1_cache.py b/tutorials/utils/1_cache.py index bf1142a9f..dca14435a 100644 --- a/tutorials/utils/1_cache.py +++ b/tutorials/utils/1_cache.py @@ -60,9 +60,9 @@ def response(_: Context, __: Pipeline) -> Message: } happy_path = ( - (Message(), Message("1-2-1-2")), - (Message(), Message("3-4-3-4")), - (Message(), Message("5-6-5-6")), + (Message(), "1-2-1-2"), + (Message(), "3-4-3-4"), + (Message(), "5-6-5-6"), ) pipeline = Pipeline.from_script(toy_script, start_label=("flow", "node1")) diff --git a/tutorials/utils/2_lru_cache.py b/tutorials/utils/2_lru_cache.py index faca106d7..f764789e5 100644 --- a/tutorials/utils/2_lru_cache.py +++ b/tutorials/utils/2_lru_cache.py @@ -59,9 +59,9 @@ def response(_: Context, __: Pipeline) -> Message: } happy_path = ( - (Message(), Message("1-2-3-2-4")), - (Message(), Message("5-6-7-6-8")), - (Message(), Message("9-10-11-10-12")), + (Message(), "1-2-3-2-4"), + (Message(), "5-6-7-6-8"), + (Message(), "9-10-11-10-12"), ) pipeline = Pipeline.from_script(toy_script, start_label=("flow", "node1"))