Skip to content

Quizzes are a useful way to practice what you or your students have learned and to keep track of progress. In this project, you'll build a educational quiz bot to help you generate quiz questions. Imagine you are a teacher needing to create a pop quiz based on your most recent lecture, or you are a student wanting to quiz yourself on this lecture. This tool will help you automate this task, track which questions have been asked to avoid repetition, and validate the responses.

You'll leverage OpenAI and prompt engineering to generate relevant questions.

The Data:

This quiz bot allows you to generate quizzes from any educational text. You have been provided with a sample data file from a physics-focused corpus, stored in the physics_lecture.txt file.

The text covers fundamental physics concepts, including speed, velocity, Newton's third law, and more. This content is sourced from ScienceQA (Saikh et al., 2022), a dataset originally designed for Machine Reading Comprehension (MRC) tasks. For this project, the dataset has been filtered to focus specifically on natural science topics related to physics, making it ideal for use with the quiz bot.

Source: https://huggingface.co/datasets/tasksource/ScienceQA_text_only?row=40 Saikh, Tanik et al. “ScienceQA: a novel resource for question answering on scholarly articles.” International Journal on Digital Libraries 23 (2022): 289 - 301.

Image generated with DALL-E (OpenAI)

# Import OpenAI and supporting libraries
import os
from openai import OpenAI

def read_text_from_file(filename):
    """
    Reads the first 500 lines of content from a file and returns it as a string.
    Args: filename (str): The name of the file to read.
    Returns: str: The content of the file as a string, or an empty string if the file is not found.
    """
    try:
        with open(filename, 'r') as file:
            return ''.join([next(file) for _ in range(500)])
    except FileNotFoundError:
        print(f"Error: {filename} not found.")
        return ""

# Read content from the file
content = read_text_from_file("physics_lecture.txt")

# Set up the OpenAI client
client = OpenAI()

# Setting up the recommended model
model = "gpt-4o-mini"
import json

# Start coding here
system_prompt = f"""
You are a quiz bot who act as an assistant to teachers. You will be given a large amount of text delimited by triple backticks below. Your task is to create a multiple-choice questions from the given text and give output by creating structured quizzes to support educators. If the user asks any questions which are not related to the source text delimited by triple backticks then reply politely saying 'Question is not related to the source provided.
```{content}```
"""

user_prompt = f"""
You will be provided with a text delimited by triple backticks. Generate 1 quiz question, the output should be a Python List with exactly 1 element and should follow the below template:

"Question: ---", "Options: ---", and "Answer: ---"

"""

def gen_quiz_questions(system_prompt, user_prompt):
    response = client.chat.completions.create(
        model=model,
        messages=[
            { "role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt }
        ],
        temperature=0
    )
    return response.choices[0].message.content


quiz_data = []

#print(gen_quiz_questions(system_prompt, user_prompt))

for item in range(5):
    quiz_data.append(gen_quiz_questions(system_prompt, user_prompt))
    
print(quiz_data)