|
|
|
@ -6,7 +6,6 @@ import getpass
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit
|
|
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QLineEdit
|
|
|
|
|
|
|
|
|
|
|
|
# TODO gérer les erreurs de timeout, retour et overload de l'API
|
|
|
|
|
|
|
|
# TODO gérer le formattage / affichage de la réponse quand c'est une liste à points.
|
|
|
|
# TODO gérer le formattage / affichage de la réponse quand c'est une liste à points.
|
|
|
|
|
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
class MainWindow(QMainWindow):
|
|
|
|
@ -69,10 +68,39 @@ class MainWindow(QMainWindow):
|
|
|
|
|
|
|
|
|
|
|
|
self.chat_log.append({"role": "user", "content": user_message})
|
|
|
|
self.chat_log.append({"role": "user", "content": user_message})
|
|
|
|
self.write_to_file()
|
|
|
|
self.write_to_file()
|
|
|
|
|
|
|
|
try:
|
|
|
|
reply = openai.ChatCompletion.create(
|
|
|
|
reply = openai.ChatCompletion.create(
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
messages=self.chat_log)
|
|
|
|
messages=self.chat_log)
|
|
|
|
response=reply['choices'][0]['message']['content']
|
|
|
|
response=reply['choices'][0]['message']['content']
|
|
|
|
|
|
|
|
except openai.error.APIError as e:
|
|
|
|
|
|
|
|
#Handle API error here, e.g. retry or log
|
|
|
|
|
|
|
|
self.chat_log.append({"OpenAI API returned an API Error: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.Timeout as e:
|
|
|
|
|
|
|
|
#Handle connection error here
|
|
|
|
|
|
|
|
self.chat_log.append({"Failed to connect to OpenAI API: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.RateLimitError as e:
|
|
|
|
|
|
|
|
#Handle rate limit error (we recommend using exponential backoff)
|
|
|
|
|
|
|
|
self.chat_log.append({"OpenAI API request exceeded rate limit: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.APIConnectionError as e:
|
|
|
|
|
|
|
|
#Handle Issue connecting to our services
|
|
|
|
|
|
|
|
self.chat_log.append({"Check your network settings, proxy configuration, SSL certificates, or firewall rules: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.InvalidRequestError as e:
|
|
|
|
|
|
|
|
#Handle when your request was malformed or missing some required parameters, such as a token or an input
|
|
|
|
|
|
|
|
self.chat_log.append({"The error message should advise you on the specific error made: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.AuthenticationError as e:
|
|
|
|
|
|
|
|
#Handle validation of your API key or token fail
|
|
|
|
|
|
|
|
self.chat_log.append({"Your API key or token was invalid, expired, or revoked: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
except openai.error.ServiceUnavailableError as e:
|
|
|
|
|
|
|
|
#Handle Issue on OpenAI servers
|
|
|
|
|
|
|
|
self.chat_log.append({"Retry your request after a brief wait and contact us if the issue persists: {e}"})
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Increment message counter
|
|
|
|
# Increment message counter
|
|
|
|
###self.num_messages += 1
|
|
|
|
###self.num_messages += 1
|
|
|
|
|