mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-08-27 09:21:30 -04:00
5ca7ce77cd
Use numexpr evaluate instead of the python REPL to avoid malicious code injection. Tested against the (limited) math dataset and got the same score as before. For more permissive tools (like the REPL tool itself), other approaches ought to be provided (some combination of Sanitizer + Restricted python + unprivileged-docker + ...), but for a calculator tool, only mathematical expressions should be permitted. See https://github.com/hwchase17/langchain/issues/814
36 lines
719 B
Python
36 lines
719 B
Python
# 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.
|
|
|
|
Question: ${{Question with math problem.}}
|
|
```text
|
|
${{single line mathematical expression that solves the problem}}
|
|
```
|
|
...numexpr.evaluate(text)...
|
|
```output
|
|
${{Output of running the code}}
|
|
```
|
|
Answer: ${{Answer}}
|
|
|
|
Begin.
|
|
|
|
Question: What is 37593 * 67?
|
|
|
|
```text
|
|
37593 * 67
|
|
```
|
|
...numexpr.evaluate("37593 * 67")...
|
|
```output
|
|
2518731
|
|
```
|
|
Answer: 2518731
|
|
|
|
Question: {question}
|
|
"""
|
|
|
|
PROMPT = PromptTemplate(
|
|
input_variables=["question"],
|
|
template=_PROMPT_TEMPLATE,
|
|
)
|