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

@@ -10,6 +10,18 @@
"We can construct agents to consume arbitrary APIs, here APIs conformant to the `OpenAPI`/`Swagger` specification."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "798a442b",
"metadata": {},
"outputs": [],
"source": [
"# NOTE: In this example. We must set `allow_dangerous_request=True` to enable the OpenAPI Agent to automatically use the Request Tool.\n",
"# This can be dangerous for calling unwanted requests. Please make sure your custom OpenAPI spec (yaml) is safe.\n",
"ALLOW_DANGEROUS_REQUEST = True"
]
},
{
"cell_type": "markdown",
"id": "a389367b",
@@ -46,6 +58,14 @@
"import yaml"
]
},
{
"cell_type": "markdown",
"id": "816011d8",
"metadata": {},
"source": [
"You will be able to get OpenAPI specs from here: [APIs-guru/openapi-directory](https://github.com/APIs-guru/openapi-directory)"
]
},
{
"cell_type": "code",
"execution_count": 2,
@@ -261,9 +281,9 @@
],
"source": [
"from langchain_community.agent_toolkits.openapi import planner\n",
"from langchain_openai import OpenAI\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = OpenAI(model_name=\"gpt-4\", temperature=0.0)"
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.0)"
]
},
{
@@ -335,11 +355,17 @@
}
],
"source": [
"spotify_agent = planner.create_openapi_agent(spotify_api_spec, requests_wrapper, llm)\n",
"# NOTE: set allow_dangerous_requests manually for security concern https://python.langchain.com/docs/security\n",
"spotify_agent = planner.create_openapi_agent(\n",
" spotify_api_spec,\n",
" requests_wrapper,\n",
" llm,\n",
" allow_dangerous_requests=ALLOW_DANGEROUS_REQUEST,\n",
")\n",
"user_query = (\n",
" \"make me a playlist with the first song from kind of blue. call it machine blues.\"\n",
")\n",
"spotify_agent.run(user_query)"
"spotify_agent.invoke(user_query)"
]
},
{
@@ -420,7 +446,7 @@
],
"source": [
"user_query = \"give me a song I'd like, make it blues-ey\"\n",
"spotify_agent.run(user_query)"
"spotify_agent.invoke(user_query)"
]
},
{
@@ -549,12 +575,12 @@
],
"source": [
"# Meta!\n",
"llm = OpenAI(model_name=\"gpt-4\", temperature=0.25)\n",
"llm = ChatOpenAI(model_name=\"gpt-4\", temperature=0.25)\n",
"openai_agent = planner.create_openapi_agent(\n",
" openai_api_spec, openai_requests_wrapper, llm\n",
")\n",
"user_query = \"generate a short piece of advice\"\n",
"openai_agent.run(user_query)"
"openai_agent.invoke(user_query)"
]
},
{
@@ -606,7 +632,10 @@
" OpenAI(temperature=0), json_spec, openai_requests_wrapper, verbose=True\n",
")\n",
"openapi_agent_executor = create_openapi_agent(\n",
" llm=OpenAI(temperature=0), toolkit=openapi_toolkit, verbose=True\n",
" llm=OpenAI(temperature=0),\n",
" toolkit=openapi_toolkit,\n",
" allow_dangerous_requests=ALLOW_DANGEROUS_REQUEST,\n",
" verbose=True,\n",
")"
]
},