Update utils/quiz_generator.py
Browse files- utils/quiz_generator.py +11 -31
utils/quiz_generator.py
CHANGED
@@ -1,42 +1,22 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
load_dotenv()
|
6 |
-
|
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 =
|
31 |
-
model=
|
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 |
-
|
40 |
-
return
|
41 |
except Exception as e:
|
42 |
-
return f"Error generating quiz
|
|
|
1 |
+
from openai import OpenAI
|
2 |
import os
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
load_dotenv()
|
6 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def generate_quiz(topic, num_questions=5):
|
9 |
+
prompt = f"Generate {num_questions} multiple choice questions with 4 options and answers from this content:\n\n{topic}"
|
10 |
+
|
11 |
try:
|
12 |
+
response = client.chat.completions.create(
|
13 |
+
model="gpt-3.5-turbo",
|
14 |
messages=[
|
15 |
{"role": "system", "content": "You are a helpful quiz generator."},
|
16 |
{"role": "user", "content": prompt}
|
17 |
+
]
|
|
|
|
|
18 |
)
|
19 |
+
quiz = response.choices[0].message.content.strip()
|
20 |
+
return quiz
|
21 |
except Exception as e:
|
22 |
+
return f"Error generating quiz:\n{e}"
|