MVP status. Fix context bug and add typing indicator.

This commit is contained in:
armatik 2023-07-31 23:12:18 +03:00
parent b5e08b19e8
commit 83e92c445f
2 changed files with 22 additions and 10 deletions

View File

@ -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):

View File

@ -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))