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}秒")