-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
299 lines (234 loc) · 12.1 KB
/
chat.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
import os
import openai
from abc import ABC, abstractmethod
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
# TODO: Give account number as digits instead of the full number
# TODO: Prompt engineering to be as direct as possible
# TODO: It should be a lot more direct
class ContextManager():
def __init__(self):
self.context = []
def load_from_file(self, file_path):
with open(file_path, 'r') as file_handler:
text = file_handler.read()
self.context.append(text)
def load_from_directory(self, directory):
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path) and file_path.endswith('.txt'):
self.load_from_file(file_path)
def load_from_database(self):
pass
@staticmethod
def generate_questions_from_task(task, model="gpt-3.5-turbo"):
prompt = f"""Given the context of {task}, what are some possible personal questions,
such as date of birth, account number, etc. that the customer service agent might ask the user?
Phrase questions as key words, such as "Date of Birth". Give multiple questions seperated by a new line."""
messages = [{"role": "user", "content": prompt}]
completion = openai.ChatCompletion.create(
model=model,
messages=messages
)
questions = completion.choices[0].message.content.split("\n")
questions = [q.strip() for q in questions]
# for q in questions:
# context.append((q, input(f"Enter the answer to the question - {q}: ")))
return questions
def test(agent):
with open("transcript.txt", "r") as file_handler:
transcript = file_handler.read().splitlines()
for customer_support_response in transcript:
print("Customer support:", customer_support_response)
print("Agent response:", agent(customer_support_response))
# TODO: Chat engine should keep track of both the context and the dialogue
# TODO: The dialogue is important for the classification task
# class OpenAIChatEngine(ABC):
# def __init__(self, model, request_limit=1):
# self.model = model
# self.requests_till_error = request_limit
# def set_agent_description(self, agent_description=""):
# if len(agent_description) > 0:
# return [{"role": "system", "content": agent_description}]
# def __call__(self, messages, counter=0):
# try:
# completion = openai.ChatCompletion.create(
# model=self.model,
# messages=messages
# )
# return completion.choices[0].message
# except:
# if counter < self.requests_till_error:
# return self.__call__(messages, counter+1)
# else:
# return "I'm sorry, I'm having trouble understanding you. Could you rephrase your request?"
class Agent():
def __init__(self, task, recipient, efficient_messages=True):
self.task = task
self.recipient = recipient
# Setting to understand how to set up the messages
self.efficient_messages = efficient_messages
# Setup context manager's default value
self.context_manager = None
# Setup chat engine
self.model = "gpt-3.5-turbo"
agent_description_prompt = self.generate_agent_description(task, recipient)
self.agent_description = {"role": "system", "content": 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, task, recipient):
prompt = f"""
You're imitating a human that is trying to {task} with {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. Pretend that you're speeking on the phone, so if you need to say any numbers, write them as digits with spaces in between. Like 5032 should be 5 0 3 2.
You could use filler words like 'um' and 'uh' to sound more human. To end the call, just return 'bye'. For information you are unsure about, return "/user <question>".
"""
if len(self.context_manager.context) > 0:
prompt += "\nHere is some information about you:"
for c in self.context_manager.context:
prompt += f"\n{c}"
return prompt
def connect_context(self, context_manager : ContextManager):
self.context_manager = context_manager
def prompt_enhance_with_context(self):
if self.context_manager:
context = "Here's information about the human you're imitating, you can use this to help you respond:"
for c in self.context_manager.context:
context += f"\n{c}"
return context
else:
return ""
def engineer_prompt(self, customer_service_response):
context = self.prompt_enhance_with_context()
ending_prompt = "If the call is over and you've resolved your issue, you can return 'bye' to end the call. If you are unsure about a question and need more information, you can return '/user help' to get assistance."
formatting_prompt = "Pretend that you're speeking on the phone, so if you need to say any numbers, write them as digits with spaces in between. Like 5032 should be 5 0 3 2."
customer_service_prompt = f"Customer Service Agent: {customer_service_response}"
agent_response_prompt = f"Your Response:"
components = [context, ending_prompt, formatting_prompt, customer_service_prompt, agent_response_prompt]
return "\n".join(components)
def add(self, message, role='user'):
self.messages.append({'role': role, 'content': message})
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 generate_response(self, customer_service_response):
# new_dialogue_chain = {"role": "user", "content": customer_service_response}
# new_messages_chain = {"role": "user", "content": self.engineer_prompt(customer_service_response)}
# self.dialogue_history.append(new_dialogue_chain)
# self.messages.append(new_messages_chain)
# if self.efficient_messages:
# messages = [self.agent_description] + self.dialogue_history[:-1] + [new_messages_chain]
# else:
# messages = self.messages
# completion = openai.ChatCompletion.create(
# model=self.model,
# messages=messages
# )
# print(completion.choices[0])
# response = completion.choices[0].message
def __call__(self, customer_service_response):
self.add(customer_service_response)
agent_response = self.generate()
self.add(agent_response, role="assistant")
def start_conversation(self):
return self("<silence from the customer service agent>")
class Interaction():
def __init__(self, task, context_directory):
self.task = task
self.context_directory = context_directory
self.context = self.load_context(self.context_directory)
print(self.context)
self.messages = []
# OpenAI Settings
self.model = "gpt-3.5-turbo"
def __repr__(self):
return f"{self.role}: {self.content}"
@staticmethod
def load_context(directory):
"""Loads the context from the specified directory.
Args:
directory (str): The path to the directory containing text files.
Returns:
list of str: A list of strings containing the content of all the text files in the directory.
"""
context = []
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path) and file_path.endswith('.txt'):
with open(file_path, 'r') as file_handler:
text = file_handler.read()
context.append(text)
return context
def set_agent_config(self):
self.agent_description = 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.
"""
self.messages = []
self.messages.append({"role": "system", "content": self.agent_description})
def __call__(self, response=None):
# Initialize the agent
self.set_agent_config()
def step(customer_service_response):
if customer_service_response == "":
self.messages.append({"role": "user", "content": "Respond imagining you're a human. Imagine you're on a call with a customer service agent. Describe your problem and what you need help with. Be succint."})
else:
self.messages.append({"role": "user", "content": self.engineer_prompt(customer_service_response)})
response = self.chat()
print("BlueberryAI: ", response.content)
self.messages.append(response)
return response.content
# While the conversation is still continuing
if response is None:
while True:
customer_service_response = input("Customer Service Agent: ")
step(customer_service_response)
else:
return step(response)
def engineer_prompt(self, customer_service_response):
"""Generates the prompt for the engineer to respond to.
"""
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:
{self.context}
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 5 0 3 2.
Customer Service Agent:
{customer_service_response}
Your Response:
"""
return prompt
def chat(self):
completion = openai.ChatCompletion.create(
model=self.model,
messages=self.messages
)
return completion.choices[0].message
# interaction = Interaction(task="create a new account", context_directory="twilio/")
# interaction.recipient = "People's Gas"
# interaction()
# data = SimpleDirectoryReader(input_dir="data/ekrem/").load_data()
# print(data)
# index = VectorStoreIndex.from_documents(data)
# print(index)
# chat_engine = index.as_chat_engine(chat_mode='react', verbose=True)
# task_explanation = "Hey I want to create a new account with People's Gas company."
# while True:
# agent_input = input("Enter customer service request: ")
# prompt = f"You're talking to a customer service agent. Your task is to {task_explanation}. Use your context to return the appropriate response.\n\Customer Service Agent: {agent_input}\nYour Response:"
# response = chat_engine.chat(prompt)