File size: 803 Bytes
b1289d5
61f9943
b1289d5
 
61f9943
b1289d5
61f9943
 
b1289d5
697a5f8
61f9943
 
b1289d5
697a5f8
 
02a5d3b
61f9943
02a5d3b
697a5f8
b1289d5
61f9943
b1289d5
61f9943
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)}"