smart-lms-suite / utils /quiz_generator.py
sathwikabhavaraju2005's picture
Update utils/quiz_generator.py
61f9943 verified
raw
history blame
803 Bytes
import os
from openai import OpenAI
from dotenv import load_dotenv
# Load your API key from .env
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)
def generate_quiz(topic, num_questions=5):
prompt = f"Generate {num_questions} multiple-choice questions from the following content. Include 4 options and indicate the correct answer:\n\n{topic}"
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a quiz-generating assistant."},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"Error generating quiz:\n{str(e)}"