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_variable[2]+'
','1',''+question_variable[3]+'
', '',''+question_variable[4]+'
', '',''+question_variable[5]+'
', ''] 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