feat: + RateLimitError retry

This commit is contained in:
莘权 马 2023-08-26 10:21:49 +08:00
parent 1aeebc85fb
commit 0821e6d099

View file

@ -9,6 +9,7 @@
import asyncio
import re
import time
import random
from typing import NamedTuple, List
import traceback
@ -152,15 +153,25 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter):
self.rpm = int(self._options.get("RPM", 10))
async def _achat_completion_stream(self, messages: list[dict]) -> str:
try:
response = await openai.ChatCompletion.acreate(
**self._cons_kwargs(messages),
stream=True
)
except Exception as e:
error_str = traceback.format_exc()
logger.error(f"Exception:{e}, stack:{error_str}")
raise e
max_try = 5
response = None
for i in range(max_try):
try:
response = await openai.ChatCompletion.acreate(
**self._cons_kwargs(messages),
stream=True
)
break
except openai.error.RateLimitError as e:
random_time = random.uniform(0, 3) # 生成0到5秒之间的随机时间
rounded_time = round(random_time, 1) # 保留一位小数以实现0.1秒的精度
logger.warning(f"Exception:{e}, sleeping for {rounded_time} seconds")
await asyncio.sleep(rounded_time)
continue
except Exception as e:
error_str = traceback.format_exc()
logger.error(f"Exception:{e}, stack:{error_str}")
raise e
# create variables to collect the stream of chunks
collected_chunks = []