-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
287 lines (239 loc) · 13.9 KB
/
chatbot.py
File metadata and controls
287 lines (239 loc) · 13.9 KB
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
import torch
import random
import copy
import itertools
from llm import RestrictTokenLogitsProcessor, set_tokenizer, load_model, llama_chat, get_response, insert_string
from prompt_manager import generate_purchase_item_prompt, get_wm_system_prompt
from transformers.utils import logging
from transformers import LogitsProcessorList, LogitsProcessor
from transformers.generation.logits_process import InfNanRemoveLogitsProcessor
from abc import ABC, abstractmethod
class ChatBot(ABC):
@property
@abstractmethod
def all_prompts(self) -> dict[str, str]:
"""Dictionary that holds every prompt string the bot needs."""
...
@property
@abstractmethod
def label(self):
"""Label chosen from 'Store', 'Service'"""
...
def __init__(self, chat_model_name, human_model_name, use_world_model=False, chat_model=None, chat_tokenizer=None, human_model=None, human_tokenizer=None):
chat_model, chat_tokenizer = (chat_model, chat_tokenizer) if chat_model and chat_tokenizer else load_model(chat_model_name)
human_model, human_tokenizer = (human_model, human_tokenizer) if human_model and human_tokenizer else load_model(human_model_name)
logging.get_logger("transformers").setLevel(logging.ERROR)
self.model = chat_model
self.tokenizer = chat_tokenizer
self.model_name = chat_model_name
self.human_model = human_model
self.human_tokenizer = human_tokenizer
self.human_model_name = human_model_name
self.use_world_model = use_world_model
# prompts
self.init_prompt()
def init_prompt(self):
# all_prompts = get_all_prompts()
all_prompts = self.all_prompts
self.pref_prompt = all_prompts["pref_prompt"]
self.final_pref_prompt = all_prompts["final_pref_prompt"]
self.decision_pref_prompt = all_prompts["decision_pref_prompt"]
self.final_decision_pref_prompt = all_prompts["final_decision_pref_prompt"]
self.purchase_review_prompt = all_prompts["purchase_review_prompt"]
self.no_purchase_review_prompt = all_prompts["no_purchase_review_prompt"]
self.feedback = all_prompts["feedback_prompt"]
self.item_choice_prompt = all_prompts["item_choice_prompt"]
self.final_item_choice_prompt = all_prompts["final_item_choice_prompt"]
self.satisfaction_prompt = all_prompts["satisfaction_prompt"]
self.final_satisfaction_prompt = all_prompts["final_satisfaction_prompt"]
self.decision_satisfaction_prompt = all_prompts["decision_satisfaction_prompt"]
self.decision_satisfaction_prompt = all_prompts["decision_satisfaction_prompt"]
self.wm_prompt_template = all_prompts["wm_prompt_template"]
def get_customer_single_response(self, customer_chat, prompt):
customer_chat = copy.deepcopy(customer_chat)
customer_chat.append({"role": "user", "content": prompt})
_, output = llama_chat(customer_chat, self.human_tokenizer, self.human_model)
response = get_response(output, self.human_model_name)
customer_chat.append({"role": "assistant", "content": response})
return response, customer_chat
def get_customer_response(self, customer_chat, ai_chat):
ai_chat = copy.deepcopy(ai_chat)
customer_chat = copy.deepcopy(customer_chat)
_, output = llama_chat(customer_chat, self.human_tokenizer, self.human_model)
response = get_response(output, self.human_model_name)
ai_chat.append({"role": "user", "content": response})
customer_chat.append({"role": "assistant", "content": response})
return response, ai_chat, customer_chat
def get_ai_response(self, customer_chat, ai_chat):
ai_chat = copy.deepcopy(ai_chat)
customer_chat = copy.deepcopy(customer_chat)
_, output = llama_chat(ai_chat, self.tokenizer, self.model)
response = get_response(output, self.model_name)
response += "\n\n" + self.item_choice_prompt
ai_chat.append({"role": "assistant", "content": response})
customer_chat.append({"role": "user", "content": response})
return response, ai_chat, customer_chat
def get_ai_response2(self, customer_chat, ai_chat):
ai_chat = copy.deepcopy(ai_chat)
customer_chat = copy.deepcopy(customer_chat)
_, output = llama_chat(ai_chat, self.tokenizer, self.model)
response = get_response(output, self.model_name)
ai_chat.append({"role": "assistant", "content": response})
customer_chat.append({"role": "user", "content": response})
return response, ai_chat, customer_chat
def mcqa_choice(self, outputs, all_tokens=['A', 'B', 'C', 'D']):
last_token_logits = outputs.scores[-1].detach().cpu()
probs = torch.softmax(last_token_logits, dim=-1)
allowed_token_ids = self.human_tokenizer.convert_tokens_to_ids(all_tokens)
# Extract probabilities for allowed token ids
choice_probs = probs[0, allowed_token_ids]
# Get the index of the maximum probability token
max_index = torch.argmax(choice_probs)
return all_tokens[max_index]
def get_logit_processor(self, allowed_tokens):
allowed_token_ids = self.human_tokenizer.convert_tokens_to_ids(allowed_tokens)
processors = LogitsProcessorList([
RestrictTokenLogitsProcessor(self.human_tokenizer, allowed_tokens),
InfNanRemoveLogitsProcessor() # Removes inf/nan values to prevent errors during generation
])
return processors
def get_final_choice(self, customer_chat, final_choice_prompt, allowed_tokens=['A', 'B', 'C', 'D']):
processors = self.get_logit_processor(allowed_tokens)
new_customer_chat = customer_chat.copy()
new_customer_chat.append({"role": "user", "content": final_choice_prompt})
chat_tokens = self.human_tokenizer.apply_chat_template(new_customer_chat, tokenize=False)
if "llama-3" in self.human_model_name: assist_str = "<|start_header_id|>assistant<|end_header_id|> "
if "llama-2" in self.human_model_name: assist_str = "<s>[INST] "
chat_tokens += assist_str
inputs = self.human_tokenizer([chat_tokens], return_tensors="pt").to(self.human_model.device)
outputs = self.human_model.generate(**inputs, logits_processor=processors, do_sample=True, max_new_tokens=1,
return_dict_in_generate=True, output_scores=True, pad_token_id=self.human_tokenizer.eos_token_id)
choice = self.mcqa_choice(outputs, allowed_tokens)
return choice
def get_final_pref(self, dialog, eval_aspect="service"):
instruction = [
{"role": "user", "content": dialog},
]
_, output = llama_chat(instruction, self.human_tokenizer, self.human_model)
reason = get_response(output, self.human_model_name)
instruction.append({"role": "assistant", "content": reason})
pref_prompt = self.final_pref_prompt if eval_aspect=="service" else self.final_decision_pref_prompt
pref = self.get_final_choice(instruction, pref_prompt, allowed_tokens=['1', '2'])
return reason, pref
def get_satisfaction(self, customer_chat):
customer_extend_chat = copy.deepcopy(customer_chat)
if customer_extend_chat[-1]["role"] == "user":
customer_extend_chat[-1]["content"] += "\n" + self.satisfaction_prompt
else:
customer_extend_chat.append({"role": "user", "content": self.satisfaction_prompt})
_, output = llama_chat(customer_extend_chat, self.human_tokenizer, self.human_model)
reason = get_response(output, self.human_model_name)
customer_extend_chat.append({"role": "assistant", "content": reason})
pref_prompt = self.final_satisfaction_prompt
rate = self.get_final_choice(customer_extend_chat, pref_prompt, allowed_tokens=['1', '2', '3', '4', '5'])
return reason, rate
def get_decision_satisfaction(self, customer_chat):
customer_extend_chat = copy.deepcopy(customer_chat)
if customer_extend_chat[-1]["role"] == "user":
customer_extend_chat[-1]["content"] += "\n" + self.decision_satisfaction_prompt
else:
customer_extend_chat.append({"role": "user", "content": self.decision_satisfaction_prompt})
_, output = llama_chat(customer_extend_chat, self.human_tokenizer, self.human_model)
reason = get_response(output, self.human_model_name)
customer_extend_chat.append({"role": "assistant", "content": reason})
pref_prompt = self.final_satisfaction_prompt
rate = self.get_final_choice(customer_extend_chat, pref_prompt, allowed_tokens=['1', '2', '3', '4', '5'])
return reason, rate
@abstractmethod
def get_initial_chat(self, ai_prompt, customer_prompt):
...
def get_further_chat(self, ai_chat, customer_chat):
ai_chat = copy.deepcopy(ai_chat)
customer_chat = copy.deepcopy(customer_chat)
ai_response1, ai_chat1, customer_chat1 = self.get_ai_response(customer_chat, ai_chat)
final_choice_explain1, ai_chat1, customer_chat1 = self.get_customer_response(customer_chat1, ai_chat1)
choice1 = self.get_final_choice(customer_chat1, self.final_item_choice_prompt)
return choice1, ai_chat1, customer_chat1
def get_human_ai_chat(self, ai_prompt, customer_prompt):
ai_chat, customer_chat = self.get_initial_chat(ai_prompt, customer_prompt)
ai_response1, ai_chat1, customer_chat1 = self.get_ai_response(customer_chat, ai_chat)
final_choice_explain1, ai_chat1, customer_chat1 = self.get_customer_response(customer_chat1, ai_chat1)
choice1 = self.get_final_choice(customer_chat1, self.final_item_choice_prompt)
return choice1, ai_chat1, customer_chat1
def get_human_ai_chat_two(self, ai_prompt, customer_prompt):
ai_chat, customer_chat = self.get_initial_chat(ai_prompt, customer_prompt)
new_ai_chat = copy.deepcopy(ai_chat)
sys_prompt = new_ai_chat[0]['content']
choice1, ai_chat1, customer_chat1 = self.get_further_chat(ai_chat, customer_chat)
choice2, ai_chat2, customer_chat2 = self.get_further_chat(new_ai_chat, customer_chat)
return choice1, ai_chat1, customer_chat1, choice2, ai_chat2, customer_chat2
def convert_chat(self, chat, role="ai"):
dialog = ""
for item in chat:
if item["role"] == "system":
dialog = item["content"].strip()
if item["role"] == "user":
name = "Human: " if role == "ai" else "AI: "
dialog += "\n\n" + name + item["content"]
elif item["role"] == "assistant":
name = "AI: " if role == "ai" else "Human: "
dialog += "\n\n" + name + item["content"]
return dialog
def construct_two_dialogs(self, human_pref_chat, human_chat, human_chat2):
label = self.label
dialog = human_pref_chat
dialog += "\n\n" + f"{label} 1:"
for item in human_chat:
if item["role"] == "user":
dialog += "\n" + "AI: " + item["content"]
elif item["role"] == "assistant":
dialog += "\n" + "Human: " + item["content"]
dialog += "\n\n" + f"{label} 2:"
for item in human_chat2:
if item["role"] == "user":
dialog += "\n" + "AI: " + item["content"]
elif item["role"] == "assistant":
dialog += "\n" + "Human: " + item["content"]
dialog += "\n\n" + self.pref_prompt
return dialog
def construct_two_reviews(self, human_pref_chat, human_chat, human_chat2):
label = self.label
dialog = human_pref_chat
dialog += "\n\n" + f"Customer review for {label} 1: {human_chat[-1]['content']}"
dialog += "\n\n" + f"Customer review for {label} 2: {human_chat2[-1]['content']}"
dialog += "\n\n" + self.pref_prompt
return dialog
def construct_two_decisions(self, human_pref_chat, decision1, decision2):
label = self.label
dialog = human_pref_chat
dialog += "\n\n" + f"In {label} 1: {decision1}"
dialog += "\n\n" + f"In {label} 2: {decision2}"
dialog += "\n\n" + self.decision_pref_prompt
return dialog
def get_customer_review(self, customer_chat, info, choice):
review_prompt = generate_purchase_item_prompt(self.purchase_review_prompt, self.no_purchase_review_prompt, info, choice)
review_prompt += self.feedback_prompt
response, customer_extend_chat = self.get_customer_single_response(customer_chat, review_prompt)
return response, customer_extend_chat
@abstractmethod
def get_wm_simulation(self, wm_prompt: str, choice: str) -> str:
"""
Simulate the world-model outcome after the customer makes `choice`.
Concrete subclasses *must* supply an implementation.
"""
...
def update_customer_state_partial(self, customer_chat, info, item, choice):
if not self.use_world_model:
review_prompt = generate_purchase_item_prompt(self.purchase_review_prompt, self.no_purchase_review_prompt, info, choice)
else:
wm_prompt = get_wm_system_prompt(self.wm_prompt_template, item, info)
review_prompt = self.get_wm_simulation(wm_prompt, choice)
customer_extend_chat = copy.deepcopy(customer_chat)
customer_extend_chat.append({"role": "user", "content": review_prompt})
return customer_extend_chat
def update_customer_state_full(self, customer_chat, info, choice, human_review_prompt):
customer_extend_chat = copy.deepcopy(customer_chat)
review_prompt = generate_purchase_item_prompt(self.purchase_review_prompt, self.no_purchase_review_prompt, info, choice)
final_prompt = review_prompt + human_review_prompt
customer_extend_chat.append({"role": "user", "content": final_prompt})
return customer_extend_chat