-
Notifications
You must be signed in to change notification settings - Fork 6
/
4_multi_message.py
160 lines (149 loc) · 4.48 KB
/
4_multi_message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# %% [markdown]
"""
# Responses: 4. Multi Message
This tutorial shows how to store several messages inside a single one.
This might be useful if you want DFF Pipeline to send `response` candidates
to the messenger interface instead of a final response.
However, this approach is not recommended due to history incompleteness.
"""
# %pip install dff
# %%
from dff.script import TRANSITIONS, RESPONSE, Message
import dff.script.conditions as cnd
from dff.pipeline import Pipeline
from dff.utils.testing.common import (
check_happy_path,
is_interactive_mode,
run_interactive_mode,
)
# %%
toy_script = {
"greeting_flow": {
"start_node": { # This is an initial node,
TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))},
# If "Hi" == request of user then we make the transition
},
"node1": {
RESPONSE: Message(
misc={
"messages": [
Message("Hi, what is up?", misc={"confidences": 0.85}),
Message(
text="Hello, how are you?",
misc={"confidences": 0.9},
),
]
}
),
TRANSITIONS: {
"node2": cnd.exact_match(Message("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."))
},
},
"node3": {
RESPONSE: Message("Sorry, I can not talk about that now."),
TRANSITIONS: {"node4": cnd.exact_match(Message("Ok, goodbye."))},
},
"node4": {
RESPONSE: Message("bye"),
TRANSITIONS: {"node1": cnd.exact_match(Message("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"))},
},
}
}
# testing
happy_path = (
(
Message("Hi"),
Message(
misc={
"messages": [
Message("Hi, what is up?", misc={"confidences": 0.85}),
Message(
text="Hello, how are you?", misc={"confidences": 0.9}
),
]
}
),
), # start_node -> node1
(
Message("I'm fine, how are you?"),
Message("Good. What do you want to talk about?"),
), # node1 -> node2
(
Message("Let's talk about music."),
Message("Sorry, I can not talk about that now."),
), # node2 -> node3
(Message("Ok, goodbye."), Message("bye")), # node3 -> node4
(
Message("Hi"),
Message(
misc={
"messages": [
Message("Hi, what is up?", misc={"confidences": 0.85}),
Message(
text="Hello, how are you?", misc={"confidences": 0.9}
),
]
}
),
), # node4 -> node1
(
Message("stop"),
Message("Ooops"),
),
# node1 -> fallback_node
(
Message("one"),
Message("Ooops"),
), # f_n->f_n
(
Message("help"),
Message("Ooops"),
), # f_n->f_n
(
Message("nope"),
Message("Ooops"),
), # f_n->f_n
(
Message("Hi"),
Message(
misc={
"messages": [
Message("Hi, what is up?", misc={"confidences": 0.85}),
Message(
text="Hello, how are you?", misc={"confidences": 0.9}
),
]
}
),
), # fallback_node -> node1
(
Message("I'm fine, how are you?"),
Message("Good. What do you want to talk about?"),
), # node1 -> node2
(
Message("Let's talk about music."),
Message("Sorry, I can not talk about that now."),
), # node2 -> node3
(Message("Ok, goodbye."), Message("bye")), # node3 -> node4
)
# %%
pipeline = Pipeline.from_script(
toy_script,
start_label=("greeting_flow", "start_node"),
fallback_label=("greeting_flow", "fallback_node"),
)
if __name__ == "__main__":
check_happy_path(pipeline, happy_path)
if is_interactive_mode():
run_interactive_mode(pipeline)