mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-22 10:59:22 +00:00
community[patch]: Fix requests alias for load_tools (#23734)
CC @baskaryan
This commit is contained in:
parent
f24e38876a
commit
7791d92711
File diff suppressed because one or more lines are too long
@ -626,6 +626,25 @@ def load_huggingface_tool(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def raise_dangerous_tools_exception(name: str) -> None:
|
||||||
|
raise ValueError(
|
||||||
|
f"{name} is a dangerous tool. You cannot use it without opting in "
|
||||||
|
"by setting allow_dangerous_tools to True. "
|
||||||
|
"Most tools have some inherit risk to them merely because they are "
|
||||||
|
'allowed to interact with the "real world".'
|
||||||
|
"Please refer to LangChain security guidelines "
|
||||||
|
"to https://python.langchain.com/docs/security."
|
||||||
|
"Some tools have been designated as dangerous because they pose "
|
||||||
|
"risk that is not intuitively obvious. For example, a tool that "
|
||||||
|
"allows an agent to make requests to the web, can also be used "
|
||||||
|
"to make requests to a server that is only accessible from the "
|
||||||
|
"server hosting the code."
|
||||||
|
"Again, all tools carry some risk, and it's your responsibility to "
|
||||||
|
"understand which tools you're using and the risks associated with "
|
||||||
|
"them."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def load_tools(
|
def load_tools(
|
||||||
tool_names: List[str],
|
tool_names: List[str],
|
||||||
llm: Optional[BaseLanguageModel] = None,
|
llm: Optional[BaseLanguageModel] = None,
|
||||||
@ -684,22 +703,7 @@ def load_tools(
|
|||||||
)
|
)
|
||||||
for name in tool_names:
|
for name in tool_names:
|
||||||
if name in DANGEROUS_TOOLS and not allow_dangerous_tools:
|
if name in DANGEROUS_TOOLS and not allow_dangerous_tools:
|
||||||
raise ValueError(
|
raise_dangerous_tools_exception(name)
|
||||||
f"{name} is a dangerous tool. You cannot use it without opting in "
|
|
||||||
"by setting allow_dangerous_tools to True. "
|
|
||||||
"Most tools have some inherit risk to them merely because they are "
|
|
||||||
'allowed to interact with the "real world".'
|
|
||||||
"Please refer to LangChain security guidelines "
|
|
||||||
"to https://python.langchain.com/docs/security."
|
|
||||||
"Some tools have been designated as dangerous because they pose "
|
|
||||||
"risk that is not intuitively obvious. For example, a tool that "
|
|
||||||
"allows an agent to make requests to the web, can also be used "
|
|
||||||
"to make requests to a server that is only accessible from the "
|
|
||||||
"server hosting the code."
|
|
||||||
"Again, all tools carry some risk, and it's your responsibility to "
|
|
||||||
"understand which tools you're using and the risks associated with "
|
|
||||||
"them."
|
|
||||||
)
|
|
||||||
|
|
||||||
if name in {"requests"}:
|
if name in {"requests"}:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
@ -708,8 +712,10 @@ def load_tools(
|
|||||||
)
|
)
|
||||||
if name == "requests_all":
|
if name == "requests_all":
|
||||||
# expand requests into various methods
|
# expand requests into various methods
|
||||||
|
if not allow_dangerous_tools:
|
||||||
|
raise_dangerous_tools_exception(name)
|
||||||
requests_method_tools = [
|
requests_method_tools = [
|
||||||
_tool for _tool in _BASE_TOOLS if _tool.startswith("requests_")
|
_tool for _tool in DANGEROUS_TOOLS if _tool.startswith("requests_")
|
||||||
]
|
]
|
||||||
tool_names.extend(requests_method_tools)
|
tool_names.extend(requests_method_tools)
|
||||||
elif name in _BASE_TOOLS:
|
elif name in _BASE_TOOLS:
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
from langchain_community.agent_toolkits.load_tools import load_tools
|
||||||
|
from langchain_community.tools.requests.tool import (
|
||||||
|
RequestsDeleteTool,
|
||||||
|
RequestsGetTool,
|
||||||
|
RequestsPatchTool,
|
||||||
|
RequestsPostTool,
|
||||||
|
RequestsPutTool,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_request_tools() -> None:
|
||||||
|
request_tools = load_tools(["requests_all"], allow_dangerous_tools=True)
|
||||||
|
assert len(request_tools) == 5
|
||||||
|
assert any(isinstance(tool, RequestsDeleteTool) for tool in request_tools)
|
||||||
|
assert any(isinstance(tool, RequestsGetTool) for tool in request_tools)
|
||||||
|
assert any(isinstance(tool, RequestsPatchTool) for tool in request_tools)
|
||||||
|
assert any(isinstance(tool, RequestsPostTool) for tool in request_tools)
|
||||||
|
assert any(isinstance(tool, RequestsPutTool) for tool in request_tools)
|
Loading…
Reference in New Issue
Block a user