From 0b22e39e62bc82aff974facc924889953decb46d Mon Sep 17 00:00:00 2001 From: Matt Gideon <117586514+Haibersut@users.noreply.github.com> Date: Thu, 27 Jul 2023 08:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E5=8C=96=E9=83=A8=E7=BD=B2=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/chatgpt/api.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/adapter/chatgpt/api.py b/adapter/chatgpt/api.py index ad3c87e5..dae02995 100644 --- a/adapter/chatgpt/api.py +++ b/adapter/chatgpt/api.py @@ -71,9 +71,10 @@ def count_tokens(self, session_id: str = "default", model: str = DEFAULT_ENGINE) for message in self.conversation[session_id]: num_tokens += tokens_per_message for key, value in message.items(): - num_tokens += len(encoding.encode(value)) - if key == "name": - num_tokens += tokens_per_name + if value is not None: + num_tokens += len(encoding.encode(value)) + if key == "name": + num_tokens += tokens_per_name num_tokens += 3 # every reply is primed with assistant return num_tokens @@ -109,7 +110,6 @@ def __init__(self, session_id: str = "unknown"): "gpt-4-32k-0314", "gpt-4-0613", "gpt-4-32k-0613", - "claude-2-web", ] def manage_conversation(self, session_id: str, prompt: str): @@ -252,9 +252,10 @@ async def request_with_stream(self, session_id: str = None, messages: list = Non response_role = delta['role'] if 'content' in delta: event_text = delta['content'] - completion_text += event_text - self.latest_role = response_role - yield event_text + if event_text is not None: + completion_text += event_text + self.latest_role = response_role + yield event_text self.bot.add_to_conversation(completion_text, response_role, session_id) async def compressed_session(self, session_id: str):