-
Notifications
You must be signed in to change notification settings - Fork 6
/
3_responses.py
205 lines (177 loc) · 5.59 KB
/
3_responses.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# %% [markdown]
"""
# Core: 3. Responses
This tutorial shows different options for setting responses.
Here, [responses](%doclink(api,script.responses.std_responses))
that allow giving custom answers to users are shown.
Let's do all the necessary imports from DFF.
"""
# %pip install dff
# %%
import re
import random
from dff.script import TRANSITIONS, RESPONSE, Context, Message
import dff.script.responses as rsp
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,
)
# %% [markdown]
"""
The response can be set by Callable or *Message:
* Callable objects. If the object is callable it must have a special signature:
func(ctx: Context, pipeline: Pipeline) -> Message
* *Message objects. If the object is *Message
it will be returned by the agent as a response.
The functions to be used in the `toy_script` are declared here.
"""
# %%
def cannot_talk_about_topic_response(ctx: Context, _: Pipeline) -> Message:
request = ctx.last_request
if request is None or request.text is None:
topic = None
else:
topic_pattern = re.compile(r"(.*talk about )(.*)\.")
topic = topic_pattern.findall(request.text)
topic = topic and topic[0] and topic[0][-1]
if topic:
return Message(f"Sorry, I can not talk about {topic} now.")
else:
return Message("Sorry, I can not talk about that now.")
def upper_case_response(response: Message):
# wrapper for internal response function
def func(_: Context, __: Pipeline) -> Message:
if response.text is not None:
response.text = response.text.upper()
return response
return func
def fallback_trace_response(ctx: Context, _: Pipeline) -> Message:
return Message(
misc={
"previous_node": list(ctx.labels.values())[-2],
"last_request": ctx.last_request,
}
)
# %%
toy_script = {
"greeting_flow": {
"start_node": { # This is an initial node,
# it doesn't need a `RESPONSE`.
RESPONSE: Message(),
TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))},
# If "Hi" == request of user then we make the transition
},
"node1": {
RESPONSE: rsp.choice(
[
Message("Hi, what is up?"),
Message("Hello, how are you?"),
]
),
# Random choice from candidate list.
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: cannot_talk_about_topic_response,
TRANSITIONS: {"node4": cnd.exact_match(Message("Ok, goodbye."))},
},
"node4": {
RESPONSE: upper_case_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: fallback_trace_response,
TRANSITIONS: {"node1": cnd.exact_match(Message("Hi"))},
},
}
}
# testing
happy_path = (
(
Message("Hi"),
Message("Hello, how are you?"),
), # 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 music now."),
), # node2 -> node3
(Message("Ok, goodbye."), Message("BYE")), # node3 -> node4
(Message("Hi"), Message("Hello, how are you?")), # node4 -> node1
(
Message("stop"),
Message(
misc={
"previous_node": ("greeting_flow", "node1"),
"last_request": Message("stop"),
}
),
),
# node1 -> fallback_node
(
Message("one"),
Message(
misc={
"previous_node": ("greeting_flow", "fallback_node"),
"last_request": Message("one"),
}
),
), # f_n->f_n
(
Message("help"),
Message(
misc={
"previous_node": ("greeting_flow", "fallback_node"),
"last_request": Message("help"),
}
),
), # f_n->f_n
(
Message("nope"),
Message(
misc={
"previous_node": ("greeting_flow", "fallback_node"),
"last_request": Message("nope"),
}
),
), # f_n->f_n
(
Message("Hi"),
Message("Hi, what is up?"),
), # 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 music now."),
), # node2 -> node3
(Message("Ok, goodbye."), Message("BYE")), # node3 -> node4
)
# %%
random.seed(31415) # predestination of choice
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)