跳到主要內容

Python REPL

有時候,對於複雜的計算,與其讓 LLM 直接產生答案,不如讓 LLM 產生程式碼來計算答案,然後執行該程式碼以獲得答案。 為了更容易做到這一點,我們提供了一個簡單的 Python REPL 來執行指令。

這個介面只會傳回列印出來的東西 - 因此,如果你想用它來計算答案,請務必將答案列印出來。

注意

Python REPL 可以在主機上執行任意程式碼(例如,刪除檔案、發出網路請求)。 請謹慎使用。

有關一般安全指南的更多資訊,請參閱 https://langchain-python.dev.org.tw/docs/security/

from langchain_core.tools import Tool
from langchain_experimental.utilities import PythonREPL
API 參考:工具 (Tool) | PythonREPL
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,
)

此頁面有幫助嗎?