-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog_simulation.py
230 lines (204 loc) · 9.86 KB
/
dialog_simulation.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
# -*- coding: utf-8 -*-
import time
import json
import os
import random
import argparse
from tqdm import tqdm
from chatarena.agent import Player, Moderator
from chatarena.backends import OpenAIChat
from chatarena.environments.conversation import ModeratedConversation
from chatarena.arena import Arena
from data_utils import find_word_in_string
from instruction import create_instruct
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--cached_seed_path", type=str, required=True,
help="The cached seed dialog file.")
parser.add_argument("--profile_path", type=str, default="seed_dataset/caches/db_slot/slot_profiles.json",
help="The user profile slot-values file.")
parser.add_argument("--output_dir", type=str, default="data/TopDial",
help="The output directory to save the simulated dialog data.")
parser.add_argument("--max_interaction_step", type=int,default=12,
help="The max number of interaction steps, i.e., 2 * max rounds.")
parser.add_argument("--model_name", type=str, default="gpt-3.5-turbo",
help="The chat model to use.")
parser.add_argument("--temperature", type=float, default=0.75,
help="The temperature to use in sampling.")
parser.add_argument("--max_system_tokens", type=int, default=100,
help="The max number of tokens to generate for the system.")
parser.add_argument("--max_user_tokens", type=int, default=80,
help="The max number of tokens to generate for the user.")
parser.add_argument("--max_moderator_tokens", type=int, default=10,
help="The max number of tokens to generate for the moderator.")
parser.add_argument("--show_description", type=str2bool, default="true",
help="Whether to show the role description.")
parser.add_argument("--show_message", type=str2bool, default="true",
help="Whether to show the conversation messages.")
parser.add_argument("--random_seed", type=int, default=42)
return parser.parse_args()
def str2bool(v):
if v.lower() in ('true', 'yes', 't', 'y', '1'):
return True
elif v.lower() in ('false',' no', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError("Unsupported value encountered.")
def clean_utterance(s):
s = s.strip()
for start_str in ['[1]', '[2]', '[3]', '[4]', '[5]', '[6]', '[7]', '[8]', '[9]']:
if s.startswith(start_str):
s = s[len(start_str):].strip()
return s
def prompt_conversation(raw_goal, conversation):
"""Prompt the conversation context."""
conversation_ctx = ""
for idx, utt in enumerate(conversation):
utt = clean_utterance(utt)
if "User Initiative" in raw_goal:
if idx % 2 == 0:
conversation_ctx += f"[Role-U]: {utt}<EOS>\n\n"
else:
conversation_ctx += f"[Role-S]: {utt}<EOS>\n\n"
else:
if idx % 2 == 0:
conversation_ctx += f"[Role-S]: {utt}<EOS>\n\n"
else:
conversation_ctx += f"[Role-U]: {utt}<EOS>\n\n"
return conversation_ctx
def sample_seed_conversation(raw_goal, conversation):
"""Sample seed conversations (continue | end)."""
conv_lens = len(conversation)
continue_len = random.choice(range(1, int(conv_lens * 0.6)))
conv_continue = prompt_conversation(raw_goal, conversation[:continue_len])
conv_end = prompt_conversation(raw_goal, conversation)
seed_conv = {
"seed_continue": conv_continue,
"seed_end": conv_end
}
return seed_conv
def sample_assistant_role(profile_slots, user_profile):
"""Sample an assistant role."""
all_names = profile_slots["Name"]
user_name = user_profile["Name"]
sampled_name = random.choice(all_names)
while find_word_in_string(sampled_name, user_name):
sampled_name = random.choice(all_names)
return sampled_name
def sample_personality():
"""Sample a personality based on Big Five personality traits."""
personalities = {
"agreeableness": ["trustworthy, straightforward, and generous", "unreliable, complicated, meager, and boastful"],
"conscientiousness": ["efficient, organized, and careful", "inefficient, careless, and sloppy"],
"extraversion": ["outgoing, energetic, and talkative", "shy, reserved, and quiet"],
"neuroticism": ["sensitive, nervous, and insecure", "secure, confident, and calm"],
"openness": ["intellectual, imaginative, and curious", "unimaginative, uncreative, and conventional"]
}
sampled_personality = {}
for trait, values in personalities.items():
sampled_personality[trait] = random.choice(values)
return sampled_personality
def generate_dialog_data(
profile_path,
seed_path,
output_dir,
max_interaction_step=10,
model_name="gpt-3.5-turbo",
temperature=0.75,
max_system_tokens=100,
max_user_tokens=80,
max_moderator_tokens=10,
show_description=True,
show_message=True,
):
"""Generate dialog data from a seed dialog file."""
profile_slots = json.load(open(profile_path, "r", encoding='utf-8'))
print(f"Loaded user profiles with {len(profile_slots)} slot keys.")
seed_dialogs = []
with open(seed_path, "r", encoding='utf-8') as f:
for line in f:
seed_dialogs.append(json.loads(line))
print(f"Loaded {len(seed_dialogs)} cached dialogs.")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if "test_seen" in seed_path:
output_path = os.path.join(output_dir, "dialogue_test_seen.jsonl")
elif "test_unseen" in seed_path:
output_path = os.path.join(output_dir, "dialogue_test_unseen.jsonl")
elif "dev" in seed_path:
output_path = os.path.join(output_dir, "dialogue_dev.jsonl")
else:
output_path = os.path.join(output_dir, "dialogue_train.jsonl")
with open(output_path, "w", encoding='utf-8') as fw:
for seed_dialog in tqdm(seed_dialogs):
simulated_profile = seed_dialog["user_profile"]
sampled_knowledge = seed_dialog["knowledge"]
target = seed_dialog["target"]
conversation = seed_dialog["seed_conversation"]
seed_conv = sample_seed_conversation(seed_dialog["original_goal"], conversation)
# randomly sample a personality
simulated_personality = sample_personality()
assistant_name = sample_assistant_role(profile_slots, simulated_profile)
env_desc, user_dict, assistant_dict, moderator_dict = create_instruct(
target=target,
simulated_profile=simulated_profile,
simulated_personality=simulated_personality,
assistant_name=assistant_name,
domain_knowledge=sampled_knowledge,
seed_conversation=seed_conv
)
assistant = Player(
name=assistant_dict["name"], backend=OpenAIChat(model=model_name, temperature=temperature, max_tokens=max_system_tokens),
role_desc=assistant_dict["role_desc"], global_prompt=env_desc
)
user = Player(
name=user_dict["name"], backend=OpenAIChat(model=model_name, temperature=temperature, max_tokens=max_user_tokens),
role_desc=user_dict["role_desc"], global_prompt=env_desc
)
moderator = Moderator(
backend=OpenAIChat(model=model_name, temperature=temperature, max_tokens=max_moderator_tokens),
role_desc=moderator_dict["role_desc"], terminal_condition=moderator_dict["terminal_condition"]
)
# let assistant start the conversation
env = ModeratedConversation(player_names=[p.name for p in [assistant, user]], moderator=moderator, moderator_period="round")
arena = Arena(players=[assistant, user], environment=env, global_prompt=env_desc)
arena.launch_cli(max_steps=max_interaction_step, show_description=show_description, show_message=show_message, interactive=False)
#print("Save? (y/n)")
#if input() == "n":
# continue
# save the simulated dialog to file
messages = env.get_observation()
simulated_convs = []
for msg in messages:
if msg.agent_name == assistant.name:
utt = {"system": msg.content}
else:
utt = {"user": msg.content}
simulated_convs.append(utt)
write_line = {
"id": "s_" + str(seed_dialog["id"]),
"user_profile": simulated_profile,
"user_personality": simulated_personality,
"knowledge": sampled_knowledge,
"target": target,
"conversation": simulated_convs
}
fw.write(json.dumps(write_line, ensure_ascii=False) + "\n")
fw.flush()
print("Sleeping for 5 seconds...")
time.sleep(5)
#print("Continue? (y/n)")
#if input() == "n":
# break
if __name__ == '__main__':
args = parse_args()
random.seed(args.random_seed)
generate_dialog_data(args.profile_path, args.cached_seed_path, args.output_dir,
max_interaction_step=args.max_interaction_step,
model_name=args.model_name,
temperature=args.temperature,
max_system_tokens=args.max_system_tokens,
max_user_tokens=args.max_user_tokens,
max_moderator_tokens=args.max_moderator_tokens,
show_description=args.show_description,
show_message=args.show_message)