CoolFace
Apppublic

QL2023/Question_general

sourceHugging Facegpl-3.0updated 2y agoView on Hugging Face
0likes
stack_func.py46 linesDownload Raw Back to root
1import openai2import datetime as dt3 4def generate_stack(query):5 6 now = dt.datetime.now()7 8 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<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<quiz>\n<!-- question: 47897124  -->\n  <question type=\"stack\">\n    <name>\n      <text>Ohm's Law and Power</text>\n    </name>\n    <questiontext format=\"html\">\n      <text><![CDATA[Using Ohm's Law, calculate the current (I) and the power (P) in a circuit where the voltage (V) is {@V@} volts and the resistance (R) is {@R@} ohms.<br/>\n            Current: [[input:ans1]] [[validation:ans1]]<br/>\n            Power: [[input:ans2]] [[validation:ans2]]]]></text>\n    </questiontext>\n    <generalfeedback format=\"html\">\n      <text>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.</text>\n    </generalfeedback>\n    <defaultgrade>1</defaultgrade>\n    <penalty>0.1</penalty>\n    <hidden>0</hidden>\n    <idnumber></idnumber>\n    <stackversion>\n      <text>2022082900</text>\n    </stackversion>\n    <questionvariables>\n      <text>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</text>\n    </questionvariables>\n    <specificfeedback format=\"html\">\n      <text><![CDATA[[[feedback:prt1]]<br/>[[feedback:prt2]]]]></text>\n    </specificfeedback>\n    <questionnote>\n      <text>Generated by Qingyang Lei using GPT4</text>\n    </questionnote>\n    <questionsimplify>1</questionsimplify>\n    <assumepositive>0</assumepositive>\n    <assumereal>0</assumereal>\n    <prtcorrect format=\"html\">\n      <text></text>\n    </prtcorrect>\n    <prtpartiallycorrect format=\"html\">\n      <text></text>\n    </prtpartiallycorrect>\n    <prtincorrect format=\"html\">\n      <text></text>\n    </prtincorrect>\n    <multiplicationsign>dot</multiplicationsign>\n    <sqrtsign>1</sqrtsign>\n    <complexno>i</complexno>\n    <inversetrig>cos-1</inversetrig>\n    <logicsymbol>lang</logicsymbol>\n    <matrixparens>[</matrixparens>\n    <variantsselectionseed>i</variantsselectionseed>\n    <input>\n      <name>ans1</name>\n      <type>algebraic</type>\n      <tans>Tans1</tans>\n      <boxsize>15</boxsize>\n      <strictsyntax>1</strictsyntax>\n      <insertstars>0</insertstars>\n      <syntaxhint></syntaxhint>\n      <syntaxattribute>0</syntaxattribute>\n      <forbidwords></forbidwords>\n      <allowwords></allowwords>\n      <forbidfloat>0</forbidfloat>\n      <requirelowestterms>0</requirelowestterms>\n      <checkanswertype>0</checkanswertype>\n      <mustverify>1</mustverify>\n      <showvalidation>1</showvalidation>\n      <options></options>\n    </input>\n    <input>\n      <name>ans2</name>\n      <type>algebraic</type>\n      <tans>Tans2</tans>\n      <boxsize>15</boxsize>\n      <strictsyntax>1</strictsyntax>\n      <insertstars>0</insertstars>\n      <syntaxhint></syntaxhint>\n      <syntaxattribute>0</syntaxattribute>\n      <forbidwords></forbidwords>\n      <allowwords></allowwords>\n      <forbidfloat>0</forbidfloat>\n      <requirelowestterms>0</requirelowestterms>\n      <checkanswertype>0</checkanswertype>\n      <mustverify>1</mustverify>\n      <showvalidation>1</showvalidation>\n      <options></options>\n    </input>\n    <prt>\n      <name>prt1</name>\n      <value>1.0000000</value>\n      <autosimplify>1</autosimplify>\n      <feedbackstyle>1</feedbackstyle>\n      <feedbackvariables>\n        <text></text>\n      </feedbackvariables>\n      <node>\n        <name>0</name>\n  <description></description>\n      <answertest>NumRelative</answertest>\n        <sans>ans1</sans>\n        <tans>Tans1</tans>\n        <testoptions>0.01</testoptions>\n        <quiet>0</quiet>\n        <truescoremode>=</truescoremode>\n        <truescore>1</truescore>\n        <truepenalty></truepenalty>\n        <truenextnode>-1</truenextnode>\n        <trueanswernote>prt1-1-T</trueanswernote>\n        <truefeedback format=\"html\">\n          <text>Correct answer for the current.</text>\n        </truefeedback>\n        <falsescoremode>=</falsescoremode>\n        <falsescore>0</falsescore>\n        <falsepenalty></falsepenalty>\n        <falsenextnode>-1</falsenextnode>\n        <falseanswernote>prt1-1-F</falseanswernote>\n        <falsefeedback format=\"html\">\n          <text>Incorrect answer for the current. Remember, Ohm's Law states that the current is equal to the voltage divided by the resistance.</text>\n        </falsefeedback>\n      </node>\n    </prt>\n    <prt>\n      <name>prt2</name>\n      <value>1.0000000</value>\n      <autosimplify>1</autosimplify>\n      <feedbackstyle>1</feedbackstyle>\n      <feedbackvariables>\n        <text></text>\n      </feedbackvariables>\n      <node>\n        <name>0</name>\n    <description></description>\n    <answertest>NumRelative</answertest>\n        <sans>ans2</sans>\n        <tans>Tans2</tans>\n        <testoptions>0.01</testoptions>\n        <quiet>0</quiet>\n        <truescoremode>=</truescoremode>\n        <truescore>1</truescore>\n        <truepenalty></truepenalty>\n        <truenextnode>-1</truenextnode>\n        <trueanswernote>prt2-1-T</trueanswernote>\n        <truefeedback format=\"html\">\n          <text>Correct answer for the power based on the model answer.</text>\n        </truefeedback>\n        <falsescoremode>=</falsescoremode>\n        <falsescore>0</falsescore>\n        <falsepenalty></falsepenalty>\n        <falsenextnode>1</falsenextnode>\n        <falseanswernote>prt2-1-F</falseanswernote>\n        <falsefeedback format=\"html\">\n          <text>Incorrect answer for the power based on the model answer. Remember, power is calculated by multiplying the current by the voltage.</text>\n        </falsefeedback>\n      </node>\n      <node>\n        <name>1</name>\n    <description></description>\n    <answertest>NumRelative</answertest>\n        <sans>ans2</sans>\n        <tans>ans1*V</tans>\n        <testoptions>0.01</testoptions>\n        <quiet>0</quiet>\n        <truescoremode>=</truescoremode>\n        <truescore>0.5</truescore>\n        <truepenalty></truepenalty>\n        <truenextnode>-1</truenextnode>\n        <trueanswernote>prt2-2-T</trueanswernote>\n        <truefeedback format=\"html\">\n          <text>Correct answer for the power based on your current answer. You get 50% of the marks.</text>\n        </truefeedback>\n        <falsescoremode>=</falsescoremode>\n        <falsescore>0</falsescore>\n        <falsepenalty></falsepenalty>\n        <falsenextnode>-1</falsenextnode>\n        <falseanswernote>prt2-2-F</falseanswernote>\n        <falsefeedback format=\"html\">\n          <text>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.</text>\n        </falsefeedback>\n      </node>\n    </prt>\n  </question>\n</quiz>\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 <questionvariables> 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."9 10 response = openai.ChatCompletion.create(11  model="gpt-4",12  messages=[13    {14      "role": "system",15      "content": prompt16    },17    {18      "role": "user",19      "content": query20    },21  ],22  temperature=0.6,23  max_tokens=4000,24  top_p=1,25  frequency_penalty=0,26  presence_penalty=027 )28 29 text = response["choices"][0]["message"]["content"]30 31 part1 = text.split("<?xml")32 part2 = part1[1].split("</quiz>")33 export = "<?xml" + part2[0] + "</quiz>"34 35 filename = f"STACK export-{now:%Y%m%d-%H%M}.xml"36 37 path = "./STACK/" + filename38 39 with open(path, "w", encoding="utf-8") as file:40    file.write(export)41 42 message1 = f"Finished, file name: STACK export-{now:%Y%m%d-%H%M}.xml"43 message2 = "\nAdditional comment from GPT (if any):" + part2[1]44 comment = message1 + message245 46 return comment, path