How to Intermediate · 4 min read

How to build personalized learning with AI

Quick answer
Use large language models (LLMs) like gpt-4o to create personalized learning experiences by dynamically generating tailored content and feedback based on individual learner inputs. Combine retrieval-augmented generation (RAG) with learner profiles to adapt lessons and quizzes in real time.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python SDK and set your API key as an environment variable to access the gpt-4o model for personalized learning tasks.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example shows how to build a simple personalized learning assistant that adapts questions based on user input and provides tailored explanations.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define a simple learner profile
learner_profile = {
    "name": "Alice",
    "level": "beginner",
    "topics": ["fractions", "decimals"]
}

# Prompt template to personalize learning content
prompt = f"You are a helpful tutor. The learner is a {learner_profile['level']} in math, interested in {', '.join(learner_profile['topics'])}. " \
         "Generate a personalized question on fractions with a detailed explanation after the answer."

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print("Personalized question and explanation:\n", response.choices[0].message.content)
output
Personalized question and explanation:
What is 3/4 plus 1/8?\n\nAnswer: 3/4 + 1/8 = 6/8 + 1/8 = 7/8.\nExplanation: To add fractions, find a common denominator. Here, 8 is the common denominator. Convert 3/4 to 6/8, then add 6/8 and 1/8 to get 7/8.

Common variations

You can enhance personalization by integrating retrieval-augmented generation (RAG) to fetch learner history or external resources. Use async calls for scalability or switch to other models like claude-3-5-sonnet-20241022 for different style preferences.

python
import asyncio
import os
from openai import OpenAI

async def personalized_learning_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    prompt = "Generate a beginner-level math question on decimals with explanation."
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Async personalized question:\n", response.choices[0].message.content)

asyncio.run(personalized_learning_async())
output
Async personalized question:
What is 0.5 plus 0.25?\n\nAnswer: 0.5 + 0.25 = 0.75.\nExplanation: Adding decimals is like adding whole numbers; align the decimal points and add the digits.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If responses are generic, refine your prompt with more learner context or use fine-tuning for domain-specific adaptation.
  • For rate limits, implement exponential backoff or upgrade your API plan.

Key Takeaways

  • Use gpt-4o or similar LLMs to dynamically generate personalized learning content.
  • Incorporate learner profiles and preferences in prompts to tailor questions and explanations.
  • Combine LLMs with retrieval systems for context-aware adaptive learning.
  • Async API calls improve scalability for multi-user personalized learning platforms.
  • Refine prompts and handle API errors to ensure smooth personalized learning experiences.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