|
| 1 | +import os |
| 2 | +import dotenv |
| 3 | + |
| 4 | +from langchain_core.prompts import ChatPromptTemplate |
| 5 | +from langchain_core.messages import SystemMessage, HumanMessage, AIMessage |
| 6 | + |
| 7 | +#from in2lambda.api.question import Question |
| 8 | +from chains.llm_factory import LLMFactory |
| 9 | + |
| 10 | +class Question: |
| 11 | + def __init__(self): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class QuestionConverter: |
| 16 | + def __init__(self): |
| 17 | + if not dotenv.load_dotenv(): |
| 18 | + raise Exception('Error loading .env file') |
| 19 | + llm_factory_instance = LLMFactory() |
| 20 | + self.llm = llm_factory_instance.get_llm() |
| 21 | + |
| 22 | + self.examples = [ |
| 23 | + { |
| 24 | + "input": |
| 25 | + ''' |
| 26 | + A car is being towed with two ropes as shown in figure~\\ref{A1:fig:Q3}. If the resultant of the two forces is a \\SI{30}{\\N} force parallel to the long axis of the car, find: |
| 27 | + \\begin{enumerate} |
| 28 | + \\item the tension in each of the ropes, if $\\alpha = \\ang{30}$. |
| 29 | + \\item the value of $\\alpha$ such that the tension in rope, $T_2$ is minimal. |
| 30 | + \\end{enumerate} |
| 31 | + \\begin{marginfigure}[-20mm] |
| 32 | + \\centering |
| 33 | + \\includegraphics[width=\columnwidth]{problem1_3.png} |
| 34 | + \\caption{A car with two tow ropes attached.} |
| 35 | + \\label{A1:fig:Q3} |
| 36 | + \\end{marginfigure} |
| 37 | + ''', |
| 38 | + "output":( |
| 39 | + """ |
| 40 | + A car is being towed with two ropes as shown in figure~\\ref{A1:fig:Q3}. If the resultant of the two forces is a \\SI{30}{\\N} force parallel to the long axis of the car, find: |
| 41 | + """, |
| 42 | + [ |
| 43 | + """\\item the tension in each of the ropes, if $\\alpha = \\ang{30}$.""", |
| 44 | + """\\item the value of $\\alpha$ such that the tension in rope, $T_2$ is minimal.""" |
| 45 | + ]) |
| 46 | + }, |
| 47 | + { |
| 48 | + "input": |
| 49 | + """A builder pulls with a force of \\SI{300}{\\N} on a rope attached to a building as shown in figure~\\ref{A1:fig:Q1a}. What are the horizontal and vertical components of the force exerted by the rope at the point A? |
| 50 | + \\begin{figure} |
| 51 | + \\centering |
| 52 | + \\includegraphics[width=0.6\\columnwidth]{problem1_1a.png} |
| 53 | + \\caption{A builder applying \\SI{300}{\\N} to a building using a rope.} |
| 54 | + \\label{A1:fig:Q1a} |
| 55 | + \\end{figure}""", |
| 56 | + "output":( |
| 57 | + """A builder pulls with a force of \\SI{300}{\\N} on a rope attached to a building as shown in figure~\\ref{A1:fig:Q1a}. What are the horizontal and vertical components of the force exerted by the rope at the point A?""", |
| 58 | + [], |
| 59 | + ) |
| 60 | + }, |
| 61 | + { |
| 62 | + "input":"""Find the real and imaginary parts of: |
| 63 | + $ |
| 64 | + \\begin{array}[h!]{lll} |
| 65 | + {\\rm (a)}\\hskip5pt 8+3\\,i\\hskip24pt& |
| 66 | + {\\rm (b)}\\hskip5pt 4-15\\,i\\hskip24pt& |
| 67 | + {\\rm (c)}\\hskip5pt \\cos\\theta-i\\,\\sin\\theta\\\\ |
| 68 | + \\noalign{\\vskip12pt} |
| 69 | + {\\rm (d)}\\hskip5pt i^2& |
| 70 | + {\\rm (e)}\\hskip5pt i\\,(2-5\\,i)& |
| 71 | + {\\rm (f)}\\hskip5pt (1+2\\,i)(2-3\\,i) |
| 72 | + \\end{array} |
| 73 | + $""", |
| 74 | + "output":( |
| 75 | + """Find the real and imaginary parts of:""", |
| 76 | + [ |
| 77 | + """{\\rm (a)}\\hskip5pt 8+3\\,i\\hskip24pt""", |
| 78 | + """{\\rm (b)}\\hskip5pt 4-15\\,i\\hskip24pt""", |
| 79 | + """{\\rm (c)}\\hskip5pt \\cos\theta-i\\,\\sin\\theta\\\\""", |
| 80 | + """{\\rm (d)}\\hskip5pt i^2""", |
| 81 | + """{\\rm (e)}\\hskip5pt i\\,(2-5\\,i)""", |
| 82 | + """{\\rm (f)}\\hskip5pt (1+2\\,i)(2-3\\,i)""" |
| 83 | + ]) |
| 84 | + } |
| 85 | + ] |
| 86 | + |
| 87 | + |
| 88 | + def convert(self, question:str, solution:str) -> Question: |
| 89 | + ''' |
| 90 | + Convert a question and solution to a Question object |
| 91 | + it's possible solution is a list of solutions or no solution at all |
| 92 | + ''' |
| 93 | + |
| 94 | + prompt = ChatPromptTemplate.from_messages([ |
| 95 | + SystemMessage(content="""Analyze text and respond with: |
| 96 | + 1. Main Content (string) |
| 97 | + 2. Relevant parts (coma and new line seperated list) |
| 98 | + Use format: "Main Content: <string>\\nParts: <Part1>, \\n<Part2>, \\n..."""), |
| 99 | + *[ |
| 100 | + HumanMessage(content=ex["input"]) |
| 101 | + for ex in self.examples |
| 102 | + ], |
| 103 | + *[AIMessage(content=f"Main Content: {ex['output'][0]} nParts: {', '.join(ex['output'][1])}") for ex in self.examples], |
| 104 | + HumanMessage(content="{input}") |
| 105 | + ]) |
| 106 | + |
| 107 | + chain = prompt | self.llm |
| 108 | + |
| 109 | + result = chain.invoke({"input":question}) |
| 110 | + print(result) |
| 111 | + return Question() |
| 112 | + |
| 113 | +test_question = '''Write each of the following expressions as a complex number in the form $x+i\\,y$: |
| 114 | + $ |
| 115 | + \\begin{array}[h!]{lll} |
| 116 | + {\\rm (a)}\\hskip5pt (5-i)(2+3\\,i)\\hskip24pt& |
| 117 | + {\\rm (b)}\\hskip5pt (3-4\\,i)(3+4\\,i)\\hskip24pt& |
| 118 | + {\\rm (c)}\\hskip5pt (1+2\\,i)^2\\\\ |
| 119 | + \\noalign{\\vskip12pt} |
| 120 | + {\\rm (d)}\\hskip5pt \\displaystyle{10\\over4-2\\,i}& |
| 121 | + {\\rm (e)}\\hskip5pt \\displaystyle{3-i\\over4+3\\,i}& |
| 122 | + {\\rm (f)}\\hskip5pt \\displaystyle{1\\over i} |
| 123 | + \\end{array} |
| 124 | + $''' |
| 125 | +test_converter = QuestionConverter() |
| 126 | +test_converter.convert(test_question, "") |
0 commit comments