-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent_handler.py
More file actions
297 lines (238 loc) · 10.5 KB
/
agent_handler.py
File metadata and controls
297 lines (238 loc) · 10.5 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
288
289
290
291
292
293
294
295
296
297
import yaml
import streamlit as st
from study_agents import StudyAgents
from rag_helper import RAGHelper
from typing import Optional, Dict, Any
class StudyAssistantHandler:
def __init__(self, topic, subject_category, knowledge_level, learning_goal,
time_available, learning_style, model_name="gpt-4o", provider="openai"):
"""
Initialize the study assistant handler.
Args:
topic (str): The specific topic or subject to study
subject_category (str): The category of the subject
knowledge_level (str): Student's current knowledge level
learning_goal (str): What the student wants to achieve
time_available (str): How much time the student has
learning_style (str): Student's preferred learning style
model_name (str): The model to use
provider (str): The AI provider ("openai" or "groq")
"""
self.topic = topic
self.subject_category = subject_category
self.knowledge_level = knowledge_level
self.learning_goal = learning_goal
self.time_available = time_available
self.learning_style = learning_style
self.model_name = model_name
self.provider = provider
self.agents = StudyAgents(
topic, subject_category, knowledge_level, learning_goal,
time_available, learning_style, model_name, provider
)
self.config = self._load_config()
self.rag_helper = None
def _load_config(self):
"""
Load the complete configuration from the YAML file.
Returns:
dict: A dictionary of configuration data
"""
with open("prompts.yaml", "r") as file:
return yaml.safe_load(file)
def _format_prompt(self, prompt_template, **kwargs):
"""
Format a prompt template with variables.
Args:
prompt_template (str): The prompt template to format
**kwargs: The variables to insert into the template
Returns:
str: The formatted prompt
"""
return prompt_template.format(**kwargs)
def analyze_student(self):
"""
Analyze the student's learning needs and create a profile.
Returns:
dict: Analysis results
"""
results = {}
with st.status("Analyzing your learning needs...", expanded=True) as status:
status.update(label="Creating student profile...", state="running")
analyzer = self.agents.student_analyzer_agent()
analysis_prompt = self._format_prompt(
self.config["prompts"]["student_analysis"]["base"],
topic=self.topic,
subject_category=self.subject_category,
knowledge_level=self.knowledge_level,
learning_goal=self.learning_goal,
time_available=self.time_available,
learning_style=self.learning_style
)
analysis_resp = analyzer.run(analysis_prompt, stream=False)
analysis_result = analysis_resp.content
results["analysis"] = analysis_result
st.session_state.student_analysis = analysis_result
status.update(label="Analysis complete!", state="complete")
return results
def create_roadmap(self, student_analysis: str):
"""
Create a personalized learning roadmap based on student analysis.
Args:
student_analysis (str): The student analysis from analyze_student()
Returns:
dict: Roadmap results
"""
results = {}
with st.status("Creating your personalized learning roadmap...", expanded=True) as status:
status.update(label="Designing learning path...", state="running")
roadmap_creator = self.agents.roadmap_creator_agent()
roadmap_prompt = self._format_prompt(
self.config["prompts"]["roadmap_creation"]["base"],
student_analysis=student_analysis,
topic=self.topic,
learning_goal=self.learning_goal,
time_available=self.time_available,
knowledge_level=self.knowledge_level
)
roadmap_resp = roadmap_creator.run(roadmap_prompt, stream=False)
roadmap_result = roadmap_resp.content
results["roadmap"] = roadmap_result
st.session_state.learning_roadmap = roadmap_result
status.update(label="Roadmap created!", state="complete")
return results
def find_resources(self):
"""
Find and recommend learning resources for the topic.
Returns:
dict: Resource recommendations
"""
results = {}
with st.status("Finding learning resources...", expanded=True) as status:
status.update(label="Searching for resources...", state="running")
resource_finder = self.agents.resource_finder_agent()
resource_prompt = self._format_prompt(
self.config["prompts"]["resource_finding"]["base"],
topic=self.topic,
learning_goal=self.learning_goal,
knowledge_level=self.knowledge_level,
learning_style=self.learning_style
)
resource_resp = resource_finder.run(resource_prompt, stream=False)
resource_result = resource_resp.content
results["resources"] = resource_result
st.session_state.learning_resources = resource_result
status.update(label="Resources found!", state="complete")
return results
def generate_quiz(self, difficulty_level: str = "intermediate",
focus_areas: str = "general", num_questions: int = 10):
"""
Generate a quiz to test understanding.
Args:
difficulty_level (str): The difficulty level of the quiz
focus_areas (str): Specific areas to focus on
num_questions (int): Number of questions to generate
Returns:
dict: Quiz content
"""
results = {}
with st.status("Generating quiz...", expanded=True) as status:
status.update(label="Creating questions...", state="running")
quiz_generator = self.agents.quiz_generator_agent()
quiz_prompt = self._format_prompt(
self.config["prompts"]["quiz_generation"]["base"],
topic=self.topic,
difficulty_level=difficulty_level,
focus_areas=focus_areas,
num_questions=num_questions
)
quiz_resp = quiz_generator.run(quiz_prompt, stream=False)
quiz_result = quiz_resp.content
results["quiz"] = quiz_result
status.update(label="Quiz ready!", state="complete")
return results
def get_tutoring(self, student_question: str, context: str = ""):
"""
Get tutoring help on a specific question.
Args:
student_question (str): The student's question
context (str): Additional context
Returns:
str: Tutoring response
"""
tutor = self.agents.tutor_agent()
tutor_prompt = self._format_prompt(
self.config["prompts"]["tutoring"]["base"],
student_question=student_question,
context=context,
knowledge_level=self.knowledge_level
)
tutor_resp = tutor.run(tutor_prompt, stream=False)
return tutor_resp.content
def initialize_rag(self, collection_name: str = "study_materials"):
"""
Initialize RAG helper for document-based learning.
Args:
collection_name (str): Name for the document collection
"""
self.rag_helper = RAGHelper(collection_name=collection_name)
def add_document_to_rag(self, file_path: str, file_type: str = "pdf") -> bool:
"""
Add a document to the RAG knowledge base.
Args:
file_path (str): Path to the document
file_type (str): Type of document ("pdf" or "text")
Returns:
bool: Success status
"""
if not self.rag_helper:
self.initialize_rag()
if file_type == "pdf":
return self.rag_helper.load_pdf(file_path)
elif file_type == "text":
return self.rag_helper.load_text(file_path)
return False
def query_documents(self, question: str, k: int = 4):
"""
Query the uploaded documents using RAG.
Args:
question (str): The question to ask
k (int): Number of relevant chunks to retrieve
Returns:
str: Answer based on documents
"""
if not self.rag_helper:
return "No documents have been uploaded yet. Please upload study materials first."
# Retrieve relevant context
relevant_docs = self.rag_helper.query(question, k=k)
if not relevant_docs:
return "I couldn't find relevant information in your uploaded documents. Please try rephrasing your question or upload more materials."
# Combine context
context = "\n\n".join(relevant_docs)
# Use RAG tutor agent
rag_tutor = self.agents.rag_tutor_agent()
rag_prompt = self._format_prompt(
self.config["prompts"]["rag_query"]["base"],
question=question,
context=context
)
rag_resp = rag_tutor.run(rag_prompt, stream=False)
return rag_resp.content
def get_document_count(self) -> int:
"""
Get the number of documents in the RAG knowledge base.
Returns:
int: Number of documents
"""
if not self.rag_helper:
return 0
return self.rag_helper.get_document_count()
def clear_documents(self) -> bool:
"""
Clear all documents from the RAG knowledge base.
Returns:
bool: Success status
"""
if not self.rag_helper:
return False
return self.rag_helper.clear_database()