From 6e0b4a8124a22edb92bb269cb51daeffaf4fc3fb Mon Sep 17 00:00:00 2001 From: yym68686 Date: Tue, 14 May 2024 19:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Bug:=201.=20Fix=20the=20bug=20ca?= =?UTF-8?q?using=20Ratelimit=20error=20202=20on=20DuckDuckGo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💻 Code: 1. Upgrade ModelMerge version to 0.3.11 2. Refactor the code, streamline the bot.py code. --- bot.py | 47 ++++++++--------------------------------------- config.py | 34 +++++++++++++++++++++++++++++++++- requirements.txt | 2 +- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/bot.py b/bot.py index 380c4506..1a041d9f 100644 --- a/bot.py +++ b/bot.py @@ -20,7 +20,9 @@ get_current_lang, update_info_message, update_ENGINE, - update_language + reset_ENGINE, + update_language, + get_robot ) from utils.i18n import strings @@ -124,12 +126,7 @@ async def command_bot(update, context, language=None, prompt=translator_prompt, elif reply_to_message_text and not update_message.reply_to_message.from_user.is_bot: message = reply_to_message_text + "\n" + message - if "claude-3" in config.GPT_ENGINE and config.CLAUDE_API: - robot = config.claude3Bot - if ("mixtral" in config.GPT_ENGINE or "llama" in config.GPT_ENGINE) and config.GROQ_API_KEY: - robot = config.groqBot - if "gemini" in config.GPT_ENGINE and config.GOOGLE_AI_API_KEY: - robot = config.gemini_Bot + robot, role = get_robot() if "gpt" in config.GPT_ENGINE or (config.CLAUDE_API and "claude-3" in config.GPT_ENGINE): message = [{"type": "text", "text": message}] if image_url and (config.GPT_ENGINE == "gpt-4-turbo-2024-04-09" or "gpt-4o" in config.GPT_ENGINE): @@ -156,16 +153,7 @@ async def command_bot(update, context, language=None, prompt=translator_prompt, @decorators.GroupAuthorization @decorators.Authorization async def reset_chat(update, context): - if config.API and config.ChatGPTbot: - config.ChatGPTbot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt) - if config.CLAUDE_API and config.claudeBot: - config.claudeBot.reset(convo_id=str(update.message.chat_id), system_prompt=config.claude_systemprompt) - if config.CLAUDE_API and config.claude3Bot: - config.claude3Bot.reset(convo_id=str(update.message.chat_id), system_prompt=config.claude_systemprompt) - if config.GROQ_API_KEY and config.groqBot: - config.groqBot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt) - if config.GOOGLE_AI_API_KEY and config.gemini_Bot: - config.gemini_Bot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt) + reset_ENGINE(update.message.chat_id) await context.bot.send_message( chat_id=update.message.chat_id, @@ -386,18 +374,7 @@ async def handle_pdf(update, context): new_file = await context.bot.get_file(file_id) file_url = new_file.file_path extracted_text_with_prompt = Document_extract(file_url) - if config.CLAUDE_API and "claude-2.1" in config.GPT_ENGINE: - robot = config.claudeBot - role = "Human" - elif config.CLAUDE_API and "claude-3" in config.GPT_ENGINE: - robot = config.claude3Bot - role = "user" - elif config.GOOGLE_AI_API_KEY and "gemini" in config.GPT_ENGINE: - robot = config.gemini_Bot - role = "user" - else: - robot = config.ChatGPTbot - role = "user" + robot, role = get_robot() robot.add_to_conversation(extracted_text_with_prompt, role, str(update.effective_chat.id)) if config.CLAUDE_API and "claude-3" in config.GPT_ENGINE: robot.add_to_conversation(claude3_doc_assistant_prompt, "assistant", str(update.effective_chat.id)) @@ -422,18 +399,10 @@ async def handle_photo(update, context): photo_file = await context.bot.getFile(file_id) image_url = photo_file.file_path - if config.CLAUDE_API and "claude-2.1" in config.GPT_ENGINE: - robot = config.claudeBot - role = "Human" - elif config.CLAUDE_API and "claude-3" in config.GPT_ENGINE: - robot = config.claude3Bot - role = "user" - else: - robot = config.ChatGPTbot - role = "user" + robot, role = get_robot() base64_image = get_encode_image(image_url) - if image_url and (config.GPT_ENGINE == "gpt-4-turbo-2024-04-09" or "gpt-4o" in config.GPT_ENGINE or (config.CLAUDE_API is None and "claude-3" in config.GPT_ENGINE)): + if image_url and ("gpt-4" in config.GPT_ENGINE or (config.CLAUDE_API is None and "claude-3" in config.GPT_ENGINE)): message = [ { "type": "image_url", diff --git a/config.py b/config.py index 6183f370..18437cff 100644 --- a/config.py +++ b/config.py @@ -80,9 +80,41 @@ def update_ENGINE(data = None): groqBot = groq(api_key=f"{GROQ_API_KEY}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature) if GOOGLE_AI_API_KEY and "gemini" in GPT_ENGINE: gemini_Bot = gemini(api_key=f"{GOOGLE_AI_API_KEY}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature) - update_ENGINE() +def reset_ENGINE(chat_id): + global ChatGPTbot, translate_bot, claudeBot, claude3Bot, groqBot, gemini_Bot + if API and ChatGPTbot: + ChatGPTbot.reset(convo_id=str(chat_id), system_prompt=systemprompt) + if CLAUDE_API and claudeBot: + claudeBot.reset(convo_id=str(chat_id), system_prompt=claude_systemprompt) + if CLAUDE_API and claude3Bot: + claude3Bot.reset(convo_id=str(chat_id), system_prompt=claude_systemprompt) + if GROQ_API_KEY and groqBot: + groqBot.reset(convo_id=str(chat_id), system_prompt=systemprompt) + if GOOGLE_AI_API_KEY and gemini_Bot: + gemini_Bot.reset(convo_id=str(chat_id), system_prompt=systemprompt) + +def get_robot(): + global ChatGPTbot, claudeBot, claude3Bot, groqBot, gemini_Bot + if CLAUDE_API and "claude-2.1" in GPT_ENGINE: + robot = claudeBot + role = "Human" + elif CLAUDE_API and "claude-3" in GPT_ENGINE: + robot = claude3Bot + role = "user" + elif ("mixtral" in GPT_ENGINE or "llama" in GPT_ENGINE) and GROQ_API_KEY: + robot = groqBot + elif GOOGLE_AI_API_KEY and "gemini" in GPT_ENGINE: + robot = gemini_Bot + role = "user" + else: + robot = ChatGPTbot + role = "user" + + return robot, role + + whitelist = os.environ.get('whitelist', None) if whitelist: whitelist = [int(id) for id in whitelist.split(",")] diff --git a/requirements.txt b/requirements.txt index ebc6da49..7c5ccba4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,6 @@ pytz python-dotenv md2tgmd==0.1.9 fake_useragent -ModelMerge==0.3.8 +ModelMerge==0.3.11 oauth2client==3.0.0 python-telegram-bot[webhooks,rate-limiter]==21.0.1 \ No newline at end of file