Create quiz_generator.py
Browse files- utils/quiz_generator.py +26 -17
utils/quiz_generator.py
CHANGED
@@ -5,29 +5,38 @@ from dotenv import load_dotenv
|
|
5 |
load_dotenv()
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
-
|
|
|
9 |
prompt = f"""
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
"""
|
23 |
|
24 |
try:
|
25 |
response = openai.ChatCompletion.create(
|
26 |
-
model=
|
27 |
-
messages=[
|
|
|
|
|
|
|
28 |
temperature=0.7,
|
29 |
max_tokens=800
|
30 |
)
|
31 |
-
|
|
|
32 |
except Exception as e:
|
33 |
-
return f"Error: {str(e)}"
|
|
|
5 |
load_dotenv()
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
+
|
9 |
+
def generate_quiz(text, num_questions=5, engine="gpt-3.5-turbo"):
|
10 |
prompt = f"""
|
11 |
+
You are a quiz creator.
|
12 |
+
Generate {num_questions} multiple choice questions from the following content.
|
13 |
+
Each question should have 4 options (a, b, c, d), and the correct answer should be indicated clearly.
|
14 |
+
|
15 |
+
Content:
|
16 |
+
{text}
|
17 |
+
|
18 |
+
Format:
|
19 |
+
Q1. Question text?
|
20 |
+
a) Option A
|
21 |
+
b) Option B
|
22 |
+
c) Option C
|
23 |
+
d) Option D
|
24 |
+
Answer: a
|
25 |
+
|
26 |
+
Q2. ...
|
27 |
"""
|
28 |
|
29 |
try:
|
30 |
response = openai.ChatCompletion.create(
|
31 |
+
model=engine,
|
32 |
+
messages=[
|
33 |
+
{"role": "system", "content": "You are a helpful quiz generator."},
|
34 |
+
{"role": "user", "content": prompt}
|
35 |
+
],
|
36 |
temperature=0.7,
|
37 |
max_tokens=800
|
38 |
)
|
39 |
+
quiz_text = response['choices'][0]['message']['content']
|
40 |
+
return quiz_text
|
41 |
except Exception as e:
|
42 |
+
return f"Error generating quiz: {str(e)}"
|