Mathematics using LLMs using Langchains

vTeam.ai
Data Science in your pocket
3 min readSep 11, 2023
Photo by Antoine Dautry on Unsplash

READ THE FULL BLOG BELOW

VTeam | Mathematics using LLMs and Langchain

In this post, 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 Mathematics
  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

#!pip install langchain openai
from langchain import OpenAI, LLMMathChain
llm = OpenAI(temperature=0,openai_api_key=api_key)
llm_math = LLMMathChain.from_llm(llm, verbose=True)

Time for some examples

llm_math.run("What is 13 raised to the .3432 power?")

The output being

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

llm_math.run("What is 13.87 multiplied 15.34 minus 3.32 ?")

Which will give an output like this

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.

Read the full blog on vTeam.ai for tutorial on Symbolic maths and Word problems examples

--

--