Python REPL
有时候,对于复杂的计算,与其让 LLM 直接生成答案,不如让 LLM 生成用于计算答案的代码,然后运行该代码以获取答案。为了方便执行这些操作,我们提供了一个简单的 Python REPL,可以用来执行命令。
这个界面只会返回被打印出来的内容 - 因此,如果您想使用它来计算答案,请确保它将答案打印出来。
from langchain.agents import Tool
from langchain.utilities import PythonREPL
python_repl = PythonREPL()
python_repl.run("print(1+1)")
'2\n'
# You can create the tool to pass to an agent
repl_tool = Tool(
name="python_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run
)