Skip to content
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

Swap in Sympy instead of numexpr for more expressive LLMMath tool #8627

Closed
wants to merge 11 commits into from
14 changes: 3 additions & 11 deletions libs/langchain/langchain/chains/llm_math/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Chain that interprets a prompt and executes python code to do math."""
from __future__ import annotations

import math
import re
import warnings
from typing import Any, Dict, List, Optional

import numexpr
from pydantic import Extra, root_validator
from sympy import sympify

from langchain.callbacks.manager import (
AsyncCallbackManagerForChainRun,
Expand All @@ -21,7 +20,7 @@


class LLMMathChain(Chain):
"""Chain that interprets a prompt and executes python code to do math.
"""Chain that interprets a prompt and uses the sympy python package to do math.

Example:
.. code-block:: python
Expand Down Expand Up @@ -75,14 +74,7 @@ def output_keys(self) -> List[str]:

def _evaluate_expression(self, expression: str) -> str:
try:
local_dict = {"pi": math.pi, "e": math.e}
output = str(
numexpr.evaluate(
expression.strip(),
global_dict={}, # restrict access to globals
local_dict=local_dict, # add common mathematical functions
)
)
output = str(sympify(expression.strip()))
except Exception as e:
raise ValueError(
f'LLMMathChain._evaluate("{expression}") raised error: {e}.'
Expand Down
10 changes: 6 additions & 4 deletions libs/langchain/langchain/chains/llm_math/prompt.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# flake8: noqa
from langchain.prompts.prompt import PromptTemplate

_PROMPT_TEMPLATE = """Translate a math problem into a expression that can be executed using Python's numexpr library. Use the output of running this code to answer the question.
_PROMPT_TEMPLATE = """Translate a math problem into a expression that can be executed using the sympify function from the Python package Sympy.
Use the output of running this code to answer the question.
Sympify can handle most elementary mathematical expressions, including rounding, factorials, and calculus. It cannot handle string manipulation.

Question: ${{Question with math problem.}}
```text
${{single line mathematical expression that solves the problem}}
```
...numexpr.evaluate(text)...
...sympify(text)...
```output
${{Output of running the code}}
```
Expand All @@ -19,7 +21,7 @@
```text
37593 * 67
```
...numexpr.evaluate("37593 * 67")...
...sympify("37593 * 67")...
```output
2518731
```
Expand All @@ -29,7 +31,7 @@
```text
37593**(1/5)
```
...numexpr.evaluate("37593**(1/5)")...
...sympify("37593**(1/5)")...
```output
8.222831614237718
```
Expand Down
Loading
Loading