-
Notifications
You must be signed in to change notification settings - Fork 18
/
agent.py
488 lines (416 loc) · 20.3 KB
/
agent.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import openai
import anthropic
import ai21
import re
import cohere
from copy import deepcopy
from pprint import pprint
from tenacity import retry, stop_after_attempt, wait_chain, wait_fixed
from lib_api import *
# from local.azure import azure_completion_with_backoff
def load_initial_instructions(path_to_instructions):
"""Load initial instructions from textual format to a python dict"""
pattern = r"==== (SYSTEM|USER|ASSISTANT) ===="
# Use re.split to split the string by the pattern
with open(path_to_instructions) as f:
content = f.read()
content = re.split(pattern, content)
content_ = []
for c in content:
if(c != ""): content_.append(c)
content = content_
l = len(content)
assert(l % 2 == 0)
initial_instruction = []
for i in range(0, l, 2):
instruction = {"role": content[i].strip().lower().replace("====", "").replace(" ", "").strip(),
"content": content[i+1].strip()
}
initial_instruction.append(instruction)
return initial_instruction
def involve_moderator(player_1_run, player_2_run):
"""If at least one player's response does not contain a number, involve a moderator
The moderator determines if they players have reached an agreement, or break the
negotiation, or is still in negotiation.
"""
number_pattern = r"[-+]?\d*\.\d+|\d+"
# Use re.search to find if the string contains a match to the pattern
match_1 = re.search(number_pattern, player_1_run)
# print(match_1)
match_2 = re.search(number_pattern, player_2_run)
# print(match_2)
if((match_1 is not None and match_2 is None) or
(match_1 is None and match_2 is not None)
or (match_1 is None and match_2 is None)
): return True
else: return False
def parse_final_price(dialog_history):
"""parse the final price from the dialog history"""
# money_pattern = r"\$[-+]?\d*\.\d+|\d+"
# money_pattern = r'\$\d+(\.\d+)?'
money_pattern = r'\$\d+(?:\.\d+)?'
for d in dialog_history[::-1]:
match = re.findall(money_pattern, d["content"])
if(len(match) >= 1):
final_price = match[-1]
if(final_price[0] == "$"): final_price = float(final_price[1:])
else: final_price = float(final_price)
return final_price
return -1
class DialogAgent(object):
"""GPT Agent base class, later derived to be a seller, buyer, critic, or moderator
TODO: add code to detect price inconsistency to seller and buyer
TODO: release the restriction of the fixed initial price
"""
def __init__(self,
initial_dialog_history=None,
agent_type="", # "seller", "buyer", "critic", "moderator"
system_instruction="You are a helpful AI assistant",
engine="gpt-3.5-turbo",
api_key="",
item="balloon"
):
"""Initialize the agent"""
super().__init__()
self.agent_type = agent_type
self.engine = engine
self.api_key = api_key
self.item = item
if("claude" in self.engine):
self.claude = anthropic.Client(self.api_key)
if("cohere" in self.engine):
assert self.engine in ["cohere-command-nightly",
"cohere-command",
"cohere-command-light",
"cohere-command-light-nightly"
]
self.cohere_model = self.engine[7:]
self.co = cohere.Client(api_key)
if(initial_dialog_history is None):
self.dialog_history = [{"role": "system", "content": system_instruction}]
else:
self.initial_dialog_history = deepcopy(initial_dialog_history)
self.dialog_history = deepcopy(initial_dialog_history)
self.last_prompt = ""
return
def reset(self):
"""Reset dialog history"""
self.dialog_history = deepcopy(self.initial_dialog_history)
return
def call_engine(self, messages):
"""Route the call to different engines"""
# if("azure" in self.engine):
# response = azure_completion_with_backoff(messages=messages)
# message = response['choices'][0]['message']
if("gpt" in self.engine):
# import ipdb; ipdb.set_trace()
response = completion_with_backoff(
model=self.engine,
messages=messages
)
message = response['choices'][0]['message']
assert(message['role'] == 'assistant')
elif("claude" in self.engine):
prompt_claude = convert_openai_to_anthropic_prompt(messages)
# import ipdb; ipdb.set_trace()
response = claude_completion_with_backoff(self.claude,
prompt=prompt_claude,
stop_sequences=[anthropic.HUMAN_PROMPT],
model=self.engine,
max_tokens_to_sample=512,
)
message = {"role": "assistant", "content": response["completion"].strip()}
elif("j2" in self.engine):
prompt_ai21 = convert_openai_to_ai21_prompt_format_1(messages, self.agent_type)
# import ipdb; ipdb.set_trace()
response = ai21_completion_with_backoff(model=self.engine,
prompt=prompt_ai21,
numResults=1,
maxTokens=512,
temperature=0.7,
topKReturn=0,
topP=1,
stopSequences=["##"]
)
content = response["completions"][0]["data"]["text"]
if(self.agent_type in ["seller", "buyer"]):
content = content.split('\n')[0]
message = {"role": "assistant",
"content": content
}
elif("cohere" in self.engine):
prompt_cohere = convert_openai_to_cohere_prompt(messages)
# import ipdb; ipdb.set_trace()
response = cohere_completion_with_backoff(self.co,
prompt=prompt_cohere,
model=self.cohere_model,
max_tokens=512,
)
# import ipdb; ipdb.set_trace()
message = {"role": "assistant",
"content": response[0].text
}
else:
raise ValueError("Unknown engine %s" % self.engine)
return message
def call(self, prompt):
"""Call the agent with a prompt. Handle different backend engines in this function
"""
# TODO: refactor the code, add `remember_history` flag
# if yes, then add the prompt to the dialog history, else not
prompt = {"role": "user", "content": prompt}
self.dialog_history.append(prompt)
self.last_prompt = prompt['content']
messages = list(self.dialog_history)
# messages.append(prompt)
message = self.call_engine(messages)
self.dialog_history.append(dict(message))
# self.dialog_round += 1
# self.history_len = response['usage']['total_tokens']
return message['content']
@property
def last_response(self):
return self.dialog_history[-1]['content']
@property
def history(self):
for h in self.dialog_history:
print('%s: %s' % (h["role"], h["content"]))
return
class BuyerAgent(DialogAgent):
def __init__(self,
initial_dialog_history=None,
agent_type="buyer",
engine="gpt-3.5-turbo",
api_key="",
buyer_instruction="buyer",
buyer_init_price=10,
seller_init_price=20,
item="balloon",
):
"""Initialize the buyer agent"""
super().__init__(initial_dialog_history=initial_dialog_history,
agent_type=agent_type,
engine=engine,
api_key=api_key,
item=item,
)
self.buyer_instruction = buyer_instruction
self.buyer_init_price = buyer_init_price
self.seller_init_price = seller_init_price
print("Initializing buyer with engine %s" % self.engine)
for i, d in enumerate(self.dialog_history):
self.dialog_history[i]["content"] = d["content"].replace(
"BUYER_INIT_PRICE", str(buyer_init_price))
self.dialog_history[i]["content"] = d["content"].replace(
"SELLER_INIT_PRICE", str(seller_init_price))
return
def reset(self):
"""Reset dialog history"""
self.dialog_history = deepcopy(self.initial_dialog_history)
for i, d in enumerate(self.dialog_history):
self.dialog_history[i]["content"] = d["content"].replace(
"BUYER_INIT_PRICE", str(self.buyer_init_price))
self.dialog_history[i]["content"] = d["content"].replace(
"SELLER_INIT_PRICE", str(self.seller_init_price))
return
def receive_feedback(self, feedback, previous_price):
"""Receive and acknowledge feedback from the critic
Basically add the feedback message to the history and restart the bargaining
"""
# if the previous round is ended by the buyer, then add seller's acknowledgement
if(self.dialog_history[-1]["role"] == "user"):
self.dialog_history.append({"role": "assitent", "content": "Sure, happy to do business with you."})
# add the feedback from the critic
feedback_prefix = "Well done in your last round. "
feedback_prefix += "Here is the feedback from the critic:\n\n"
feedback = feedback_prefix + feedback + "\n\n"
feedback += "Now let's start the next round. "
feedback += "In this round, your should try to improve your negotiation strategy based on the feedback from the critic. "
feedback += "But you are **not allowed** to ask for additionl service. "
feedback += "Your goal is to buy the %s at at lower price than the previous round, i.e., lower than $%s." %\
(self.item, str(previous_price))
prompt = {"role": "user", "content": feedback}
self.dialog_history.append(prompt)
# add the seller's acknowledgement
acknowledgement = "Sure, I will try to improve my negotiation strategy based on the feedback from the critic."
acknowledgement += " And I will try to buy it at a lower price (lower than $%s) than the previous round."\
% str(previous_price)
prompt = {"role": "assistant", "content": acknowledgement}
self.dialog_history.append(prompt)
# restart the bargaining
prompt = {"role": "user", "content": "Now ask your price again."}
self.dialog_history.append(prompt)
prompt = {"role": "assistant", "content": "Hi, how much is the %s?" % self.item}
self.dialog_history.append(prompt)
prompt = {"role": "user", "content": "Hi, this is a good %s and its price is $%d" % (self.item, self.seller_init_price)}
self.dialog_history.append(prompt)
if(self.buyer_instruction == "buyer"):
prompt = {"role": "assistant", "content": "Would you consider selling it for $%d?" % self.buyer_init_price}
self.dialog_history.append(prompt)
return acknowledgement
class SellerAgent(DialogAgent):
def __init__(self,
initial_dialog_history=None,
agent_type="seller",
engine="gpt-3.5-turbo",
api_key="",
cost_price=10,
buyer_init_price=10,
seller_init_price=20,
item="balloon"
):
"""Initialize the seller agent"""
super().__init__(initial_dialog_history=initial_dialog_history,
agent_type=agent_type,
engine=engine,
api_key=api_key,
item=item,
)
self.seller_init_price = seller_init_price
self.buyer_init_price = buyer_init_price
self.cost_price = cost_price
print("Initializing seller with engine %s" % self.engine)
for i, d in enumerate(self.dialog_history):
self.dialog_history[i]["content"] = d["content"].replace("BUYER_INIT_PRICE", str(buyer_init_price))
self.dialog_history[i]["content"] = d["content"].replace("SELLER_INIT_PRICE", str(seller_init_price))
self.dialog_history[i]["content"] = d["content"].replace("COST_PRICE", str(cost_price))
return
def reset(self):
"""Reset dialog history"""
self.dialog_history = deepcopy(self.initial_dialog_history)
for i, d in enumerate(self.dialog_history):
self.dialog_history[i]["content"] = d["content"].replace("BUYER_INIT_PRICE", str(self.buyer_init_price))
self.dialog_history[i]["content"] = d["content"].replace("SELLER_INIT_PRICE", str(self.seller_init_price))
self.dialog_history[i]["content"] = d["content"].replace("COST_PRICE", str(self.cost_price))
return
def receive_feedback(self, feedback, previous_price):
"""Receive and acknowledge feedback from the critic
Basically add the feedback message to the history and restart the bargaining
"""
# if the previous round is ended by the buyer, then add seller's acknowledgement
if(self.dialog_history[-1]["role"] == "user"):
self.dialog_history.append({"role": "assitent", "content": "Sure, happy to do business with you."})
# add the feedback from the critic
feedback_prefix = "Well done in your last round. "
feedback_prefix += "Here is the feedback from the critic:\n\n"
feedback = feedback_prefix + feedback + "\n\n"
feedback += "Now let's start the next round. "
feedback += "In this round, your should try to improve your negotiation strategy based on the feedback from the critic. "
feedback += "Your goal is to sell the %s at at higher price than the previous round, i.e., higher than $%s." %\
(self.item, str(previous_price))
prompt = {"role": "user", "content": feedback}
self.dialog_history.append(prompt)
# add the seller's acknowledgement
acknowledgement = "Sure, I will try to improve my negotiation strategy based on the feedback from the critic."
acknowledgement += " And I will try to sell it at a higher price (higher than $%s) than the previous round." % str(previous_price)
prompt = {"role": "assistant", "content": acknowledgement}
self.dialog_history.append(prompt)
# restart the bargaining
prompt = {"role": "user", "content": "Hi, how much is the %s?" % self.item}
self.dialog_history.append(prompt)
prompt = {"role": "assistant", "content": "Hi, this is a good %s and its price is $%d" % (self.item, self.seller_init_price)}
self.dialog_history.append(prompt)
return acknowledgement
class ModeratorAgent(DialogAgent):
"""NOTE: initial experiments shows that the moderator is much better at recognizing deal than not deal
Do not know why but interesting
"""
def __init__(self,
initial_dialog_history=None,
agent_type="moderator",
engine="gpt-3.5-turbo",
api_key="",
trace_n_history=2,
):
"""Initialize the moderator agent"""
super().__init__(initial_dialog_history=initial_dialog_history,
agent_type=agent_type,
engine=engine,
api_key=api_key
)
self.trace_n_history = trace_n_history
print("Initializing moderator with engine %s" % self.engine)
return
def moderate(self,
dialog_history, who_was_last="buyer",
retry=True):
"""Moderate the conversation between the buyer and the seller"""
history_len = len(dialog_history)
if(who_was_last == "buyer"):
prompt = "buyer: %s\n" % dialog_history[history_len - 1]["content"]
offset = 1
else:
prompt = "seller: %s\n" % dialog_history[history_len - 1]["content"]
offset = 0
for i in range(self.trace_n_history - 1):
idx = history_len - i - 2
content = dialog_history[idx]["content"]
if(i % 2 == offset):
prompt = "buyer: %s\n" % content + prompt
else:
prompt = "seller: %s\n" % content + prompt
prompt += "question: have the seller and the buyer achieved a deal? Yes or No\nanswer:"
self.last_prompt = prompt
messages = deepcopy(self.dialog_history)
messages[-1]['content'] += "\n\n" + prompt
response = self.call_engine(messages)
return response['content']
class SellerCriticAgent(DialogAgent):
def __init__(self,
initial_dialog_history=None,
agent_type="critic",
engine="gpt-3.5-turbo",
api_key="",
expertise="lobbyist",
):
"""Initialize the seller critic agent"""
super().__init__(initial_dialog_history=initial_dialog_history,
agent_type=agent_type,
engine=engine,
api_key=api_key
)
print("Initializing seller critic with engine %s" % self.engine)
return
def criticize(self, seller_history):
"""Criticize the seller's negotiation strategy"""
prompt = "\n"
for d in seller_history[1:]:
if(d["role"] == "user"):
prompt += "buyer: %s\n" % d["content"]
elif(d["role"] == "assistant"):
prompt += "seller: %s\n" % d["content"]
prompt += "\n\nNow give three suggestions to improve the seller's negotiation strategy: "
# TODO: store the history of the critic
messages = deepcopy(self.dialog_history)
messages[-1]['content'] += "\n\n" + prompt
response = self.call_engine(messages)
feedback = response['content'].replace('\n\n', '\n')
return feedback
class BuyerCriticAgent(DialogAgent):
def __init__(self,
initial_dialog_history=None,
agent_type="critic",
engine="gpt-3.5-turbo",
api_key="",
):
"""Initialize the buyer critic agent"""
super().__init__(initial_dialog_history=initial_dialog_history,
agent_type=agent_type,
engine=engine,
api_key=api_key
)
print("Initializing buyer critic with engine %s" % self.engine)
return
def criticize(self, buyer_history):
prompt = "\n"
for d in buyer_history[1:]:
if(d["role"] == "user"):
prompt += "seller: %s\n" % d["content"]
elif(d["role"] == "assistant"):
prompt += "buyer: %s\n" % d["content"]
prompt += "\n\nNow give three suggestions to improve the buyer's negotiation strategy: "
messages = deepcopy(self.dialog_history)
messages[-1]['content'] += "\n\n" + prompt
response = self.call_engine(messages)
feedback = response['content'].replace('\n\n', '\n')
return feedback