From ebdc93aba0b78a5a40fc77d689f0ac0342cba816 Mon Sep 17 00:00:00 2001 From: yym68686 Date: Sat, 16 Dec 2023 19:44:09 +0800 Subject: [PATCH] 1. Add the balance of dalle3 when it is insufficient and display an error message. 2. Fixed bug: potential timeout 3. Fixed bug: when switching to the claude2 model. 4. Fixed bug: where claude2 may receive empty messages for string concatenation. 5. Fixed bug: truncate function call in search. --- bot.py | 9 +++++++-- test/test_claude.py | 3 +-- test/test_tikitoken.py | 4 ++++ utils/chatgpt2api.py | 17 +++++++---------- 4 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 test/test_tikitoken.py diff --git a/bot.py b/bot.py index 99a665b0..f72a47e6 100644 --- a/bot.py +++ b/bot.py @@ -181,6 +181,8 @@ async def image(update, context): result += "当前 prompt 未能成功生成图片,可能因为版权,政治,色情,暴力,种族歧视等违反 OpenAI 的内容政策😣,换句话试试吧~" elif "server is busy" in str(e): result += "服务器繁忙,请稍后再试~" + elif "billing_hard_limit_reached" in str(e): + result += "当前账号余额不足~" else: result += f"`{e}`" await context.bot.edit_message_text(chat_id=chatid, message_id=start_messageid, text=result, parse_mode='MarkdownV2', disable_web_page_preview=True) @@ -225,7 +227,7 @@ async def delete_message(update, context, messageid, delay=10): # ], [ InlineKeyboardButton("claude-2", callback_data="claude-2"), - InlineKeyboardButton("claude-2-web", callback_data="claude-2-web"), + # InlineKeyboardButton("claude-2-web", callback_data="claude-2-web"), ], [ InlineKeyboardButton("返回上一级", callback_data="返回上一级"), @@ -571,4 +573,7 @@ async def post_init(application: Application) -> None: print("WEB_HOOK:", WEB_HOOK) application.run_webhook("127.0.0.1", PORT, webhook_url=WEB_HOOK) else: - application.run_polling() \ No newline at end of file + # application.run_polling() + time_out = 600 + application.run_polling(read_timeout=time_out, write_timeout=time_out) + # application.run_polling(read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out, timeout=time_out) \ No newline at end of file diff --git a/test/test_claude.py b/test/test_claude.py index 0e533157..b8dd87a2 100644 --- a/test/test_claude.py +++ b/test/test_claude.py @@ -109,8 +109,7 @@ def get_token_count(self, convo_id: str = "default") -> int: raise NotImplementedError( f"Engine {self.engine} is not supported. Select from {ENGINES}", ) - tiktoken.model.MODEL_TO_ENCODING["gpt-4"] = "cl100k_base" - tiktoken.model.MODEL_TO_ENCODING["claude-2-web"] = "cl100k_base" + tiktoken.get_encoding("cl100k_base") tiktoken.model.MODEL_TO_ENCODING["claude-2"] = "cl100k_base" encoding = tiktoken.encoding_for_model(self.engine) diff --git a/test/test_tikitoken.py b/test/test_tikitoken.py new file mode 100644 index 00000000..84e85d95 --- /dev/null +++ b/test/test_tikitoken.py @@ -0,0 +1,4 @@ +import tiktoken +# tiktoken.get_encoding("cl100k_base") +tiktoken.model.MODEL_TO_ENCODING["claude-2.1"] = "cl100k_base" +encoding = tiktoken.encoding_for_model("claude-2.1") \ No newline at end of file diff --git a/utils/chatgpt2api.py b/utils/chatgpt2api.py index ff47b162..1090e13f 100644 --- a/utils/chatgpt2api.py +++ b/utils/chatgpt2api.py @@ -125,10 +125,7 @@ def get_token_count(self, convo_id: str = "default") -> int: raise NotImplementedError( f"Engine {self.engine} is not supported. Select from {ENGINES}", ) - tiktoken.model.MODEL_TO_ENCODING["gpt-4"] = "cl100k_base" - tiktoken.model.MODEL_TO_ENCODING["claude-2-web"] = "cl100k_base" tiktoken.model.MODEL_TO_ENCODING["claude-2"] = "cl100k_base" - encoding = tiktoken.encoding_for_model(self.engine) num_tokens = 0 @@ -195,8 +192,9 @@ def ask_stream( # print(line) resp: dict = json.loads(line) content = resp.get("completion") - full_response += content - yield content + if content: + full_response += content + yield content self.add_to_conversation(full_response, response_role, convo_id=convo_id) # print(repr(self.conversation.Conversation(convo_id))) # print("total tokens:", self.get_token_count(convo_id)) @@ -395,7 +393,7 @@ def truncate_conversation( while True: json_post = self.get_post_body(prompt, role, convo_id, model, pass_history, **kwargs) url = config.bot_api_url.chat_url - if self.engine == "gpt-4-1106-preview" or self.engine == "gpt-3.5-turbo-1106": + if self.engine == "gpt-4-1106-preview" or self.engine == "gpt-3.5-turbo-1106" or self.engine == "claude-2": message_token = { "total": self.get_token_count(convo_id), } @@ -430,10 +428,9 @@ def get_token_count(self, convo_id: str = "default") -> int: raise NotImplementedError( f"Engine {self.engine} is not supported. Select from {ENGINES}", ) - # tiktoken.model.MODEL_TO_ENCODING["gpt-4"] = "cl100k_base" - # tiktoken.model.MODEL_TO_ENCODING["claude-2-web"] = "cl100k_base" - # tiktoken.model.MODEL_TO_ENCODING["claude-2"] = "cl100k_base" tiktoken.get_encoding("cl100k_base") + tiktoken.model.MODEL_TO_ENCODING["gpt-4"] = "cl100k_base" + tiktoken.model.MODEL_TO_ENCODING["claude-2"] = "cl100k_base" encoding = tiktoken.encoding_for_model(self.engine) @@ -593,7 +590,7 @@ def ask_stream( if "name" in delta["function_call"]: function_call_name = delta["function_call"]["name"] full_response += function_call_content - if full_response.count("\\n") > 2: + if full_response.count("\\n") > 2 or "}" in full_response: break if need_function_call: full_response = check_json(full_response)