File size: 833 Bytes
b1289d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}"