Update utils/quiz_generator.py
Browse files- utils/quiz_generator.py +9 -8
utils/quiz_generator.py
CHANGED
@@ -1,22 +1,23 @@
|
|
1 |
-
from openai import OpenAI
|
2 |
import os
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
|
|
|
5 |
load_dotenv()
|
6 |
-
|
|
|
7 |
|
8 |
def generate_quiz(topic, num_questions=5):
|
9 |
-
prompt = f"Generate {num_questions} multiple
|
10 |
-
|
11 |
try:
|
12 |
response = client.chat.completions.create(
|
13 |
model="gpt-3.5-turbo",
|
14 |
messages=[
|
15 |
-
{"role": "system", "content": "You are a
|
16 |
{"role": "user", "content": prompt}
|
17 |
]
|
18 |
)
|
19 |
-
|
20 |
-
return quiz
|
21 |
except Exception as e:
|
22 |
-
return f"Error generating quiz:\n{e}"
|
|
|
|
|
1 |
import os
|
2 |
+
from openai import OpenAI
|
3 |
from dotenv import load_dotenv
|
4 |
|
5 |
+
# Load your API key from .env
|
6 |
load_dotenv()
|
7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
client = OpenAI(api_key=api_key)
|
9 |
|
10 |
def generate_quiz(topic, num_questions=5):
|
11 |
+
prompt = f"Generate {num_questions} multiple-choice questions from the following content. Include 4 options and indicate the correct answer:\n\n{topic}"
|
12 |
+
|
13 |
try:
|
14 |
response = client.chat.completions.create(
|
15 |
model="gpt-3.5-turbo",
|
16 |
messages=[
|
17 |
+
{"role": "system", "content": "You are a quiz-generating assistant."},
|
18 |
{"role": "user", "content": prompt}
|
19 |
]
|
20 |
)
|
21 |
+
return response.choices[0].message.content.strip()
|
|
|
22 |
except Exception as e:
|
23 |
+
return f"Error generating quiz:\n{str(e)}"
|