File size: 1,018 Bytes
b1289d5 02a5d3b b1289d5 02a5d3b b1289d5 02a5d3b b1289d5 02a5d3b b1289d5 02a5d3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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)}"
|