Skip to content

Commit

Permalink
community: Fix #22975 (Add SSL Verification Option to Requests Class …
Browse files Browse the repository at this point in the history
…in langchain_community) (#22977)

- **PR title**: "community: Fix #22975 (Add SSL Verification Option to
Requests Class in langchain_community)"
- **PR message**: 
    - **Description:**
- Added an optional verify parameter to the Requests class with a
default value of True.
- Modified the get, post, patch, put, and delete methods to include the
verify parameter.
- Updated the _arequest async context manager to include the verify
parameter.
- Added the verify parameter to the GenericRequestsWrapper class and
passed it to the Requests class.
    - **Issue:** This PR fixes issue #22975.
- **Dependencies:** No additional dependencies are required for this
change.
    - **Twitter handle:** @lunara_x

You can check this change with below code.
```python
from langchain_openai.chat_models import ChatOpenAI
from langchain.requests import RequestsWrapper
from langchain_community.agent_toolkits.openapi import planner
from langchain_community.agent_toolkits.openapi.spec import reduce_openapi_spec

with open("swagger.yaml") as f:
    data = yaml.load(f, Loader=yaml.FullLoader)
swagger_api_spec = reduce_openapi_spec(data)

llm = ChatOpenAI(model='gpt-4o')
swagger_requests_wrapper = RequestsWrapper(verify=False) # modified point
superset_agent = planner.create_openapi_agent(swagger_api_spec, swagger_requests_wrapper, llm, allow_dangerous_requests=True, handle_parsing_errors=True)

superset_agent.run(
    "Tell me the number and types of charts and dashboards available."
)
```

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
  • Loading branch information
eunhye1kim and hwchase17 authored Jun 18, 2024
1 parent bf83967 commit 70761af
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions libs/community/langchain_community/utilities/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Requests(BaseModel):
headers: Optional[Dict[str, str]] = None
aiosession: Optional[aiohttp.ClientSession] = None
auth: Optional[Any] = None
verify: Optional[bool] = True

class Config:
"""Configuration for this pydantic object."""
Expand All @@ -27,29 +28,48 @@ class Config:

def get(self, url: str, **kwargs: Any) -> requests.Response:
"""GET the URL and return the text."""
return requests.get(url, headers=self.headers, auth=self.auth, **kwargs)
return requests.get(
url, headers=self.headers, auth=self.auth, verify=self.verify, **kwargs
)

def post(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
"""POST to the URL and return the text."""
return requests.post(
url, json=data, headers=self.headers, auth=self.auth, **kwargs
url,
json=data,
headers=self.headers,
auth=self.auth,
verify=self.verify,
**kwargs,
)

def patch(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
"""PATCH the URL and return the text."""
return requests.patch(
url, json=data, headers=self.headers, auth=self.auth, **kwargs
url,
json=data,
headers=self.headers,
auth=self.auth,
verify=self.verify,
**kwargs,
)

def put(self, url: str, data: Dict[str, Any], **kwargs: Any) -> requests.Response:
"""PUT the URL and return the text."""
return requests.put(
url, json=data, headers=self.headers, auth=self.auth, **kwargs
url,
json=data,
headers=self.headers,
auth=self.auth,
verify=self.verify,
**kwargs,
)

def delete(self, url: str, **kwargs: Any) -> requests.Response:
"""DELETE the URL and return the text."""
return requests.delete(url, headers=self.headers, auth=self.auth, **kwargs)
return requests.delete(
url, headers=self.headers, auth=self.auth, verify=self.verify, **kwargs
)

@asynccontextmanager
async def _arequest(
Expand All @@ -59,12 +79,22 @@ async def _arequest(
if not self.aiosession:
async with aiohttp.ClientSession() as session:
async with session.request(
method, url, headers=self.headers, auth=self.auth, **kwargs
method,
url,
headers=self.headers,
auth=self.auth,
verify=self.verify,
**kwargs,
) as response:
yield response
else:
async with self.aiosession.request(
method, url, headers=self.headers, auth=self.auth, **kwargs
method,
url,
headers=self.headers,
auth=self.auth,
verify=self.verify,
**kwargs,
) as response:
yield response

Expand Down Expand Up @@ -116,6 +146,7 @@ class GenericRequestsWrapper(BaseModel):
aiosession: Optional[aiohttp.ClientSession] = None
auth: Optional[Any] = None
response_content_type: Literal["text", "json"] = "text"
verify: bool = True

class Config:
"""Configuration for this pydantic object."""
Expand All @@ -126,7 +157,10 @@ class Config:
@property
def requests(self) -> Requests:
return Requests(
headers=self.headers, aiosession=self.aiosession, auth=self.auth
headers=self.headers,
aiosession=self.aiosession,
auth=self.auth,
verify=self.verify,
)

def _get_resp_content(self, response: Response) -> Union[str, Dict[str, Any]]:
Expand Down

0 comments on commit 70761af

Please sign in to comment.