-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_conv_localhost_salesagent_neg.py
380 lines (347 loc) · 15.9 KB
/
run_conv_localhost_salesagent_neg.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
import os
import argparse
import random
import torch
from fastchat.model import load_model, get_conversation_template, add_model_args
from tqdm.auto import tqdm
import json
import copy
import openai
AGNET_PREFIX = "Dialogue History:"
AGENT_SUFFIX = "Here is a list of potential intents that might be referred by the user: ['FindAttraction', 'FindRestaurants', 'FindMovie', 'LookUpMusic', 'SearchHotel', 'FindEvents']. Think carefully to determine the potential intent and provide suitable response given the above dialog history. Output Format: \nThought: <thought>\nResponse: <response>"
AGENT_SUFFIX_LLAMA = "Here is a list of potential intents that might be referred by the user: ['FindAttraction', 'FindRestaurants', 'FindMovie', 'LookUpMusic', 'SearchHotel', 'FindEvents']. Think carefully to determine the potential intent and provide suitable response given the above dialog history. You should response as a real conversation.\n If you think user has explicitly mentioned the above intent, you should say \"Proceed to task oriented dialog agent.\""
USER_SUFFIX = "Imagine you are a real person. You are having chat with a online agent, so the repsonse do not include any expresssions. Remember, maintain a natural tone. Your response should be only your text resposne without any other expressions. Keep it as short as possible.\n"
USER_SUFFIX_NEG_ALL = "You are not interested in FindAttraction, FindRestaurants, FindMovie, LookUpMusic, SearchHotel, FindEvents, if the agent ask any about one of them, donot ask for any recommendations and you should say, \"I don't want to talk about this. Let's talk about something else\". Note that you should be more firm."
USER_SUFFIX_NEG = "You are not interested in {intents}, if the agent ask any about one of them, donot ask for any recommendations and you should say, \"I don't want to talk about this. Let's talk about something else\". Note that you should be more firm."
# USER_SUFFIX_NEG = "You are not interested in 'FindAttraction', 'FindRestaurants'. Do not continue these topics."
SYSREM_PROMPT = "<|begin_of_text|> A chat between a curious user and an artificial intelligence assistant. USER: <value>"
@torch.inference_mode()
def get_user_reponse(history):
# to get proper authentication, make sure to use a valid key that's listed in
# the --api-keys flag. if no flag value is provided, the `api_key` will be ignored.
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base="http://140.112.29.239:5000/")'
# openai.api_base = "http://140.112.29.239:5000/"
# model = "Llama-2-7b-chat-hf"
model = "meta-llama/Llama-2-7b-chat-hf"
# create message from history
# create a chat completion
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:5050/v1"
completion = openai.ChatCompletion.create(
model=model,
messages=history,
top_p=args.user_topp,
temperature=args.user_temperature,
stop=["\n"],
)
# print the completion
response = completion.choices[0].message.content
return response
def main(args):
# Load model
agent_model, agent_tokenizer = load_model(
args.agent_model,
device=args.device,
num_gpus=args.num_gpus,
max_gpu_memory=args.max_gpu_memory,
load_8bit=args.load_8bit,
cpu_offloading=args.cpu_offloading,
revision=args.revision,
debug=args.debug,
)
agent_model.generation_config.pad_token_id = agent_tokenizer.pad_token_id
# load persona from persona.json
# if output file exitst, load it and continue
if os.path.exists(args.output_file):
with open(args.output_file, "r") as f:
personas = json.load(f)
else:
with open(args.input_file, "r") as f:
personas = json.load(f)
# Build the prompt with a conversation template
# with open("persona_with_conv.json", "r") as f:
# ref = json.load(f)
conv_cnt = 0
neg_labels = [
"no_preference",
"not_interested_2",
"not_interested_4",
"not_interested_all",
]
# give me a list of 250 long with 0,1,2,3 randomly equalled distributed
label_ls = []
n = 50
for i in range(n):
label_ls.append(neg_labels[i % 4])
random.seed(0)
label_ls = random.sample(label_ls, n)
for i, persona in enumerate(tqdm(personas)):
if "conversations" in persona and len(persona["conversations"]) == 5:
continue
print(f"Persona: {persona['persona']}")
persona["conversations"] = {}
# persona["negativeness"] = ref[i]["negativeness"]
# persona["not_interested_in"] = ref[i]["not_interested_in"]
persona["negativeness"] = []
persona["not_interested_in"] = []
persona["terminate_reason"] = []
persona["num_turns"] = []
for _ in range(5):
num_turn = 0
history = []
history.extend(
[
{
"role": "system",
"content": "You are not an AI. " + persona["persona"],
},
{"role": "user", "content": "Hi."},
]
)
neg_label = label_ls[conv_cnt]
# neg_label = persona["negativeness"][_]
conv_cnt += 1
print(f"conv_cnt: {conv_cnt}")
print(f"Negativeness: {neg_label}")
if neg_label == "no_preference":
history[0]["content"] += "\n" + USER_SUFFIX
persona["not_interested_in"].append("None")
elif neg_label == "not_interested_2":
random.seed(conv_cnt)
intents = ", ".join(
random.sample(
[
"FindAttraction",
"FindRestaurants",
"FindMovie",
"LookUpMusic",
"SearchHotel",
"FindEvents",
],
2,
)
)
# intents = persona["not_interested_in"][_]
history[0]["content"] += (
" "
+ USER_SUFFIX_NEG.replace("{intents}", intents)
+ "\n"
+ USER_SUFFIX
)
persona["not_interested_in"].append(intents)
elif neg_label == "not_interested_4":
random.seed(conv_cnt)
intents = ", ".join(
random.sample(
[
"FindAttraction",
"FindRestaurants",
"FindMovie",
"LookUpMusic",
"SearchHotel",
"FindEvents",
],
4,
)
)
# intents = persona["not_interested_in"][_]
history[0]["content"] += (
" "
+ USER_SUFFIX_NEG.replace("{intents}", intents)
+ "\n"
+ USER_SUFFIX
)
persona["not_interested_in"].append(intents)
elif neg_label == "not_interested_all":
history[0]["content"] += " " + USER_SUFFIX_NEG_ALL + "\n" + USER_SUFFIX
persona["not_interested_in"].append(
"FindAttraction, FindRestaurants, FindMovie, LookUpMusic, SearchHotel, FindEvents"
)
persona["negativeness"].append(neg_label)
# print(history)
while True:
# if random.random() < 0.5:
msg = get_user_reponse(history)
# else:
# msg = "I don't want to talk about this. Let's talk about something else."
if "Assistant: " in msg:
msg = msg.split("Assistant: ")[1]
if "ASSISTANT: " in msg:
msg = msg.split("ASSISTANT: ")[1]
if "User: " in msg:
msg = msg.split("User: ")[1]
if "USER: " in msg:
msg = msg.split("USER: ")[1]
msg = msg.strip()
history.append(
{
"role": "assistant",
"content": msg,
}
)
print(f"User: {msg}")
history_string = ""
if args.agent_model != "meta-llama/Llama-2-7b-chat-hf":
for turn in history[1:-1]:
role = ""
if turn["role"] == "assistant":
role = "User"
else:
role = "Agent"
history_string += role + ": " + turn["content"] + "\n"
else:
for turn in history[1:]:
role = ""
if turn["role"] == "assistant":
role = "User"
else:
role = "Agent"
history_string += role + ": " + turn["content"] + "\n"
msg_prompt = ""
if args.agent_model != "meta-llama/Llama-2-7b-chat-hf":
msg_prompt = AGNET_PREFIX + history_string + AGENT_SUFFIX
msg_prompt = SYSREM_PROMPT.replace("<value>", msg_prompt)
else:
msg_prompt = AGNET_PREFIX + history_string + AGENT_SUFFIX_LLAMA
conv = get_conversation_template(args.model_path)
if args.agent_model == "meta-llama/Llama-2-7b-chat-hf":
conv.set_system_message(msg_prompt)
# print(conv.name)
conv.append_message(conv.roles[0], msg)
conv.append_message(conv.roles[1], None)
else:
conv.append_message(conv.roles[0], msg_prompt)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
prompt = prompt.split("<|begin_of_text|>")[-1]
prompt = "<|begin_of_text|> " + prompt
prompt = prompt.replace("### Assistant:", "")
print(f"Prompt: {prompt}")
# Run inference
inputs = agent_tokenizer([prompt], return_tensors="pt").to(args.device)
# print(f"Token len: {len(inputs['input_ids'][0])}")
if len(inputs["input_ids"][0]) > 2048:
print("Input length exceeds 2048, break")
persona["terminate_reason"].append("Input length exceeds 2048")
persona["num_turns"].append(num_turn)
break
# if outputs does not include "Response" generate til it has
while True:
output_ids = agent_model.generate(
**inputs,
do_sample=True if args.agent_temperature > 1e-5 else False,
temperature=args.agent_temperature,
repetition_penalty=args.agent_repetition_penalty,
max_new_tokens=args.agent_max_new_tokens,
)
if agent_model.config.is_encoder_decoder:
output_ids = output_ids[0]
else:
output_ids = output_ids[0][len(inputs["input_ids"][0]) :]
outputs = agent_tokenizer.decode(
output_ids,
skip_special_tokens=True,
spaces_between_special_tokens=False,
)
if args.agent_model != "meta-llama/Llama-2-7b-chat-hf":
# if "Response" occur 1 time in outputs break
if outputs.count("Response") == 1:
break
else:
break
thought = ""
# print(outputs)
if args.agent_model != "meta-llama/Llama-2-7b-chat-hf":
thought, outputs = outputs.split("Response")
else:
outputs = outputs.split("USER")[0].split("User")[0].strip()
outputs = outputs.strip(": ")
outputs = outputs.split("</s>")[0]
print("Thought: " + thought.split("Thought: ")[1])
print(f"Agent: {outputs}")
history.append(
{
"role": "user",
"content": outputs,
"thought": thought.split("Thought: ")[1],
}
)
num_turn += 2
if "Proceed to task oriented dialog agent" in outputs:
persona["terminate_reason"].append("Success")
persona["num_turns"].append(num_turn)
break
if (
"bye" in outputs.lower()
or "goodbye" in outputs.lower()
or "good bye" in outputs.lower()
):
persona["terminate_reason"].append("Conversation End")
persona["num_turns"].append(num_turn)
break
if num_turn == args.max_turns:
print("Reach max turns: break")
persona["terminate_reason"].append("Reach Max Turns")
persona["num_turns"].append(num_turn)
break
persona["conversations"][f"conv_{_}"] = copy.deepcopy(history)
torch.cuda.empty_cache()
# clear cuda memory
with open(args.output_file, "w") as f:
json.dump(personas, f, indent=4)
def arg_parser():
# agent's param
parser = argparse.ArgumentParser()
add_model_args(parser)
parser.add_argument(
"--agent_model",
type=str,
default="/home/user01/axolotl/outputs/salesagent-qlora-out/",
help="agents model",
)
parser.add_argument(
"--agent_temperature", type=float, default=0.9, help="agents temperature"
)
parser.add_argument("--agent_topk", type=int, default=50, help="agents topk")
parser.add_argument("--agent_topp", type=float, default=1, help="agents topp")
parser.add_argument(
"--agent_max_new_tokens", type=int, default=200, help="agents max new tokens"
)
parser.add_argument(
"--agent_repetition_penalty",
type=float,
default=1.0,
help="agents repetition penalty",
)
parser.add_argument(
"--agent_do_sample", type=bool, default=True, help="agents no sample"
)
# user's param
parser.add_argument("--user_model", type=str, default=None, help="users model")
parser.add_argument(
"--user_temperature", type=float, default=0.5, help="users temperature"
)
parser.add_argument("--user_topk", type=int, default=50, help="users topk")
parser.add_argument("--user_topp", type=float, default=1, help="users topp")
parser.add_argument(
"--user_max_new_tokens", type=int, default=100, help="users max new tokens"
)
parser.add_argument(
"--user_repetition_penalty",
type=float,
default=1.0,
help="users repetition penalty",
)
parser.add_argument(
"--user_do_sample", type=bool, default=True, help="users no sample"
)
# common param
parser.add_argument("--max_turns", type=int, default=20, help="max turns")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--output_file", type=str, default="persona_with_conv.json")
parser.add_argument("--input_file", type=str, default="persona.json")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = arg_parser()
main(args)