sathwikabhavaraju2005 commited on
Commit
02a5d3b
·
verified ·
1 Parent(s): 823bf5e

Create quiz_generator.py

Browse files
Files changed (1) hide show
  1. 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
- def generate_quiz(topic_text, num_questions=5):
 
9
  prompt = f"""
10
- Generate {num_questions} multiple-choice questions with 4 options each and mention the correct answer for the topic below.
11
-
12
- Topic:
13
- {topic_text}
14
-
15
- Format:
16
- Q1. Question?
17
- a)
18
- b)
19
- c)
20
- d)
21
- Answer: <correct option>
 
 
 
 
22
  """
23
 
24
  try:
25
  response = openai.ChatCompletion.create(
26
- model="gpt-3.5-turbo", # You can also use text-davinci-003 if preferred
27
- messages=[{"role": "user", "content": prompt}],
 
 
 
28
  temperature=0.7,
29
  max_tokens=800
30
  )
31
- return response['choices'][0]['message']['content']
 
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)}"