Compare commits

..

No commits in common. "3db3f13cdae33e53e050231d8af2b265a88efb74" and "7fc6373fcaf424921f5755c2fe9a58dc5737cac5" have entirely different histories.

3 changed files with 16 additions and 30 deletions

View file

@ -5,7 +5,7 @@ from openrouter import OpenRouter
SYSTEM_PROMPT = """
Ты - помощник в групповом чате.
Ты - помощник в групповом чате Telegram.
Отвечай на вопросы и поддерживай контекст беседы.
Ты не можешь обсуждать политику и религию.
Сообщения пользователей будут приходить в следующем формате: '[Имя]: текст сообщения'
@ -13,6 +13,7 @@ SYSTEM_PROMPT = """
"""
@dataclass
class ChatContext:
def __init__(self, max_messages: int):
self.max_messages: int = max_messages
@ -27,9 +28,6 @@ class ChatContext:
def get_messages_for_api(self) -> List[Dict[str, str]]:
return self.messages
def remove_last_message(self):
self.messages.pop()
class AiAgent:
def __init__(self, api_token: str):
@ -39,12 +37,13 @@ class AiAgent:
async def get_reply(self, chat_id: int, chat_prompt: str, user_name: str, message: str) -> str:
context = self._get_chat_context(chat_id, chat_prompt)
context.add_message(role="user", content=f"[{user_name}]: {message}")
messages_for_api = context.get_messages_for_api()
try:
# Get response from OpenRouter
response = await self.client.chat.send_async(
model="meta-llama/llama-3.3-70b-instruct:free",
messages=context.get_messages_for_api(),
messages=messages_for_api,
max_tokens=500,
temperature=0.7
)
@ -58,9 +57,8 @@ class AiAgent:
return ai_response
except Exception as e:
context.remove_last_message()
print(f"Ошибка выполнения запроса к ИИ: {e}")
return f"Извините, при обработке запроса произошла ошибка:\n{e}"
print(f"Error processing message: {e}")
return "Извините, при обработке запроса произошла ошибка."
def clear_chat_context(self, chat_id: int):
self.chat_contexts.pop(chat_id, None)

View file

@ -54,16 +54,12 @@ async def any_message_handler(message: Message):
if bot_user is None:
bot_user = await message.bot.get_me()
# Ответ на сообщение бота
if message.reply_to_message and message.reply_to_message.from_user.id == bot_user.id:
if message.content_type == ContentType.TEXT and message.text.find('@' + bot_user.username) != -1:
message_text = message.text.replace('@' + bot_user.username, bot_user.first_name)
elif message.reply_to_message and message.reply_to_message.from_user.id == bot_user.id:
message_text = message.text
else:
# Сообщение содержит @bot_username
bot_username_mention = '@' + bot_user.username
if message.content_type == ContentType.TEXT and message.text.find(bot_username_mention) != -1:
message_text = message.text.replace(bot_username_mention, bot_user.first_name)
else:
return
return
if message.from_user.first_name and message.from_user.last_name:
user_name = "{} {}".format(message.from_user.first_name, message.from_user.last_name)

View file

@ -1,4 +1,3 @@
import re
from typing import Optional
from vkbottle.bot import Message
@ -40,21 +39,14 @@ async def any_message_handler(message: Message):
if bot_user is None:
bot_user = (await message.ctx_api.groups.get_by_id()).groups[0]
# Ответ на сообщение бота
if message.reply_message and message.reply_message.from_id == -bot_user.id:
if message.text is not None and message.text.find('@' + bot_user.screen_name) != -1:
message_text = message.text.replace('@' + bot_user.screen_name, bot_user.name)
elif message.text is not None and message.text.find('club' + str(bot_user.id)) != -1:
message_text = message.text.replace('club' + str(bot_user.id), bot_user.name)
elif message.reply_message and message.reply_message.from_id == -bot_user.id:
message_text = message.text
else:
# Сообщение содержит @bot_username
bot_username_mention = '@' + bot_user.screen_name
if message.text is not None and message.text.find(bot_username_mention) != -1:
message_text = message.text.replace(bot_username_mention, bot_user.name)
else:
# Сообщение содержит [club<bot_id>|<some_name>]
pattern = r"\[club" + str(bot_user.id) + r"\|(.+)]"
if message.text is not None and re.search(pattern, message.text) is not None:
message_text = re.sub(pattern, r'\1', message.text)
else:
return
return
user = await message.ctx_api.users.get(user_ids=[message.from_id])
if len(user) == 1: