VTeam AI

Beginner Tutorials

Mathematics using LLMs and Langchain

Let's get real: big language models like LLMs aren't exactly math whizzes. But there's a new player in town, LangChains, that's changing the game. Stick with us as we break down three ways LangChains gives these models a math boost.

In the last 3 parts, we have discussed some major use cases that can be accomplished using Langchains over LLMs. If you haven’t checked them out yet, we have got you covered:

Beginners guide for LangChain

LangChains For NER, Summarization, And Text-Tagging

Langchains For SQL generation, Fact-Checking, And Rewriting


Going a bit deeper this time, we will explore different mathematical problems using LangChains. Why this post is important? Because LLMs have limitations when it comes to mathematics. The reasons being:

Lack of Inherent Math Knowledge: LLMs are primarily trained on large text corpora and lack inherent mathematical knowledge. They do not have an understanding of mathematical concepts as humans do.

Single Correct Answer Requirement: Math problems typically have a single correct answer, making it challenging for LLMs to generate accurate solutions consistently.

Focus on Language Generation: LLMs are designed for language generation and may not inherently perform mathematical calculations. They aim to predict the next word or token in a sequence rather than solve mathematical equations.

Need for Explicit Instructions: To make LLMs perform mathematical tasks, users often need to provide explicit instructions and frame problems in a way that LLMs can understand.

With the incoming of Langchain, this limitation has been resolved to some extent. In this particular post, we will be running through 3 tutorials on how Langchains can be used for different types of Mathematical problems and to some extent improving an LLMs performance for mathematical problems. The 3 demos are:

  1. Basic mathematics
  2. Symbolic Mathematic
  3. Word Problems


So let's get started

Basic Mathematics

Here, we will first pip installing a few required packages, create an OpenAI object using api_key, and then eventually create an LLMMathChain object.


Now, just to show LLMs limitation on mathematics, we will ask the same question to the public version of ChatGPT.


Which is wrong. If you don’t believe it, take a calculator and check for yourself. Similarly, try running the below code snippet as well.


And now using default ChatGPT 3.5


So you saw, how LLMs aren’t the best for mathematics but by using Langchain, this limitation can be improved to some extent.

Moving on to the next tutorial.

Symbolic Mathematics

Symbolic mathematics is manipulating math using symbols, not numbers. Example: a + b - c can be simplified without specific values. This also includes sin(), cos(), etc.

Similar to previous tutorials, pip install the required packages, create OpenAI object by passing openai api_key, and eventually create a SymbolicMathChain object.


Time for some action


You can check the results for cross-validation. Moving on to the last segment.

Word Problems

A word problem in math is a math question presented as a real-world scenario, requiring problem-solving using mathematical concepts. It's like converting a mathematical problem into a story. LLMs usually hallucinate when it comes to word problems. But Langchain has got you covered.

For solving word problems, we will first up setup PALChain (Program-Aided Language) object.


Next, let’s try our luck with a big word problem and see how Langchain perform.



The final output for the problem statement is which is correct.

In conclusion, Langchains offers a powerful and versatile tool for not only basic mathematics but also for tackling complex symbolic math and real-world word problems through the magic of code. With their ability to bridge the gap between human-readable language and computational logic, Langchains empowers learners and problem solvers to explore mathematics in a dynamic, interactive, and code-driven way, making math more accessible and engaging than ever before. So, whether you're a student looking to enhance your math skills or a developer seeking efficient solutions, Langchains opens up a world of mathematical possibilities at your fingertips.



Discover how Langchains revolutionizes problem-solving in basic math, symbolic math, and word problems with the power of code. Math enthusiasts and tech lovers, this one's for you! 

Need a math problem solver that's as sharp as a tack? Look no further! Langchain's powerful capabilities make tackling math a breeze. 

Need help with complex equations? Langchain has your back! Unlock the potential of seamless math problem-solving

Langchain isn't just a language wizard – it's a math genius too! Say goodbye to math anxiety

Hashtags: #llm #langchain #maths #artificialintelligence #datascience #machinelearning

Disclaimer: The views and opinions expressed in this blog post are solely those of the authors and do not reflect the official policy or position of any of the mentioned tools. This blog post is not a form of advertising and no remuneration was received for the creation and publication of this post. The intention is to share our findings and experiences using these tools and is intended purely for informational purposes.