Python REPL
有时,对于复杂的计算任务,与其让LLM直接生成答案,不如让LLM生成用于计算的答案的代码,然后运行该代码来获取答案。为了方便这样做,我们提供了一个简单的Python交互式命令行环境(REPL),可以在此环境中执行命令。
这界面只会返回打印的内容——因此,如果你想用它来计算答案,请确保让其打印出答案。
注意
Python REPL 可以在宿主机器上执行任意代码(例如,删除文件、进行网络请求)。请谨慎使用。
对于更多关于安全性的通用指南,请参见https://python.langchain.com/docs/security/。
from langchain_core.tools import Tool
from langchain_experimental.utilities import PythonREPL
API 参考:工具 |Python REPL
python_repl = PythonREPL()
python_repl.run("print(1+1)")
Python REPL can execute arbitrary code. Use with caution.
'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,
)