Skip to content

Commit

Permalink
Feat/positional text (#327)
Browse files Browse the repository at this point in the history
* feat: Support Message creation with positional text arg

* chore: Replace kw-text with positional one in Message instantiations
  • Loading branch information
Ramimashkouk authored Feb 9, 2024
1 parent bd3d6ad commit 6d982e0
Show file tree
Hide file tree
Showing 39 changed files with 438 additions and 426 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ import dff.script.conditions.std_conditions as cnd
script = {
GLOBAL: {
TRANSITIONS: {
("flow", "node_hi"): cnd.exact_match(Message(text="Hi")),
("flow", "node_hi"): cnd.exact_match(Message("Hi")),
("flow", "node_ok"): cnd.true()
}
},
"flow": {
"node_hi": {RESPONSE: Message(text="Hi!")},
"node_ok": {RESPONSE: Message(text="OK")},
"node_hi": {RESPONSE: Message("Hi!")},
"node_ok": {RESPONSE: Message("OK")},
},
}

Expand All @@ -112,7 +112,7 @@ def turn_handler(in_request: Message, pipeline: Pipeline) -> Message:

while True:
in_request = input("Your message: ")
out_response = turn_handler(Message(text=in_request), pipeline)
out_response = turn_handler(Message(in_request), pipeline)
print("Response: ", out_response.text)
```

Expand Down
2 changes: 1 addition & 1 deletion dff/messengers/common/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __init__(
self._descriptor: Optional[TextIO] = out_descriptor

def _request(self) -> List[Tuple[Message, Any]]:
return [(Message(text=input(self._prompt_request)), self._ctx_id)]
return [(Message(input(self._prompt_request)), self._ctx_id)]

def _respond(self, responses: List[Context]):
print(f"{self._prompt_response}{responses[0].last_response.text}", file=self._descriptor)
Expand Down
2 changes: 1 addition & 1 deletion dff/pipeline/pipeline/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def validate_script(self, pipeline: Pipeline, verbose: bool = True):
for flow_label, node_label, label, condition in zip(flow_labels, node_labels, labels, conditions):
ctx = Context()
ctx.validation = True
ctx.add_request(Message(text="text"))
ctx.add_request(Message("text"))

label = label(ctx, pipeline) if callable(label) else normalize_label(label, flow_label)

Expand Down
12 changes: 12 additions & 0 deletions dff/script/core/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ class level variables to store message information.
# state: Optional[Session] = Session.ACTIVE
# ui: Optional[Union[Keyboard, DataModel]] = None

def __init__(
self,
text: Optional[str] = None,
commands: Optional[List[Command]] = None,
attachments: Optional[Attachments] = None,
annotations: Optional[dict] = None,
misc: Optional[dict] = None,
):
super().__init__(
text=text, commands=commands, attachments=attachments, annotations=annotations, misc=misc
)

def __eq__(self, other):
if isinstance(other, Message):
for field in self.model_fields:
Expand Down
2 changes: 1 addition & 1 deletion dff/utils/testing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ def run_interactive_mode(pipeline: Pipeline): # pragma: no cover
print("Start a dialogue with the bot")
while True:
request = input(">>> ")
ctx = pipeline(request=Message(text=request), ctx_id=ctx_id)
ctx = pipeline(request=Message(request), ctx_id=ctx_id)
print(f"<<< {repr(ctx.last_response)}")
114 changes: 57 additions & 57 deletions dff/utils/testing/toy_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@
"greeting_flow": {
"start_node": {
RESPONSE: Message(),
TRANSITIONS: {"node1": exact_match(Message(text="Hi"))},
TRANSITIONS: {"node1": exact_match(Message("Hi"))},
},
"node1": {
RESPONSE: Message(text="Hi, how are you?"),
TRANSITIONS: {"node2": exact_match(Message(text="i'm fine, how are you?"))},
RESPONSE: Message("Hi, how are you?"),
TRANSITIONS: {"node2": exact_match(Message("i'm fine, how are you?"))},
},
"node2": {
RESPONSE: Message(text="Good. What do you want to talk about?"),
TRANSITIONS: {"node3": exact_match(Message(text="Let's talk about music."))},
RESPONSE: Message("Good. What do you want to talk about?"),
TRANSITIONS: {"node3": exact_match(Message("Let's talk about music."))},
},
"node3": {
RESPONSE: Message(text="Sorry, I can not talk about music now."),
TRANSITIONS: {"node4": exact_match(Message(text="Ok, goodbye."))},
RESPONSE: Message("Sorry, I can not talk about music now."),
TRANSITIONS: {"node4": exact_match(Message("Ok, goodbye."))},
},
"node4": {RESPONSE: Message(text="bye"), TRANSITIONS: {"node1": exact_match(Message(text="Hi"))}},
"node4": {RESPONSE: Message("bye"), TRANSITIONS: {"node1": exact_match(Message("Hi"))}},
"fallback_node": {
RESPONSE: Message(text="Ooops"),
TRANSITIONS: {"node1": exact_match(Message(text="Hi"))},
RESPONSE: Message("Ooops"),
TRANSITIONS: {"node1": exact_match(Message("Hi"))},
},
}
}
Expand All @@ -51,11 +51,11 @@
"""

HAPPY_PATH = (
(Message(text="Hi"), Message(text="Hi, how are you?")),
(Message(text="i'm fine, how are you?"), Message(text="Good. What do you want to talk about?")),
(Message(text="Let's talk about music."), Message(text="Sorry, I can not talk about music now.")),
(Message(text="Ok, goodbye."), Message(text="bye")),
(Message(text="Hi"), Message(text="Hi, how are you?")),
(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?")),
)
"""
An example of a simple dialog.
Expand All @@ -66,97 +66,97 @@
MULTIFLOW_SCRIPT = {
"root": {
"start": {
RESPONSE: Message(text="Hi"),
RESPONSE: Message("Hi"),
TRANSITIONS: {
("small_talk", "ask_some_questions"): exact_match(Message(text="hi")),
("animals", "have_pets"): exact_match(Message(text="i like animals")),
("animals", "like_animals"): exact_match(Message(text="let's talk about animals")),
("news", "what_news"): exact_match(Message(text="let's talk about news")),
("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")),
},
},
"fallback": {RESPONSE: Message(text="Oops")},
"fallback": {RESPONSE: Message("Oops")},
},
"animals": {
"have_pets": {
RESPONSE: Message(text="do you have pets?"),
TRANSITIONS: {"what_animal": exact_match(Message(text="yes"))},
RESPONSE: Message("do you have pets?"),
TRANSITIONS: {"what_animal": exact_match(Message("yes"))},
},
"like_animals": {
RESPONSE: Message(text="do you like it?"),
TRANSITIONS: {"what_animal": exact_match(Message(text="yes"))},
RESPONSE: Message("do you like it?"),
TRANSITIONS: {"what_animal": exact_match(Message("yes"))},
},
"what_animal": {
RESPONSE: Message(text="what animals do you have?"),
RESPONSE: Message("what animals do you have?"),
TRANSITIONS: {
"ask_about_color": exact_match(Message(text="bird")),
"ask_about_breed": exact_match(Message(text="dog")),
"ask_about_color": exact_match(Message("bird")),
"ask_about_breed": exact_match(Message("dog")),
},
},
"ask_about_color": {RESPONSE: Message(text="what color is it")},
"ask_about_color": {RESPONSE: Message("what color is it")},
"ask_about_breed": {
RESPONSE: Message(text="what is this breed?"),
RESPONSE: Message("what is this breed?"),
TRANSITIONS: {
"ask_about_breed": exact_match(Message(text="pereat")),
"tell_fact_about_breed": exact_match(Message(text="bulldog")),
"ask_about_training": exact_match(Message(text="I don't know")),
"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")),
},
},
"tell_fact_about_breed": {
RESPONSE: Message(text="Bulldogs appeared in England as specialized bull-baiting dogs. "),
RESPONSE: Message("Bulldogs appeared in England as specialized bull-baiting dogs. "),
},
"ask_about_training": {RESPONSE: Message(text="Do you train your dog? ")},
"ask_about_training": {RESPONSE: Message("Do you train your dog? ")},
},
"news": {
"what_news": {
RESPONSE: Message(text="what kind of news do you prefer?"),
RESPONSE: Message("what kind of news do you prefer?"),
TRANSITIONS: {
"ask_about_science": exact_match(Message(text="science")),
"ask_about_sport": exact_match(Message(text="sport")),
"ask_about_science": exact_match(Message("science")),
"ask_about_sport": exact_match(Message("sport")),
},
},
"ask_about_science": {
RESPONSE: Message(text="i got news about science, do you want to hear?"),
RESPONSE: Message("i got news about science, do you want to hear?"),
TRANSITIONS: {
"science_news": exact_match(Message(text="yes")),
("small_talk", "ask_some_questions"): exact_match(Message(text="let's change the topic")),
"science_news": exact_match(Message("yes")),
("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")),
},
},
"science_news": {
RESPONSE: Message(text="This is science news"),
RESPONSE: Message("This is science news"),
TRANSITIONS: {
"what_news": exact_match(Message(text="ok")),
("small_talk", "ask_some_questions"): exact_match(Message(text="let's change the topic")),
"what_news": exact_match(Message("ok")),
("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")),
},
},
"ask_about_sport": {
RESPONSE: Message(text="i got news about sport, do you want to hear?"),
RESPONSE: Message("i got news about sport, do you want to hear?"),
TRANSITIONS: {
"sport_news": exact_match(Message(text="yes")),
("small_talk", "ask_some_questions"): exact_match(Message(text="let's change the topic")),
"sport_news": exact_match(Message("yes")),
("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")),
},
},
"sport_news": {
RESPONSE: Message(text="This is sport news"),
RESPONSE: Message("This is sport news"),
TRANSITIONS: {
"what_news": exact_match(Message(text="ok")),
("small_talk", "ask_some_questions"): exact_match(Message(text="let's change the topic")),
"what_news": exact_match(Message("ok")),
("small_talk", "ask_some_questions"): exact_match(Message("let's change the topic")),
},
},
},
"small_talk": {
"ask_some_questions": {
RESPONSE: Message(text="how are you"),
RESPONSE: Message("how are you"),
TRANSITIONS: {
"ask_talk_about": exact_match(Message(text="fine")),
("animals", "like_animals"): exact_match(Message(text="let's talk about animals")),
("news", "what_news"): exact_match(Message(text="let's talk about news")),
"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": {
RESPONSE: Message(text="what do you want to talk about"),
RESPONSE: Message("what do you want to talk about"),
TRANSITIONS: {
("animals", "like_animals"): exact_match(Message(text="dog")),
("news", "what_news"): exact_match(Message(text="let's talk about news")),
("animals", "like_animals"): exact_match(Message("dog")),
("news", "what_news"): exact_match(Message("let's talk about news")),
},
},
},
Expand Down
26 changes: 13 additions & 13 deletions docs/source/user_guides/basic_conceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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(text="/start")),
cnd.exact_match(Message("/start")),
},
},
"greeting_node": {
RESPONSE: Message(text="Hi!"),
RESPONSE: Message("Hi!"),
TRANSITIONS: {
("ping_pong_flow", "game_start_node"):
cnd.exact_match(Message(text="Hello!"))
cnd.exact_match(Message("Hello!"))
}
},
"fallback_node": {
Expand All @@ -108,17 +108,17 @@ Example flow & script
},
"ping_pong_flow": {
"game_start_node": {
RESPONSE: Message(text="Let's play ping-pong!"),
RESPONSE: Message("Let's play ping-pong!"),
TRANSITIONS: {
("ping_pong_flow", "response_node"):
cnd.exact_match(Message(text="Ping!")),
cnd.exact_match(Message("Ping!")),
},
},
"response_node": {
RESPONSE: Message(text="Pong!"),
RESPONSE: Message("Pong!"),
TRANSITIONS: {
("ping_pong_flow", "response_node"):
cnd.exact_match(Message(text="Ping!")),
cnd.exact_match(Message("Ping!")),
},
},
},
Expand Down Expand Up @@ -240,8 +240,8 @@ The latter allows you to customize the response based on the specific scenario a
def sample_response(ctx: Context, _: Pipeline) -> Message:
if ctx.misc["user"] == 'vegan':
return Message(text="Here is a list of vegan cafes.")
return Message(text="Here is a list of cafes.")
return Message("Here is a list of vegan cafes.")
return Message("Here is a list of cafes.")
Handling Fallbacks
==================
Expand All @@ -265,7 +265,7 @@ This ensures a smoother user experience even when the bot encounters unexpected
if ctx.last_request is not None:
if ctx.last_request.text != "/start" and ctx.last_label is None:
# an empty last_label indicates start_node
return Message(text="You should've started the dialog with '/start'")
return Message("You should've started the dialog with '/start'")
else:
return Message(
text=f"That was against the rules!\n"
Expand All @@ -289,9 +289,9 @@ conversational service.
.. code-block:: python
happy_path = (
(Message(text="/start"), Message(text="Hi!")),
(Message(text="Hello!"), Message(text="Let's play ping-pong!")),
(Message(text="Ping!"), Message(text="Pong!"))
(Message("/start"), Message("Hi!")),
(Message("Hello!"), Message("Let's play ping-pong!")),
(Message("Ping!"), Message("Pong!"))
)
A special function is then used to ascertain complete identity of the messages taken from
Expand Down
4 changes: 2 additions & 2 deletions tests/messengers/telegram/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async def test_keyboard_remove(pipeline_instance, api_credentials, bot_user, ses
attachments=Attachments(files=[video]),
),
),
(Message(text="test", attachments=Attachments(files=[document])),),
(Message("test", attachments=Attachments(files=[document])),),
],
)
@pytest.mark.telegram
Expand Down Expand Up @@ -150,7 +150,7 @@ async def test_telegram_attachment(generic_response, pipeline_instance, api_cred
attachments=Attachments(files=2 * [video]),
),
),
(Message(text="test", attachments=Attachments(files=2 * [document])),),
(Message("test", attachments=Attachments(files=2 * [document])),),
],
)
@pytest.mark.telegram
Expand Down
8 changes: 4 additions & 4 deletions tests/pipeline/test_messenger_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
RESPONSE: {
"text": "",
},
TRANSITIONS: {"node1": cnd.exact_match(Message(text="Ping"))},
TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))},
},
"node1": {
RESPONSE: {
"text": "Pong",
},
TRANSITIONS: {"node1": cnd.exact_match(Message(text="Ping"))},
TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))},
},
"fallback_node": {
RESPONSE: {
"text": "Ooops",
},
TRANSITIONS: {"node1": cnd.exact_match(Message(text="Ping"))},
TRANSITIONS: {"node1": cnd.exact_match(Message("Ping"))},
},
}
}
Expand Down Expand Up @@ -60,4 +60,4 @@ def test_callback_messenger_interface(monkeypatch):
pipeline.run()

for _ in range(0, 5):
assert interface.on_request(Message(text="Ping"), 0).last_response == Message(text="Pong")
assert interface.on_request(Message("Ping"), 0).last_response == Message("Pong")
Loading

0 comments on commit 6d982e0

Please sign in to comment.