Spaces:
Sleeping
Sleeping
Update learning_platform.py
Browse files- learning_platform.py +23 -1
learning_platform.py
CHANGED
|
@@ -7,6 +7,10 @@ from langchain_community.chat_models import ChatOpenAI
|
|
| 7 |
from langchain_community.embeddings import OpenAIEmbeddings
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@dataclass
|
| 12 |
class Section:
|
|
@@ -90,21 +94,25 @@ class CourseBuilder:
|
|
| 90 |
|
| 91 |
async def plan_course(self, topic: str, difficulty: str) -> LearningPath:
|
| 92 |
prompt = self.prompts.course_planning_prompt().format(topic=topic, difficulty=difficulty)
|
|
|
|
| 93 |
response = await self.llm.apredict(prompt)
|
|
|
|
| 94 |
|
| 95 |
# Debug: Log the raw response for troubleshooting
|
| 96 |
if not response.strip():
|
|
|
|
| 97 |
raise ValueError("Empty response from API")
|
| 98 |
|
| 99 |
try:
|
| 100 |
course_plan = json.loads(response)
|
| 101 |
except json.JSONDecodeError as e:
|
|
|
|
| 102 |
raise ValueError(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 103 |
|
| 104 |
modules = [
|
| 105 |
CourseModule(
|
| 106 |
title=module["title"],
|
| 107 |
-
objectives=module
|
| 108 |
prerequisites=module.get("prerequisites", []),
|
| 109 |
sections=[
|
| 110 |
Section(
|
|
@@ -133,6 +141,7 @@ class CourseBuilder:
|
|
| 133 |
metadatas=[{"type": "course_plan", "topic": topic}]
|
| 134 |
)
|
| 135 |
|
|
|
|
| 136 |
return learning_path
|
| 137 |
|
| 138 |
async def create_module_content(self, module: CourseModule) -> List[Section]:
|
|
@@ -140,14 +149,18 @@ class CourseBuilder:
|
|
| 140 |
title=module.title,
|
| 141 |
objectives=", ".join(module.objectives)
|
| 142 |
)
|
|
|
|
| 143 |
response = await self.llm.apredict(prompt)
|
|
|
|
| 144 |
|
| 145 |
if not response.strip():
|
|
|
|
| 146 |
raise ValueError("Empty response from API")
|
| 147 |
|
| 148 |
try:
|
| 149 |
content_json = json.loads(response)
|
| 150 |
except json.JSONDecodeError as e:
|
|
|
|
| 151 |
raise ValueError(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 152 |
|
| 153 |
sections = [Section(**section) for section in content_json.get("sections", [])]
|
|
@@ -159,6 +172,7 @@ class CourseBuilder:
|
|
| 159 |
metadatas=[{"type": "module_content", "module": module.title}]
|
| 160 |
)
|
| 161 |
|
|
|
|
| 162 |
return sections
|
| 163 |
|
| 164 |
async def answer_user_question(self, topic: str, module_title: str, question: str) -> str:
|
|
@@ -167,9 +181,12 @@ class CourseBuilder:
|
|
| 167 |
module_title=module_title,
|
| 168 |
question=question
|
| 169 |
)
|
|
|
|
| 170 |
response = await self.llm.apredict(prompt)
|
|
|
|
| 171 |
|
| 172 |
if not response.strip():
|
|
|
|
| 173 |
raise ValueError("Empty response from API")
|
| 174 |
|
| 175 |
return response
|
|
@@ -184,20 +201,25 @@ class LearningPlatform:
|
|
| 184 |
learning_path = await self.course_builder.plan_course(topic, difficulty)
|
| 185 |
return learning_path
|
| 186 |
except Exception as e:
|
|
|
|
| 187 |
raise Exception(f"Course creation error: {str(e)}")
|
| 188 |
|
| 189 |
async def generate_next_module(self, path: LearningPath, module_index: int):
|
| 190 |
if module_index < len(path.modules):
|
| 191 |
module = path.modules[module_index]
|
| 192 |
if not module.is_complete:
|
|
|
|
| 193 |
sections = await self.course_builder.create_module_content(module)
|
| 194 |
module.sections = sections
|
| 195 |
module.is_complete = True
|
|
|
|
| 196 |
|
| 197 |
async def handle_user_question(self, path: LearningPath, module_index: int, question: str) -> str:
|
| 198 |
if module_index < len(path.modules):
|
| 199 |
module = path.modules[module_index]
|
|
|
|
| 200 |
answer = await self.course_builder.answer_user_question(path.topic, module.title, question)
|
| 201 |
return answer
|
| 202 |
else:
|
|
|
|
| 203 |
raise ValueError("Invalid module index")
|
|
|
|
| 7 |
from langchain_community.embeddings import OpenAIEmbeddings
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
import streamlit as st
|
| 10 |
+
import logging
|
| 11 |
+
|
| 12 |
+
# Set up logging for debugging purposes
|
| 13 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
@dataclass
|
| 16 |
class Section:
|
|
|
|
| 94 |
|
| 95 |
async def plan_course(self, topic: str, difficulty: str) -> LearningPath:
|
| 96 |
prompt = self.prompts.course_planning_prompt().format(topic=topic, difficulty=difficulty)
|
| 97 |
+
logging.debug(f"Sending course planning prompt: {prompt}")
|
| 98 |
response = await self.llm.apredict(prompt)
|
| 99 |
+
logging.debug(f"Received response for course planning: {response}")
|
| 100 |
|
| 101 |
# Debug: Log the raw response for troubleshooting
|
| 102 |
if not response.strip():
|
| 103 |
+
logging.error("Empty response from API")
|
| 104 |
raise ValueError("Empty response from API")
|
| 105 |
|
| 106 |
try:
|
| 107 |
course_plan = json.loads(response)
|
| 108 |
except json.JSONDecodeError as e:
|
| 109 |
+
logging.error(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 110 |
raise ValueError(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 111 |
|
| 112 |
modules = [
|
| 113 |
CourseModule(
|
| 114 |
title=module["title"],
|
| 115 |
+
objectives=module.get("objectives", []),
|
| 116 |
prerequisites=module.get("prerequisites", []),
|
| 117 |
sections=[
|
| 118 |
Section(
|
|
|
|
| 141 |
metadatas=[{"type": "course_plan", "topic": topic}]
|
| 142 |
)
|
| 143 |
|
| 144 |
+
logging.info(f"Created learning path for topic '{topic}' with difficulty '{difficulty}'")
|
| 145 |
return learning_path
|
| 146 |
|
| 147 |
async def create_module_content(self, module: CourseModule) -> List[Section]:
|
|
|
|
| 149 |
title=module.title,
|
| 150 |
objectives=", ".join(module.objectives)
|
| 151 |
)
|
| 152 |
+
logging.debug(f"Sending module content prompt: {prompt}")
|
| 153 |
response = await self.llm.apredict(prompt)
|
| 154 |
+
logging.debug(f"Received response for module content: {response}")
|
| 155 |
|
| 156 |
if not response.strip():
|
| 157 |
+
logging.error("Empty response from API")
|
| 158 |
raise ValueError("Empty response from API")
|
| 159 |
|
| 160 |
try:
|
| 161 |
content_json = json.loads(response)
|
| 162 |
except json.JSONDecodeError as e:
|
| 163 |
+
logging.error(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 164 |
raise ValueError(f"Invalid JSON response from API: {str(e)}\nResponse: {response}")
|
| 165 |
|
| 166 |
sections = [Section(**section) for section in content_json.get("sections", [])]
|
|
|
|
| 172 |
metadatas=[{"type": "module_content", "module": module.title}]
|
| 173 |
)
|
| 174 |
|
| 175 |
+
logging.info(f"Created {len(sections)} sections for module: {module.title}")
|
| 176 |
return sections
|
| 177 |
|
| 178 |
async def answer_user_question(self, topic: str, module_title: str, question: str) -> str:
|
|
|
|
| 181 |
module_title=module_title,
|
| 182 |
question=question
|
| 183 |
)
|
| 184 |
+
logging.debug(f"Sending user question prompt: {prompt}")
|
| 185 |
response = await self.llm.apredict(prompt)
|
| 186 |
+
logging.debug(f"Received response for user question: {response}")
|
| 187 |
|
| 188 |
if not response.strip():
|
| 189 |
+
logging.error("Empty response from API")
|
| 190 |
raise ValueError("Empty response from API")
|
| 191 |
|
| 192 |
return response
|
|
|
|
| 201 |
learning_path = await self.course_builder.plan_course(topic, difficulty)
|
| 202 |
return learning_path
|
| 203 |
except Exception as e:
|
| 204 |
+
logging.error(f"Course creation error: {str(e)}")
|
| 205 |
raise Exception(f"Course creation error: {str(e)}")
|
| 206 |
|
| 207 |
async def generate_next_module(self, path: LearningPath, module_index: int):
|
| 208 |
if module_index < len(path.modules):
|
| 209 |
module = path.modules[module_index]
|
| 210 |
if not module.is_complete:
|
| 211 |
+
logging.info(f"Generating content for module: {module.title}")
|
| 212 |
sections = await self.course_builder.create_module_content(module)
|
| 213 |
module.sections = sections
|
| 214 |
module.is_complete = True
|
| 215 |
+
logging.info(f"Module '{module.title}' is now complete.")
|
| 216 |
|
| 217 |
async def handle_user_question(self, path: LearningPath, module_index: int, question: str) -> str:
|
| 218 |
if module_index < len(path.modules):
|
| 219 |
module = path.modules[module_index]
|
| 220 |
+
logging.info(f"Answering user question for module: {module.title}")
|
| 221 |
answer = await self.course_builder.answer_user_question(path.topic, module.title, question)
|
| 222 |
return answer
|
| 223 |
else:
|
| 224 |
+
logging.error(f"Invalid module index: {module_index}")
|
| 225 |
raise ValueError("Invalid module index")
|