🔧 chore(openaicaller.py): remove unnecessary sleep statements to improve code readability and performance

🔊 refactor(openaicaller.py): add print statements to indicate when OpenAI API is being called and when retries are being made for better debugging and monitoring
This commit is contained in:
2023-09-04 12:45:52 +02:00
parent 0c182d82c2
commit f165610b70

View File

@@ -136,8 +136,10 @@ class openai_caller:
async def retryal_call(self, recall_func, callable): async def retryal_call(self, recall_func, callable):
i = 0 i = 0
response = None response = None
print(f"{bcolors.BOLD}Calling OpenAI API...{bcolors.ENDC}")
while i < 10: while i < 10:
try: try:
print(f"{bcolors.BOLD}Retryal {i+1}...{bcolors.ENDC}")
response = await callable() response = await callable()
return response return response
except APIError as e: except APIError as e:
@@ -147,21 +149,18 @@ class openai_caller:
await recall_func( await recall_func(
"`An APIError occurred. This is not your fault, it is OpenAI's fault. We apologize for the inconvenience. Retrying...`" "`An APIError occurred. This is not your fault, it is OpenAI's fault. We apologize for the inconvenience. Retrying...`"
) )
await asyncio.sleep(5)
i += 1 i += 1
except Timeout as e: except Timeout as e:
print( print(
f"\n\n{bcolors.BOLD}{bcolors.WARNING}The request timed out. Retrying...{bcolors.ENDC}" f"\n\n{bcolors.BOLD}{bcolors.WARNING}The request timed out. Retrying...{bcolors.ENDC}"
) )
await recall_func("`The request timed out. Retrying...`") await recall_func("`The request timed out. Retrying...`")
await asyncio.sleep(5)
i += 1 i += 1
except RateLimitError as e: except RateLimitError as e:
print( print(
f"\n\n{bcolors.BOLD}{bcolors.WARNING}RateLimitError. You are being rate limited. Retrying...{bcolors.ENDC}" f"\n\n{bcolors.BOLD}{bcolors.WARNING}RateLimitError. You are being rate limited. Retrying...{bcolors.ENDC}"
) )
await recall_func("`You are being rate limited. Retrying...`") await recall_func("`You are being rate limited. Retrying...`")
await asyncio.sleep(5)
i += 1 i += 1
except APIConnectionError as e: except APIConnectionError as e:
print( print(
@@ -185,7 +184,6 @@ class openai_caller:
f"\n\n{bcolors.BOLD}{bcolors.WARNING}ServiceUnavailableError. The OpenAI API is not responding. Retrying...{bcolors.ENDC}" f"\n\n{bcolors.BOLD}{bcolors.WARNING}ServiceUnavailableError. The OpenAI API is not responding. Retrying...{bcolors.ENDC}"
) )
await recall_func("`The OpenAI API is not responding. Retrying...`") await recall_func("`The OpenAI API is not responding. Retrying...`")
await asyncio.sleep(5)
await recall_func() await recall_func()
i += 1 i += 1
finally: finally: