Spaces:
Sleeping
Sleeping
Update learning_platform.py
Browse files- learning_platform.py +69 -70
learning_platform.py
CHANGED
|
@@ -125,76 +125,75 @@ class CoursePrompts:
|
|
| 125 |
Return in the same JSON format."""
|
| 126 |
|
| 127 |
class CourseBuilder:
|
| 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 |
class LearningPlatform:
|
| 199 |
def __init__(self, api_key: str = None):
|
| 200 |
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
|
|
|
|
| 125 |
Return in the same JSON format."""
|
| 126 |
|
| 127 |
class CourseBuilder:
|
| 128 |
+
def __init__(self, api_key: str = None):
|
| 129 |
+
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
|
| 130 |
+
self.setup_agents()
|
| 131 |
+
self.setup_graph()
|
| 132 |
+
|
| 133 |
+
def setup_agents(self):
|
| 134 |
+
self.llm = ChatOpenAI(
|
| 135 |
+
temperature=0.7,
|
| 136 |
+
model="gpt-4",
|
| 137 |
+
openai_api_key=self.api_key
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
def course_planner(self, state: CourseState) -> CourseState:
|
| 141 |
+
"""Course planning agent node"""
|
| 142 |
+
print("---COURSE PLANNER---")
|
| 143 |
+
prompt = PromptTemplate(
|
| 144 |
+
template=CoursePrompts.planner_prompt(),
|
| 145 |
+
input_variables=["topic", "difficulty"]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
response = self.llm.predict(
|
| 149 |
+
prompt.format(
|
| 150 |
+
topic=state["current_topic"],
|
| 151 |
+
difficulty=state["difficulty"]
|
| 152 |
+
)
|
| 153 |
+
)
|
| 154 |
+
return {
|
| 155 |
+
"messages": [AIMessage(content=response)],
|
| 156 |
+
"status": "planning",
|
| 157 |
+
"current_topic": state["current_topic"],
|
| 158 |
+
"difficulty": state["difficulty"],
|
| 159 |
+
"modules": []
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
async def generate_module_content(self, module: CourseModule) -> List[Section]:
|
| 163 |
+
try:
|
| 164 |
+
section = Section(
|
| 165 |
+
title=f"Introduction to {module.title}",
|
| 166 |
+
content=f"Basic concepts of {module.title}",
|
| 167 |
+
key_points=["Key concept 1", "Key concept 2"],
|
| 168 |
+
quiz_questions=[{
|
| 169 |
+
"question": f"What is {module.title}?",
|
| 170 |
+
"options": ["A", "B", "C", "D"],
|
| 171 |
+
"correct_answer": "A",
|
| 172 |
+
"explanation": "Basic explanation"
|
| 173 |
+
}]
|
| 174 |
+
)
|
| 175 |
+
return [section]
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"Error generating content: {e}")
|
| 178 |
+
return []
|
| 179 |
+
|
| 180 |
+
def check_quality(self, state: CourseState) -> Literal["edit", "complete"]:
|
| 181 |
+
messages = state["messages"]
|
| 182 |
+
last_message = messages[-1]
|
| 183 |
+
|
| 184 |
+
if len(last_message.content) > 1000 and "code example" in last_message.content.lower():
|
| 185 |
+
return "complete"
|
| 186 |
+
return "edit"
|
| 187 |
+
|
| 188 |
+
def setup_graph(self):
|
| 189 |
+
workflow = StateGraph(CourseState)
|
| 190 |
+
|
| 191 |
+
workflow.add_node("planner", self.course_planner)
|
| 192 |
+
workflow.add_edge(START, "planner")
|
| 193 |
+
workflow.add_edge("planner", END)
|
| 194 |
+
|
| 195 |
+
self.graph = workflow.compile()
|
| 196 |
+
|
|
|
|
| 197 |
class LearningPlatform:
|
| 198 |
def __init__(self, api_key: str = None):
|
| 199 |
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
|