forked from uber-archive/plato-research-dialogue-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunPlatoRDS.py
402 lines (317 loc) · 14.5 KB
/
runPlatoRDS.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
Copyright (c) 2019 Uber Technologies, Inc.
Licensed under the Uber Non-Commercial License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at the root directory of this project.
See the License for the specific language governing permissions and
limitations under the License.
"""
__author__ = "Alexandros Papangelis"
from ConversationalAgent.ConversationalSingleAgent import \
ConversationalSingleAgent
from ConversationalAgent.ConversationalMultiAgent import \
ConversationalMultiAgent
from ConversationalAgent.ConversationalGenericAgent import \
ConversationalGenericAgent
import configparser
import yaml
import sys
import os.path
import time
import random
"""
This is the main entry point to Plato Research Dialogue System.
The Controller class is responsible for running each dialogue until the
agent(s) terminate. In the multi-agent case, the Controller will pass the
appropriate input to each agent.
"""
class Controller(object):
def __init__(self):
"""
Initializes some basic structs for the Controller.
"""
self.sys_output = ''
self.user_output = ''
self.sys_output_dacts = []
self.user_output_dacts = []
self.goal = None
@staticmethod
def run_single_agent(config, num_dialogues):
"""
This function will create an agent and orchestrate the conversation.
:param config: a dictionary containing settings
:param num_dialogues: how many dialogues to run for
:return: some statistics
"""
if 'GENERAL' in config and 'generic' in config['GENERAL'] \
and config['GENERAL']['generic']:
ca = ConversationalGenericAgent(config, 0)
else:
ca = ConversationalSingleAgent(config)
ca.initialize()
for dialogue in range(num_dialogues):
print('\n=====================================================\n\n'
'Dialogue %d (out of %d)\n' % (dialogue+1, num_dialogues))
ca.start_dialogue()
while not ca.terminated():
ca.continue_dialogue()
ca.end_dialogue()
# Collect statistics
statistics = {'AGENT_0': {}}
statistics['AGENT_0']['dialogue_success_percentage'] = \
100 * float(ca.num_successful_dialogues / num_dialogues)
statistics['AGENT_0']['avg_cumulative_rewards'] = \
float(ca.cumulative_rewards / num_dialogues)
statistics['AGENT_0']['avg_turns'] = \
float(ca.total_dialogue_turns / num_dialogues)
statistics['AGENT_0']['objective_task_completion_percentage'] = \
100 * float(ca.num_task_success / num_dialogues)
print('\n\nDialogue Success Rate: {0}\nAverage Cumulative Reward: {1}'
'\nAverage Turns: {2}'.
format(statistics['AGENT_0']['dialogue_success_percentage'],
statistics['AGENT_0']['avg_cumulative_rewards'],
statistics['AGENT_0']['avg_turns']))
return statistics
@staticmethod
def run_multi_agent(config, num_dialogues, num_agents):
"""
This function will create multiple conversational agents and
orchestrate the conversation among them.
Note: In Plato v. 0.1 this function will create two agents.
:param config: a dictionary containing settings
:param num_dialogues: how many dialogues to run for
:param num_agents: how many agents to spawn
:return: some statistics
"""
conv_sys_agents = []
conv_user_agents = []
objective_success = 0
generic_agents = False
if 'GENERAL' in config and 'generic' in config['GENERAL'] \
and config['GENERAL']['generic']:
generic_agents = bool(config['GENERAL']['generic'])
# Verify that we have a DM section for each agent in the config and
# initialize agents
for a in range(num_agents):
ag_id_str = 'AGENT_' + str(a)
if 'role' in config[ag_id_str]:
if config[ag_id_str]['role'] == 'system':
if generic_agents:
conv_sys_agents.append(
ConversationalGenericAgent(config, a))
else:
conv_sys_agents.append(
ConversationalMultiAgent(config, a))
elif config[ag_id_str]['role'] == 'user':
if generic_agents:
conv_user_agents.append(
ConversationalGenericAgent(config, a))
else:
conv_user_agents.append(
ConversationalMultiAgent(config, a))
else:
print('WARNING: Unknown agent role: {0}!'
.format(config[ag_id_str]['role']))
else:
raise ValueError('Role for agent {0} not defined in config.'
.format(a))
# TODO: WARNING: FOR NOW ASSUMING WE HAVE ONE SYSTEM AND ONE USER AGENT
conv_user_agents[0].initialize()
conv_sys_agents[0].initialize()
for dialogue in range(num_dialogues):
print('\n=====================================================\n\n'
'Dialogue %d (out of %d)\n' % (dialogue+1, num_dialogues))
# WARNING: FOR NOW ASSUMING WE HAVE ONE SYSTEM AGENT.
_, _, goal = conv_user_agents[0].start_dialogue()
sys_output, sys_output_dacts, _ = \
conv_sys_agents[0].start_dialogue(goal)
while not all(ca.terminated() for ca in conv_sys_agents) \
and not all(ca.terminated() for ca in conv_user_agents):
# WARNING: FOR NOW ASSUMING WE HAVE ONE USER AGENT.
user_output, user_output_dacts, goal = \
conv_user_agents[0].continue_dialogue(
{'other_input_raw': sys_output,
'other_input_dacts': sys_output_dacts})
# Need to check for termination condition again, for the case
# where the system says 'bye' and then the user says bye too.
if all(ca.terminated() for ca in conv_sys_agents) \
or all(ca.terminated() for ca in conv_user_agents):
break
# WARNING: FOR NOW ASSUMING WE HAVE ONE SYSTEM AGENT.
sys_output, sys_output_dacts, goal = \
conv_sys_agents[0].continue_dialogue(
{'other_input_raw': user_output,
'other_input_dacts': user_output_dacts})
# Sync goals (user has ground truth)
conv_sys_agents[0].set_goal(conv_user_agents[0].get_goal())
# Check if there is a goal. For example, if the agents are generic
# there may not be a tracked goal.
if not goal:
continue
# Consolidate goals to track objective success (each agent tracks
# different things)
# From the Sys we keep everything except the status of actual
# requests (filled or not)
goal = conv_sys_agents[0].agent_goal
goal.actual_requests = \
conv_user_agents[0].agent_goal.actual_requests
_, _, obj_succ = conv_sys_agents[0].reward_func.calculate(
conv_sys_agents[0].curr_state, [], goal=goal,
agent_role="system")
objective_success += 1 if obj_succ else 0
print(f'OBJECTIVE TASK COMPLETION: {obj_succ}')
for ca in conv_sys_agents:
ca.end_dialogue()
for ca in conv_user_agents:
ca.end_dialogue()
# Collect statistics
statistics = {}
for i in range(num_agents):
ag_id_str = 'AGENT_'+str(i)
statistics[ag_id_str] = {'role': config[ag_id_str]['role']}
statistics['AGENT_0']['dialogue_success_percentage'] = \
100 * \
float(conv_sys_agents[0].num_successful_dialogues / num_dialogues)
statistics['AGENT_0']['avg_cumulative_rewards'] = \
float(conv_sys_agents[0].cumulative_rewards / num_dialogues)
statistics['AGENT_0']['avg_turns'] = \
float(conv_sys_agents[0].total_dialogue_turns / num_dialogues)
statistics['AGENT_0']['objective_task_completion_percentage'] = \
100 * float(objective_success / num_dialogues)
statistics['AGENT_1']['dialogue_success_percentage'] = \
100 * \
float(conv_user_agents[0].num_successful_dialogues / num_dialogues)
statistics['AGENT_1']['avg_cumulative_rewards'] = \
float(conv_user_agents[0].cumulative_rewards / num_dialogues)
statistics['AGENT_1']['avg_turns'] = \
float(conv_user_agents[0].total_dialogue_turns / num_dialogues)
statistics['AGENT_1']['objective_task_completion_percentage'] = \
100 * float(objective_success / num_dialogues)
print('\n\nSYSTEM Dialogue Success Rate: {0}\n'
'Average Cumulative Reward: {1}\n'
'Average Turns: {2}'.
format(100 *
float(conv_sys_agents[0].num_successful_dialogues
/ num_dialogues),
float(conv_sys_agents[0].cumulative_rewards
/ num_dialogues),
float(conv_sys_agents[0].total_dialogue_turns
/ num_dialogues)))
print('\n\nUSER Dialogue Success Rate: {0}\n'
'Average Cumulative Reward: {1}\n'
'Average Turns: {2}'.
format(100 *
float(conv_user_agents[0].num_successful_dialogues
/ num_dialogues),
float(conv_user_agents[0].cumulative_rewards
/ num_dialogues),
float(conv_user_agents[0].total_dialogue_turns
/ num_dialogues)))
avg_rew = 0.5 * (
float(conv_sys_agents[0].cumulative_rewards / num_dialogues) +
float(conv_user_agents[0].cumulative_rewards / num_dialogues))
print(f'\n\nAVERAGE rewards: {avg_rew}')
print('\n\nObjective Task Success Rate: {0}'.format(
100 * float(objective_success / num_dialogues)))
return statistics
def arg_parse():
"""
This function will parse the configuration file that was provided as a
system argument into a dictionary.
:return: a dictionary containing the parsed config file.
"""
cfg_parser = None
# Parse arguments
if len(sys.argv) < 3:
print('WARNING: No configuration file.')
# Initialize random seed
random.seed(time.time())
cfg_filename = sys.argv[2]
if isinstance(cfg_filename, str):
if os.path.isfile(cfg_filename):
# Choose config parser
parts = cfg_filename.split('.')
if len(parts) > 1:
if parts[1] == 'yaml':
with open(cfg_filename, 'r') as file:
cfg_parser = yaml.load(file, Loader=yaml.Loader)
elif parts[1] == 'cfg':
cfg_parser = configparser.ConfigParser()
cfg_parser.read(cfg_filename)
else:
raise ValueError('Unknown configuration file type: %s'
% parts[1])
else:
raise FileNotFoundError('Configuration file %s not found'
% cfg_filename)
else:
raise ValueError('Unacceptable value for configuration file name: %s '
% cfg_filename)
tests = 1
dialogues = 10
interaction_mode = 'simulation'
num_agents = 1
if cfg_parser:
dialogues = int(cfg_parser['DIALOGUE']['num_dialogues'])
if 'interaction_mode' in cfg_parser['GENERAL']:
interaction_mode = cfg_parser['GENERAL']['interaction_mode']
if 'agents' in cfg_parser['GENERAL']:
num_agents = int(cfg_parser['GENERAL']['agents'])
elif interaction_mode == 'multi_agent':
print('WARNING! Multi-Agent interaction mode selected but '
'number of agents is undefined in config.')
if 'tests' in cfg_parser['GENERAL']:
tests = int(cfg_parser['GENERAL']['tests'])
return {'cfg_parser': cfg_parser,
'tests': tests,
'dialogues': dialogues,
'interaction_mode': interaction_mode,
'num_agents': num_agents}
def run_controller(args):
"""
This function will create and run a Controller. It iterates over the
desired number of tests and prints some basic results.
:param args: the parsed configuration file
:return: nothing
"""
# Extract arguments
cfg_parser = args['cfg_parser']
tests = args['tests']
dialogues = args['dialogues']
interaction_mode = args['interaction_mode']
num_agents = args['num_agents']
# Create a Controller
controller = Controller()
for test in range(tests):
# Run simulation
print('\n\n=======================================')
print('# Running {0} dialogues (test {1} of {2}) #'.format(dialogues,
(test + 1),
tests))
print('=======================================\n')
statistics = {}
try:
if interaction_mode in ['simulation', 'text', 'speech']:
# YAML version
statistics = controller.run_single_agent(
cfg_parser, dialogues)
elif interaction_mode == 'multi_agent':
# YAML version
statistics = controller.run_multi_agent(
cfg_parser, dialogues, num_agents)
else:
ValueError('Unknown interaction mode: {0}'.format(
interaction_mode))
except (ValueError, FileNotFoundError, TypeError, AttributeError) \
as err:
print('\nPlato error! {0}\n'.format(err))
print(f'Results:\n{statistics}')
if __name__ == '__main__':
"""
This is the main entry point to Plato.
Usage:
python runPlatoRDS.py -c <path_to_config.yaml>
Remember, Plato runs with Python 3.
"""
arguments = arg_parse()
run_controller(arguments)