Skip to content

Commit

Permalink
✨ Feature: Add i18n support, set the default language to English.
Browse files Browse the repository at this point in the history
🤖 Model: Set the default context length of gpt-4-turbo to 128k, and set the search token length limit to 32k.

💻 Code: Upgrade ModelMerge version to 0.3.2
  • Loading branch information
yym68686 committed May 13, 2024
1 parent ae406fa commit 20b491f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 68 deletions.
65 changes: 14 additions & 51 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
sys.dont_write_bytecode = True
import logging
import traceback
import decorators
import utils.decorators as decorators
from md2tgmd import escape

from ModelMerge.models import chatgpt, claude, groq, claude3, gemini
Expand All @@ -12,7 +12,8 @@
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, buttons
from config import WEB_HOOK, PORT, BOT_TOKEN, update_first_buttons_message, update_model_buttons, get_current_lang
from utils.i18n import strings

from telegram.constants import ChatAction
from telegram import BotCommand, InlineKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent, Update
Expand Down Expand Up @@ -175,7 +176,7 @@ async def getChatGPT(update, context, title, robot, message, chatid, messageid):

message = await context.bot.send_message(
chat_id=chatid,
text="思考中💭",
text=strings['message_think'][get_current_lang()],
parse_mode='MarkdownV2',
reply_to_message_id=messageid,
)
Expand Down Expand Up @@ -316,7 +317,7 @@ def update_info_message(update):
f"**WEB_HOOK:** `{config.WEB_HOOK}`\n\n"
)

banner = "👇下面可以随时更改默认模型:"

