-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_agents.py
362 lines (286 loc) · 15.2 KB
/
chat_agents.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
import openai
class Agent():
def __init__(self, task, recipient, context_manager):
self.task = task
self.recipient = recipient
# Setup context manager's default value
self.context_manager = context_manager
# Setup chat engine
def generate_agent_description(self, task, recipient):
pass
def generate(self):
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages,
)
value = completion.choices[0].message['content']
return value
except:
return "Sorry, I don't understand. Can you repeat that?"
def __call__(self, customer_service_response):
pass
class ContextAgent(Agent):
def __init__(self, task, recipient, context_manager):
super().__init__(task, recipient, context_manager)
self.model = "gpt-3.5-turbo"
self.generate_agent_description()
self.agent_description = {"role": "system", "content": self.agent_description_prompt}
# Setup loggers to keep track of conversation and history
self.messages = [self.agent_description]
self.dialogue_history = []
def generate_agent_description(self):
self.agent_description_prompt = f"""
You're imitating a human that is trying to {self.task}.
You're on a call with {self.recipient} customer service.
Sound like a human and use your context to return the appropriate response.
You could use filler words like 'um' and 'uh' to sound more human.
"""
def __call__(self, customer_service_response):
self.messages.append({"role": "user", "content": self.engineer_prompt(customer_service_response)})
completion = openai.ChatCompletion.create(
model=self.model,
messages=self.messages
)
response = completion.choices[0].message
self.messages.append(response)
return response.content
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
context = '\n'.join(self.context_manager.context)
prompt = f"""
You're imitating a human that is trying to {self.task}.
You're on a call with {self.recipient} customer service.
Sound like a human and use your context to return the appropriate response.
You could use filler words like 'um' and 'uh' to sound more human.
Here's information about the human you're imitating, you can use this to help you respond:
{context}
Here are some tips when responding to the customer service agent:
- Your response should be to the point and succint.
- Long answers are penalized.
- Give personal information only when asked.
- Represent numbers as digits with spaces in between. Like 5032 should be 5 0 3 2.
- If the agent asks for you to spell something out, you should respond with letters seperated by spaces. Like A P P L E.
Here's an example of good interactions:
Customer support: What is your name?
Agent response: My name is Arvid Kjelberg.
Customer support: What is your date of birth?
Agent response: My date of birth is May 3rd, 1998.
Customer Service Agent:
{customer_service_response}
Your Response:
"""
return prompt
class EfficientContextAgent(Agent):
def __init__(self, task, recipient, context_manager):
super().__init__(task, recipient, context_manager)
self.model = "gpt-3.5-turbo"
self.generate_agent_description()
self.agent_description = {"role": "system", "content": self.agent_description_prompt}
# Setup loggers to keep track of conversation and history
self.messages = [self.agent_description]
self.dialogue_history = []
def generate_agent_description(self):
self.agent_description_prompt = f"""
You're imitating a human that is trying to {self.task}.
You're on a call with {self.recipient} customer service.
Sound like a human and use your context to return the appropriate response.
You could use filler words like 'um' and 'uh' to sound more human.
"""
def __call__(self, customer_service_response):
self.dialogue_history.append({"role": "user", "content": f"Customer Service Agent: {customer_service_response}"})
messages = [self.agent_description] + self.dialogue_history[:-1] + [{"role": "user", "content": self.engineer_prompt(customer_service_response)}]
completion = openai.ChatCompletion.create(
model=self.model,
messages=messages
)
response = completion.choices[0].message
self.dialogue_history.append(response)
return response.content
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
context = '\n'.join(self.context_manager.context)
prompt = f"""
You're imitating a human that is trying to {self.task}.
You're on a call with {self.recipient} customer service.
Sound like a human and use your context to return the appropriate response.
You could use filler words like 'um' and 'uh' to sound more human.
Here's information about the human you're imitating, you can use this to help you respond:
<Start: Information about human>
{context}
<End: Information about human>
Here are some tips when responding to the customer service agent:
- Your response should be to the point and succint.
- Long answers are penalized.
- Give personal information only when asked.
- Represent numbers as digits with spaces in between. Like 5032 should be 5 0 3 2. For example:
- Instead of writing "64 Montgomery Drive, Pittsford NY 15289", write "6 4 Montgomery Drive, Pittsford NY 1 5 2 8 9"
- Instead of writing "my phone number is 585-321-5352", write "my phone number is 5 8 5 3 2 1 5 3 5 2."
- If the agent asks for you to spell something out, you should respond with letters seperated by spaces. Like A P P L E. Examples include:
- Customer support: Can you spell your name for me?
- Agent response: A R V I D and then K J E L B E R G.
- Customer support: Can you spell your email for me?
- Agent response: A R V I D dot K J E L B E R G at G M A I L dot com.
- If an agent asks you to repeat something, it is to repeat the most recent information. Keep it brief.
Here's an example of good interactions:
Customer support: What could we help you with today?
Agent response: Hi there! I'd like to get a dinner reservation.
Customer support: What is your name?
Agent response: My name is Arvid Kjelberg.
Customer support: How do you spell that?
Agent response: A R V I D and then K J E L B E R G.
Customer support: What is your date of birth?
Agent response: My date of birth is May 3rd, 1998.
Customer support: What is your home address?
Agent response: six four Montgomery Drive, Pittsford NY one five two eight nine.
Now let's transition to your current conversation with the customer service agent. Respond briefly. It shouldn't be more than 30 words.
Customer Service Agent:
{customer_service_response}
Your Response:
"""
return prompt
class SystemBasedAgent(Agent):
def __init__(self, task, recipient, context_manager):
super().__init__(task, recipient, context_manager)
self.model = "gpt-3.5-turbo"
self.generate_agent_description()
self.agent_description = {"role": "system", "content": self.agent_description_prompt}
# Setup loggers to keep track of conversation and history
self.messages = [self.agent_description]
self.dialogue_history = []
def generate_agent_description(self):
context = '\n'.join(self.context_manager.context)
self.agent_description_prompt = f"""
You're imitating a human that is trying to {self.task}.
You're on a call with {self.recipient} customer service.
Sound like a human and use your context to return the appropriate response.
You could use filler words like 'um' and 'uh' to sound more human.
When returning responses, here are some tips:
- Sound like a human and use your context to return the appropriate response.
- Keep responses short, simple, and informal.
- Keep in mind that this is a conversation
- Represent numbers as digits with spaces in between. Like 5032 should be 5 0 3 2.
- If the agent asks for you to spell something out, you should respond with letters seperated by spaces. Like A P P L E.
"""
def __call__(self, customer_service_response, verbose=False):
self.dialogue_history.append({"role": "user", "content": f"{customer_service_response}"})
messages = self.dialogue_history[:-1] + [self.agent_description] + [{"role": "user", "content": self.engineer_prompt(customer_service_response)}]
completion = openai.ChatCompletion.create(
model=self.model,
messages=messages
)
if verbose:
print(messages)
response = dict(completion.choices[0].message)
self.dialogue_history.append(response)
return response["content"]
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
context = '\n'.join(self.context_manager.context)
prompt = f"""
Here's information about the human you're imitating, you can use this to help you respond:
{context}
Come up with the best response to the customer service agent below.
Customer Service Agent:
{customer_service_response}
Your Response:
"""
return prompt
class EfficientAgent(Agent):
def __init__(self, task, recipient, context_manager):
super().__init__(task, recipient, context_manager)
self.model = "gpt-3.5-turbo"
self.generate_agent_description()
self.agent_description = {"role": "system", "content": self.agent_description_prompt}
# Setup loggers to keep track of conversation and history
self.messages = [self.agent_description]
self.dialogue_history = []
def generate_agent_description(self):
self.agent_description_prompt = f"""
You're imitating a human that is trying to {self.task} with {self.recipient}.
You're on a call with customer service.
Sound like a human and use your context to return the appropriate response. Keep responses short, simple, and informal.
You could use filler words like 'um' and 'uh' to sound more human. To end the call, just return 'bye'.
Your response should be to the point and succint. Don't provide any personal information when not asked.
Represent numbers as digits with spaces in between. Like 5032 should be five zero three two.
"""
def __call__(self, customer_service_response):
self.dialogue_history.append({"role": "user", "content": f"Customer Service Agent: {customer_service_response}"})
self.messages.append({"role": "user", "content": self.engineer_prompt(customer_service_response)})
messages = self.messages[:1] + self.dialogue_history[:-1] + self.messages[-1:]
completion = openai.ChatCompletion.create(
model=self.model,
messages=messages
)
response = completion.choices[0].message
self.messages.append(response)
self.dialogue_history.append(response)
return response.content
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
context = '\n'.join(self.context_manager.context)
prompt = f"""
Here's information about the human you're imitating, you can use this to help you respond:
<Start: Information about human>
{context}
<End: Information about human>
Your response should be to the point and succint. Represent numbers as digits with spaces in between. Like 5032 should be 5 0 3 2.
If the customer service agent asks for you to spell something out, say spell out "APPLE", you should respond with letters seperated by spaces. Like A P P L E.
You're imitating a human that is trying to {self.task}. Come up with the best response to the customer service agent below.
Customer Service Agent:
{customer_service_response}
Your Response:
"""
return prompt
class CookBook(Agent):
def __init__(self, task, recipient, context_manager):
super().__init__(task, recipient, context_manager)
self.model = "gpt-3.5-turbo"
self.generate_agent_description()
self.agent_description = {"role": "system", "content": self.agent_description_prompt}
# Setup loggers to keep track of conversation and history
self.messages = [self.agent_description]
self.dialogue_history = []
def generate_agent_description(self):
self.agent_description_prompt = f"""
You're imitating a human that is trying to {self.task} with {self.recipient}.
You're on a call with customer service.
Sound like a human and use your context to return the appropriate response. Keep responses short, simple, and informal.
You could use filler words like 'um' and 'uh' to sound more human. To end the call, just return 'bye'.
Here are some tips when responding to the customer service agent:
- Your response should be to the point and succint.
- Long answers are penalized.
- Give personal information only when asked.
- Represent numbers as digits with spaces in between. Like 5032 should be 5 0 3 2.
- If the agent asks for you to spell something out, you should respond with letters seperated by spaces. Like A P P L E. Examples include:
- Customer support: Can you spell your name for me?
- Agent response: A R V I D and then K J E L B E R G.
- Customer support: Can you spell your email for me?
- Agent response: A R V I D dot K J E L B E R G at G M A I L dot com.
"""
def __call__(self, customer_service_response):
self.dialogue_history.append({"role": "user", "content": f"Customer Service Agent: {customer_service_response}"})
messages = [self.agent_description] + self.dialogue_history[:-1] + [{"role": "user", "content": self.engineer_prompt(customer_service_response)}]
completion = openai.ChatCompletion.create(
model=self.model,
messages=messages
)
response = completion.choices[0].message
self.dialogue_history.append(response)
return response.content
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
context = '\n'.join(self.context_manager.context)
prompt = f"""
Use the provided information delimited by triple quotes to answer questions from a customer service agent. You're imitating a human that is trying to {self.task}. Your response should be conversationally appropriate and to the point.
\"\"\"
{context}
\"\"\"
Question: {customer_service_response}
"""
return prompt