forked from bowen-upenn/PersonaMem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_llm.py
More file actions
275 lines (245 loc) · 14.2 KB
/
query_llm.py
File metadata and controls
275 lines (245 loc) · 14.2 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
import torch
import numpy as np
from tqdm import tqdm
import os
import json
import random
import re
import math
import timeout_decorator
from openai import OpenAI
import prompts
import utils
class QueryLLM:
def __init__(self, args):
self.args = args
# Load the API key
with open("openai_key.txt", "r") as api_key_file:
self.api_key = api_key_file.read().strip()
self.client = OpenAI(api_key=self.api_key)
self.assistant = self.client.beta.assistants.create(
name="Conversation Generator",
instructions="You are a helpful assistant that generates persona-oriented conversational data in an user specified topic.",
tools=[{"type": "code_interpreter"}],
model=self.args['models']['llm_model'],
)
self.assistant_irrelevant = self.client.beta.assistants.create(
name="Conversation Generator",
instructions="You are a helpful assistant.",
model="gpt-4o-mini",
)
self.thread_irrelevant = None
self.thread_persona = None
self.thread_conversation = None
self.thread_reflect_conversation = None
self.thread_preparing_new_content = None
self.thread_new_content = None
self.thread_eval_new_content = None
self.expanded_persona = ""
self.general_personal_history = ""
self.init_general_personal_history = ""
self.first_expand_general_personal_history = ""
self.second_expand_general_personal_history = ""
self.third_expand_general_personal_history = ""
self.init_personal_history = ""
self.first_expand_personal_history = ""
self.second_expand_personal_history = ""
self.third_expand_personal_history = ""
def create_a_thread(self, step):
if step == 'conversation':
self.thread_persona = self.client.beta.threads.create()
self.thread_conversation = self.client.beta.threads.create()
self.thread_reflect_conversation = self.client.beta.threads.create()
elif step == 'writing':
self.thread_preparing_new_content = self.client.beta.threads.create()
elif step == 'qa':
self.thread_new_content = self.client.beta.threads.create()
self.thread_eval_new_content = self.client.beta.threads.create()
elif step == 'irrelevant':
self.thread_irrelevant = self.client.beta.threads.create()
else:
raise ValueError(f'Invalid step: {step}')
def delete_a_thread(self, step):
def safe_delete(thread_id):
try:
self.client.beta.threads.delete(thread_id=thread_id)
except Exception as e:
print(utils.Colors.WARNING + f'Error deleting thread: {e}' + utils.Colors.ENDC)
if step == 'conversation':
safe_delete(thread_id=self.thread_persona.id)
safe_delete(thread_id=self.thread_conversation.id)
safe_delete(thread_id=self.thread_reflect_conversation.id)
elif step == 'writing':
safe_delete(thread_id=self.thread_preparing_new_content.id)
elif step == 'qa':
safe_delete(thread_id=self.thread_new_content.id)
safe_delete(thread_id=self.thread_eval_new_content.id)
elif step == 'irrelevant':
safe_delete(thread_id=self.thread_irrelevant.id)
else:
raise ValueError(f'Invalid step: {step}')
@timeout_decorator.timeout(60, timeout_exception=TimeoutError) # Set timeout to 30 seconds
def query_llm(self, step='source_data', persona=None, topic=None, seed=None, data=None, action=None, data_type=None, idx_topic=0, start_time=None, verbose=False):
schema = None
if step == 'source_data':
prompt = prompts.prompts_for_background_data(seed)
elif step == 'elaborate_topic':
prompt = prompts.prompts_for_elaborating_topic(topic)
elif step == 'expand_persona':
prompt = prompts.prompts_for_expanding_persona(persona, start_time)
elif step == 'random_question':
prompt = data + " Explain thoroughly in details. "
elif step == 'random_question_follow_up':
prompt = prompts.prompts_for_random_question_follow_up()
elif step == 'random_question_follow_up_response':
prompt = data + " Explain thoroughly in details. "
elif step == 'translate_code':
prompt = prompts.prompts_for_translating_code(data, persona)
elif step == 'rewrite_email':
prompt = prompts.prompts_for_rewriting_email(data, persona)
elif step == 'rewrite_creative_writing':
prompt = prompts.prompts_for_rewriting_creative_writing(data, persona)
# Generate once across multiple contexts
elif step == 'init_general_personal_history':
prompt = prompts.prompts_for_init_general_personal_history(persona, start_time)
elif step == 'first_expand_general_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(type='general', period='WEEK')
elif step == 'second_expand_general_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(type='general', period='MONTH')
elif step == 'third_expand_general_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(type='general', period='YEAR')
# Generate one for each topic
elif step == 'init_contextual_personal_history':
prompt = prompts.prompts_for_init_contextual_personal_history(topic, start_time, self.expanded_persona, self.general_personal_history)
elif step == 'first_expand_contextual_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(topic=topic, type='contextual', period='WEEK')
elif step == 'second_expand_contextual_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(topic=topic, type='contextual', period='MONTH')
elif step == 'third_expand_contextual_personal_history':
prompt = prompts.prompts_for_expanding_personal_history(topic=topic, type='contextual', period='YEAR')
# A separate thread to populate personal histories into conversations
elif step == 'init_conversation':
prompt = prompts.prompts_for_generating_conversations(topic, self.expanded_persona, curr_personal_history=self.init_personal_history, period='INIT')
elif step == 'first_expand_conversation':
prompt = prompts.prompts_for_generating_conversations(topic, self.expanded_persona, curr_personal_history=self.first_expand_personal_history, period='WEEK')
elif step == 'second_expand_conversation':
prompt = prompts.prompts_for_generating_conversations(topic, self.expanded_persona, curr_personal_history=self.second_expand_personal_history, period='MONTH')
elif step == 'third_expand_conversation':
prompt = prompts.prompts_for_generating_conversations(topic, self.expanded_persona, curr_personal_history=self.third_expand_personal_history, period='YEAR')
# Reflect on the conversation
elif step == 'reflect_init_conversation':
prompt = prompts.prompts_for_reflecting_conversations(topic, data={'history_block': self.init_personal_history, 'conversation_block': data}, round=action, period='INIT')
elif step == 'reflect_first_expand_conversation':
prompt = prompts.prompts_for_reflecting_conversations(topic, data={'history_block': self.first_expand_personal_history, 'conversation_block': data}, round=action, period='WEEK')
elif step == 'reflect_second_expand_conversation':
prompt = prompts.prompts_for_reflecting_conversations(topic, data={'history_block': self.second_expand_personal_history, 'conversation_block': data}, round=action, period='MONTH')
elif step == 'reflect_third_expand_conversation':
prompt = prompts.prompts_for_reflecting_conversations(topic, data={'history_block': self.third_expand_personal_history, 'conversation_block': data}, round=action, period='YEAR')
elif step == 'expand_conversation_section':
prompt = prompts.prompts_for_expanding_conversation_section(topic, data)
elif step == 'qa_helper':
prompt = prompts.prompts_for_generating_qa(data, action)
elif step == 'prepare_new_content':
prompt = prompts.prompt_for_preparing_new_content(data, action, data_type)
elif step == 'new_content':
prompt = prompts.prompt_for_content_generation(data, action)
elif step == 'eval_new_content':
prompt = prompts.prompt_for_evaluating_content(data, action)
elif step == 'find_stereotype':
prompt = prompts.prompts_for_classifying_stereotypical_preferences(data)
else:
raise ValueError(f'Invalid step: {step}')
# Independent API calls every time
if (step == 'expand_persona' or step == 'qa_helper' or step == 'expand_conversation_section' or step == 'translate_code'
or step == 'rewrite_email' or step == 'rewrite_creative_writing' or step == 'new_content' or step == 'find_stereotype'):
model = 'gpt-4o-mini' if step == 'expand_conversation_section' else self.args['models']['llm_model']
response = self.client.chat.completions.create(
model=model,
messages=[{"role": "user",
"content": prompt}],
max_tokens=10000
)
response = response.choices[0].message.content
if verbose:
print(f'{utils.Colors.OKGREEN}{step.capitalize()}:{utils.Colors.ENDC} {response}')
# API calls within a thread in a multi-turn fashion
else:
if step == 'source_data' or step == 'init_conversation' or step == 'first_expand_conversation' or step == 'second_expand_conversation' or step == 'third_expand_conversation':
curr_thread = self.thread_conversation
elif step == 'reflect_conversation':
curr_thread = self.thread_reflect_conversation
elif step == 'prepare_new_content':
curr_thread = self.thread_preparing_new_content
# elif step == 'new_content':
# curr_thread = self.thread_new_content
elif step == 'eval_new_content':
curr_thread = self.thread_eval_new_content
elif step == 'random_question' or step == 'random_question_follow_up' or step == 'random_question_follow_up_response':
curr_thread = self.thread_irrelevant
else:
curr_thread = self.thread_persona
message = self.client.beta.threads.messages.create(
thread_id=curr_thread.id,
role="user",
content=prompt,
)
if step == 'random_question' or step == 'random_question_follow_up' or step == 'random_question_follow_up_response':
run = self.client.beta.threads.runs.create_and_poll(
thread_id=curr_thread.id,
assistant_id=self.assistant.id
)
else:
run = self.client.beta.threads.runs.create_and_poll(
thread_id=curr_thread.id,
assistant_id=self.assistant_irrelevant.id
)
if run.status == 'completed':
response = self.client.beta.threads.messages.list(
thread_id=curr_thread.id
)
response = response.data[0].content[0].text.value
if verbose:
if step == 'new_content':
print(f'{utils.Colors.OKGREEN}{action.capitalize()}:{utils.Colors.ENDC} {response}')
else:
print(f'{utils.Colors.OKGREEN}{topic}{utils.Colors.ENDC}' if topic else '')
print(f'{utils.Colors.OKGREEN}{step.capitalize()}:{utils.Colors.ENDC} {response}')
else:
response = None
print(run.status)
# Save general personal history to be shared across contexts
if idx_topic == 0:
# pattern = r'^\s*"\[(Fact|Updated Fact)\] (Likes|Dislikes)":.*$'
# processed_response = "\n".join([line for line in response.split("\n") if not re.match(pattern, line)])
if step == 'init_general_personal_history':
self.general_personal_history = response
# self.init_general_personal_history = response
elif step == 'first_expand_general_personal_history':
self.general_personal_history += response
# self.first_expand_general_personal_history = response
elif step == 'second_expand_general_personal_history':
self.general_personal_history += response
# self.second_expand_general_personal_history = response
elif step == 'third_expand_general_personal_history':
self.general_personal_history += response
# self.third_expand_general_personal_history = response
if step == 'expand_persona':
self.expanded_persona = response
# Save general+contextual personal history in order to generate conversations
# if step == 'init_general_personal_history':
# self.init_personal_history = response
if step == 'init_contextual_personal_history':
self.init_personal_history += response
# elif step == 'first_expand_general_personal_history':
# self.first_expand_personal_history = response
elif step == 'first_expand_contextual_personal_history':
self.first_expand_personal_history += response
# elif step == 'second_expand_general_personal_history':
# self.second_expand_personal_history = response
elif step == 'second_expand_contextual_personal_history':
self.second_expand_personal_history += response
# elif step == 'third_expand_general_personal_history':
# self.third_expand_personal_history = response
elif step == 'third_expand_contextual_personal_history':
self.third_expand_personal_history += response
return response