improved error handling

This commit is contained in:
konrad 2023-12-17 11:36:53 +00:00
parent 95ce7866b2
commit 43e84a2a3b
2 changed files with 16 additions and 4 deletions

View File

@ -12,7 +12,7 @@ Install python3 and the official Python bindings:
```
$ apt install python3 python3-pip (Debian/Ubuntu)
$ yum install python3 python3-pip (RedHat/CentOS)
$ pip3 install openai pyshorteners
$ pip3 install openai==0.28 pyshorteners
$ git clone https://github.com/knrd1/chatgpt.git
$ cd chatgpt
$ cp example-chat.conf chat.conf

View File

@ -126,9 +126,12 @@ while True:
except openai.error.Timeout as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call timed out. Try again later.\n", "UTF-8"))
except openai.error.OpenAIError as e:
print("Error: " + str(e))
irc.send(bytes(f"PRIVMSG {channel} :API call failed. {str(e)}\n", "UTF-8"))
except Exception as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call failed. Check console for error message.\n", "UTF-8"))
irc.send(bytes(f"PRIVMSG {channel} :An unexpected error occurred. {str(e)}\n", "UTF-8"))
elif model in completion_models:
try:
response = openai.Completion.create(
@ -156,9 +159,12 @@ while True:
except openai.error.Timeout as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call timed out. Try again later.\n", "UTF-8"))
except openai.error.OpenAIError as e:
print("Error: " + str(e))
irc.send(bytes(f"PRIVMSG {channel} :API call failed. {str(e)}\n", "UTF-8"))
except Exception as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call failed. Check console for error message.\n", "UTF-8"))
irc.send(bytes(f"PRIVMSG {channel} :An unexpected error occurred. {str(e)}\n", "UTF-8"))
elif model == "dalle":
try:
response = openai.Image.create(
@ -170,9 +176,15 @@ while True:
type_tiny = pyshorteners.Shortener()
short_url = type_tiny.tinyurl.short(long_url)
irc.send(bytes("PRIVMSG " + channel + " :" + short_url + "\n", "UTF-8"))
except openai.error.Timeout as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call timed out. Try again later.\n", "UTF-8"))
except openai.error.OpenAIError as e:
print("Error: " + str(e))
irc.send(bytes(f"PRIVMSG {channel} :API call failed. {str(e)}\n", "UTF-8"))
except Exception as e:
print("Error: " + str(e))
irc.send(bytes("PRIVMSG " + channel + " :API call failed. Check console for error message..\n", "UTF-8"))
irc.send(bytes(f"PRIVMSG {channel} :An unexpected error occurred. {str(e)}\n", "UTF-8"))
else:
print("Invalid model.")
irc.send(bytes("PRIVMSG " + channel + " :Invalid model.\n", "UTF-8"))