ChatGPT: How to Overcome Calculation Errors

Mustafa Marzouk
Innovies Club
Published in
5 min readApr 11, 2023

If you’ve been using ChatGPT for some time, you might have observed its exceptional intelligence in several fields. However, it’s not always accurate when it comes to mathematical calculations, which can be quite frustrating. But don’t worry, we’ll be exploring ways to enhance ChatGPT’s accuracy in math, enabling you to make the most of this powerful tool. Let’s get started!

Program-aided Language Models (PAL)

Gao et al. (2022) created a new method called Program-Aided Language Models (PAL) that reads natural language problems and generates programs as intermediate steps. The final step of solving the problem is then handled by a programmatic runtime like a Python interpreter.

Gao et al. (2022) https://reasonwithpal.com/

we’ll explore a method similar to the one created by Gao et al. (2022) to make up ChatGPT’s calculations more accurate. By delegating the final step of the calculation to a specialized runtime environment, Let’s dive in!

from langchain.llms import OpenAI
llm = OpenAI(model_name='text-davinci-003', temperature=0)

To interact with the GPT model programmatically, we will use the langchain library. It’s important to note that an API key will be required.

math_prompt = '''
Q: What is the product of 150 and 1542?
A: #Write a Python program to find the product of 150 and 1542 (only provide the code and no other text)

Q: Calculate the result of multiplying 150 by 1542 using Python.
A: #Write a Python program to calculate the result of multiplying 150 by 1542 (only provide the code and no other text)

Q: If I have 150 items and each item costs 1542 dollars, what is the total cost?
A: #Write a Python program to calculate the total cost of 150 items that cost 1542 dollars each (only provide the code and no other text)

Q: How can I multiply 150 by 1542 in Python?
A: #Write a Python program to multiply 150 by 1542 (only provide the code and no other text)

Q: I need to multiply 150 by 1542 in Python, can you help me with the code?
A: #Write a Python program to multiply 150 by 1542 (only provide the code and no other text)

Q: If I add 150 to 2500 and then multiply the result by 1023913, what is the final value?
A: #Write a Python program to add 150 to 2500 and then multiply the result by 1023913 (only provide the code and no other text)

Q: I want to calculate the result of adding 150 to 2500 and then multiplying by 1023913 using Python.
A: #Write a Python program to add 150 to 2500 and then multiply the result by 1023913 (only provide the code and no other text)

Q: In Python, how can I add 150 to 2500 and then multiply the result by 1023913?
A: #Write a Python program to add 150 to 2500 and then multiply the result by 1023913 (only provide the code and no other text)

Q: Can you provide me with the Python code to add 150 to 2500 and then multiply the result by 1023913?
A: #Write a Python program to add 150 to 2500 and then multiply the result by 1023913 (only provide the code and no other text)

Q: {question}
A:
'''

The math_prompt is a carefully crafted string containing multiple math-related questions and their corresponding GPT answer. By providing these examples, we "teach" the GPT model the desired format for answering math questions in our specific context.

When a user submits a math question, we replace the {question} placeholder in math_prompt with the user's question. The GPT model then analyzes the entire prompt, including the examples and the user's question, and generates a response that follows the pattern established by the examples. This ensures that the model’s output is consistent for every prompt.

question = 'Can you provide me with the Python code to add 150 to 2500 and then multiply the result by 1023913'
formatted_prompt = math_prompt.format(question=question)
llm_out = llm(formatted_prompt)

Here, we create a new math question to solve. The code replaces the {question} placeholder in math_prompt with our question. After that, we pass the formatted_prompt to the GPT model and receive the response, which is stored in the llm_out variable.

The content of the llm_out variable that was received from chatGPT would be:

#Write a Python program to sum numbers from 1 to 50 with step of 2 (only provide the code and no other text)

result = (150 + 2500) * 1023913
print(result)
exec(llm_out)
#2713369450

Finally, our last step is to execute the code stored in the llm_out variable using Python to check the output and confirm the solution.

We can use the exec() function in Python to run the code stored in the llm_out variable. The exec() the function takes a single argument, which is a string containing one or more lines of Python code. It dynamically executes the code within the current scope, allowing you to run code generated at runtime.

Challenges associated with this code

Sometimes the prompts may not contain any actual Python code, only comments. To help the GPT generate better code, provide diverse examples with different calculation techniques in your prompts. This can prevent the GPT from recognizing a pattern and getting biased toward a single technique of coding. If you’re still not satisfied with the generated code, you can resend the comment as a new prompt and use the exec() function to run the code. This lets the GPT use its “imagination” to solve the problem.

Recommendations

Program-aided Language Models (PAL) have a lot of room for improvement, and we can use our imagination to create more creative programs that are even more accurate and efficient at solving natural language problems. This will make ChatGPT even more powerful and useful for different applications.

If you have any questions, feel free to connect with me on social media:

References

1- A research paper titled “PAL: Pretraining-Free Adaptation for Language Understanding” authored by Luyu Gao, Aman Madaan, Shuyan Zhou, Uri Alon, Pengfei Liu, Yiming Yang, Jamie Callan, Graham Neubig, and published by the Language Technologies Institute, School of CS, Carnegie Mellon University. The paper was also co-authored with Inspired Cognition and is available on the website https://reasonwithpal.com/

2- https://learnprompting.org/docs/advanced_applications/pal

--

--

Mustafa Marzouk
Innovies Club

NLP Engineer | Nextjs Developer | Python Manim Animator (3blue1brown)