diff --git a/ai_agent.py b/ai_agent.py index 242461f..e5e0f95 100644 --- a/ai_agent.py +++ b/ai_agent.py @@ -1,8 +1,8 @@ from dataclasses import dataclass from typing import List, Dict, Optional -from openrouter import OpenRouter - +from openrouter import OpenRouter, RetryConfig +from openrouter.utils import BackoffStrategy SYSTEM_PROMPT = """ Ты - помощник в групповом чате. @@ -39,7 +39,11 @@ class AiMessage: class AiAgent: def __init__(self, api_token: str): - self.client = OpenRouter(api_key=api_token, timeout_ms=15000) + retry_config = RetryConfig(strategy="backoff", + backoff=BackoffStrategy( + initial_interval=2000, max_interval=8000, exponent=2, max_elapsed_time=14000), + retry_connection_errors=True) + self.client = OpenRouter(api_key=api_token, retry_config=retry_config) self.chat_contexts: Dict[int, ChatContext] = {} async def get_reply(self, chat_id: int, chat_prompt: str,