ClemensGruber / climart-gptree

MIT License
2 stars 1 forks source link

Catch and handle OpenAI errors (retry, message to user) #6

Open ClemensGruber opened 8 months ago

ClemensGruber commented 8 months ago

We should output something like the traceback / debug information below but intercept it and make the request again if OpenAI doesn't make it the first run.

GPTree crashed with this output

Traceback (most recent call last):

   File "/home/climart/gptree/main.py", line 150, in <module>
     main()
   File "/home/climart/gptree/main.py", line 131, in main
     ai_response = query_chatgpt(user_text,system_context,history)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/climart/gptree/main.py", line 63, in query_chatgpt
     response = openai.ChatCompletion.create(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File
"/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_resources/chat_completion.py",
line 25, in create
     return super().create(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File
"/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py",
line 153, in create
     response, _, api_key = requestor.request(
                            ^^^^^^^^^^^^^^^^^^
   File
"/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py",
line 226, in request
     resp, got_stream = self._interpret_response(result, stream)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File
"/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py",
line 619, in _interpret_response
     self._interpret_response_line(
   File
"/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py",
line 682, in _interpret_response_line
     raise self.handle_error_response(
openai.error.APIError: The server had an error while processing your
request. Sorry about that! {
   "error": {
     "message": "The server had an error while processing your request.
Sorry about that!",
     "type": "server_error",
     "param": null,
     "code": null
   }
}
  500 {'error': {'message': 'The server had an error while processing
your request. Sorry about that!', 'type': 'server_error', 'param': None,
'code': None}} {'Date': 'Sun, 03 Dec 2023 21:34:40 GMT', 'Content-Type':
'application/json', 'Content-Length': '176', 'Connection': 'keep-alive',
'access-control-allow-origin': '*', 'openai-model':
'gpt-3.5-turbo-0613', 'openai-organization':
'user-15uvr5fodqwgmg9led2gcc3f', 'openai-processing-ms': '4022',
'openai-version': '2020-10-01', 'strict-transport-security':
'max-age=15724800; includeSubDomains', 'x-ratelimit-limit-requests':
'10000', 'x-ratelimit-limit-tokens': '60000',
'x-ratelimit-limit-tokens_usage_based': '60000',
'x-ratelimit-remaining-requests': '9999',
'x-ratelimit-remaining-tokens': '58020',
'x-ratelimit-remaining-tokens_usage_based': '58020',
'x-ratelimit-reset-requests': '8.64s', 'x-ratelimit-reset-tokens':
'1.98s', 'x-ratelimit-reset-tokens_usage_based': '1.98s',
'x-request-id': 'a9fe539ac91e2f3c499d3114a0e37570', 'CF-Cache-Status':
'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '82feef45dca070c3-WAW',
'alt-svc': 'h3=":443"; ma=86400'}
ClemensGruber commented 8 months ago

An other crash

Traceback (most recent call last):

  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
               ^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connection.py", line 461, in getresponse
    httplib_response = super().getresponse()
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/lib/python3.11/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/util/retry.py", line 470, in increment
    raise reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/util/util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
               ^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connection.py", line 461, in getresponse
    httplib_response = super().getresponse()
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse
    response.begin()
  File "/usr/lib/python3.11/http/client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/http/client.py", line 287, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 516, in request_raw
    result = _thread_context.session.request(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

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

Traceback (most recent call last):
  File "/home/climart/gptree/main.py", line 150, in <module>
    main()
  File "/home/climart/gptree/main.py", line 125, in main
    user_text = transcribe_audio(filename_input)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/main.py", line 43, in transcribe_audio
    transcript = openai.Audio.transcribe("whisper-1", audio_file)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_resources/audio.py", line 57, in transcribe
    response, _, api_key = requestor.request("post", url, files=files, params=data)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 216, in request
    result = self.request_raw(
             ^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 528, in request_raw
    raise error.APIConnectionError(
openai.error.APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
ClemensGruber commented 8 months ago

Crash after GPTree is running but was not asked / had no conversation since some minutes (don't konw if it is a timeout problem).

This happend several times, especially after muting mic and try to restart after some minutes.

Traceback (most recent call last): File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 536, in _make_request response = conn.getresponse() ^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connection.py", line 461, in getresponse httplib_response = super().getresponse() ^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse response.begin() File "/usr/lib/python3.11/http/client.py", line 318, in begin version, status, reason = self._read_status() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/http/client.py", line 287, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/adapters.py", line 486, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 844, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/util/retry.py", line 470, in increment raise reraise(type(error), error, _stacktrace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/util/util.py", line 38, in reraise raise value.with_traceback(tb) File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connectionpool.py", line 536, in _make_request response = conn.getresponse() ^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/urllib3/connection.py", line 461, in getresponse httplib_response = super().getresponse() ^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/http/client.py", line 1374, in getresponse response.begin() File "/usr/lib/python3.11/http/client.py", line 318, in begin version, status, reason = self._read_status() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/http/client.py", line 287, in _read_status raise RemoteDisconnected("Remote end closed connection without" urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 516, in request_raw result = _thread_context.session.request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/sessions.py", line 589, in request resp = self.send(prep, send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/sessions.py", line 703, in send r = adapter.send(request, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/requests/adapters.py", line 501, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

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

Traceback (most recent call last): File "/home/climart/gptree/main.py", line 150, in main() File "/home/climart/gptree/main.py", line 125, in main user_text = transcribe_audio(filename_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/main.py", line 43, in transcribe_audio transcript = openai.Audio.transcribe("whisper-1", audio_file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/apiresources/audio.py", line 57, in transcribe response, , api_key = requestor.request("post", url, files=files, params=data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 216, in request result = self.request_raw( ^^^^^^^^^^^^^^^^^ File "/home/climart/gptree/gptree/lib/python3.11/site-packages/openai/api_requestor.py", line 528, in request_raw raise error.APIConnectionError( openai.error.APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))