From e253e86a50f97e4b7809e726e37c3dd0e4747e9a Mon Sep 17 00:00:00 2001 From: WxMichelleYang Date: Mon, 24 Feb 2025 13:29:17 +0000 Subject: [PATCH 1/5] using few-short prompting to extract main content and parts from input questions --- wizard/to_question.py | 126 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 wizard/to_question.py diff --git a/wizard/to_question.py b/wizard/to_question.py new file mode 100644 index 0000000..d48c2a6 --- /dev/null +++ b/wizard/to_question.py @@ -0,0 +1,126 @@ +import os +import dotenv + +from langchain_core.prompts import ChatPromptTemplate +from langchain_core.messages import SystemMessage, HumanMessage, AIMessage + +#from in2lambda.api.question import Question +from chains.llm_factory import LLMFactory + +class Question: + def __init__(self): + pass + + +class QuestionConverter: + def __init__(self): + if not dotenv.load_dotenv(): + raise Exception('Error loading .env file') + llm_factory_instance = LLMFactory() + self.llm = llm_factory_instance.get_llm() + + self.examples = [ + { + "input": + ''' + 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: + \\begin{enumerate} + \\item the tension in each of the ropes, if $\\alpha = \\ang{30}$. + \\item the value of $\\alpha$ such that the tension in rope, $T_2$ is minimal. + \\end{enumerate} + \\begin{marginfigure}[-20mm] + \\centering + \\includegraphics[width=\columnwidth]{problem1_3.png} + \\caption{A car with two tow ropes attached.} + \\label{A1:fig:Q3} + \\end{marginfigure} + ''', + "output":( + """ + 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: + """, + [ + """\\item the tension in each of the ropes, if $\\alpha = \\ang{30}$.""", + """\\item the value of $\\alpha$ such that the tension in rope, $T_2$ is minimal.""" + ]) + }, + { + "input": + """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? + \\begin{figure} + \\centering + \\includegraphics[width=0.6\\columnwidth]{problem1_1a.png} + \\caption{A builder applying \\SI{300}{\\N} to a building using a rope.} + \\label{A1:fig:Q1a} + \\end{figure}""", + "output":( + """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?""", + [], + ) + }, + { + "input":"""Find the real and imaginary parts of: + $ + \\begin{array}[h!]{lll} + {\\rm (a)}\\hskip5pt 8+3\\,i\\hskip24pt& + {\\rm (b)}\\hskip5pt 4-15\\,i\\hskip24pt& + {\\rm (c)}\\hskip5pt \\cos\\theta-i\\,\\sin\\theta\\\\ + \\noalign{\\vskip12pt} + {\\rm (d)}\\hskip5pt i^2& + {\\rm (e)}\\hskip5pt i\\,(2-5\\,i)& + {\\rm (f)}\\hskip5pt (1+2\\,i)(2-3\\,i) + \\end{array} + $""", + "output":( + """Find the real and imaginary parts of:""", + [ + """{\\rm (a)}\\hskip5pt 8+3\\,i\\hskip24pt""", + """{\\rm (b)}\\hskip5pt 4-15\\,i\\hskip24pt""", + """{\\rm (c)}\\hskip5pt \\cos\theta-i\\,\\sin\\theta\\\\""", + """{\\rm (d)}\\hskip5pt i^2""", + """{\\rm (e)}\\hskip5pt i\\,(2-5\\,i)""", + """{\\rm (f)}\\hskip5pt (1+2\\,i)(2-3\\,i)""" + ]) + } + ] + + + def convert(self, question:str, solution:str) -> Question: + ''' + Convert a question and solution to a Question object + it's possible solution is a list of solutions or no solution at all + ''' + + prompt = ChatPromptTemplate.from_messages([ + SystemMessage(content="""Analyze text and respond with: + 1. Main Content (string) + 2. Relevant parts (coma and new line seperated list) + Use format: "Main Content: \\nParts: , \\n, \\n..."""), + *[ + HumanMessage(content=ex["input"]) + for ex in self.examples + ], + *[AIMessage(content=f"Main Content: {ex['output'][0]} nParts: {', '.join(ex['output'][1])}") for ex in self.examples], + HumanMessage(content="{input}") + ]) + + chain = prompt | self.llm + + result = chain.invoke({"input":question}) + print(result) + return Question() + +test_question = '''Write each of the following expressions as a complex number in the form $x+i\\,y$: + $ + \\begin{array}[h!]{lll} + {\\rm (a)}\\hskip5pt (5-i)(2+3\\,i)\\hskip24pt& + {\\rm (b)}\\hskip5pt (3-4\\,i)(3+4\\,i)\\hskip24pt& + {\\rm (c)}\\hskip5pt (1+2\\,i)^2\\\\ + \\noalign{\\vskip12pt} + {\\rm (d)}\\hskip5pt \\displaystyle{10\\over4-2\\,i}& + {\\rm (e)}\\hskip5pt \\displaystyle{3-i\\over4+3\\,i}& + {\\rm (f)}\\hskip5pt \\displaystyle{1\\over i} + \\end{array} + $''' +test_converter = QuestionConverter() +test_converter.convert(test_question, "") From 894d9303d163b1687d993c7974dd4efe49896ff3 Mon Sep 17 00:00:00 2001 From: WxMichelleYang Date: Mon, 24 Feb 2025 13:46:22 +0000 Subject: [PATCH 2/5] the example questions used as examples in prompt and test --- wizard/example_questions/q1.tex | 7 +++++++ wizard/example_questions/q2.tex | 7 +++++++ wizard/example_questions/q3.tex | 11 +++++++++++ wizard/example_questions/q4.tex | 7 +++++++ wizard/example_questions/q5.tex | 13 +++++++++++++ wizard/example_questions/q6.tex | 13 +++++++++++++ wizard/example_questions/q7.tex | 10 ++++++++++ wizard/example_questions/s1.tex | 23 ++++++++++++++++++++++ wizard/example_questions/s2.tex | 19 ++++++++++++++++++ wizard/example_questions/s3.tex | 34 +++++++++++++++++++++++++++++++++ wizard/example_questions/s4.tex | 14 ++++++++++++++ wizard/example_questions/s5.tex | 20 +++++++++++++++++++ wizard/example_questions/s6.tex | 20 +++++++++++++++++++ wizard/example_questions/s7.tex | 18 +++++++++++++++++ 14 files changed, 216 insertions(+) create mode 100644 wizard/example_questions/q1.tex create mode 100644 wizard/example_questions/q2.tex create mode 100644 wizard/example_questions/q3.tex create mode 100644 wizard/example_questions/q4.tex create mode 100644 wizard/example_questions/q5.tex create mode 100644 wizard/example_questions/q6.tex create mode 100644 wizard/example_questions/q7.tex create mode 100644 wizard/example_questions/s1.tex create mode 100644 wizard/example_questions/s2.tex create mode 100644 wizard/example_questions/s3.tex create mode 100644 wizard/example_questions/s4.tex create mode 100644 wizard/example_questions/s5.tex create mode 100644 wizard/example_questions/s6.tex create mode 100644 wizard/example_questions/s7.tex diff --git a/wizard/example_questions/q1.tex b/wizard/example_questions/q1.tex new file mode 100644 index 0000000..7a76049 --- /dev/null +++ b/wizard/example_questions/q1.tex @@ -0,0 +1,7 @@ +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? +\begin{figure} + \centering + \includegraphics[width=0.6\columnwidth]{problem1_1a.png} + \caption{A builder applying \SI{300}{\N} to a building using a rope.} + \label{A1:fig:Q1a} +\end{figure} \ No newline at end of file diff --git a/wizard/example_questions/q2.tex b/wizard/example_questions/q2.tex new file mode 100644 index 0000000..1ee0062 --- /dev/null +++ b/wizard/example_questions/q2.tex @@ -0,0 +1,7 @@ +Four forces act on a bolt as shown in figure~\ref{A1:fig:Q2}. Determine the resultant of the forces on the bolt. +\begin{figure} + \centering + \includegraphics[width=0.65\columnwidth]{problem1_2.png} + \caption{A bolt with four forces acting on it.} + \label{A1:fig:Q2} +\end{figure} \ No newline at end of file diff --git a/wizard/example_questions/q3.tex b/wizard/example_questions/q3.tex new file mode 100644 index 0000000..c90be18 --- /dev/null +++ b/wizard/example_questions/q3.tex @@ -0,0 +1,11 @@ +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: +\begin{enumerate} + \item the tension in each of the ropes, if $\alpha = \ang{30}$. + \item the value of $\alpha$ such that the tension in rope, $T_2$ is minimal. +\end{enumerate} +\begin{marginfigure}[-20mm] + \centering + \includegraphics[width=\columnwidth]{problem1_3.png} + \caption{A car with two tow ropes attached.} + \label{A1:fig:Q3} +\end{marginfigure} \ No newline at end of file diff --git a/wizard/example_questions/q4.tex b/wizard/example_questions/q4.tex new file mode 100644 index 0000000..1d4904b --- /dev/null +++ b/wizard/example_questions/q4.tex @@ -0,0 +1,7 @@ +A \SI{30}{\N} force acts on the end of a \SI{3}{\m} lever as shown in figure~\ref{A1:fig:Q4a}. Determine the moment of the force about the point O. +\begin{figure} + \centering + \includegraphics[width=0.8\columnwidth]{problem1_4a.png} + \caption{A lever with a single force acting on it.} + \label{A1:fig:Q4a} +\end{figure} \ No newline at end of file diff --git a/wizard/example_questions/q5.tex b/wizard/example_questions/q5.tex new file mode 100644 index 0000000..e72d23a --- /dev/null +++ b/wizard/example_questions/q5.tex @@ -0,0 +1,13 @@ +Find the real and imaginary parts of: + +$ +\begin{array}[h!]{lll} +{\rm (a)}\hskip5pt 8+3\,i\hskip24pt& +{\rm (b)}\hskip5pt 4-15\,i\hskip24pt& +{\rm (c)}\hskip5pt \cos\theta-i\,\sin\theta\\ +\noalign{\vskip12pt} +{\rm (d)}\hskip5pt i^2& +{\rm (e)}\hskip5pt i\,(2-5\,i)& +{\rm (f)}\hskip5pt (1+2\,i)(2-3\,i) +\end{array} +$ \ No newline at end of file diff --git a/wizard/example_questions/q6.tex b/wizard/example_questions/q6.tex new file mode 100644 index 0000000..ef42b5a --- /dev/null +++ b/wizard/example_questions/q6.tex @@ -0,0 +1,13 @@ +Write each of the following expressions as a complex number in the form $x+i\,y$: + +$ +\begin{array}[h!]{lll} +{\rm (a)}\hskip5pt (5-i)(2+3\,i)\hskip24pt& +{\rm (b)}\hskip5pt (3-4\,i)(3+4\,i)\hskip24pt& +{\rm (c)}\hskip5pt (1+2\,i)^2\\ +\noalign{\vskip12pt} +{\rm (d)}\hskip5pt \displaystyle{10\over4-2\,i}& +{\rm (e)}\hskip5pt \displaystyle{3-i\over4+3\,i}& +{\rm (f)}\hskip5pt \displaystyle{1\over i} +\end{array} +$ \ No newline at end of file diff --git a/wizard/example_questions/q7.tex b/wizard/example_questions/q7.tex new file mode 100644 index 0000000..7c36e04 --- /dev/null +++ b/wizard/example_questions/q7.tex @@ -0,0 +1,10 @@ +Define $z=(5+7\,i)(5+b\,i)$. + +$ +\begin{array}[h!]{l} +{\rm (a)}\hskip5pt {\rm If}\ b\ {\rm and}\ z\ {\rm are\ both\ real,\ find}\ b.\\ +\noalign{\vskip12pt} +{\rm (b)}\hskip5pt {\rm If}\ {\rm Im}(b)={4\over5},\ {\rm and}\ z\ {\rm is\ pure\ imaginary,\ find}\ +{\rm Re}(b). +\end{array} +$ \ No newline at end of file diff --git a/wizard/example_questions/s1.tex b/wizard/example_questions/s1.tex new file mode 100644 index 0000000..1abf99e --- /dev/null +++ b/wizard/example_questions/s1.tex @@ -0,0 +1,23 @@ +The angle $\alpha$ between the rope and the horizontal line can be calculated using the relation between $\tan\alpha$ and the lengths of the sides of the right angled triangle as shown in figure~\ref{A1:fig:Q1b}, +\begin{align*} + \tan\alpha &=\frac{\SI{6}{\m}}{\SI{8}{\m}}\\ + &=0.75 \, , +\end{align*} +from which $\alpha=\ang{36.87}$. +\begin{marginfigure} +\centering +\includegraphics[width=\columnwidth]{problem1_1b.png} +\caption{The right angle triangle formed by the rope.} +\label{A1:fig:Q1b} +\end{marginfigure} + +The horizontal component, $F_H$, is the magnitude of the force projected onto the horizontal axis, +\begin{align*} + F_H &=\SI{300}{\N} \cos\alpha \\ + &= \SI{240}{\N} \,, +\end{align*} and the vertical component, $F_V$, is the magnitude of the force projected onto the vertical axis, +\begin{align*} + F_V &=\SI{300}{\N} \sin\alpha \\ + &= \SI{180}{\N}\, . +\end{align*} +\clearpage diff --git a/wizard/example_questions/s2.tex b/wizard/example_questions/s2.tex new file mode 100644 index 0000000..9162bef --- /dev/null +++ b/wizard/example_questions/s2.tex @@ -0,0 +1,19 @@ +To obtain the resultant force, the projection of the resultant on the vertical and horizontal axes is determined as the sum of the projections of the four forces onto the respective axes, +\begin{align*} + R_H &= F_1 \cos\ang{30} - F_2\sin\ang{20} + F_4 \cos\ang{15}\\ + &= \SI{199.1}{\N} \,,\\ + R_V &= F_1 \sin\ang{30} +F_2 \cos\ang{20}-F_3 -F_4\sin\ang{15}\\ + &= \SI{14.3}{\N} \, . +\end{align*} +The angle with the horizontal then follows from +\begin{align*} + \frac{R_V}{R_H} &= \frac{R\sin\alpha}{R\cos\alpha}\\ + &= \tan\alpha \\ + &=0.0718\, . +\end{align*} +Hence, $\alpha=\ang{4.1}$. The magnitude of the resultant follows from, +\begin{align*} + |R| &=\sqrt{R_H^2+R_V^2}\\ + &= \SI{199.6}{\N} +\end{align*} +\clearpage \ No newline at end of file diff --git a/wizard/example_questions/s3.tex b/wizard/example_questions/s3.tex new file mode 100644 index 0000000..20542e2 --- /dev/null +++ b/wizard/example_questions/s3.tex @@ -0,0 +1,34 @@ +In order for the resultant force to be a force with magnitude \SI{30}{\N} oriented along the horizontal, the following conditions must be true, +\begin{align} + T_2 \cos\alpha + T_1 \cos\ang{20} &= \SI{30}{\N} \\ + T_2 \sin\alpha - T_1 \sin\ang{20} &= 0 \,. +\end{align} +Hence, +\begin{align} + T_2 &= T_1\frac{\sin\ang{20}}{\sin\alpha} \label{A1:eq:Q3:T1}\\ + \left( T_1 \frac{\sin\ang{20}}{\sin\alpha} \right) \cos\alpha &+ T_1\cos\ang{20} = \SI{30}{\N} \nonumber \\ + T_1 &= \frac{\SI{30}{\N}}{\frac{\sin\ang{20}}{\tan\alpha} + \cos\ang{20}} \,. \label{A1:eq:Q3:T2} +\end{align} +When $\alpha=\ang{30}$, this yields +\begin{align*} + T_1&=\SI{19.58}{\N}\\ + T_2&=\SI{13.39}{\N} +\end{align*} + +Substituting the expression for $T_1$, equation~\ref{A1:eq:Q3:T1}, into the expression for $T_2$, equation~\ref{A1:eq:Q3:T2} allows us to determine $T_2$ as a function of $\alpha$, +\begin{align} + T_2 &=\frac{\SI{30}{\N}}{\frac{\sin\ang{20}}{\tan\alpha} + \cos\ang{20}} \frac{\sin\ang{20}}{\sin\alpha} \nonumber \\ + &=\frac{\SI{30}{\N}} {\sin\ang{20}\frac{\cos\alpha}{\sin\alpha} + \cos\ang{20}} \frac{\sin\ang{20}}{\sin\alpha} \nonumber \\ + &=\frac{\SI{30}{\N}} {\sin\ang{20}\frac{\cos\alpha}{\sin\alpha}\frac{\sin\alpha}{\sin\ang{20}} + \cos\ang{20}\frac{\sin\alpha}{\sin\ang{20}}} \nonumber \\ + &=\frac{\SI{30}{\N}} {\cancel{\sin\ang{20}} \frac{\cos\alpha} {\cancel{\sin\alpha}} \frac{\cancel{\sin\alpha}}{\cancel{\sin\ang{20}}} + \cos\ang{20}\frac{\sin\alpha}{\sin\ang{20}}} \nonumber \\ + &=\frac{\SI{30}{\N}}{\cos\alpha + \frac{\sin\alpha}{\tan\ang{20}}} \,. +\end{align} +Examining the form of this, we can see that $T_2$ will be minimal at $\alpha_{min}$ when $\left(\cos\alpha + \frac{\sin\alpha}{\tan\ang{20}}\right)$ is maximal. Searching for maximal stationary points in the usual manner, +\begin{align} + \frac{\mathrm{d}}{\mathrm{d}\alpha} \left(\cos\alpha + \frac{\sin\alpha}{\tan\ang{20}}\right) &= 0 \nonumber\\ + &= -\sin\alpha_{min} +\frac{\cos\alpha_{min}}{\tan\ang{20}} \nonumber \\ + \tan\alpha_{min} &= \frac{1}{\tan\ang{20}} \label{A1:eq:Q3:alpha}\\ + \Rightarrow\,\alpha_{min} &= \ang{70} \nonumber +\end{align} +Hence, $T_2$ is minimal when $T_2$ is perpendicular to $T_1$.\sidenote[][]{This is true for all angles of $T_2$, see if you can convince yourself of this by examining equation~\ref{A1:eq:Q3:alpha} analytically.} +\clearpage \ No newline at end of file diff --git a/wizard/example_questions/s4.tex b/wizard/example_questions/s4.tex new file mode 100644 index 0000000..4e6afcb --- /dev/null +++ b/wizard/example_questions/s4.tex @@ -0,0 +1,14 @@ +We can make many problems easier by choosing a suitable set of axes. Looking at the special directions of this system, we can choose to orient the x-axis parallel to the line connecting point $O$ and the point where the force acts and the y-axis perpendicular to this, as shown in figure~\ref{A1:fig:Q4b}. The moment of the force is then given by +\begin{align*} + M &= F_y\, d \\ + &= \SI{30}{\N} \sin\ang{20} \SI{3}{\m}\\ + &= \SI{30.8}{\N} \,. +\end{align*} +\begin{figure} + \centering + \includegraphics[width=0.8\columnwidth]{problem1_4b.png} + \caption{The x-axis oriented parallel to the line between $O$ and the force's point of action.} + \label{A1:fig:Q4b} +\end{figure} +We can think of this in two ways. From one viewpoint the moment is the component of the force parallel to the y-axis, multiplied by the distance from the pivot. From the other viewpoint, the moment is the whole force multiple by the perpendicular distance from the pivot. Note that these are equivalent, it just depends where you choose to group the $\sin\ang{20}$ term. This is because only the component of the force that is non-parallel to the line joining the point of action and the pivot will induce rotation. +\clearpage \ No newline at end of file diff --git a/wizard/example_questions/s5.tex b/wizard/example_questions/s5.tex new file mode 100644 index 0000000..95c21fc --- /dev/null +++ b/wizard/example_questions/s5.tex @@ -0,0 +1,20 @@ +For a complex number $a+i\,b$, in which $a$ and $b$ are real, the real and imaginary parts +are given by ${\rm Re}(a+i\,b)=a$ and ${\rm Im}(a+i\,b)=b$, respectively. Thus, + +$ +\begin{array}[h!]{l} +{\rm (a)}\hskip5pt {\rm Re}(8+3\,i)=8\, ,\qquad{\rm Im}(8+3\,i)=3\, .\\ +\noalign{\vskip12pt} +{\rm (b)}\hskip5pt {\rm Re}(4-15\,i)=4\, ,\qquad {\rm Im}(4-15\,i)=-15\, .\\ +\noalign{\vskip12pt} +{\rm (c)}\hskip5pt {\rm Re}(\cos\theta-i\,\sin\theta)=\cos\theta\, ,\qquad {\rm +Im}(\cos\theta-i\,\sin\theta)=-\sin\theta\, .\\ +\noalign{\vskip12pt} +{\rm (d)}\hskip5pt i^2=-1.\quad {\rm Re}(i^2)=-1\, ,\qquad {\rm Im}(i^2)=0\, .\\ +\noalign{\vskip12pt} +{\rm (e)}\hskip5pt i\,(2-5\,i)=5+2\,i.\qquad {\rm Re}(5+2\,i)=5\, ,\qquad {\rm Im}(5+2\,i)=2\, .\\ +\noalign{\vskip12pt} +{\rm (f)}\hskip5pt (1+2\,i)(2-3\,i)=2-3\,i+4\,i+6=8+i\, .\qquad {\rm Re}(8+i)=8\, ,\qquad {\rm +Im}(8+i)=1\, . +\end{array} +$ diff --git a/wizard/example_questions/s6.tex b/wizard/example_questions/s6.tex new file mode 100644 index 0000000..9f70cb8 --- /dev/null +++ b/wizard/example_questions/s6.tex @@ -0,0 +1,20 @@ +Applying the rules for the multiplication and division of + complex numbers yields: + +$ +\begin{array}[h!]{lll} +{\rm (a)}\hskip5pt (5-i)(2+3\,i)=10+15\,i-2\,i+3=13+13\,i\, .\\ +\noalign{\vskip12pt} +{\rm (b)}\hskip5pt (3-4\,i)(3+4\,i)=9+12\,i-12\,i+16=25\, .\\ +\noalign{\vskip12pt} +{\rm (c)}\hskip5pt (1+2\,i)^2=(1+2\,i)(1+2\,i)=1+2\,i+2\,i-4=-3+4\,i\, .\\ +\noalign{\vskip12pt} +{\rm (d)}\hskip5pt \displaystyle{{10\over4-2\,i}={10\over4-2\,i}\times{4+2\,i\over4+2\,i}= +{40+20\,i\over16+8\,i-8\,i+4}={40+20\,i\over20}=2+i\, .}\\ +\noalign{\vskip12pt} +{\rm (e)}\hskip5pt \displaystyle{{3-i\over4+3\,i}={3-i\over4+3\,i}\times{4-3\,i\over4-3\,i}= +{12-9\,i-4\,i-3\over16-12\,i+12\,i+9}={9-13\,i\over25}={9\over25}-i\,{13\over25}\, .}\\ +\noalign{\vskip12pt} +{\rm (f)}\hskip5pt \displaystyle{{1\over i}={1\over i}\times{-i\over-i}=-i\, .} +\end{array} +$ \ No newline at end of file diff --git a/wizard/example_questions/s7.tex b/wizard/example_questions/s7.tex new file mode 100644 index 0000000..b446b36 --- /dev/null +++ b/wizard/example_questions/s7.tex @@ -0,0 +1,18 @@ +We have that $z=(5+7\,i)(5+b\,i)=25+5b\,i+35\,i-7b$. + +$ +\begin{array}[h!]{l} +{\rm (a)}\hskip5pt {\rm If}\ b\ {\rm and}\ z\ {\rm are\ both\ real},\ {\rm then\ the \ imaginary\ +parts\ of\ both\ quantities\ vanish.}\ {\rm Thus,}\\ +\noalign{\vskip12pt} +\hskip20pt {\rm Im}(z)=35+5b=0, {\rm so}\ b=-7.\\ +\noalign{\vskip12pt} +{\rm (b)}\hskip5pt {\rm If}\ {\rm Im}(b)={4\over5},\ {\rm and}\ z\ {\rm is\ pure\ imaginary,}\ +{\rm then\ the\ real\ part\ of}\ z\ {\rm vanishes\!:}\\ +\noalign{\vskip12pt} +\hskip20pt{\rm Re}(z)=25+5\bigl[i\,{\rm Im}(b)\bigr]i-7\,{\rm Re}(b)=25-4-7\,{\rm Re}(b)=21-7\,{\rm +Re}(b)=0,\\ +\noalign{\vskip12pt} +\hskip20pt{\rm so}\ {\rm Re}(b)=3. +\end{array} +$ From 3aa8d64ab2644bed4b38dd9fd4145a619757c494 Mon Sep 17 00:00:00 2001 From: WxMichelleYang Date: Mon, 24 Feb 2025 16:17:46 +0000 Subject: [PATCH 3/5] fix a bug of always displaying an example question --- wizard/to_question.py | 56 +++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/wizard/to_question.py b/wizard/to_question.py index d48c2a6..2d813b5 100644 --- a/wizard/to_question.py +++ b/wizard/to_question.py @@ -3,6 +3,8 @@ from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import SystemMessage, HumanMessage, AIMessage +from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate, FewShotChatMessagePromptTemplate + #from in2lambda.api.question import Question from chains.llm_factory import LLMFactory @@ -19,6 +21,7 @@ def __init__(self): llm_factory_instance = LLMFactory() self.llm = llm_factory_instance.get_llm() + self.examples = [ { "input": @@ -90,22 +93,49 @@ def convert(self, question:str, solution:str) -> Question: Convert a question and solution to a Question object it's possible solution is a list of solutions or no solution at all ''' + + # This is a prompt template used to format each individual example. + example_prompt = ChatPromptTemplate.from_messages( + [ + ("human", "{input}"), + ("ai", "{output}"), + ] + ) + few_shot_prompt = FewShotChatMessagePromptTemplate( + example_prompt=example_prompt, + examples=self.examples, + ) + + final_prompt = ChatPromptTemplate.from_messages( + [ + ("system", """You are intelligent assistant to process the given input question, + Please analyze the input question and respond with: + 1. Main Content (String). + 2. Relevant parts (Comma and new line separated list). + Use format: "Main Content: \\nParts: , \\n, \\n..."""), + few_shot_prompt, + ("human", "{input}"), + ] +) + + # print(few_shot_prompt.format()) - prompt = ChatPromptTemplate.from_messages([ - SystemMessage(content="""Analyze text and respond with: - 1. Main Content (string) - 2. Relevant parts (coma and new line seperated list) - Use format: "Main Content: \\nParts: , \\n, \\n..."""), - *[ - HumanMessage(content=ex["input"]) - for ex in self.examples - ], - *[AIMessage(content=f"Main Content: {ex['output'][0]} nParts: {', '.join(ex['output'][1])}") for ex in self.examples], - HumanMessage(content="{input}") - ]) + # prompt = ChatPromptTemplate.from_messages([ + # SystemMessage(content="""Analyze text and respond with: + # 1. Main Content (string) + # 2. Relevant parts (coma and new line seperated list) + # Use format: "Main Content: \\nParts: , \\n, \\n..."""), + # *[ + # HumanMessage(content=ex["input"]) + # for ex in self.examples + # ], + # *[AIMessage(content=f"Main Content: {ex['output'][0]} Parts: {', '.join(ex['output'][1])}") for ex in self.examples], + # HumanMessage(content="{input}") + # ]) - chain = prompt | self.llm + chain = final_prompt | self.llm + print(question) result = chain.invoke({"input":question}) print(result) return Question() From a792a249a0760e21505fce911963b03ab702dfda Mon Sep 17 00:00:00 2001 From: WxMichelleYang Date: Mon, 24 Feb 2025 16:22:29 +0000 Subject: [PATCH 4/5] fixed formats --- wizard/to_question.py | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/wizard/to_question.py b/wizard/to_question.py index 2d813b5..253d4c3 100644 --- a/wizard/to_question.py +++ b/wizard/to_question.py @@ -107,31 +107,16 @@ def convert(self, question:str, solution:str) -> Question: ) final_prompt = ChatPromptTemplate.from_messages( - [ - ("system", """You are intelligent assistant to process the given input question, - Please analyze the input question and respond with: - 1. Main Content (String). - 2. Relevant parts (Comma and new line separated list). - Use format: "Main Content: \\nParts: , \\n, \\n..."""), - few_shot_prompt, - ("human", "{input}"), - ] -) - - # print(few_shot_prompt.format()) - - # prompt = ChatPromptTemplate.from_messages([ - # SystemMessage(content="""Analyze text and respond with: - # 1. Main Content (string) - # 2. Relevant parts (coma and new line seperated list) - # Use format: "Main Content: \\nParts: , \\n, \\n..."""), - # *[ - # HumanMessage(content=ex["input"]) - # for ex in self.examples - # ], - # *[AIMessage(content=f"Main Content: {ex['output'][0]} Parts: {', '.join(ex['output'][1])}") for ex in self.examples], - # HumanMessage(content="{input}") - # ]) + [ + ("system", """You are intelligent assistant to process the given input question, + Please analyze the input question and respond with: + 1. Main Content (String). + 2. Relevant parts (Comma and new line separated list). + Use format: "Main Content: \\nParts: , \\n, \\n..."""), + few_shot_prompt, + ("human", "{input}"), + ] + ) chain = final_prompt | self.llm @@ -152,5 +137,6 @@ def convert(self, question:str, solution:str) -> Question: {\\rm (f)}\\hskip5pt \\displaystyle{1\\over i} \\end{array} $''' +test_question2 = "Which city is the capital of China? \\item Beijing \\item Shanghai \\item Guangzhou" test_converter = QuestionConverter() -test_converter.convert(test_question, "") +test_converter.convert(test_question2, "") From 2e8b4ed61eb3d74a494b3e274c57057f3a9376c3 Mon Sep 17 00:00:00 2001 From: WxMichelleYang Date: Mon, 24 Feb 2025 16:26:46 +0000 Subject: [PATCH 5/5] fixed slashes --- wizard/to_question.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wizard/to_question.py b/wizard/to_question.py index 253d4c3..97a5700 100644 --- a/wizard/to_question.py +++ b/wizard/to_question.py @@ -33,7 +33,7 @@ def __init__(self): \\end{enumerate} \\begin{marginfigure}[-20mm] \\centering - \\includegraphics[width=\columnwidth]{problem1_3.png} + \\includegraphics[width=\\columnwidth]{problem1_3.png} \\caption{A car with two tow ropes attached.} \\label{A1:fig:Q3} \\end{marginfigure}