File size: 871 Bytes
41159b4
e0a6302
16699fe
7472187
16699fe
e0a6302
16699fe
 
 
 
 
 
41159b4
2243e17
e0a6302
 
41159b4
16699fe
4fdee98
 
16699fe
4fdee98
 
 
 
 
 
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
import openai
from pathlib import Path
from prompts import LESSON_PROMPT

def generate_lesson(topic, prompt_template=LESSON_PROMPT):
    """
    Generates a beginner-friendly coding lesson for a given topic.
    Returns a tuple: (file_path, lesson_text)
    """
    # Fill in the topic into the prompt
    tutorial_prompt = f"{prompt_template}\n\nTopic: '{topic}'"

    response = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": tutorial_prompt}],
        temperature=0.7,
    )

    lesson_text = response.choices[0].message.content.strip()

    # Save to outputs folder
    output_path = Path("outputs") / f"{topic.replace(' ', '_')}.txt"
    output_path.parent.mkdir(exist_ok=True)
    with open(output_path, "w", encoding="utf-8") as f:
        f.write(lesson_text)

    return str(output_path), lesson_text