-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgenerate_prereq_practice.py
More file actions
254 lines (196 loc) · 8.19 KB
/
Copy pathgenerate_prereq_practice.py
File metadata and controls
254 lines (196 loc) · 8.19 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
#!/usr/bin/env python3
"""
================================
CONFIGURATION & SETUP
================================
"""
# Dependencies
import os
import sys
import requests
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables
load_dotenv()
# Domain Constants
GENERATE_PRACTICE = True
TARGET_CODE = '6.NS.B.4'
# OpenAI configuration
OPENAI_MODEL = 'gpt-4'
OPENAI_TEMPERATURE = 0.7
# Environment Setup
api_key = os.getenv('API_KEY')
base_url = os.getenv('BASE_URL')
if not api_key:
print('❌ API_KEY environment variable is not set.')
sys.exit(1)
if not base_url:
print('❌ BASE_URL environment variable is not set.')
sys.exit(1)
openai_client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY')
)
# Setup headers for API requests
headers = {"x-api-key": api_key}
"""
================================
HELPER FUNCTIONS
================================
"""
def make_api_request(endpoint, params=None):
"""Make API request to Knowledge Graph API"""
try:
url = f"{base_url}{endpoint}"
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as error:
print(f'❌ Error making API request to {endpoint}: {str(error)}')
raise error
"""
================================
STEP 2: GET PREREQUISITE STANDARDS
================================
"""
def get_standard_and_prerequisites():
"""Find the target standard and its prerequisites"""
# Find the target standard by statement code
search_result = make_api_request(
'/academic-standards/search',
params={
'statementCode': TARGET_CODE,
'jurisdiction': 'Multi-State'
}
)
target_standard = search_result[0] if search_result else None
if not target_standard:
print(f'❌ No standard found for {TARGET_CODE}')
return None
print(f'✅ Found standard {TARGET_CODE}:')
print(f' UUID: {target_standard["caseIdentifierUUID"]}')
print(f' Description: {target_standard["description"]}')
# Get prerequisites
prereq_result = make_api_request(
f'/academic-standards/{target_standard["caseIdentifierUUID"]}/prerequisites'
)
prerequisite_standards = prereq_result['data']
print(f'✅ Found {len(prerequisite_standards)} prerequisite(s):')
for prereq in prerequisite_standards:
description = prereq.get('description', 'No description')
truncated = description[:80] + '...' if len(description) > 80 else description
print(f' {prereq["statementCode"]}: {truncated}')
return {'target_standard': target_standard, 'prerequisite_standards': prerequisite_standards}
def get_learning_components_for_prerequisites(prerequisite_standards):
"""Get learning components for each prerequisite standard"""
prerequisite_learning_components = []
for prereq in prerequisite_standards:
lc_result = make_api_request(
f'/academic-standards/{prereq["caseIdentifierUUID"]}/learning-components'
)
for lc in lc_result['data']:
prerequisite_learning_components.append({
'caseIdentifierUUID': prereq['caseIdentifierUUID'],
'statementCode': prereq['statementCode'],
'standardDescription': prereq['description'],
'learningComponentDescription': lc['description']
})
print(f'✅ Found {len(prerequisite_learning_components)} supporting learning components for prerequisites:')
for lc in prerequisite_learning_components[:5]:
description = lc.get('learningComponentDescription', 'No description')
truncated = description[:80] + '...' if len(description) > 80 else description
print(f' {truncated}')
return prerequisite_learning_components
"""
================================
STEP 3: GENERATE PRACTICE
================================
"""
def package_context_data(target_standard, prerequisite_learning_components):
"""
Package the standards and learning components data for text generation
This creates a structured context that can be used for generating practice questions
"""
standards_map = {}
# Group learning components by standard for context
for row in prerequisite_learning_components:
case_id = row['caseIdentifierUUID']
if case_id not in standards_map:
standards_map[case_id] = {
'statementCode': row['statementCode'],
'description': row['standardDescription'] or '(no statement)',
'supportingLearningComponents': []
}
standards_map[case_id]['supportingLearningComponents'].append({
'description': row['learningComponentDescription'] or '(no description)'
})
full_standards_context = {
'targetStandard': {
'statementCode': target_standard['statementCode'],
'description': target_standard['description'] or '(no statement)'
},
'prereqStandards': list(standards_map.values())
}
print('✅ Packaged full standards context for text generation')
return full_standards_context
def generate_practice(full_standards_context):
"""Generate practice questions using OpenAI API"""
print(f'🔄 Generating practice questions for {full_standards_context["targetStandard"]["statementCode"]}...')
try:
# Build prompt inline
prerequisite_text = ''
for prereq in full_standards_context['prereqStandards']:
prerequisite_text += f'- {prereq["statementCode"]}: {prereq["description"]}\n'
prerequisite_text += ' Supporting Learning Components:\n'
for lc in prereq['supportingLearningComponents']:
prerequisite_text += f' • {lc["description"]}\n'
prompt = f"""You are a math tutor helping middle school students. Based on the following information, generate 3 practice questions for the target standard. Questions should help reinforce the key concept and build on prerequisite knowledge.
Target Standard:
- {full_standards_context["targetStandard"]["statementCode"]}: {full_standards_context["targetStandard"]["description"]}
Prerequisite Standards & Supporting Learning Components:
{prerequisite_text}"""
response = openai_client.chat.completions.create(
model=OPENAI_MODEL,
messages=[
{'role': 'system', 'content': 'You are an expert middle school math tutor.'},
{'role': 'user', 'content': prompt}
],
temperature=OPENAI_TEMPERATURE
)
practice_questions = response.choices[0].message.content.strip()
print('✅ Generated practice questions:\n')
print(practice_questions)
return {
'aiGenerated': practice_questions,
'targetStandard': full_standards_context['targetStandard']['statementCode'],
'prerequisiteCount': len(full_standards_context['prereqStandards'])
}
except Exception as err:
print(f'❌ Error generating practice questions: {str(err)}')
raise err
"""
================================
MAIN EXECUTION
================================
"""
def main():
"""Main execution function - orchestrates all tutorial steps"""
print('\n=== GENERATE PREREQUISITE PRACTICE TUTORIAL ===\n')
print('🔄 Step 2: Get prerequisite standards for 6.NS.B.4...\n')
# Get target standard and prerequisites
prerequisite_data = get_standard_and_prerequisites()
if not prerequisite_data:
print('❌ Failed to find prerequisite data')
return
target_standard = prerequisite_data['target_standard']
prerequisite_standards = prerequisite_data['prerequisite_standards']
# Get learning components for prerequisites
print()
prerequisite_learning_components = get_learning_components_for_prerequisites(prerequisite_standards)
print('\n🔄 Step 3: Generate practice problems...\n')
full_standards_context = package_context_data(target_standard, prerequisite_learning_components)
if GENERATE_PRACTICE:
generate_practice(full_standards_context)
else:
print('🚫 Practice question generation disabled')
if __name__ == '__main__':
main()