knrd1 / chatgpt

ChatGPT IRC bot
https://github.com/knrd1/chatgpt
MIT License
33 stars 7 forks source link

Crash after many minutes not answer for question #3

Closed takenek closed 1 year ago

takenek commented 1 year ago

Hey, I am not sure this is a bot problem or maybe this is API issue but he do not answer for question and leave IRC with "timeout" 14:29 <@TaKeN> Zygmunt: Wydaje mi się, że premiera 14th generacji intela będzie chyba w 2023 roku? 14:31 <@TaKeN> Zygmunt: jesteś tam? 14:32 <@TaKeN> Zygmunt: która jest godzina? 14:34 -!- Zygmunt [~Zygmunt@xxx.pl] has quit [Ping timeout] and after another few minutes he crash:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/openai/api_requestor.py", line 672, in _interpret_response_line
    data = json.loads(rbody)
  File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/chatgpt/./chatgpt.py", line 31, in <module>
    response = openai.Completion.create(
  File "/usr/local/lib/python3.9/dist-packages/openai/api_resources/completion.py", line 25, in create
    return super().create(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
    response, _, api_key = requestor.request(
  File "/usr/local/lib/python3.9/dist-packages/openai/api_requestor.py", line 226, in request
    resp, got_stream = self._interpret_response(result, stream)
  File "/usr/local/lib/python3.9/dist-packages/openai/api_requestor.py", line 619, in _interpret_response
    self._interpret_response_line(
  File "/usr/local/lib/python3.9/dist-packages/openai/api_requestor.py", line 674, in _interpret_response_line
    raise error.APIError(
openai.error.APIError: HTTP code 502 from API (<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
)
knrd1 commented 1 year ago

The main reason is the availability of the endpoint API. If you have a free account on OpenAI and currently their endpoints handle a lot of traffic (and this happens almost non-stop), then unfortunately the bot will return 502/500 errors.

Another issue is the error handling by the bot and this needs to be fixed, e.g. adding a "timeout" after which the bot will abandon the request and tell us to try again later, instead of quitting with "Ping Timeout".

takenek commented 1 year ago

So maybe add support for 502/500 errors? and ofc handling timeouts.

knrd1 commented 1 year ago

I will take care of it this week, thanks for reporting all the issues!

knrd1 commented 1 year ago

Solved. Error handling added for both, IRC and API events.