langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.47k stars 15.28k forks source link

ConnectionResetError: [Errno 54] Connection reset by peer #8483

Closed betterMax closed 12 months ago

betterMax commented 1 year ago

System Info

I use this code:

search = GoogleSearchAPIWrapper()

tool = Tool(
    name="Google Search",
    description="Search Google for recent results.",
    func=search.run,
)

tool.run("Obama's first name?")

The result looks fine. But when I use the code below

search = GoogleSearchAPIWrapper()

tools = [
    Tool(
        name="google-search",
        func=search.run,
        description="useful when you need to search google to answer questions about current events"
    )
]

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    max_iterations=6)

response = agent("What is the latest news about the Mars rover?")
print(response)

I get this error:

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/site-packages/googleapiclient/http.py:191, in _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs)
    189 try:
    190     exception = None
--> 191     resp, content = http.request(uri, method, *args, **kwargs)
    192 # Retry on SSL errors and socket timeout errors.
    193 except _ssl_SSLError as ssl_error:

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/site-packages/httplib2/__init__.py:1724, in Http.request(self, uri, method, body, headers, redirections, connection_type)
   1722             content = b""
   1723         else:
-> 1724             (response, content) = self._request(
   1725                 conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,
   1726             )
   1727 except Exception as e:
   1728     is_timeout = isinstance(e, socket.timeout)

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/site-packages/httplib2/__init__.py:1444, in Http._request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1441 if auth:
   1442     auth.request(method, request_uri, headers, body)
-> 1444 (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   1446 if auth:
   1447     if auth.response(response, body):

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/site-packages/httplib2/__init__.py:1396, in Http._conn_request(self, conn, request_uri, method, body, headers)
   1394     pass
   1395 try:
-> 1396     response = conn.getresponse()
   1397 except (http.client.BadStatusLine, http.client.ResponseNotReady):
   1398     # If we get a BadStatusLine on the first try then that means
   1399     # the connection just went stale, so retry regardless of the
   1400     # number of RETRIES set.
   1401     if not seen_bad_status_line and i == 1:

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/http/client.py:1348, in HTTPConnection.getresponse(self)
   1346 try:
   1347     try:
-> 1348         response.begin()
   1349     except ConnectionError:
   1350         self.close()

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/http/client.py:316, in HTTPResponse.begin(self)
    314 # read until we get a non-100 response
    315 while True:
--> 316     version, status, reason = self._read_status()
    317     if status != CONTINUE:
    318         break

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/http/client.py:277, in HTTPResponse._read_status(self)
    276 def _read_status(self):
--> 277     line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    278     if len(line) > _MAXLINE:
    279         raise LineTooLong("status line")

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/socket.py:669, in SocketIO.readinto(self, b)
    667 while True:
    668     try:
--> 669         return self._sock.recv_into(b)
    670     except timeout:
    671         self._timeout_occurred = True

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/ssl.py:1241, in SSLSocket.recv_into(self, buffer, nbytes, flags)
   1237     if flags != 0:
   1238         raise ValueError(
   1239           "non-zero flags not allowed in calls to recv_into() on %s" %
   1240           self.__class__)
-> 1241     return self.read(nbytes, buffer)
   1242 else:
   1243     return super().recv_into(buffer, nbytes, flags)

File /opt/homebrew/anaconda3/envs/common_3.8/lib/python3.8/ssl.py:1099, in SSLSocket.read(self, len, buffer)
   1097 try:
   1098     if buffer is not None:
-> 1099         return self._sslobj.read(len, buffer)
   1100     else:
   1101         return self._sslobj.read(len)

ConnectionResetError: [Errno 54] Connection reset by peer

Who can help?

No response

Information

Related Components

Reproduction

search = GoogleSearchAPIWrapper()

tools = [
    Tool(
        name="google-search",
        func=search.run,
        description="useful when you need to search google to answer questions about current events"
    )
]

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    max_iterations=6)

response = agent("What is the latest news about the Mars rover?")
print(response)

Expected behavior

Hope to know the reason and how to prevent the issue.

dosubot[bot] commented 1 year ago

Answer generated by a 🤖

Answer

From your description, it appears you're encountering a ConnectionResetError: [Errno 54] Connection reset by peer error when trying to use the Google Search API within the LangChain framework. This error typically arises when the server unexpectedly closes the connection. This could be due to several reasons such as rate limiting, server downtime, or network issues.

To address this issue, I suggest the following steps:

  1. Introduce a delay between your requests to avoid hitting any potential rate limits set by the Google Search API.
  2. Implement a try-except block to catch the ConnectionResetError and retry the request after a delay.
  3. Check your network connection to ensure it's stable and reliable.
  4. Verify that your Google API key and Custom Search Engine ID are correctly configured and valid.

Here's an example of how you could modify your code to include a delay and error handling:

import time
import httplib2

search = GoogleSearchAPIWrapper()

tools = [
    Tool(
        name="google-search",
        func=search.run,
        description="useful when you need to search google to answer questions about current events"
    )
]

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    max_iterations=6)

try:
    response = agent("What is the latest news about the Mars rover?")
    print(response)
except httplib2.ServerNotFoundError:
    print("Server not found. Retrying...")
    time.sleep(5)  # wait for 5 seconds before retrying
    response = agent("What is the latest news about the Mars rover?")
    print(response)

Please note that this is just one potential solution. The delay time and the number of retries should be adjusted according to the API's rate limit policy and your application's requirements.

If you find this solution to be correct and you believe this is a bug that could impact other users, we encourage you to make a pull request. If this solution doesn't align with your experience, I recommend upgrading to the latest version of LangChain to ensure you have all the recent updates and fixes.

Sources


This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

betterMax commented 1 year ago

Answer generated by a 🤖

Answer

From your description, it appears you're encountering a ConnectionResetError: [Errno 54] Connection reset by peer error when trying to use the Google Search API within the LangChain framework. This error typically arises when the server unexpectedly closes the connection. This could be due to several reasons such as rate limiting, server downtime, or network issues.

To address this issue, I suggest the following steps:

  1. Introduce a delay between your requests to avoid hitting any potential rate limits set by the Google Search API.
  2. Implement a try-except block to catch the ConnectionResetError and retry the request after a delay.
  3. Check your network connection to ensure it's stable and reliable.
  4. Verify that your Google API key and Custom Search Engine ID are correctly configured and valid.

Here's an example of how you could modify your code to include a delay and error handling:

import time
import httplib2

search = GoogleSearchAPIWrapper()

tools = [
    Tool(
        name="google-search",
        func=search.run,
        description="useful when you need to search google to answer questions about current events"
    )
]

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
    max_iterations=6)

try:
    response = agent("What is the latest news about the Mars rover?")
    print(response)
except httplib2.ServerNotFoundError:
    print("Server not found. Retrying...")
    time.sleep(5)  # wait for 5 seconds before retrying
    response = agent("What is the latest news about the Mars rover?")
    print(response)

Please note that this is just one potential solution. The delay time and the number of retries should be adjusted according to the API's rate limit policy and your application's requirements.

If you find this solution to be correct and you believe this is a bug that could impact other users, we encourage you to make a pull request. If this solution doesn't align with your experience, I recommend upgrading to the latest version of LangChain to ensure you have all the recent updates and fixes.

Sources

This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

Hi, thanks a lot for your suggestions. I have tried as you suggested and have some further results. As I mentioned before. The code below just works fine. I guess it can approve the Google related settings are all right and the network is stable. `search = GoogleSearchAPIWrapper()

tool = Tool( name="Google Search", description="Search Google for recent results.", func=search.run, )

tool.run("Obama's first name?")`

