smart-lms-suite / utils /quiz_generator.py
sathwikabhavaraju2005's picture
Create quiz_generator.py
02a5d3b verified
raw
history blame
1.02 kB
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_quiz(text, num_questions=5, engine="gpt-3.5-turbo"):
prompt = f"""
You are a quiz creator.
Generate {num_questions} multiple choice questions from the following content.
Each question should have 4 options (a, b, c, d), and the correct answer should be indicated clearly.
Content:
{text}
Format:
Q1. Question text?
a) Option A
b) Option B
c) Option C
d) Option D
Answer: a
Q2. ...
"""
try:
response = openai.ChatCompletion.create(
model=engine,
messages=[
{"role": "system", "content": "You are a helpful quiz generator."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=800
)
quiz_text = response['choices'][0]['message']['content']
return quiz_text
except Exception as e:
return f"Error generating quiz: {str(e)}"