From d4878f23a0042bf983c1fef8947c649f7d4f4878 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Mon, 4 Sep 2023 10:50:21 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E6=94=B9get=5Fsummary=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=98=AF=E5=90=A6=E4=BF=9D=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/provider/openai_api.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 844cd4c1c..26929575c 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -221,18 +221,18 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return CONFIG.max_tokens_rsp return get_max_completion_tokens(messages, self.model, CONFIG.max_tokens_rsp) - async def get_summary(self, text: str, max_words=200): + async def get_summary(self, text: str, max_words=200, keep_language: bool = False): max_token_count = DEFAULT_MAX_TOKENS max_count = 100 while max_count > 0: if len(text) < max_token_count: - return await self._get_summary(text, max_words=max_words) + return await self._get_summary(text=text, max_words=max_words,keep_language=keep_language) padding_size = 20 if max_token_count > 20 else 0 text_windows = self.split_texts(text, window_size=max_token_count - padding_size) summaries = [] for ws in text_windows: - response = await self._get_summary(ws, max_words=max_words) + response = await self._get_summary(text=ws, max_words=max_words,keep_language=keep_language) summaries.append(response) if len(summaries) == 1: return summaries[0] @@ -243,11 +243,14 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): max_count -= 1 # safeguard raise openai.error.InvalidRequestError("text too long") - async def _get_summary(self, text: str, max_words=20): + async def _get_summary(self, text: str, max_words=20, keep_language: bool = False): """Generate text summary""" if len(text) < max_words: return text - command = f"Translate the above content into a summary of less than {max_words} words." + if keep_language: + command = f".Translate the above content into a summary of less than {max_words} words in language of the content." + else: + command = f"Translate the above content into a summary of less than {max_words} words." msg = text + "\n\n" + command logger.info(f"summary ask:{msg}") response = await self.aask(msg=msg, system_msgs=[])