🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
feat: rebase geekan:env_refactor
莘权 马390065c680dc
diff --cc metagpt/provider/openai_api.pyindex 97bc67069,a73bb0aa0..a24748e41--- a/metagpt/provider/openai_api.py+++ b/metagpt/provider/openai_api.py@@@ -185,63 -196,55 +184,63 @@@ class OpenAIGPTAPI(BaseGPTAPI, RateLimikwargs.update(configs)if CONFIG.openai_api_type == "azure":- if CONFIG.deployment_name and CONFIG.deployment_id:- raise ValueError("You can only use one of the `deployment_id` or `deployment_name` model")- elif not CONFIG.deployment_name and not CONFIG.deployment_id:- raise ValueError("You must specify `DEPLOYMENT_NAME` or `DEPLOYMENT_ID` parameter")- kwargs_mode = (- {"engine": CONFIG.deployment_name}- if CONFIG.deployment_name- else {"deployment_id": CONFIG.deployment_id}- )+ kwargs["model"] = CONFIG.deployment_idelse:- kwargs_mode = {"model": self.model}- kwargs.update(kwargs_mode)+ kwargs["model"] = self.model+ kwargs["timeout"] = max(CONFIG.TIMEOUT, timeout) if CONFIG.TIMEOUT is not None else timeout+return kwargs- async def _achat_completion(self, messages: list[dict]) -> dict:- rsp = await self.llm.ChatCompletion.acreate(**self._cons_kwargs(messages))- self._update_costs(rsp.get("usage"))- return rsp+ async def _achat_completion(self, messages: list[dict], timeout=3) -> dict:+ kwargs = self._cons_kwargs(messages, timeout=timeout)+ rsp = await self._client.chat.completions.create(**kwargs)+ self._update_costs(rsp.usage)+ return rsp.dict()- def _chat_completion(self, messages: list[dict]) -> dict:- rsp = self.llm.ChatCompletion.create(**self._cons_kwargs(messages))- self._update_costs(rsp)- return rsp-- def completion(self, messages: list[dict]) -> dict:- # if isinstance(messages[0], Message):- # messages = self.messages_to_dict(messages)- return self._chat_completion(messages)+ def completion(self, messages: list[dict], timeout=3) -> dict:+ loop = self.get_event_loop()+ return loop.run_until_complete(self.acompletion(messages, timeout=timeout))- async def acompletion(self, messages: list[dict]) -> dict:+ async def acompletion(self, messages: list[dict], timeout=3) -> dict:# if isinstance(messages[0], Message):# messages = self.messages_to_dict(messages)- return await self._achat_completion(messages)+ return await self._achat_completion(messages, timeout=timeout)@retry(- stop=stop_after_attempt(3),- wait=wait_fixed(1),+ wait=wait_random_exponential(min=1, max=60),+ stop=stop_after_attempt(6),after=after_log(logger, logger.level("WARNING").name),retry=retry_if_exception_type(APIConnectionError),retry_error_callback=log_and_reraise,)- async def acompletion_text(self, messages: list[dict], stream=False) -> str:+ @retry(++ wait=wait_random_exponential(min=1, max=60),+ stop=stop_after_attempt(6),- wait=wait_exponential(1),+ after=after_log(logger, logger.level("WARNING").name),+ retry=retry_if_exception_type(RateLimitError),+ reraise=True,+ )+ async def acompletion_text(self, messages: list[dict], stream=False, generator: bool = False, timeout=3) -> str:"""when streaming, print each token in place."""if stream:- return await self._achat_completion_stream(messages)- rsp = await self._achat_completion(messages)+ resp = self._achat_completion_stream(messages, timeout=timeout)+ if generator:+ return resp++ collected_messages = []+ async for i in resp:+ print(i, end="")+ collected_messages.append(i)++ full_reply_content = "".join(collected_messages)+ usage = self._calc_usage(messages, full_reply_content)+ self._update_costs(usage)+ return full_reply_content++ rsp = await self._achat_completion(messages, timeout=timeout)return self.get_choice_text(rsp)- def _func_configs(self, messages: list[dict], **kwargs) -> dict:+ def _func_configs(self, messages: list[dict], timeout=3, **kwargs) -> dict:"""Note: Keep kwargs consistent with the parameters in the https://platform.openai.com/docs/api-reference/chat/create"""diff --cc metagpt/provider/zhipuai_api.pyindex 3161c0e88,92119b764..206f0dab9--- a/metagpt/provider/zhipuai_api.py+++ b/metagpt/provider/zhipuai_api.py@@@ -9,12 -13,8 +9,12 @@@ from tenacity importretry,retry_if_exception_type,stop_after_attempt,- wait_fixed,+ wait_random_exponential,)+from requests import ConnectionError++import openai+import zhipuaifrom metagpt.config import CONFIGfrom metagpt.logs import logger@@@ -126,13 -122,13 +126,13 @@@ class ZhiPuAIGPTAPI(BaseGPTAPI)@retry(stop=stop_after_attempt(3),- wait=wait_fixed(1),+ wait=wait_random_exponential(min=1, max=60),after=after_log(logger, logger.level("WARNING").name),retry=retry_if_exception_type(ConnectionError),- retry_error_callback=log_and_reraise,+ retry_error_callback=log_and_reraise)async def acompletion_text(self, messages: list[dict], stream=False) -> str:- """response in async with stream or non-stream mode"""+ """ response in async with stream or non-stream mode """if stream:return await self._achat_completion_stream(messages)resp = await self._achat_completion(messages)diff --cc requirements.txtindex de80b0949,99f738448..e622a4b9e--- a/requirements.txt+++ b/requirements.txt@@@ -42,13 -42,9 +42,13 @@@ qdrant-client==1.4.pytest-mock==3.11.1open-interpreter==0.1.7; python_version>"3.9"ta==0.10.2- semantic-kernel-semantic-kernel==0.3.13.dev0++semantic-kernel==0.4.0.dev0wrapt==1.15.0-websocket-client==0.58.0+#aiohttp_jinja2+#azure-cognitiveservices-speech~=1.31.0+#aioboto3~=11.3.0+#redis==4.3.5+websocket-client==1.6.2aiofiles==23.2.1gitpython==3.1.40zhipuai==1.0.7