-
Notifications
You must be signed in to change notification settings - Fork 0
/
tesla_ai.jac
314 lines (284 loc) · 9.46 KB
/
tesla_ai.jac
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {*} with "./faq.jac";
import {*} with "./ai_model.jac";
node dialogue_root;
node dialogue_state {
has name;
has response;
}
edge intent_transition {
has intent;
}
node vehicle {
has plate_numer;
can get_plate_numer {
report here.plate_number;
}
}
node cai_state {
has name;
can init_wlk_ctx {
new_wlk_ctx = {
"intent": null,
"entities": {},
"prev_state": null,
"next_state": null,
"respond": false
};
if ("entities" in visitor.wlk_ctx) {
// Carry over extracted entities from previous interaction
new_wlk_ctx["entities"] = visitor.wlk_ctx["entities"];
}
visitor.wlk_ctx = new_wlk_ctx;
}
can nlu {}
can process {
if (visitor.wlk_ctx["prev_state"]): visitor.wlk_ctx["respond"] = true;
else {
visitor.wlk_ctx["next_state"] = net.root();
visitor.wlk_ctx["prev_state"] = here;
}
}
can nlg {}
}
node dialogue_state:cai_state {
can bi_enc.infer;
can tfm_ner.extract_entity;
can classify_intent {
intent_labels = -[intent_transition]->.edge.intent;
visitor.wlk_ctx["intent"] = bi_enc.infer(
contexts = [visitor.question],
candidates = intent_labels,
context_type = "text",
candidate_type = "text"
)[0]["predicted"]["label"];
}
can extract_entities {
res = tfm_ner.extract_entity(visitor.question);
std.out(res);
for ent in res {
ent_type = ent["entity_value"];
ent_text = ent["entity_text"];
if (!(ent_type in visitor.wlk_ctx["entities"])){
visitor.wlk_ctx["entities"][ent_type] = [];
}
visitor.wlk_ctx["entities"][ent_type].l::append(ent_text);
}
}
can init_wlk_ctx {
new_wlk_ctx = {
"intent": null,
"entities": {},
"prev_state": null,
"next_state": null,
"respond": false
};
if ("entities" in visitor.wlk_ctx) {
// Carry over extracted entities from previous interaction
new_wlk_ctx["entities"] = visitor.wlk_ctx["entities"];
}
visitor.wlk_ctx = new_wlk_ctx;
}
can nlu {}
can process {
if (visitor.wlk_ctx["prev_state"]): visitor.wlk_ctx["respond"] = true;
else {
visitor.wlk_ctx["next_state"] = net.root();
visitor.wlk_ctx["prev_state"] = here;
}
}
can nlg {}
}
node dialogue_root:dialogue_state {
has name = "dialogue_root";
can nlu {
::classify_intent;
}
can process {
visitor.wlk_ctx["next_state"] = (-[intent_transition(intent==visitor.wlk_ctx["intent"])]->)[0];
}
can nlg {
visitor.response = "Sorry I can't handle that just yet. Anything else I can help you with?";
}
}
node how_to_order_state:dialogue_state {
has name = "how_to_order";
can nlg {
visitor.response = "You can order a Tesla through our design studio";
}
}
node test_drive_state:dialogue_state {
has name = "test_drive";
can nlu {
if (!visitor.wlk_ctx["intent"]): ::classify_intent;
::extract_entities;
}
can process {
// Check entity transition
required_entities = -[entity_transition]->.edge[0].context["entities"];
if (vector.sort_by_key(visitor.wlk_ctx["entities"].d::keys) == vector.sort_by_key(required_entities)) {
visitor.wlk_ctx["next_state"] = -[entity_transition]->[0];
visitor.wlk_ctx["prev_state"] = here;
} elif (visitor.wlk_ctx["prev_state"] and !visitor.wlk_ctx["prev_state"].context["name"] in ["test_drive", "td_confirmation"]){
next_state = -[intent_transition(intent==visitor.wlk_ctx["intent"])]->;
if (next_state.length > 0 and visitor.wlk_ctx["intent"] != "no") {
visitor.wlk_ctx["next_state"] = next_state[0];
visitor.wlk_ctx["prev_state"] = here;
} else {
visitor.wlk_ctx["respond"] = true;
}
} else {
visitor.wlk_ctx["respond"] = true;
}
}
can nlg {
if ("name" in visitor.wlk_ctx["entities"] and "address" not in visitor.wlk_ctx["entities"]):
visitor.response = "What is your address?";
elif ("address" in visitor.wlk_ctx["entities"] and "name" not in visitor.wlk_ctx["entities"]):
visitor.response = "What is your name?";
else:
visitor.response = "To set you up with a test drive, we will need your name and address.";
}
}
node td_confirmation:dialogue_state {
has name = "test_drive_confirmation";
can nlu {
if (!visitor.wlk_ctx["intent"]): ::classify_intent;
}
can process {
if (visitor.wlk_ctx["prev_state"]): visitor.wlk_ctx["respond"] = true;
else {
visitor.wlk_ctx["next_state"] = -[intent_transition(intent==visitor.wlk_ctx["intent"])]->[0];
visitor.wlk_ctx["prev_state"] = here;
}
}
can nlg {
visitor.response =
"Can you confirm your name to be " + visitor.wlk_ctx["entities"]["name"][0] + " and your address as " + visitor.wlk_ctx["entities"]["address"][0] + "?";
}
}
node td_confirmed:dialogue_state {
has name = "test_drive_confirmed";
can nlg {
visitor.response = "You are all set for a Tesla test drive!";
}
}
node td_canceled:dialogue_state {
has name = "test_drive_canceled";
can nlg {
visitor.response = "No worries. We look forward to hearing from you in the future!";
}
}
// Static graph definition
graph tesla_ai {
has anchor dialogue_root;
spawn {
dialogue_root = spawn node::dialogue_root;
test_drive_state = spawn node::test_drive_state;
td_confirmation = spawn node::td_confirmation;
td_confirmed = spawn node::td_confirmed;
td_canceled = spawn node::td_canceled;
dialogue_root +[intent_transition(intent="test drive")]+> test_drive_state;
test_drive_state +[intent_transition(intent="cancel")]+> td_canceled;
test_drive_state +[entity_transition(entities=["name", "address"])]+> td_confirmation;
test_drive_state +[intent_transition(intent="provide name or address")]+> test_drive_state;
td_confirmation +[intent_transition(intent="yes")]+> td_confirmed;
td_confirmation +[intent_transition(intent="no")]+> test_drive_state;
td_confirmation +[intent_transition(intent="cancel")]+> td_canceled;
faq_root = spawn graph::faq;
dialogue_root +[intent_transition(intent="i have a question")]+> faq_root;
}
}
walker init {
root {
spawn here ++> graph::tesla_ai;
}
}
walker ask {
can use.qa_classify;
has question;
root {
question = std.input(">");
take --> node::faq_root;
}
faq_root {
answers = -->.answer;
best_answer = use.qa_classify(
text = question,
classes = answers
);
take --> node::faq_state(answer==best_answer["match"]);
}
faq_state {
std.out(here.answer);
}
}
walker ingest_faq {
has kb_file;
root: take --> node::faq_root;
faq_root {
kb = file.load_json(kb_file);
for faq in kb {
answer = faq["answer"];
spawn here ++> node::faq_state(answer=answer);
}
}
}
graph dialogue_system {
has anchor dialogue_root;
spawn {
dialogue_root = spawn node::dialogue_root;
test_drive_state = spawn node::test_drive_state;
td_confirmation = spawn node::td_confirmation;
td_confirmed = spawn node::td_confirmed;
td_canceled = spawn node::td_canceled;
how_to_order_state = spawn node::how_to_order_state;
dialogue_root +[intent_transition(intent="test drive")]+> test_drive_state;
test_drive_state +[intent_transition(intent="cancel")]+> td_canceled;
test_drive_state +[entity_transition(entities=["name", "address"])]+> td_confirmation;
test_drive_state +[intent_transition(intent="provide name or address")]+> test_drive_state;
td_confirmation +[intent_transition(intent="yes")]+> td_confirmed;
td_confirmation +[intent_transition(intent="no")]+> test_drive_state;
td_confirmation +[intent_transition(intent="cancel")]+> td_canceled;
dialogue_root +[intent_transition(intent="order a tesla")]+> how_to_order_state;
}
}
walker talk {
has question, interactive = false;
has wlk_ctx = {
"intent": null,
"entities": {},
"prev_state": null,
"next_state": null,
"respond": false
};
has response;
root {
take --> node::dialogue_root;
}
cai_state {
if (!question and interactive) {
question = std.input("Question (Ctrl-C to exit)> ");
here::init_wlk_ctx;
} elif (!question and !interactive){
std.err("ERROR: question is required for non-interactive mode");
disengage;
}
here::nlu;
here::process;
if (visitor.wlk_ctx["respond"]) {
here::nlg;
if (interactive): std.out(response);
else {
yield report response;
here::init_wlk_ctx;
}
question = null;
take here;
} else {
take visitor.wlk_ctx["next_state"] else: take here;
}
}
}
edge entity_transition {
has entities;
}