import openai import datetime as dt def generate_stack(query): now = dt.datetime.now() prompt = "You are a online question author writing Moodle questions and provide XML format, following XML examples. Provide some comments around the random variables and math calculation after generating XML. Always include XML in reply and follow this sample STACK question format:\n\n\n\n\n \n \n Ohm's Law and Power\n \n \n \n Current: [[input:ans1]] [[validation:ans1]]
\n Power: [[input:ans2]] [[validation:ans2]]]]>
\n
\n \n Ohm's Law states that the current (I) is equal to the voltage (V) divided by the resistance (R): I = V/R. The power (P) in a circuit is given by P=IV.\n \n 1\n 0.1\n 0\n \n \n 2022082900\n \n \n V: rand_with_step(1, 10, 1);\n R: rand_with_step(1, 10, 1);\n I: V/R;\n P: I*V;\n Tans1: decimalplaces(I,2);\n Tans2: decimalplaces(P,2);\n\n \n \n [[feedback:prt2]]]]>\n \n \n Generated by Qingyang Lei using GPT4\n \n 1\n 0\n 0\n \n \n \n \n \n \n \n \n \n dot\n 1\n i\n cos-1\n lang\n [\n i\n \n ans1\n algebraic\n Tans1\n 15\n 1\n 0\n \n 0\n \n \n 0\n 0\n 0\n 1\n 1\n \n \n \n ans2\n algebraic\n Tans2\n 15\n 1\n 0\n \n 0\n \n \n 0\n 0\n 0\n 1\n 1\n \n \n \n prt1\n 1.0000000\n 1\n 1\n \n \n \n \n 0\n \n NumRelative\n ans1\n Tans1\n 0.01\n 0\n =\n 1\n \n -1\n prt1-1-T\n \n Correct answer for the current.\n \n =\n 0\n \n -1\n prt1-1-F\n \n Incorrect answer for the current. Remember, Ohm's Law states that the current is equal to the voltage divided by the resistance.\n \n \n \n \n prt2\n 1.0000000\n 1\n 1\n \n \n \n \n 0\n \n NumRelative\n ans2\n Tans2\n 0.01\n 0\n =\n 1\n \n -1\n prt2-1-T\n \n Correct answer for the power based on the model answer.\n \n =\n 0\n \n 1\n prt2-1-F\n \n Incorrect answer for the power based on the model answer. Remember, power is calculated by multiplying the current by the voltage.\n \n \n \n 1\n \n NumRelative\n ans2\n ans1*V\n 0.01\n 0\n =\n 0.5\n \n -1\n prt2-2-T\n \n Correct answer for the power based on your current answer. You get 50% of the marks.\n \n =\n 0\n \n -1\n prt2-2-F\n \n Incorrect answer for the power based on your current answer. Remember, the power is calculated by multiplying the current (your answer for the first part) by the voltage.\n \n \n \n
\n
\n\n Use function decimalplaces(x,2) on top of calculated answers, so the model answers have two decimalplaces. The correct answers in above example should be Tans1: decimalplaces(I,2)\n\n Never add comment in section.\n\n An array can be defined as T: [\"A\",\"B\",\"C\"];\nX: rand_with_step(1,3,1);\nT[X] effectively become a random text. Pay attention to each element and those closing brackets." response = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": prompt }, { "role": "user", "content": query }, ], temperature=0.6, max_tokens=4000, top_p=1, frequency_penalty=0, presence_penalty=0 ) text = response["choices"][0]["message"]["content"] part1 = text.split("") export = "" filename = f"STACK export-{now:%Y%m%d-%H%M}.xml" path = "./STACK/" + filename with open(path, "w", encoding="utf-8") as file: file.write(export) message1 = f"Finished, file name: STACK export-{now:%Y%m%d-%H%M}.xml" message2 = "\nAdditional comment from GPT (if any):" + part2[1] comment = message1 + message2 return comment, path