diff --git a/config/config.yaml b/config/config.yaml index bed67083c..684d7d848 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -30,6 +30,7 @@ RPM: 10 #OPENAI_API_VERSION: "YOUR_AZURE_API_VERSION" #DEPLOYMENT_NAME: "YOUR_DEPLOYMENT_NAME" #DEPLOYMENT_ID: "YOUR_DEPLOYMENT_ID" +STREAM: true #### if zhipuai from `https://open.bigmodel.cn`. You can set here or export API_KEY="YOUR_API_KEY" # ZHIPUAI_API_KEY: "YOUR_API_KEY" diff --git a/metagpt/config.py b/metagpt/config.py index 3f9e742bd..4553a71f1 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -62,6 +62,7 @@ def __init__(self, yaml_file=default_yaml_file): self.max_tokens_rsp = self._get("MAX_TOKENS", 2048) self.deployment_name = self._get("DEPLOYMENT_NAME") self.deployment_id = self._get("DEPLOYMENT_ID") + self.stream = self._get("STREAM") self.spark_appid = self._get("SPARK_APPID") self.spark_api_secret = self._get("SPARK_API_SECRET") diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index b6b034329..cf6f553dc 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -15,7 +15,7 @@ class BaseGPTAPI(BaseChatbot): """GPT API abstract class, requiring all inheritors to provide a series of standard capabilities""" - + stream = True system_prompt = "You are a helpful assistant." def _user_msg(self, msg: str) -> dict[str, str]: @@ -45,7 +45,7 @@ async def aask(self, msg: str, system_msgs: Optional[list[str]] = None) -> str: else: message = [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt \ else [self._user_msg(msg)] - rsp = await self.acompletion_text(message, stream=True) + rsp = await self.acompletion_text(message, stream=self.stream) logger.debug(message) # logger.debug(rsp) return rsp @@ -53,6 +53,9 @@ async def aask(self, msg: str, system_msgs: Optional[list[str]] = None) -> str: def _extract_assistant_rsp(self, context): return "\n".join([i["content"] for i in context if i["role"] == "assistant"]) + def set_stream(self, stream: bool): + self.stream = stream + def ask_batch(self, msgs: list) -> str: context = [] for msg in msgs: diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 34e5693f8..e2590ec20 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -152,6 +152,7 @@ def __init__(self): def __init_openai(self, config): openai.api_key = config.openai_api_key + self.set_stream(config.stream) if config.openai_api_base: openai.api_base = config.openai_api_base if config.openai_api_type: