From 6e90406e0f87595ca4d858dbb1e48ef0abaaf854 Mon Sep 17 00:00:00 2001 From: "Jiaping(JP) Zhang" Date: Sun, 11 Jun 2023 13:13:57 -0700 Subject: [PATCH] [APIChain] enhance the robustness or url (#6008) I used the APIChain sometimes it failed during the intermediate step when generating the api url and calling the `request` function. After some digging, I found the url sometimes includes the space at the beginning, like `%20https://...api.com` which causes the ` self.requests_wrapper.get` internal function to fail. Including a little string preprocessing `.strip` to remove the space seems to improve the robustness of the APIchain to make sure it can send the request and retrieve the API result more reliably. Fixes # (issue) #### Before submitting #### Who can review? @vowelparrot Tag maintainers/contributors who might be interested: --- langchain/chains/api/base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/langchain/chains/api/base.py b/langchain/chains/api/base.py index e5af03a009..7e199fe42f 100644 --- a/langchain/chains/api/base.py +++ b/langchain/chains/api/base.py @@ -78,6 +78,7 @@ class APIChain(Chain): callbacks=_run_manager.get_child(), ) _run_manager.on_text(api_url, color="green", end="\n", verbose=self.verbose) + api_url = api_url.strip() api_response = self.requests_wrapper.get(api_url) _run_manager.on_text( api_response, color="yellow", end="\n", verbose=self.verbose @@ -106,6 +107,7 @@ class APIChain(Chain): await _run_manager.on_text( api_url, color="green", end="\n", verbose=self.verbose ) + api_url = api_url.strip() api_response = await self.requests_wrapper.aget(api_url) await _run_manager.on_text( api_response, color="yellow", end="\n", verbose=self.verbose