From 4f35663784f5eee21080fc168a3a66246d0b3abc Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Mon, 9 Feb 2026 20:48:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87=D0=B0?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=B8=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=D1=87=D0=B0=D1=82=D0=B0=20=D0=B2=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=81=D0=B0=D1=85=20=D0=BA=20OpenRouter.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai_agent.py | 19 ++++++++++++++----- tg/__main__.py | 4 +--- vk/__main__.py | 4 +--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ai_agent.py b/ai_agent.py index a16b59e..ab254d7 100644 --- a/ai_agent.py +++ b/ai_agent.py @@ -21,6 +21,10 @@ PRIVATE_CHAT_SYSTEM_PROMPT = """ """ PRIVATE_CHAT_MAX_MESSAGES = 40 +OPENROUTER_HEADERS = { + 'HTTP-Referer': 'https://ultracoder.org', + 'X-Title': 'TG/VK Chat Bot' +} @dataclass() class Message: @@ -30,13 +34,14 @@ class Message: class AiAgent: - def __init__(self, api_token: str, model: str, db: BasicDatabase): + def __init__(self, api_token: str, model: str, db: BasicDatabase, platform: str): retry_config = RetryConfig(strategy="backoff", backoff=BackoffStrategy( initial_interval=2000, max_interval=8000, exponent=2, max_elapsed_time=14000), retry_connection_errors=True) self.db = db self.model = model + self.platform = platform self.client = OpenRouter(api_key=api_token, retry_config=retry_config) async def get_group_chat_reply(self, bot_id: int, chat_id: int, @@ -55,7 +60,9 @@ class AiAgent: response = await self.client.chat.send_async( model=self.model, messages=context, - max_tokens=500 + max_tokens=500, + user=f'{self.platform}_{bot_id}_{chat_id}', + http_headers=OPENROUTER_HEADERS ) # Extract AI response @@ -85,7 +92,9 @@ class AiAgent: response = await self.client.chat.send_async( model=self.model, messages=context, - max_tokens=500 + max_tokens=500, + user=f'{self.platform}_{bot_id}_{chat_id}', + http_headers=OPENROUTER_HEADERS ) # Extract AI response @@ -133,6 +142,6 @@ class AiAgent: agent: AiAgent -def create_ai_agent(api_token: str, model: str, db: BasicDatabase): +def create_ai_agent(api_token: str, model: str, db: BasicDatabase, platform: str): global agent - agent = AiAgent(api_token, model, db) + agent = AiAgent(api_token, model, db, platform) diff --git a/tg/__main__.py b/tg/__main__.py index 6edd133..782dd2e 100644 --- a/tg/__main__.py +++ b/tg/__main__.py @@ -24,9 +24,7 @@ async def main() -> None: database.create_database(config['db_connection_string']) - create_ai_agent(config['openrouter_token'], - config['openrouter_model'], - database.DB) + create_ai_agent(config['openrouter_token'], config['openrouter_model'], database.DB, 'tg') bots: list[Bot] = [] for item in database.DB.get_bots(): diff --git a/vk/__main__.py b/vk/__main__.py index fb31f06..74c58b4 100644 --- a/vk/__main__.py +++ b/vk/__main__.py @@ -24,9 +24,7 @@ if __name__ == '__main__': database.create_database(config['db_connection_string']) - create_ai_agent(config['openrouter_token'], - config['openrouter_model'], - database.DB) + create_ai_agent(config['openrouter_token'], config['openrouter_model'], database.DB, 'vk') bot = Bot(labeler=handlers.labeler)