Skip to content

Generating Lesson Plans with Python

We used Python to create a set of prompts and we created a function that iterates through the grade levels.

Seed Prompt

Our "Seed Prompt" is a prompt that generates Python code that iterates through the prompts for each activity.

Create a Python function called generate_discussion that will call the OpenAI API using the GPT-4 model.
Iterate through three grade levels (5th, 9th and 11th grades) for two topic prompts
("Batteries" and "Motors").

topics = ["Batteries", "Motors", "Sensors"]
grade_levels = [5, 7, 9]

for topic in topics:
    for grade_level in grade_levels:
       generate_discussion(topic, grade_level)

The entire prompt should look like this:

"Generate a single paragraph description of a discussion between a teacher and a students about robotics.  The topic is {$topic} and the grade level is {$grade_level}.  

The generate_discussion function can assume that the OPENAI_API_KEY
is in an global variable called OPENAI_API_KEY.

ChatGPT Seed Generator

 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
import openai

def generate_discussion(topic, grade_level):
    prompt = f"Generate a single paragraph description of a discussion between a teacher and students about robotics. The topic is {topic} and the grade level is {grade_level}."

    try:
        response = openai.Completion.create(
            model="gpt-4",
            prompt=prompt,
            max_tokens=150,
            api_key=OPENAI_API_KEY
        )
        return response.choices[0].text.strip()
    except Exception as e:
        return f"An error occurred: {e}"

# Example usage
OPENAI_API_KEY = "your-api-key-here"

topics = ["Batteries", "Motors", "Sensors"]
grade_levels = [5, 7, 9]

for topic in topics:
    for grade_level in grade_levels:
        print(generate_discussion(topic, grade_level))