I tried another code below and it can also approve the Google related settings are all right and the network is stable. `import requests

def test_google_search_api(api_key, search_term): url = f'https://www.googleapis.com/customsearch/v1' params = { 'key': api_key, 'cx': 'MY_CUSTOM_SEARCH_ENGINE_ID', 'q': search_term, }

try:
    response = requests.get(url, params=params)
    response.raise_for_status()
    data = response.json()
    if 'items' in data:
        print(f"GoogleSearch API succeed!Find {len(data['items'])} results.")
    else:
        print("No further issues")
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP issue:{http_err}")
except requests.exceptions.RequestException as req_err:
    print(f"request issue:{req_err}")
except Exception as err:
    print(f"other issue:{err}")

api_key = 'MY_API_KEY' search_term = 'Python programming' test_google_search_api(api_key, search_term) `

The result is GoogleSearch API succeed!Find 10 results.

Then I tried your suggestion and here is the result. `> Entering new chain... I need to find out what the latest news is about the Mars rover Action: google-search Action Input: "latest news Mars rover" Observation: No good Google Search Result was found Thought: I should try a different search query Action: google-search Action Input: "latest news Mars rover Curiosity" Observation: No good Google Search Result was found Thought: I should try a different search query Action: google-search Action Input: "latest news Mars rover Curiosity 2020" Observation: No good Google Search Result was found Thought: I should try a different search query Action: google-search Action Input: "latest news Mars rover Curiosity 2021" Observation: No good Google Search Result was found Thought: I should try a different search query Action: google-search Action Input: "latest news Mars rover Perseverance" Observation: No good Google Search Result was found Thought: I should try a different search query Action: google-search Action Input: "latest news Mars rover Perseverance 2021" Observation: No good Google Search Result was found Thought:

Finished chain. {'input': 'What is the latest news about the Mars rover?', 'output': 'Agent stopped due to iteration limit or time limit.'}`

Then I just realized I used two different questions in the two cases. Then I use “Obama's first name?” as the question for the agent usage and it just works.

dosubot[bot] commented 1 year ago

Answer generated by a 🤖

Answer

Hello,

Thank you for providing detailed information about your issue. The ConnectionResetError: [Errno 54] Connection reset by peer error typically occurs when the server (in this case, the Google Search API) unexpectedly closes the connection. This could be due to a variety of reasons such as rate limiting, server downtime, or network issues.

From the code and error traceback you provided, it seems like the error occurs when the initialize_agent function is used to create an agent that uses the Google Search API. However, when you use the tool.run function or directly call the Google Search API, it works fine. This suggests that the issue might be related to how the Google Search API is used within the initialize_agent function.

In the initialize_agent function, an agent executor is created that uses the tools provided. The agent executor handles the execution of the tools. If the Google Search API is one of these tools, it will be used by the agent executor to run queries. The agent executor might be making multiple concurrent requests to the Google Search API, which could be causing the ConnectionResetError if the API is rate limiting the requests.

One way to potentially solve this issue is to add some delay between the requests to the Google Search API. This can be done by modifying the GoogleSearchAPIWrapper class to include a delay before each request. Here is an example of how you can do this:

import time

class GoogleSearchAPIWrapper:
    # ...

    def _google_serper_api_results(self, search_term: str, search_type: str = "search", **kwargs: Any) -> dict:
        time.sleep(1)  # Add a delay before each request
        # ...

This will ensure that there is at least a 1-second delay between each request to the Google Search API, which might help avoid the ConnectionResetError.

Please try this solution and let me know if it helps. If the issue persists, it would be helpful to know more about the frequency and timing of the requests being made to the Google Search API. This information can help us further diagnose the issue.

Best, Dosu

Sources


This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

dosubot[bot] commented 1 year ago

Hi, @betterMax! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

Based on my understanding of the issue, you are experiencing a ConnectionResetError when using the Google Search API within the LangChain framework. There have been suggestions to introduce a delay between requests, implement error handling, check network stability, and verify the API key and Custom Search Engine ID. You have provided additional code examples and results, which indicate that the issue might be related to how the Google Search API is used within the initialize_agent function. Some suggestions have been made to modify the GoogleSearchAPIWrapper class to include a delay before each request.

Before we proceed, we would like to confirm if this issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you for your understanding, and we look forward to hearing from you soon.