forked from theSohamTUmbare/RoadmapMakerAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompts.py
More file actions
275 lines (226 loc) · 14.3 KB
/
prompts.py
File metadata and controls
275 lines (226 loc) · 14.3 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
"""
In this file we are writing the prompts to pass them into the llm model to retrive the relavant data from the data
we have in the desired format. Here we first create the prompt for getting the most relavant feild for the student
considering the info provided by the student as well as the month of the roadmap and the previous activities that
student have done in the earilier month in the roadmap
"""
from llm import llm
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
EPSILON_DECAY = 0.9
EPSILON_INCREMENT = 0.4
class Prompt_Generator:
def __init__(self, embedder, student_input, files_dict, month, year):
self.student_input = student_input
self.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
self.No_of_events = 0
self.events = ""
self.year = year
self.files_dict = files_dict
self.files = list(files_dict.keys())
self.current_month = month - 1
self.current_date = f'{self.months[self.current_month]} {self.year}'
self.starting_date = f'{self.months[month - 1]} {year}'
self.No_to_text = {
0 : 'first',
1 : 'second',
2 : 'third',
3 : 'fourth',
4 : 'fifth',
5 : 'sixth',
6 : 'seventh',
7 : 'eighth',
8 : 'ninth',
9 : 'tenth',
10 : 'eleventh',
11 : 'twelfth',
12 : 'thirteenth',
13 : 'fourteenth',
14 : 'fifteenth',
15 : 'sixteenth',
16 : 'seventeenth',
17 : 'eighteenth',
18 : 'nineteenth',
19 : 'twentieth'
}
self.embedder = embedder
self.device = self.embedder.device
self.done_events = {}
self.best_events = []
def query_embd(self, query):
"""
Embeds the given query using the provided model.
"""
query_inputs = self.embedder.question_tokenizer(query, return_tensors="pt", max_length=512, truncation=True).to(self.device)
query_embedding = self.embedder.question_encoder(**query_inputs).pooler_output
return query_embedding
def increment_month(self):
if self.months[self.current_month] == 'December':
self.year = str(int(self.year) + 1)
self.current_month = 0
else:
self.current_month += 1
self.current_date = f'{self.months[self.current_month]} {self.year}'
def similarity_check(self, query, context_embeddings):
"""
Computes cosine similarity between query and context embeddings.
"""
similarities = cosine_similarity(
query.detach().cpu().numpy(),
context_embeddings.detach().cpu().numpy()
)
# print("Similarities:", similarities)
return similarities
def generate_file_prompt(self):
epsilon = 1 if self.No_of_events == 0 else 0.6
a = np.random.rand()
if a < epsilon:
original_query_for_file = f"""
You are the professional roadmap guide, according to the given student info as follows {self.student_input}
you are generating a roadmap for the student. Now you are providing the task to the student for the
{self.No_to_text[self.No_of_events]} month of their road map you will suggest him the task for this month
while considering his interests goals and events he has already done in previous months. The student has
done {self.No_of_events} events till now which are: {self.events}
Now please suggest the student a sector among these 16 sectors {self.files}
the student should choose for the current month's task according to his current progress as per the roadmap
and according to his interests and skills and long-term goals. Your answer should only contain the sector name and nothing else.
Do not say anything else.
"""
print("Same: ", end='')
epsilon -= EPSILON_DECAY * epsilon
else:
print("Diff: ", end='')
original_query_for_file = f"""
You are the professional roadmap guide, according to the given student info as follows {self.student_input}
you are generating a roadmap for the student. Now you are providing the task to the student for the
{self.No_to_text[self.No_of_events]} month of their road map you will suggest him the task for this month
while considering his interests goals and events he has already done in previous months. The student has
done {self.No_of_events} events till now which are: {self.events}
Now please suggest the student a sector among these 16 sectors {self.files} other than {self.file_header}
the student should choose for the current month's task according to his current progress as per the roadmap
and according to his interests and skills and long-term goals. Your answer should only contain the sector name and nothing else.
Do not say anything else.
"""
epsilon += EPSILON_INCREMENT * epsilon
prompt = """
You are a knowledgeable professor who is very knowledgeable on matters of students' careers. You will help your student choose the best sector for them while deciding their roadmap. You will just tell the student the name of the sector most suitable for them.
"""
try:
response = llm(original_query_for_file, prompt)
except Exception as e:
print(f"Error in generating LLM response: {e}")
return None
response_embedding = self.query_embd(response)
# print("This is the response we get:- ",response)
similiarites = self.similarity_check(response_embedding, self.embedder.filenames_embeddings)
self.file_header = self.files[np.argmax(similiarites)]
print("This is the file_header afterr similarity check:- ", self.file_header)
def generate_event_prompt(self):
query_for_llm = f"""
You are the professional roadmap guide, according to the given student info {self.student_input} on {self.starting_date} you are generating a
one-year roadmap for the student. The roadmap began at {self.starting_date} and now is {self.months[self.current_month]}, Now you have to give him a perfect hypothetical event for the student for their
{self.No_to_text[self.No_of_events]} month of their road map you will suggest him the most suitable event for this month while considering the
events he has already done in previous months his interests and his skills and the hypothetical event dates should be in the current month of the event. Till now the student has already done {self.No_of_events} events till now which are:
{self.events} The event you decide for him should be from {self.file_header} setor . Make sure you don't include the events he already did and don't say anything extra other than event fields.
"""
example_answers = []
prompt = """You are a helpful professional roadmap guide.
Provide an example tasks to the given sector that you will suggest from from the information in the question."""
for i in range(5):
example_answers.append(self.query_embd(llm(query_for_llm, prompt)))
query_embedding = self.query_embd(query_for_llm)
similarities = np.array([(np.zeros(self.embedder.context_embeddings[self.file_header].size(0)))])
print(self.file_header, len(similarities[0]))
for i in range(5):
similarities += self.similarity_check(example_answers[i], self.embedder.context_embeddings[self.file_header])
if self.file_header not in self.done_events.keys():
self.done_events[self.file_header] = []
for event in self.done_events[self.file_header]:
print("Already Done" , self.files_dict[self.file_header].iloc[event])
similarities[0][event] = -1
most_relevant_idx = np.argmax(similarities)
self.done_events[self.file_header] += [most_relevant_idx]
df = self.files_dict[self.file_header]
best_event = df.iloc[most_relevant_idx]
self.increment_month()
print(best_event)
return best_event
def generate_prompt(self, count):
for i in range(count):
self.generate_file_prompt()
best_event = self.generate_event_prompt()
# columns = ['Title', 'Description', 'Details', 'Highlights', 'Cost' 'Website', 'website']
event = ''
event += f'On {self.current_date} the students does his {i + 1}th event from the {self.file_header} sector It\'s details are: \n '
for idx, (column, value) in enumerate(best_event.items()):
event += f"{chr(idx + 97)}) The {str(column)} of the event is {str(value)}\n"
self.best_events.append(best_event)
self.events += event
self.No_of_events += 1
return self.events
class RoadMapGenerator:
def __init__(self, events, student_input, no_of_events):
self.events = events
self.student_input = student_input
self.no_of_events = no_of_events
# explaining required format for the roadmap for the LLM model in detail
def generate_roadmap(self):
system_prompt = """
You are a professional Career and health advisor and you care for students mental strengh but will push them to their limits to achieve there goals
"""
user_prompt = f"""
So we created a roadmap of events for this student {self.student_input} where he does the following events {self.events} monthwise
So using this information that you are given i want you to create a perfect roadmap for the student while considering his mental state and his goals. You can suggest some small extra things that he may need to do like learning some language ,
going into some social media , doing exercises , taking rests etc and also mention some guidelines to follow while doing these events. Some strict rules he should follow to build self-discipline.
Roadmap output Guidelines:
Personalized advice to help the student maintain a balanced lifestyle, avoid burnout, and stay disciplined.
Strategies for self-care, mental health, and self-discipline that are relevant to the student's goals and personality.
Main Checklist for Applications and Preparations:
Clearly outline what the student needs to accomplish for their upcoming applications and competitions. Include any specific requirements, deadlines, or preparation tips.
Monthly Tasks:
For each month, provide a detailed breakdown of activities the student should focus on. This should include:
Event Preparation: Specific study or skill-building activities needed for upcoming events or competitions. For example, if participating in the American Collegiate Programming Contest (ACPC) in May, outline a study plan focusing on algorithms and problem-solving skills.
Life Balance: Include activities that promote a healthy work-life balance, such as exercise routines, meditation, social activities, and relaxation techniques.
Networking and Learning: Suggest networking opportunities with professionals in the student's field of interest and ways to stay updated with the latest developments (e.g., following relevant news, attending webinars).
Self-Reflection and Adaptation: Encourage regular reflection on what was learned, what can be improved, and how to adapt strategies moving forward.
Extra Tips and Activities:
Suggest additional activities that the student could engage in to enhance their skills, such as learning a new language, engaging in online courses, or joining study groups.
The ouput format should only contain the roadmap and nothing else. and it shold have a format like this and do not use this as an example just see the format
'Name's Roadmap
Guidelines -
1. Before you begin going through your roadmap, keep the following in your mind:
....
.
.
.
.
Main checklist to complete for application -
....
Monthly Tasks -
April-
..
website name (for each month)
May-
.
.
.website name (for each month)
.
.
.
.
December-
..
January -
.
.
.
April
'
You must generate the roadmap for each month that should include what events along with the event's website he should attend what aspects of his life he should take care of and other imp things he can do that month on his own according to the events
he is attending next month preparing for upcoming events and his goals.
You must Whenever you are mentioning an event mention the event's website also for each month
YOU must Do this for {self.no_of_events} months using the data on the events we provided
The student should know what specificaly what to do for each month
"""
response = llm(user_prompt, system_prompt, max_tokens=10000)
return response