From 83e92c445f647d65dd9b72cfa2f0991cbd19141b Mon Sep 17 00:00:00 2001 From: armatik Date: Mon, 31 Jul 2023 23:12:18 +0300 Subject: [PATCH] MVP status. Fix context bug and add typing indicator. --- src/OpenAI/GPT35turbo/OA_processing.py | 9 ++++++--- src/TelegramBot/main.py | 23 ++++++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/OpenAI/GPT35turbo/OA_processing.py b/src/OpenAI/GPT35turbo/OA_processing.py index f54013b..5d818dd 100644 --- a/src/OpenAI/GPT35turbo/OA_processing.py +++ b/src/OpenAI/GPT35turbo/OA_processing.py @@ -13,6 +13,7 @@ database = sqlite3.connect(os.path.join(mother_path, 'DataBase/OCAB_DB.db')) cursor = database.cursor() reply_ignore = config['Telegram']['reply_ignore'].split('| ') reply_ignore = list(map(int, reply_ignore)) +#print(reply_ignore) min_token_for_answer = int(config['Openai']['min_token_for_answer']) @@ -33,7 +34,7 @@ def openai_response(message_formated_text): #print(message_formated_text) count_length = 0 for message in message_formated_text: - print(message["content"]) + #print(message["content"]) count_length += len(message["content"]) #print(count_length) try: @@ -77,8 +78,10 @@ def sort_message_from_user(message_formated_text, message_id): count_length = 0 for message in message_formated_text: count_length += len(message['content']) - if count_length > max_token_count-min_token_for_answer: - message_formated_text.pop(1) + while count_length > max_token_count-min_token_for_answer: + message_formated_text.pop(1) + for message in message_formated_text: + count_length += len(message['content']) return message_formated_text def openai_collecting_message(message_id, message_formated_text): diff --git a/src/TelegramBot/main.py b/src/TelegramBot/main.py index 84c3866..0b690b3 100644 --- a/src/TelegramBot/main.py +++ b/src/TelegramBot/main.py @@ -103,32 +103,41 @@ async def in_message(message: types.Message): database.commit() # Обработка сообщения OpenAI send_answer = False + typing_mode = False # импортируем массив триггеров из файла .ini if message.reply_to_message and message.reply_to_message.from_user.id == (await bot.me).id: send_answer = True + typing_mode = True for trigger in bot_trigger_all: if trigger.lower() in message.text.lower(): send_answer = True + typing_mode = False for trigger in bot_trigger_front: if message.text.lower().startswith(trigger.lower()): send_answer = True + typing_mode = False if send_answer: - your_id = message.from_id - your_name = message.from_user.username - temp_msg = await message.reply( - f"[{your_name}](tg://user?id={str(your_id)}), Подожди немного и я обязательно отвечу тебе!", - parse_mode="Markdown") + if typing_mode is False: + your_id = message.from_id + your_name = message.from_user.username + temp_msg = await message.reply( + f"[{your_name}](tg://user?id={str(your_id)}), Подожди немного и я обязательно отвечу тебе!", + parse_mode="Markdown") + # Пишем что бот печатает + await bot.send_chat_action(message.chat.id, "typing") response = openai_message_processing(message.message_id) if response is None: bot_message_id = await message.reply("Я не понял тебя, попробуй перефразировать") - asyncio.create_task(delete_message(temp_msg, 0)) + if typing_mode is False: + asyncio.create_task(delete_message(temp_msg, 0)) # заносим сообщение в базу данных в качестве message_id пишем id сообщения которое отправил бот cursor.execute("INSERT INTO message_list VALUES (?, ?, ?, ?)", (bot_message_id, "Я не понял тебя, попробуй перефразировать", 0, message.message_id)) else: bot_message_id = await message.reply(response['choices'][0]['message']['content'], parse_mode="markdown") - asyncio.create_task(delete_message(temp_msg, 0)) + if typing_mode is False: + asyncio.create_task(delete_message(temp_msg, 0)) # заносим сообщение в базу данных в качестве message_id мы пишем id сообщения в bot_message_id cursor.execute("INSERT INTO message_list VALUES (?, ?, ?, ?)", (bot_message_id.message_id, response['choices'][0]['message']['content'], 0, message.message_id))