community[minor]: add allow_dangerous_requests for OpenAPI toolkits (#19493)

**OpenAPI allow_dangerous_requests**: community: add
allow_dangerous_requests for OpenAPI toolkits

**Description:** a description of the change

Due to BaseRequestsTool changes, we need to pass
allow_dangerous_requests manually.


b617085af0/libs/community/langchain_community/tools/requests/tool.py (L26-L46)

While OpenAPI toolkits didn't pass it in the arguments.


b617085af0/libs/community/langchain_community/agent_toolkits/openapi/planner.py (L262-L269)


**Issue:** the issue # it fixes, if applicable

https://github.com/langchain-ai/langchain/issues/19440

If not passing allow_dangerous_requests, it won't be able to do
requests.

**Dependencies:** any dependencies required for this change

Not much

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
David Lee
2024-04-10 01:14:02 +08:00
committed by GitHub
parent 301dc3dfd2
commit 0394c6e126
3 changed files with 72 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
"""Agent that interacts with OpenAPI APIs via a hierarchical planning approach."""
import json
import re
from functools import partial
@@ -252,6 +253,7 @@ def _create_api_controller_agent(
api_docs: str,
requests_wrapper: RequestsWrapper,
llm: BaseLanguageModel,
allow_dangerous_requests: bool,
) -> Any:
from langchain.agents.agent import AgentExecutor
from langchain.agents.mrkl.base import ZeroShotAgent
@@ -261,10 +263,14 @@ def _create_api_controller_agent(
post_llm_chain = LLMChain(llm=llm, prompt=PARSING_POST_PROMPT)
tools: List[BaseTool] = [
RequestsGetToolWithParsing(
requests_wrapper=requests_wrapper, llm_chain=get_llm_chain
requests_wrapper=requests_wrapper,
llm_chain=get_llm_chain,
allow_dangerous_requests=allow_dangerous_requests,
),
RequestsPostToolWithParsing(
requests_wrapper=requests_wrapper, llm_chain=post_llm_chain
requests_wrapper=requests_wrapper,
llm_chain=post_llm_chain,
allow_dangerous_requests=allow_dangerous_requests,
),
]
prompt = PromptTemplate(
@@ -290,6 +296,7 @@ def _create_api_controller_tool(
api_spec: ReducedOpenAPISpec,
requests_wrapper: RequestsWrapper,
llm: BaseLanguageModel,
allow_dangerous_requests: bool,
) -> Tool:
"""Expose controller as a tool.
@@ -318,7 +325,9 @@ def _create_api_controller_tool(
if not found_match:
raise ValueError(f"{endpoint_name} endpoint does not exist.")
agent = _create_api_controller_agent(base_url, docs_str, requests_wrapper, llm)
agent = _create_api_controller_agent(
base_url, docs_str, requests_wrapper, llm, allow_dangerous_requests
)
return agent.run(plan_str)
return Tool(
@@ -336,6 +345,7 @@ def create_openapi_agent(
callback_manager: Optional[BaseCallbackManager] = None,
verbose: bool = True,
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
allow_dangerous_requests: bool = False,
**kwargs: Any,
) -> Any:
"""Instantiate OpenAI API planner and controller for a given spec.
@@ -345,6 +355,14 @@ def create_openapi_agent(
We use a top-level "orchestrator" agent to invoke the planner and controller,
rather than a top-level planner
that invokes a controller with its plan. This is to keep the planner simple.
You need to set allow_dangerous_requests to True to use Agent with BaseRequestsTool.
Requests can be dangerous and can lead to security vulnerabilities.
For example, users can ask a server to make a request to an internal
server. It's recommended to use requests through a proxy server
and avoid accepting inputs from untrusted sources without proper sandboxing.
Please see: https://python.langchain.com/docs/security
for further security information.
"""
from langchain.agents.agent import AgentExecutor
from langchain.agents.mrkl.base import ZeroShotAgent
@@ -352,7 +370,9 @@ def create_openapi_agent(
tools = [
_create_api_planner_tool(api_spec, llm),
_create_api_controller_tool(api_spec, requests_wrapper, llm),
_create_api_controller_tool(
api_spec, requests_wrapper, llm, allow_dangerous_requests
),
]
prompt = PromptTemplate(
template=API_ORCHESTRATOR_PROMPT,