cryogenic22 commited on
Commit
d86cfde
·
verified ·
1 Parent(s): a29f061

Create learning_platform.py

Browse files
Files changed (1) hide show
  1. learning_platform.py +140 -0
learning_platform.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import List, Dict, Any
3
+ from datetime import datetime
4
+ import openai
5
+ from dataclasses import dataclass
6
+ import pandas as pd
7
+
8
+ class Config:
9
+ def __init__(self):
10
+ self.openai_model = "gpt-3.5-turbo"
11
+ self.max_tokens = 1000
12
+ self.temperature = 0.7
13
+
14
+ @dataclass
15
+ class LearningModule:
16
+ title: str
17
+ description: str
18
+ content: str
19
+ difficulty: str
20
+ prerequisites: List[str]
21
+ learning_objectives: List[str]
22
+ quiz_questions: List[Dict[str, Any]]
23
+
24
+ @dataclass
25
+ class LearningPath:
26
+ topic: str
27
+ description: str
28
+ modules: List[LearningModule]
29
+ created_at: datetime
30
+ difficulty_level: str
31
+
32
+ class LLMService:
33
+ def __init__(self, api_key: str, config: Config):
34
+ openai.api_key = api_key
35
+ self.config = config
36
+
37
+ async def generate_text(self, prompt: str) -> str:
38
+ try:
39
+ completion = await openai.ChatCompletion.acreate(
40
+ model=self.config.openai_model,
41
+ messages=[{"role": "user", "content": prompt}],
42
+ max_tokens=self.config.max_tokens,
43
+ temperature=self.config.temperature
44
+ )
45
+ return completion.choices[0].message.content
46
+ except Exception as e:
47
+ print(f"Error in LLM generation: {e}")
48
+ return ""
49
+
50
+ class ContentGenerator:
51
+ def __init__(self, llm_service: LLMService):
52
+ self.llm_service = llm_service
53
+
54
+ async def create_module_content(self, topic: str, difficulty: str) -> str:
55
+ prompt = f"""
56
+ Create educational content for the topic: {topic}
57
+ Difficulty level: {difficulty}
58
+
59
+ Include:
60
+ 1. Clear explanations
61
+ 2. Examples
62
+ 3. Practice exercises
63
+ 4. Key takeaways
64
+
65
+ Format the content in markdown.
66
+ """
67
+ return await self.llm_service.generate_text(prompt)
68
+
69
+ async def generate_quiz(self, topic: str, difficulty: str) -> List[Dict[str, Any]]:
70
+ prompt = f"""
71
+ Create 5 quiz questions for the topic: {topic}
72
+ Difficulty level: {difficulty}
73
+
74
+ Format: JSON array of objects with:
75
+ - question
76
+ - options (array)
77
+ - correct_answer
78
+ - explanation
79
+ """
80
+ response = await self.llm_service.generate_text(prompt)
81
+ try:
82
+ return json.loads(response)
83
+ except json.JSONDecodeError:
84
+ return []
85
+
86
+ class LearningPathGenerator:
87
+ def __init__(self, content_generator: ContentGenerator):
88
+ self.content_generator = content_generator
89
+
90
+ async def create_learning_path(self, topic: str, difficulty: str) -> LearningPath:
91
+ modules = []
92
+ module_topics = await self._generate_module_topics(topic, difficulty)
93
+
94
+ for module_topic in module_topics:
95
+ content = await self.content_generator.create_module_content(
96
+ module_topic, difficulty
97
+ )
98
+ quiz = await self.content_generator.generate_quiz(
99
+ module_topic, difficulty
100
+ )
101
+
102
+ module = LearningModule(
103
+ title=module_topic,
104
+ description=f"Learn about {module_topic}",
105
+ content=content,
106
+ difficulty=difficulty,
107
+ prerequisites=[],
108
+ learning_objectives=[],
109
+ quiz_questions=quiz
110
+ )
111
+ modules.append(module)
112
+
113
+ return LearningPath(
114
+ topic=topic,
115
+ description=f"Complete learning path for {topic}",
116
+ modules=modules,
117
+ created_at=datetime.now(),
118
+ difficulty_level=difficulty
119
+ )
120
+
121
+ async def _generate_module_topics(self, topic: str, difficulty: str) -> List[str]:
122
+ prompt = f"""
123
+ Break down the topic '{topic}' into 3-5 logical sub-topics for a {difficulty} level course.
124
+ Return as a JSON array of strings.
125
+ """
126
+ response = await self.llm_service.generate_text(prompt)
127
+ try:
128
+ return json.loads(response)
129
+ except json.JSONDecodeError:
130
+ return [f"{topic} Basics", f"Intermediate {topic}", f"Advanced {topic}"]
131
+
132
+ class LearningPlatform:
133
+ def __init__(self, api_key: str):
134
+ config = Config()
135
+ llm_service = LLMService(api_key, config)
136
+ content_generator = ContentGenerator(llm_service)
137
+ self.path_generator = LearningPathGenerator(content_generator)
138
+
139
+ async def create_course(self, topic: str, difficulty: str) -> LearningPath:
140
+ return await self.path_generator.create_learning_path(topic, difficulty)