Skip to content

using few-short prompting to extract main content and parts from input latex questions #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: hackathon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions wizard/example_questions/q1.tex
Original file line number Diff line number Diff line change
@@ -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}
7 changes: 7 additions & 0 deletions wizard/example_questions/q2.tex
Original file line number Diff line number Diff line change
@@ -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}
11 changes: 11 additions & 0 deletions wizard/example_questions/q3.tex
Original file line number Diff line number Diff line change
@@ -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}
7 changes: 7 additions & 0 deletions wizard/example_questions/q4.tex
Original file line number Diff line number Diff line change
@@ -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}
13 changes: 13 additions & 0 deletions wizard/example_questions/q5.tex
Original file line number Diff line number Diff line change
@@ -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}
$
13 changes: 13 additions & 0 deletions wizard/example_questions/q6.tex
Original file line number Diff line number Diff line change
@@ -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}
$
10 changes: 10 additions & 0 deletions wizard/example_questions/q7.tex
Original file line number Diff line number Diff line change
@@ -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}
$
23 changes: 23 additions & 0 deletions wizard/example_questions/s1.tex
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions wizard/example_questions/s2.tex
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions wizard/example_questions/s3.tex
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions wizard/example_questions/s4.tex
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions wizard/example_questions/s5.tex
Original file line number Diff line number Diff line change
@@ -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}
$
20 changes: 20 additions & 0 deletions wizard/example_questions/s6.tex
Original file line number Diff line number Diff line change
@@ -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}
$
18 changes: 18 additions & 0 deletions wizard/example_questions/s7.tex
Original file line number Diff line number Diff line change
@@ -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}
$
142 changes: 142 additions & 0 deletions wizard/to_question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import os
import dotenv

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

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
'''

# 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: <string>\\nParts: <Part1>, \\n<Part2>, \\n..."""),
few_shot_prompt,
("human", "{input}"),
]
)

chain = final_prompt | self.llm

print(question)
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_question2 = "Which city is the capital of China? \\item Beijing \\item Shanghai \\item Guangzhou"
test_converter = QuestionConverter()
test_converter.convert(test_question2, "")
Loading