mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 01:45:27 +03:00
4.5 KiB
4.5 KiB
In [1]:
from langchain import OpenAI, LLMMathChain
llm = OpenAI(temperature=0)
llm_math = LLMMathChain(llm=llm, verbose=True)
llm_math.run("What is 13 raised to the .3432 power?")Out [1]:
[1m> Entering new LLMMathChain chain...[0m What is 13 raised to the .3432 power?[32;1m[1;3m ```python import math print(math.pow(13, .3432)) ``` [0m Answer: [33;1m[1;3m2.4116004626599237 [0m [1m> Finished chain.[0m
'Answer: 2.4116004626599237\n'
In [24]:
from langchain.prompts.prompt import PromptTemplate
_PROMPT_TEMPLATE = """You are GPT-3, and you can't do math.
You can do basic math, and your memorization abilities are impressive, but you can't do any complex calculations that a human could not do in their head. You also have an annoying tendency to just make up highly specific, but wrong, answers.
So we hooked you up to a Python 3 kernel, and now you can execute code. If you execute code, you must print out the final answer using the print function. You MUST use the python package numpy to answer your question. You must import numpy as np.
Question: ${{Question with hard calculation.}}
```python
${{Code that prints what you need to know}}
print(${{code}})
```
```output
${{Output of your code}}
```
Answer: ${{Answer}}
Begin.
Question: What is 37593 * 67?
```python
import numpy as np
print(np.multiply(37593, 67))
```
```output
2518731
```
Answer: 2518731
Question: {question}"""
PROMPT = PromptTemplate(input_variables=["question"], template=_PROMPT_TEMPLATE)In [25]:
llm_math = LLMMathChain(llm=llm, prompt=PROMPT, verbose=True)
llm_math.run("What is 13 raised to the .3432 power?")Out [25]:
[1m> Entering new LLMMathChain chain...[0m What is 13 raised to the .3432 power?[32;1m[1;3m ```python import numpy as np print(np.power(13, .3432)) ``` [0m Answer: [33;1m[1;3m2.4116004626599237 [0m [1m> Finished chain.[0m
'Answer: 2.4116004626599237\n'
In [ ]: