import openai | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def generate_quiz(topic_text, num_questions=5): | |
prompt = f""" | |
Generate {num_questions} multiple-choice questions with 4 options each and mention the correct answer for the topic below. | |
Topic: | |
{topic_text} | |
Format: | |
Q1. Question? | |
a) | |
b) | |
c) | |
d) | |
Answer: <correct option> | |
""" | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", # You can also use text-davinci-003 if preferred | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.7, | |
max_tokens=800 | |
) | |
return response['choices'][0]['message']['content'] | |
except Exception as e: | |
return f"Error: {str(e)}" | |