mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-02 14:45:17 +02:00
feat: + RateLimitError retry
This commit is contained in:
parent
1aeebc85fb
commit
0821e6d099
1 changed files with 20 additions and 9 deletions
|
|
@ -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 = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue