Skip to content

Commit

Permalink
🐛 Bug: 1. Fix the bug that gpt-4o cannot answer
Browse files Browse the repository at this point in the history
2. Fix the bug where gpt-4o cannot read images.

3. Fix the bug of infinite function calls

4. Optimize max token calculation

💻 Code: 1. Upgrade ModelMerge version to 0.3.7

2. Refactor the code, streamline the bot.py code.

3. Delete useless test files.
  • Loading branch information
yym68686 committed May 14, 2024
1 parent 8663ab1 commit 82e54a4
Show file tree
Hide file tree
Showing 35 changed files with 122 additions and 2,257 deletions.
82 changes: 23 additions & 59 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
import utils.decorators as decorators
from md2tgmd import escape

from ModelMerge.models import chatgpt, claude, groq, claude3, gemini
from ModelMerge.models.config import PLUGINS
from ModelMerge.utils.prompt import translator_en2zh_prompt, translator_prompt, claude3_doc_assistant_prompt
from ModelMerge.utils.scripts import Document_extract, get_encode_image, claude_replace

import config
from config import WEB_HOOK, PORT, BOT_TOKEN, update_first_buttons_message, update_model_buttons, get_current_lang
from config import (
WEB_HOOK,
PORT,
BOT_TOKEN,
update_first_buttons_message,
update_model_buttons,
get_current_lang,
update_info_message,
update_ENGINE,
update_language
)

from utils.i18n import strings

