From 8ec1fa89598c6b5b2bd3fdb1a7fff3138c36d7ba Mon Sep 17 00:00:00 2001 From: "sourcery-ai[bot]" <58596630+sourcery-ai[bot]@users.noreply.github.com> Date: Tue, 18 Jul 2023 00:16:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9API=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=20(Sourcery=20refactored)=20(#1066)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update api.py * 'Refactored by Sourcery' --------- Co-authored-by: Matt Gideon <117586514+Haibersut@users.noreply.github.com> Co-authored-by: Sourcery AI <> --- adapter/chatgpt/api.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/adapter/chatgpt/api.py b/adapter/chatgpt/api.py index acb0187a..57325c81 100644 --- a/adapter/chatgpt/api.py +++ b/adapter/chatgpt/api.py @@ -202,15 +202,13 @@ async def request(self, session_id: str = None, messages: list = None) -> str: async with aiohttp.ClientSession() as session: with async_timeout.timeout(self.bot.timeout): async with session.post(f'{api_endpoint}/chat/completions', headers=headers, - data=json.dumps(data), proxy=proxy) as resp: + data=json.dumps(data), proxy=proxy) as resp: if resp.status != 200: response_text = await resp.text() raise Exception( f"{resp.status} {resp.reason} {response_text}", ) - content = await self._process_response(resp, session_id) - - return content + return await self._process_response(resp, session_id) async def request_with_stream(self, session_id: str = None, messages: list = None) -> AsyncGenerator[str, None]: proxy, api_endpoint, headers, data = self._prepare_request(session_id, messages, stream=True) @@ -249,7 +247,7 @@ async def request_with_stream(self, session_id: str = None, messages: list = Non event_text = delta['content'] completion_text += event_text self.latest_role = response_role - yield completion_text + yield event_text self.bot.add_to_conversation(completion_text, response_role, session_id) async def compressed_session(self, session_id: str): @@ -290,17 +288,18 @@ async def ask(self, prompt: str) -> AsyncGenerator[str, None]: self.bot.add_to_conversation(prompt, "user", session_id=self.session_id) start_time = time.time() + full_response = '' + if config.openai.gpt_params.stream: - async for completion_text in self.request_with_stream(session_id=self.session_id): - yield completion_text + async for resp in self.request_with_stream(session_id=self.session_id): + full_response += resp + yield full_response token_count = self.bot.count_tokens(self.session_id, self.bot.engine) - logger.debug(f"[ChatGPT-API:{self.bot.engine}] 响应:{completion_text}") + logger.debug(f"[ChatGPT-API:{self.bot.engine}] 响应:{full_response}") logger.debug(f"[ChatGPT-API:{self.bot.engine}] 使用 token 数:{token_count}") else: - completion_text = await self.request(session_id=self.session_id) - yield completion_text - + yield await self.request(session_id=self.session_id) event_time = time.time() - start_time if event_time is not None: logger.debug(f"[ChatGPT-API:{self.bot.engine}] 接收到全部消息花费了{event_time:.2f}秒")