Spaces:
Runtime error
Runtime error
File size: 12,939 Bytes
93baaeb 270274e 93baaeb 28e29b2 93baaeb 270274e 93baaeb 28e29b2 93baaeb | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | import openai
import datetime as dt
import numpy as np
from openpyxl import Workbook
function_description = [
{
"name": "generate_moodle_mcq",
"description": "Generate a MCQ question to be used in online quiz. Content to be generated including question name, question text, (by defaut) four options including the correct option, feedback for each option, general feedback if question is answered correctly, and general feedback if question is answered incorrectly. Do not generated content that you do not know, if you do not have relevant information on certain topic, put 'I do not understand this topic' in question name and 'N/A' for question text and skip remaining properties.",
"parameters": {
"type": "object",
"properties": {
"question name": {
"type": "string",
"description": "name of the question"
},
"question text": {
"type": "string",
"description": "question text or main body of the question"
},
"general feedback correct": {
"type": "string",
"description": "general feedback if question is answered correctly"
},
"general feedback wrong": {
"type": "string",
"description": "general feedback if question is answered incorrectly"
},
"correct option": {
"type": "string",
"description": "the correct option"
},
"correct option feedback": {
"type": "string",
"description": "feedback for the correct option"
},
"wrong option 1": {
"type": "string",
"description": "the wrong option"
},
"wrong option 1 feedback": {
"type": "string",
"description": "feedback for the wrong option"
},
"wrong option 2": {
"type": "string",
"description": "the wrong option"
},
"wrong option 2 feedback": {
"type": "string",
"description": "feedback for the wrong option"
},
"wrong option 3": {
"type": "string",
"description": "the wrong option"
},
"wrong option 3 feedback": {
"type": "string",
"description": "feedback for the wrong option"
},
},
"required":["question name","question text"]
}
}
]
def get_question_content(response):
function_call = response["choices"][0]["message"]["function_call"]
function_name = function_call["name"]
arguments = function_call["arguments"]
qtext = eval(arguments).get("question text")
if qtext == "N/A":
qname = eval(arguments).get("question name")
qcontent = "Name: " + qname + "\n" + "Text: " + qtext
return qcontent, [qname,qtext,"N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"]
else:
qname = eval(arguments).get("question name")
qfdbkc = eval(arguments).get("general feedback correct")
qfdbkw = eval(arguments).get("general feedback wrong")
qoptc = eval(arguments).get("correct option")
qoptfdbkc = eval(arguments).get("correct option feedback")
qopt1 = eval(arguments).get("wrong option 1")
qoptfdbk1 = eval(arguments).get("wrong option 1 feedback")
qopt2 = eval(arguments).get("wrong option 2")
qoptfdbk2 = eval(arguments).get("wrong option 2 feedback")
qopt3 = eval(arguments).get("wrong option 3")
qoptfdbk3 = eval(arguments).get("wrong option 3 feedback")
qcontent = "Name: " + qname + "\n" + "Text: " + qtext + "\n" + "Correct option: " + qoptc + "\n" + "Correct option feedback: " + qoptfdbkc + "\n" + "Wrong option 1: " + qopt1 + "\n" + "Wrong option feedback: " + qoptfdbk1 + "\n" + "Wrong option 2: " + qopt2 + "\n" + "Wrong option feedback: " + qoptfdbk2 + "\n" + "Wrong option 3: " + qopt3 + "\n" + "Wrong option feedback: " + qoptfdbk3 + "\n" + "General feedback correct: " + qfdbkc + "\n" + "General feedback wrong: " + qfdbkw
return qcontent, [qname,qtext,qfdbkc,qfdbkw,qoptc,qoptfdbkc,qopt1,qoptfdbk1,qopt2,qoptfdbk2,qopt3,qoptfdbk3]
def write_to_xml(question_variable):
question_name = question_variable[0]
question_text = question_variable[1]
question_feedback_correct = question_variable[2]
question_feedback_wrong = question_variable[3]
question_correct_option = question_variable[4]
question_correct_option_feedback = question_variable[5]
question_wrong_option1 = question_variable[6]
question_wrong_option_feedback1 = question_variable[7]
question_wrong_option2 = question_variable[8]
question_wrong_option_feedback2 = question_variable[9]
question_wrong_option3 = question_variable[10]
question_wrong_option_feedback3 = question_variable[11]
xml_export_question = f"""
<question type="multichoice">
<name>
<text>{question_name}</text>
</name>
<questiontext format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_text}</p>]]></text>
</questiontext>
<generalfeedback format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;"> </p>]]></text>
</generalfeedback>
<defaultgrade>1</defaultgrade>
<penalty>0.3333333</penalty>
<hidden>0</hidden>
<idnumber></idnumber>
<single>true</single>
<shuffleanswers>true</shuffleanswers>
<answernumbering>none</answernumbering>
<showstandardinstruction>0</showstandardinstruction>
<correctfeedback format="html">
<text>{question_feedback_correct}</text>
</correctfeedback>
<partiallycorrectfeedback format="html">
<text> </text>
</partiallycorrectfeedback>
<incorrectfeedback format="html">
<text>{question_feedback_wrong}</text>
</incorrectfeedback>
<shownumcorrect/>
<answer fraction="100" format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_correct_option}</p>]]></text>
<feedback format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_correct_option_feedback}</p>]]></text>
</feedback>
</answer>
<answer fraction="0" format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option1}</p>]]></text>
<feedback format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback1}</p>]]></text>
</feedback>
</answer>
<answer fraction="0" format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option2}</p>]]></text>
<feedback format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback2}<br></p>]]></text>
</feedback>
</answer>
<answer fraction="0" format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option3}</p>]]></text>
<feedback format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">{question_wrong_option_feedback3}<br></p>]]></text>
</feedback>
</answer>
</question>"""
return xml_export_question
def multi_mcq(query,number):
prompt = "If you do not have relevant information on certain topic, put 'I do not understand this topic' in question name and 'N/A' for question text and skip remaining properties."
messages=[
{
"role": "system",
"content": prompt
},
{
"role": "user",
"content": query
},
]
content = ""
xml = ""
for x in range(number):
response = openai.ChatCompletion.create(
model = "gpt-4o",
messages = messages,
functions = function_description,
function_call = {"name": "generate_moodle_mcq"},
temperature=0.5,
max_tokens=4000
)
y = str(x+1)
question_content = get_question_content(response)
question_preview = question_content[0] + "\n\n"
question_text = question_content[1][1]
question_variable = question_content[1]
content += "Question" + y + ":\n" + question_preview + "\n"
prompt += f"\n This topic has already been generated '{question_text}', write a different topic. \n"
xml += write_to_xml(question_variable) + "\n\n"
messages=[
{
"role": "system",
"content": prompt
},
{
"role": "user",
"content": query
},
]
prefix = """<?xml version="1.0" encoding="UTF-8"?>
<quiz>"""
suffix = """
</quiz>"""
xml_export_quiz = prefix + xml + suffix
now = dt.datetime.now()
filename = f"MCQ export-{now:%Y%m%d-%H%M%S}.xml"
path = "./MCQ/" + filename
with open(path, "w", encoding="utf-8") as file:
file.write(xml_export_quiz)
return content, path
def get_question_content2(response):
function_call = response["choices"][0]["message"]["function_call"]
function_name = function_call["name"]
arguments = function_call["arguments"]
qtext = eval(arguments).get("question text")
if qtext == "N/A":
qname = eval(arguments).get("question name")
qcontent = "Name: " + qname + "\n" + "Text: " + qtext
return qcontent, [qname,qtext,"N/A","N/A","N/A","N/A"]
else:
qname = eval(arguments).get("question name")
qfdbkc = eval(arguments).get("general feedback correct")
qfdbkw = eval(arguments).get("general feedback wrong")
qoptc = eval(arguments).get("correct option")
qoptfdbkc = eval(arguments).get("correct option feedback")
qopt1 = eval(arguments).get("wrong option 1")
qoptfdbk1 = eval(arguments).get("wrong option 1 feedback")
qopt2 = eval(arguments).get("wrong option 2")
qoptfdbk2 = eval(arguments).get("wrong option 2 feedback")
qopt3 = eval(arguments).get("wrong option 3")
qoptfdbk3 = eval(arguments).get("wrong option 3 feedback")
qcontent = "Name: " + qname + "\n" + "Text: " + qtext + "\n" + "Correct option: " + qoptc + "\n" + "Wrong option 1: " + qopt1 + "\n" + "Wrong option 2: " + qopt2 + "\n" + "Wrong option 3: " + qopt3
return qcontent, [qname,qtext,qoptc,qopt1,qopt2,qopt3]
def inspera_multi_mcq(query,number):
prompt = "If you do not have relevant information on certain topic, put 'I don't know this topic' in question name and 'N/A' for question text and skip remaining properties."
messages=[
{
"role": "system",
"content": prompt
},
{
"role": "user",
"content": query
},
]
Column_header = ['QuestionType','QuestionName','QuestionText','Randomize','OptionText','IsCorrect','OptionText','IsCorrect','OptionText','IsCorrect','OptionText','IsCorrect']
question_list = np.array([Column_header])
content = ""
for x in range(number):
response = openai.ChatCompletion.create(
model = "gpt-4o",
messages = messages,
functions = function_description,
function_call = {"name": "generate_moodle_mcq"},
temperature=0.5,
max_tokens=4000
)
y = str(x+1)
question_content = get_question_content2(response)
question_preview = question_content[0] + "\n\n"
question_text = question_content[1][1]
question_variable = question_content[1]
content += "Question" + y + ":\n" + question_preview + "\n"
prompt += f"\n This topic has already been generated '{question_text}', write a different topic. \n"
refined_question_variable = ['multiple_choice',question_variable[0],question_variable[1],'1','<p>'+question_variable[2]+'</p>','1','<p>'+question_variable[3]+'</p>', '','<p>'+question_variable[4]+'</p>', '','<p>'+question_variable[5]+'</p>', '']
question_list = np.vstack([question_list,refined_question_variable])
messages=[
{
"role": "system",
"content": prompt
},
{
"role": "user",
"content": query
},
]
question_list = question_list.tolist()
now = dt.datetime.now()
filename = f"Inspera MCQ export-{now:%Y%m%d-%H%M%S}.xlsx"
path = "./Inspera/" + filename
wb = Workbook()
ws = wb.active
ws.title = "questions"
for row in question_list:
ws.append(row)
wb.save(filename=path)
return content, path |