.ipynb
.pdf
Human-in-the-loop Tool Validation
 Contents 
Adding Human Approval
Configuring Human Approval
Human-in-the-loop Tool Validation#
This walkthrough demonstrates how to add Human validation to any Tool. We’ll do this using the HumanApprovalCallbackhandler.
Let’s suppose we need to make use of the ShellTool. Adding this tool to an automated flow poses obvious risks. Let’s see how we could enforce manual human approval of inputs going into this tool.
Note: We generally recommend against using the ShellTool. There’s a lot of ways to misuse it, and it’s not required for most use cases. We employ it here only for demonstration purposes.
from langchain.callbacks import HumanApprovalCallbackHandler
from langchain.tools import ShellTool
tool = ShellTool()
print(tool.run('echo Hello World!'))
Hello World!
Adding Human Approval#
Adding the default HumanApprovalCallbackHandler to the tool will make it so that a user has to manually approve every input to the tool before the command is actually executed.
tool = ShellTool(callbacks=[HumanApprovalCallbackHandler()])
print(tool.run("ls /usr"))
Do you approve of the following input? Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.
ls /usr
yes
X11
X11R6
bin
lib
libexec
local
sbin
share
standalone
print(tool.run("ls /private"))
Do you approve of the following input? Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.
ls /private
no
---------------------------------------------------------------------------
HumanRejectedException                    Traceback (most recent call last)
Cell In[17], line 1
----> 1 print(tool.run("ls /private"))
rtdocs_stable/api.python.langchain.com/en/stable/modules/agents/tools/human_approval.html