Implements handling of OpenAI rate limit error.

This commit is contained in:
Torantulino
2023-04-01 01:30:13 +01:00
parent 4100961745
commit ffeb1aca73

View File

@@ -1,3 +1,4 @@
import time
import openai import openai
import keys import keys
@@ -18,6 +19,8 @@ def create_chat_message(role, content):
return {"role": role, "content": content} return {"role": role, "content": content}
def chat_with_ai(prompt, user_input, full_message_history, permanent_memory, token_limit, debug = False): def chat_with_ai(prompt, user_input, full_message_history, permanent_memory, token_limit, debug = False):
while True:
try:
""" """
Interact with the OpenAI API, sending the prompt, user input, message history, and permanent memory. Interact with the OpenAI API, sending the prompt, user input, message history, and permanent memory.
@@ -57,3 +60,6 @@ def chat_with_ai(prompt, user_input, full_message_history, permanent_memory, tok
full_message_history.append(create_chat_message("assistant", assistant_reply)) full_message_history.append(create_chat_message("assistant", assistant_reply))
return assistant_reply return assistant_reply
except openai.RateLimitError:
print("Error: ", "API Rate Limit Reached. Waiting 60 seconds...")
time.sleep(60)