from telegram.constants import ChatAction
Expand Down Expand Up @@ -114,8 +124,6 @@ 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-2.1" in config.GPT_ENGINE and config.ClaudeAPI:
robot = config.claudeBot
if "claude-3" in config.GPT_ENGINE and config.ClaudeAPI:
robot = config.claude3Bot
if ("mixtral" in config.GPT_ENGINE or "llama" in config.GPT_ENGINE) and config.GROQ_API_KEY:
Expand All @@ -124,7 +132,7 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
robot = config.gemini_Bot
if "gpt" in config.GPT_ENGINE or (config.ClaudeAPI 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":
if image_url and (config.GPT_ENGINE == "gpt-4-turbo-2024-04-09" or "gpt-4o" in config.GPT_ENGINE):
base64_image = get_encode_image(image_url)
message.append(
{
Expand Down Expand Up @@ -303,47 +311,21 @@ async def delete_message(update, context, messageid, delay=10):
print("error", e)
print('\033[0m')


def replace_with_asterisk(string, start=10, end=45):
return string[:start] + '*' * (end - start) + string[end:]

def update_info_message(update):
return (
f"`Hi, {update.effective_user.username}!`\n\n"
f"**Default engine:** `{config.GPT_ENGINE}`\n"
f"**Temperature:** `{config.temperature}`\n"
f"**API_URL:** `{config.API_URL}`\n\n"
f"**API:** `{replace_with_asterisk(config.API)}`\n\n"
f"**WEB_HOOK:** `{config.WEB_HOOK}`\n\n"
)


@decorators.AdminAuthorization
@decorators.GroupAuthorization
@decorators.Authorization
async def button_press(update, context):
"""Function to handle the button press"""
info_message = update_info_message(update)
info_message = update_info_message()
callback_query = update.callback_query
await callback_query.answer()
data = callback_query.data
banner = strings['message_banner'][get_current_lang()]
if "gpt-" in data or "claude" in data or "mixtral" in data or "llama" in data or "gemini" in data or (config.CUSTOM_MODELS and data in config.CUSTOM_MODELS):
config.GPT_ENGINE = data
# print("config.GPT_ENGINE", config.GPT_ENGINE)
if (config.API and "gpt-" in data) or (config.API and not config.ClaudeAPI) or (config.API and config.CUSTOM_MODELS and data in config.CUSTOM_MODELS):
config.ChatGPTbot = chatgpt(api_key=f"{config.API}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)
config.ChatGPTbot.reset(convo_id=str(update.effective_chat.id), system_prompt=config.systemprompt)
if config.ClaudeAPI and "claude-2.1" in data:
config.claudeBot = claude(api_key=f"{config.ClaudeAPI}", engine=config.GPT_ENGINE, system_prompt=config.claude_systemprompt, temperature=config.temperature)
if config.ClaudeAPI and "claude-3" in data:
config.claude3Bot = claude3(api_key=f"{config.ClaudeAPI}", engine=config.GPT_ENGINE, system_prompt=config.claude_systemprompt, temperature=config.temperature)
if config.GROQ_API_KEY and ("mixtral" in data or "llama" in data):
config.groqBot = groq(api_key=f"{config.GROQ_API_KEY}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)
if config.GOOGLE_AI_API_KEY and "gemini" in data:
config.gemini_Bot = gemini(api_key=f"{config.GOOGLE_AI_API_KEY}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)
if data.endswith("ENGINE"):
data = data[:-6]
update_ENGINE(data)
try:
info_message = update_info_message(update)
info_message = update_info_message()
if info_message + banner != callback_query.message.text:
message = await callback_query.edit_message_text(
text=escape(info_message + banner),
Expand All @@ -366,27 +348,9 @@ async def button_press(update, context):
parse_mode='MarkdownV2'
)
elif "language" in data:
if config.LANGUAGE == "Simplified Chinese":
config.LANGUAGE = "English"
config.systemprompt = config.systemprompt.replace("Simplified Chinese", "English")
config.claude_systemprompt = config.claude_systemprompt.replace("Simplified Chinese", "English")
else:
config.LANGUAGE = "Simplified Chinese"
config.systemprompt = config.systemprompt.replace("English", "Simplified Chinese")
config.claude_systemprompt = config.claude_systemprompt.replace("English", "Simplified Chinese")
# config.systemprompt = f"You are ChatGPT, a large language model trained by OpenAI. Respond conversationally in {config.LANGUAGE}. Knowledge cutoff: 2021-09. Current date: [ {config.Current_Date} ]"
if config.API:
config.ChatGPTbot = chatgpt(api_key=f"{config.API}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)
config.ChatGPTbot.reset(convo_id=str(update.effective_chat.id), system_prompt=config.systemprompt)
if config.ClaudeAPI:
config.claudeBot = claude(api_key=f"{config.ClaudeAPI}", engine=config.GPT_ENGINE, system_prompt=config.claude_systemprompt, temperature=config.temperature)
config.claude3Bot = claude3(api_key=f"{config.ClaudeAPI}", engine=config.GPT_ENGINE, system_prompt=config.claude_systemprompt, temperature=config.temperature)
if config.GROQ_API_KEY:
config.groqBot = groq(api_key=f"{config.GROQ_API_KEY}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)
if config.GOOGLE_AI_API_KEY:
config.gemini_Bot = gemini(api_key=f"{config.GOOGLE_AI_API_KEY}", engine=config.GPT_ENGINE, system_prompt=config.systemprompt, temperature=config.temperature)

info_message = update_info_message(update)
update_language()
update_ENGINE()
info_message = update_info_message()
message = await callback_query.edit_message_text(
text=escape(info_message),
reply_markup=InlineKeyboardMarkup(update_first_buttons_message()),
Expand All @@ -397,7 +361,7 @@ async def button_press(update, context):
PLUGINS[data] = not PLUGINS[data]
except:
setattr(config, data, not getattr(config, data))
info_message = update_info_message(update)
info_message = update_info_message()
message = await callback_query.edit_message_text(
text=escape(info_message),
reply_markup=InlineKeyboardMarkup(update_first_buttons_message()),
Expand All @@ -408,7 +372,7 @@ async def button_press(update, context):
@decorators.GroupAuthorization
@decorators.Authorization
async def info(update, context):
info_message = update_info_message(update)
info_message = update_info_message()
message = await context.bot.send_message(chat_id=update.message.chat_id, text=escape(info_message), reply_markup=InlineKeyboardMarkup(update_first_buttons_message()), parse_mode='MarkdownV2', disable_web_page_preview=True)

@decorators.GroupAuthorization
Expand Down
152 changes: 98 additions & 54 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,76 @@

NICK = os.environ.get('NICK', None)
PORT = int(os.environ.get('PORT', '8080'))
WEB_HOOK = os.environ.get('WEB_HOOK', None)
BOT_TOKEN = os.environ.get('BOT_TOKEN', None)

API = os.environ.get('API', None)
API_URL = os.environ.get('API_URL', 'https://api.openai.com/v1/chat/completions')
def replace_with_asterisk(string, start=10, end=45):
return string[:start] + '*' * (end - start) + string[end:]

GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
PASS_HISTORY = (os.environ.get('PASS_HISTORY', "True") == "False") == False
API_URL = os.environ.get('API_URL', 'https://api.openai.com/v1/chat/completions')
API = os.environ.get('API', None)
WEB_HOOK = os.environ.get('WEB_HOOK', None)

USE_GOOGLE = (os.environ.get('USE_GOOGLE', "True") == "False") == False
if os.environ.get('GOOGLE_API_KEY', None) == None and os.environ.get('GOOGLE_CSE_ID', None) == None:
USE_GOOGLE = False
def update_info_message():
return (
f"**Default engine:** `{GPT_ENGINE}`\n"
f"**API_URL:** `{API_URL}`\n\n"
f"**API:** `{replace_with_asterisk(API)}`\n\n"
f"**WEB_HOOK:** `{WEB_HOOK}`\n\n"
)

temperature = float(os.environ.get('temperature', '0.5'))
LANGUAGE = os.environ.get('LANGUAGE', 'English')
GROQ_API_KEY = os.environ.get('GROQ_API_KEY', None)
GOOGLE_AI_API_KEY = os.environ.get('GOOGLE_AI_API_KEY', None)
CUSTOM_MODELS = os.environ.get('CUSTOM_MODELS', None)
if CUSTOM_MODELS:
CUSTOM_MODELS_LIST = [id for id in CUSTOM_MODELS.split(",")]
else:
CUSTOM_MODELS_LIST = None

current_date = datetime.now()
Current_Date = current_date.strftime("%Y-%m-%d")

LANGUAGE = os.environ.get('LANGUAGE', 'English')
systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
claude_systemprompt = os.environ.get('SYSTEMPROMPT', prompt.claude_system_prompt.format(LANGUAGE))

if API:
ChatGPTbot = chatgpt(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)

translate_bot = chatgpt(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
copilot_bot = chatgpt(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=prompt.search_system_prompt.format(LANGUAGE), temperature=temperature)
dallbot = dalle3(api_key=f"{API}")
else:
ChatGPTbot = None
def get_current_lang():
if LANGUAGE == "Simplified Chinese":
lang = "zh"
else:
lang = "en"
return lang

ClaudeAPI = os.environ.get('claude_api_key', None)
if ClaudeAPI:
claudeBot = claude(api_key=f"{ClaudeAPI}", system_prompt=claude_systemprompt)
claude3Bot = claude3(api_key=f"{ClaudeAPI}", system_prompt=claude_systemprompt)
def update_language():
global LANGUAGE, systemprompt, claude_systemprompt
try:
if LANGUAGE == "Simplified Chinese":
LANGUAGE = "English"
systemprompt = systemprompt.replace("Simplified Chinese", "English")
claude_systemprompt = claude_systemprompt.replace("Simplified Chinese", "English")
else:
LANGUAGE = "Simplified Chinese"
systemprompt = systemprompt.replace("English", "Simplified Chinese")
claude_systemprompt = claude_systemprompt.replace("English", "Simplified Chinese")
except:
pass

if GROQ_API_KEY:
groqBot = groq(api_key=f"{GROQ_API_KEY}")
if GOOGLE_AI_API_KEY:
gemini_Bot = gemini(api_key=f"{GOOGLE_AI_API_KEY}")
temperature = float(os.environ.get('temperature', '0.5'))
CLAUDE_API = os.environ.get('claude_api_key', None)

def update_ENGINE(data = None):
global GPT_ENGINE, ChatGPTbot, translate_bot, dallbot, claudeBot, claude3Bot, groqBot, gemini_Bot
if data:
GPT_ENGINE = data
if API:
ChatGPTbot = chatgpt(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
translate_bot = chatgpt(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
dallbot = dalle3(api_key=f"{API}")
if CLAUDE_API and "claude-2.1" in GPT_ENGINE:
claudeBot = claude(api_key=f"{CLAUDE_API}", engine=GPT_ENGINE, system_prompt=claude_systemprompt, temperature=temperature)
if CLAUDE_API and "claude-3" in GPT_ENGINE:
claude3Bot = claude3(api_key=f"{CLAUDE_API}", engine=GPT_ENGINE, system_prompt=claude_systemprompt, temperature=temperature)
if GROQ_API_KEY and ("mixtral" in GPT_ENGINE or "llama" in GPT_ENGINE):
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()

whitelist = os.environ.get('whitelist', None)
if whitelist:
Expand All @@ -67,18 +92,35 @@
if GROUP_LIST:
GROUP_LIST = [int(id) for id in GROUP_LIST.split(",")]

class userConfig:
def __init__(self, user_id: int):
class UserConfig:
def __init__(self, user_id: str = "default", language="English", engine="gpt-4o"):
self.user_id = user_id
self.language = LANGUAGE
self.temperature = temperature
self.engine = GPT_ENGINE
self.system_prompt = systemprompt
self.search_system_prompt = prompt.search_system_prompt.format(self.language)
self.search_model = "gpt-3.5-turbo-1106"

def get_plugins_status(item):
return "✅" if PLUGINS[item] else "☑️"
self.language = language
self.engine = engine
self.users = {
"default": {
"language": self.language,
"engine": self.engine,
}
}
def user_init(self, user_id):
if user_id not in self.users:
self.users[user_id] = {"language": LANGUAGE, "engine": GPT_ENGINE}
def get_language(self, user_id):
self.user_init(user_id)
return self.users[user_id]["language"]
def set_language(self, user_id, language):
self.user_init(user_id)
self.users[user_id]["language"] = language

def get_engine(self, user_id):
self.user_init(user_id)
return self.users[user_id]["engine"]
def set_engine(self, user_id, engine):
self.user_init(user_id)
self.users[user_id]["engine"] = engine

Users = UserConfig()

def delete_model_digit_tail(lst):
for i in range(len(lst) - 1, -1, -1):
Expand All @@ -97,7 +139,7 @@ def create_buttons(strings):
temp = []

for string in filtered_strings1:
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string)
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string + "ENGINE")
temp.append(button)

# 每两个按钮一组
Expand All @@ -110,7 +152,7 @@ def create_buttons(strings):
buttons.append(temp)

for string in filtered_strings2:
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string)
button = InlineKeyboardButton(delete_model_digit_tail(string.split("-")), callback_data=string + "ENGINE")
buttons.append([button])

return buttons
Expand All @@ -134,6 +176,11 @@ def create_buttons(strings):
"gemini-1.5-pro-latest",
])

CUSTOM_MODELS = os.environ.get('CUSTOM_MODELS', None)
if CUSTOM_MODELS:
CUSTOM_MODELS_LIST = [id for id in CUSTOM_MODELS.split(",")]
else:
CUSTOM_MODELS_LIST = None
if CUSTOM_MODELS_LIST:
delete_models = [model[1:] for model in CUSTOM_MODELS_LIST if model[0] == "-"]
for target in delete_models:
Expand All @@ -156,13 +203,10 @@ def update_model_buttons():
)
return buttons

def get_current_lang():
if LANGUAGE == "Simplified Chinese":
lang = "zh"
else:
lang = "en"
return lang
def get_plugins_status(item):
return "✅" if PLUGINS[item] else "☑️"

PASS_HISTORY = (os.environ.get('PASS_HISTORY', "True") == "False") == False
def update_first_buttons_message():
history = "✅" if PASS_HISTORY else "☑️"

Expand All @@ -176,15 +220,15 @@ def update_first_buttons_message():
[
InlineKeyboardButton(strings["button_change_model"][lang], callback_data="MODEL"),
InlineKeyboardButton(strings['button_language'][lang], callback_data="language"),
InlineKeyboardButton(f"{strings['button_history'][lang]} {history}", callback_data="PASS_HISTORY"),
InlineKeyboardButton(f"{history} {strings['button_history'][lang]}", callback_data="PASS_HISTORY"),
],
[
InlineKeyboardButton(f"{strings['button_search'][lang]} {get_plugins_status('SEARCH')}", callback_data='SEARCH'),
InlineKeyboardButton(f"{strings['button_current_time'][lang]} {get_plugins_status('DATE')}", callback_data='DATE'),
InlineKeyboardButton(f"{get_plugins_status('SEARCH')}{strings['button_search'][lang]}", callback_data='SEARCH'),
InlineKeyboardButton(f"{get_plugins_status('DATE')}{strings['button_current_time'][lang]}", callback_data='DATE'),
],
[
InlineKeyboardButton(f"{strings['button_url'][lang]} {get_plugins_status('URL')}", callback_data='URL'),
InlineKeyboardButton(f"{strings['button_version'][lang]} {get_plugins_status('VERSION')}", callback_data='VERSION'),
InlineKeyboardButton(f"{get_plugins_status('URL')}{strings['button_url'][lang]}", callback_data='URL'),
InlineKeyboardButton(f"{get_plugins_status('VERSION')}{strings['button_version'][lang]}", callback_data='VERSION'),
],
]
return first_buttons
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pytz
python-dotenv
md2tgmd==0.1.9
fake_useragent
ModelMerge==0.3.6
ModelMerge==0.3.7
oauth2client==3.0.0
python-telegram-bot[webhooks,rate-limiter]==21.0.1
13 changes: 0 additions & 13 deletions test/test.py

This file was deleted.

6 changes: 0 additions & 6 deletions test/test_API.py

This file was deleted.

Loading

0 comments on commit 82e54a4

Please sign in to comment.