Skip to content

Commit

Permalink
🐛 Bug: 1. Fix the bug where multiple user messages are not merged in …
Browse files Browse the repository at this point in the history
…Claude

2. Fix the bug where Claude's assistant messages are not entering the conversation history

💻 Code: 1. Upgrade ModelMerge version to 0.7.3

2. Simplify the code for the translation command.
  • Loading branch information
yym68686 committed Jun 20, 2024
1 parent af02ef9 commit 56f63c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
65 changes: 33 additions & 32 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def GetMesageInfo(update, context):

@decorators.GroupAuthorization
@decorators.Authorization
async def command_bot(update, context, language=None, prompt=translator_prompt, title="", robot=None, has_command=True):
async def command_bot(update, context, language=None, prompt=translator_prompt, title="", has_command=True):
stop_event.clear()
print("update", update)
message, rawtext, image_url, chatid, messageid, reply_to_message_text, update_message, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
Expand All @@ -162,21 +162,22 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
if has_command == False or len(context.args) > 0:
if has_command:
message = ' '.join(context.args)
pass_history = Users.get_config(convo_id, "PASS_HISTORY")
if prompt and has_command:
if translator_prompt == prompt:
if language == "english":
prompt = prompt.format(language)
else:
prompt = translator_en2zh_prompt
pass_history = False
message = prompt + message
if message:
if reply_to_message_text and update_message.reply_to_message.from_user.is_bot:
message = '\n'.join(reply_to_message_text.split('\n')[1:]) + "\n" + message
elif reply_to_message_text and not update_message.reply_to_message.from_user.is_bot:
message = reply_to_message_text + "\n" + message

if robot is None:
robot, role = get_robot(convo_id)
robot, role = get_robot(convo_id)
engine = get_ENGINE(convo_id)

if Users.get_config(convo_id, "LONG_TEXT"):
Expand Down Expand Up @@ -215,7 +216,8 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
await context.bot.send_chat_action(chat_id=chatid, message_thread_id=message_thread_id, action=ChatAction.TYPING)
if Users.get_config(convo_id, "TITLE"):
title = f"`🤖️ {engine}`\n\n"
await getChatGPT(update, context, title, robot, message, chatid, messageid, convo_id, message_thread_id)