@decorators.AdminAuthorization
@decorators.GroupAuthorization
@decorators.Authorization
Expand All @@ -326,6 +327,7 @@ async def button_press(update, context):
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)
Expand All @@ -345,19 +347,19 @@ async def button_press(update, context):
if info_message + banner != callback_query.message.text:
message = await callback_query.edit_message_text(
text=escape(info_message + banner),
reply_markup=InlineKeyboardMarkup(buttons),
reply_markup=InlineKeyboardMarkup(update_model_buttons()),
parse_mode='MarkdownV2'
)
except Exception as e:
logger.info(e)
pass
elif "更换问答模型" in data:
elif "MODEL" in data:
message = await callback_query.edit_message_text(
text=escape(info_message + banner),
reply_markup=InlineKeyboardMarkup(buttons),
reply_markup=InlineKeyboardMarkup(update_model_buttons()),
parse_mode='MarkdownV2'
)
elif "返回" in data:
elif "BACK" in data:
message = await callback_query.edit_message_text(
text=escape(info_message),
reply_markup=InlineKeyboardMarkup(update_first_buttons_message()),
Expand Down Expand Up @@ -512,44 +514,14 @@ async def inlinequery(update, context):

await update.inline_query.answer(results)

# @decorators.GroupAuthorization
# @decorators.Authorization
# async def qa(update, context):
# if (len(context.args) != 2):
# message = (
# f"格式错误哦~,需要两个参数,注意路径或者链接、问题之间的空格\n\n"
# f"请输入 `/qa 知识库链接 要问的问题`\n\n"
# f"例如知识库链接为 https://abc.com ,问题是 蘑菇怎么分类?\n\n"
# f"则输入 `/qa https://abc.com 蘑菇怎么分类?`\n\n"
# f"问题务必不能有空格,👆点击上方命令复制格式\n\n"
# f"除了输入网址,同时支持本地知识库,本地知识库文件夹路径为 `./wiki`,问题是 蘑菇怎么分类?\n\n"
# f"则输入 `/qa ./wiki 蘑菇怎么分类?`\n\n"
# f"问题务必不能有空格,👆点击上方命令复制格式\n\n"
# f"本地知识库目前只支持 Markdown 文件\n\n"
# )
# await context.bot.send_message(chat_id=update.effective_chat.id, text=escape(message), parse_mode='MarkdownV2', disable_web_page_preview=True)
# return
# print("\033[32m", update.effective_user.username, update.effective_user.id, update.message.text, "\033[0m")
# await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
# result = await docQA(context.args[0], context.args[1], get_doc_from_local)
# print(result["answer"])
# # source_url = set([i.metadata['source'] for i in result["source_documents"]])
# # source_url = "\n".join(source_url)
# # message = (
# # f"{result['result']}\n\n"
# # f"参考链接:\n"
# # f"{source_url}"
# # )
# await context.bot.send_message(chat_id=update.message.chat_id, text=escape(result["answer"]), parse_mode='MarkdownV2', disable_web_page_preview=True)

async def start(update, context): # 当用户输入/start时,返回文本
user = update.effective_user
message = (
"我是人见人爱的 ChatGPT~\n\n"
"欢迎访问 https://github.com/yym68686/ChatGPT-Telegram-Bot 查看源码\n\n"
"有 bug 可以联系 @yym68686"
f"Hi `{user.username}` ! I am an Assistant, a large language model trained by OpenAI. I will do my best to help answer your questions.\n\n"
# "我是人见人爱的 ChatGPT~\n\n"
# "欢迎访问 https://github.com/yym68686/ChatGPT-Telegram-Bot 查看源码\n\n"
# "有 bug 可以联系 @yym68686"
)
await update.message.reply_html(rf"Hi {user.mention_html()} ! I am an Assistant, a large language model trained by OpenAI. I will do my best to help answer your questions.",)
await update.message.reply_text(escape(message), parse_mode='MarkdownV2', disable_web_page_preview=True)

async def error(update, context):
Expand Down Expand Up @@ -594,15 +566,6 @@ async def process_update(update):
self.end_headers()
return

# class handler(BaseHTTPRequestHandler):
# def do_POST(self):
# content_length = int(self.headers['Content-Length'])
# post_data = self.rfile.read(content_length).decode('utf-8')
# update = Update.de_json(json.loads(post_data), application.bot)
# application.process_update(update)
# self.send_response(200)
# self.end_headers()
# return
if __name__ == '__main__':
time_out = 600
application = (
Expand Down
51 changes: 35 additions & 16 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dotenv import load_dotenv
load_dotenv()

from utils.i18n import strings
from telegram import InlineKeyboardButton

WEB_HOOK = os.environ.get('WEB_HOOK', None)
Expand All @@ -18,7 +19,7 @@
# DEFAULT_SEARCH_MODEL = os.environ.get('DEFAULT_SEARCH_MODEL', 'gpt-3.5-turbo-1106') gpt-3.5-turbo-16k
API_URL = os.environ.get('API_URL', 'https://api.openai.com/v1/chat/completions')
# PDF_EMBEDDING = (os.environ.get('PDF_EMBEDDING', "True") == "False") == False
LANGUAGE = os.environ.get('LANGUAGE', 'Simplified Chinese')
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)
Expand All @@ -33,7 +34,7 @@
current_date = datetime.now()
Current_Date = current_date.strftime("%Y-%m-%d")
systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
claude_systemprompt = os.environ.get('SYSTEMPROMPT', prompt.claude_system_prompt)
claude_systemprompt = os.environ.get('SYSTEMPROMPT', prompt.claude_system_prompt.format(LANGUAGE))

from ModelMerge.models import chatgpt, claude, groq, claude3, gemini, dalle3
if API:
Expand Down Expand Up @@ -141,30 +142,48 @@ def create_buttons(strings):

initial_model.extend([model for model in CUSTOM_MODELS_LIST if model not in initial_model and model[0] != "-"])

buttons = create_buttons(initial_model)
buttons.append(
[
InlineKeyboardButton("返回上一级", callback_data="返回上一级"),
],
)
def update_model_buttons():
buttons = create_buttons(initial_model)
if LANGUAGE == "Simplified Chinese":
lang = "zh"
else:
lang = "en"
buttons.append(
[
InlineKeyboardButton(strings['button_back'][lang], callback_data="BACK"),
],
)
return buttons

def get_current_lang():
if LANGUAGE == "Simplified Chinese":
lang = "zh"
else:
lang = "en"
return lang

def update_first_buttons_message():
history = "✅" if PASS_HISTORY else "☑️"
language = "🇨🇳 中文" if LANGUAGE == "Simplified Chinese" else "🇺🇸 English"

if LANGUAGE == "Simplified Chinese":
lang = "zh"
else:
lang = "en"


first_buttons = [
[
InlineKeyboardButton("更换问答模型", callback_data="更换问答模型"),
InlineKeyboardButton(language, callback_data="language"),
InlineKeyboardButton(f"历史记录 {history}", callback_data="PASS_HISTORY"),
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"搜索 {get_plugins_status('SEARCH')}", callback_data='SEARCH'),
InlineKeyboardButton(f"当前时间 {get_plugins_status('DATE')}", callback_data='DATE'),
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"URL 总结 {get_plugins_status('URL')}", callback_data='URL'),
InlineKeyboardButton(f"版本信息 {get_plugins_status('VERSION')}", callback_data='VERSION'),
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'),
],
]
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.1
ModelMerge==0.3.2
oauth2client==3.0.0
python-telegram-bot[webhooks,rate-limiter]==21.0.1
File renamed without changes.
46 changes: 46 additions & 0 deletions utils/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
strings = {
"welcome": {
"zh": "欢迎",
"en": "Welcome",
},
"button_change_model": {
"zh": "切换模型",
"en": "Change model",
},
"button_search": {
"zh": "搜索",
"en": "Search",
},
"button_url": {
"zh": "网址总结",
"en": "URL summary",
},
"button_history": {
"zh": "历史记录",
"en": "Chat history",
},
"button_current_time": {
"zh": "当前时间",
"en": "Current time",
},
"button_version": {
"zh": "版本信息",
"en": "Version info",
},
"button_back": {
"zh": "⬅️ 返回",
"en": "⬅️ Back",
},
"button_language": {
"zh": "🇨🇳 中文",
"en": "🇺🇸 English",
},
"message_think": {
"zh": "`思考中💭`",
"en": "`thinking💭`",
},
"message_banner": {
"zh": "👇 下面可以随时更改默认模型:",
"en": "👇 Change model below:",
},
}

0 comments on commit 20b491f

Please sign in to comment.