🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
feat: + RateLimitError retry
莘权 马0821e6d0996d
diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.pyindex 510041e98..e98acbd75 100644--- a/metagpt/provider/openai_api.py+++ b/metagpt/provider/openai_api.py@@ -9,6 +9,7 @@import asyncioimport reimport time+import randomfrom typing import NamedTuple, Listimport 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 chunkscollected_chunks = []