await getChatGPT(update, context, title, robot, message, chatid, messageid, convo_id, message_thread_id, pass_history)
else:
message = await context.bot.send_message(
chat_id=chatid,
Expand All @@ -235,31 +237,7 @@ async def delete_message(update, context, messageid, delay=60):
print("error", e)
print('\033[0m')

# 定义一个全局变量来存储 chatid
target_convo_id = None

@decorators.GroupAuthorization
@decorators.Authorization
async def reset_chat(update, context):
global target_convo_id
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
target_convo_id = convo_id
stop_event.set()
message = None
if (len(context.args) > 0):
message = ' '.join(context.args)
reset_ENGINE(target_convo_id, message)

remove_keyboard = ReplyKeyboardRemove()
message = await context.bot.send_message(
chat_id=chatid,
message_thread_id=message_thread_id,
text="重置成功!",
reply_markup=remove_keyboard,
)
await delete_message(update, context, message.message_id)

async def getChatGPT(update, context, title, robot, message, chatid, messageid, convo_id, message_thread_id):
async def getChatGPT(update, context, title, robot, message, chatid, messageid, convo_id, message_thread_id, pass_history=False):
result = ""
text = message
modifytime = 0
Expand All @@ -283,7 +261,6 @@ async def getChatGPT(update, context, title, robot, message, chatid, messageid,
reply_to_message_id=messageid,
)
answer_messageid = message.message_id
pass_history = Users.get_config(convo_id, "PASS_HISTORY")
image_has_send = 0

try:
Expand Down Expand Up @@ -525,6 +502,30 @@ async def inlinequery(update: Update, context) -> None:

await update.inline_query.answer(results)

# 定义一个全局变量来存储 chatid
target_convo_id = None

@decorators.GroupAuthorization
@decorators.Authorization
async def reset_chat(update, context):
global target_convo_id
_, _, _, chatid, _, _, _, message_thread_id, convo_id, file_url = await GetMesageInfo(update, context)
target_convo_id = convo_id
stop_event.set()
message = None
if (len(context.args) > 0):
message = ' '.join(context.args)
reset_ENGINE(target_convo_id, message)

remove_keyboard = ReplyKeyboardRemove()
message = await context.bot.send_message(
chat_id=chatid,
message_thread_id=message_thread_id,
text="重置成功!",
reply_markup=remove_keyboard,
)
await delete_message(update, context, message.message_id)

async def start(update, context): # 当用户输入/start时,返回文本
user = update.effective_user
message = (
Expand Down Expand Up @@ -601,8 +602,8 @@ async def process_update(update):
# application.add_handler(ChosenInlineResultHandler(chosen_inline_result))
# application.add_handler(MessageHandler(filters.TEXT & filters.VIA_BOT, handle_message))
application.add_handler(CommandHandler("reset", reset_chat))
application.add_handler(CommandHandler("en2zh", lambda update, context: command_bot(update, context, "Simplified Chinese", robot=config.translate_bot)))
application.add_handler(CommandHandler("zh2en", lambda update, context: command_bot(update, context, "english", robot=config.translate_bot)))
application.add_handler(CommandHandler("en2zh", lambda update, context: command_bot(update, context, "Simplified Chinese")))
application.add_handler(CommandHandler("zh2en", lambda update, context: command_bot(update, context, "english")))
application.add_handler(CommandHandler("info", info))
application.add_handler(InlineQueryHandler(inlinequery))
# application.add_handler(MessageHandler(~filters.CAPTION & , handle_file))
Expand Down
7 changes: 3 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def get_ENGINE(user_id = None):
temperature = float(os.environ.get('temperature', '0.5'))
CLAUDE_API = os.environ.get('claude_api_key', None)

ChatGPTbot, SummaryBot, translate_bot, claudeBot, claude3Bot, groqBot, gemini_Bot = None, None, None, None, None, None, None
ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot = None, None, None, None, None, None
def update_ENGINE(data = None, chat_id=None):
global Users, ChatGPTbot, SummaryBot, translate_bot, claudeBot, claude3Bot, groqBot, gemini_Bot
global Users, ChatGPTbot, SummaryBot, claudeBot, claude3Bot, groqBot, gemini_Bot
if data:
Users.set_config(chat_id, "engine", data)
engine = Users.get_config(chat_id, "engine")
Expand All @@ -142,7 +142,6 @@ def update_ENGINE(data = None, chat_id=None):
if API:
ChatGPTbot = chatgpt(api_key=f"{API}", engine=engine, system_prompt=systemprompt, temperature=temperature)
SummaryBot = chatgpt(api_key=f"{API}", engine="gpt-3.5-turbo", system_prompt=systemprompt, temperature=temperature)
translate_bot = chatgpt(api_key=f"{API}", engine=engine, system_prompt=systemprompt, temperature=temperature)
if CLAUDE_API and "claude-2.1" in engine:
claudeBot = claude(api_key=f"{CLAUDE_API}", engine=engine, system_prompt=claude_systemprompt, temperature=temperature)
if CLAUDE_API and "claude-3" in engine:
Expand Down Expand Up @@ -197,7 +196,7 @@ def update_info_message(user_id = None):
])

def reset_ENGINE(chat_id, message=None):
global ChatGPTbot, translate_bot, claudeBot, claude3Bot, groqBot, gemini_Bot
global ChatGPTbot, claudeBot, claude3Bot, groqBot, gemini_Bot
systemprompt = Users.get_config(chat_id, "systemprompt")
claude_systemprompt = Users.get_config(chat_id, "claude_systemprompt")
if message:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--index-url https://pypi.python.org/simple/
python-dotenv
md2tgmd==0.2.3
ModelMerge==0.7.2
ModelMerge==0.7.3
python-telegram-bot[webhooks,rate-limiter]==21.0.1

0 comments on commit 56f63c7

Please sign in to comment.