LangChain Python

How to Use LangChain LLM-Math in Python

Large Language Models are capable of doing mathematical operations. It only manifests at greater scales since it is an emerging feature. It does take some analytical skills to understand the sophisticated English language, and these skills can be used to perform mathematical operations including Calculus and basic math. But still, you cannot completely rely on LLM for solving math problems.

The LLMMathChain equips LLMs with mathematical capabilities. This article demonstrates how to solve the complex word math problems using Python REPLs and LLMs.

Performing Basic Math Operations Using the LangChain LLM-Math in Python

LangChain can perform complex math word problems which include addition, subtraction, multiplication, power, division, etc. In this illustration, we will perform some of these operations to see if LLM computes them correctly and show the generated output.

Let’s first install the packages that we require to execute these programs. The first and fundamental library that we need to install here is LangChain as we are going to work on it. The other one is the OpenAI package. We need this library to access the OpenAI’s GPT models. Python’s standard library does not come with these, so we have to install them separately from PyPi using the pip command. We install both of them one by one.

The pip command is used to install the LangChain library is as follows:

$ pip install langchain

While employing this command, the OpenAI package can be installed:

$ pip install openai

The installation process will finish in a short while. As soon as the packages are installed, we can start implementing the Python programs.

Apart from installing the required libraries, we also need to generate the secret API key from the OpenAI platform. We cannot access the GPT models without having the API key.

Follow these instructions to generate the API key:

Open the OpenAI website, create an account, or simply log in to an already existing one. Then, click on the user profile, and you will see a list of options. Select the “View API keys” section from this list. The new window that opens has a “Create New Secret Key” button. Hit this button and a prompt will appear where you have to specify a name for your API key. Specify the Key name and press the “Create secret key” button. Finally, get your secret API key for OpenAI GPT models. Now, copy this key and store it because it won’t be retrieved from here again. Now, you can use this key in your Python programs to access the GPT models.

Now, let’s begin creating a Python program using the LangChain llm-math to check which basic math operations can be done with it.

Using the LLM-Math chain, we calculate the sum of two numbers.

from langchain import OpenAI

from langchain import MathChain

import os

os.environ["OPENAI_API_KEY"] = "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"

llm = OpenAI(temperature=0)

compute = LLMMathChain(llm=llm)

compute.run("What is the sum of 231 and 189 ")

The initial step in the program is to import the necessary libraries. We first import the OpenAI class from the LangChain library and then import the LLMMATHCHAIN class again from the LangChain into the current program. Another module that we import into this program is “os”. This is used to set the API key as the environment variable in the program.

We initialize the “os.environ” object. This object takes two things: a name and a value. The name is what we want to name our key, so we specify it as “OPENAI_API_KEY”. For the value, we provide the secret API key.

The “LLM” object is then initialized to OpenAI with the default model and temperature value set to 0. This temperature parameter controls the randomness of the predicted output. The smaller values give a more focused output while the larger values mean that the predicted output are more random and creative.

Then, the constructor of the LLMMATHCHAIN class is called and made equal to the compute object. LLM is set to equal to the previously initialized LLM. Lastly, the “run” function is invoked with the object. The math problem that we want to solve is provided within the “run” function. As we want to do the addition of two numbers here, we specify the statement as “What is the sum of 231 and 189?”

Execute the program to get the outcome.

The generated output shows the sum of the specified numbers as 420.

Let’s now check if it can compute the correct sum if we provide more than two numbers to it.

compute.run("What is the sum of 231, 12, 189 and 153?")

So, we specify the query as “What is the sum of 231, 12, 189, and 153?

Here, we get the sum of the provided numbers. Cross-check this answer on your calculator to see if the generated output is correct.

From the previous instance, we conclude that the basic summation operations can be done with LLM Math.

We now see the subtraction of two numbers as well as more than two numbers using the LLM Math in Python. We simply change the query in the “run” function. We write a query to subtract two numbers.

compute.run("What do we get by subtracting 899 from 670?")

The query is “What do we get by subtracting 899 from 670?”

The following snapshot shows that the answer is “-299” which is correct. It means that the basic subtraction can also be done with it.

We now put a query that uses addition and subtraction along with multiplication.

compute.run("What do we get by subtracting 120 from 219, then adding the it to 150 and multiplying the answer to 3?")

The specified query says to subtract 219 and 120, add 150 to the answer, and then multiply the result with 3. Let’s execute the program.

The output that is produced is accurate.

Let’s now use our last basic math operation which is division.

compute.run("What do we get by subtracting 120 from 219, then adding the it to 150 and multiplying the answer to 3 and dividing by 12?")

With the previously-specified query, we add a new operation to perform which is dividing the answer by 12.

The provided answer is correct.

We used all the four basic math operations here which are doable with the LangChain LLM Math and the generated result from all of them are correct.

Apart from this, the power of a number can also be calculated from this function.

compute.run("What is 11 raised to the .788 power?")

The output is:

You can also try solving the math equations like quadratic equations using LLM. But make sure to double-check the answer with the calculator.

Conclusion

Complex math word problems can be solved by LLM using the OpenAI models. In this article, we demonstrated some basic math exercises using Python’s LangChain library. Basic arithmetic operations are performed by passing a word query. The calculations that we performed are addition, subtraction, multiplication, division, and power of a number.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.