Compare commits
No commits in common. "50c984cfd72979926bd11601523f60432fa9e746" and "24ce709fb147c7d280ffee588e0e7e222faa6d95" have entirely different histories.
50c984cfd7
...
24ce709fb1
4 changed files with 12 additions and 48 deletions
10
ai_agent.py
10
ai_agent.py
|
|
@ -1,8 +1,8 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List, Dict, Optional
|
from typing import List, Dict, Optional
|
||||||
|
|
||||||
from openrouter import OpenRouter, RetryConfig
|
from openrouter import OpenRouter
|
||||||
from openrouter.utils import BackoffStrategy
|
|
||||||
|
|
||||||
SYSTEM_PROMPT = """
|
SYSTEM_PROMPT = """
|
||||||
Ты - помощник в групповом чате.
|
Ты - помощник в групповом чате.
|
||||||
|
|
@ -39,11 +39,7 @@ class AiMessage:
|
||||||
|
|
||||||
class AiAgent:
|
class AiAgent:
|
||||||
def __init__(self, api_token: str):
|
def __init__(self, api_token: str):
|
||||||
retry_config = RetryConfig(strategy="backoff",
|
self.client = OpenRouter(api_key=api_token, timeout_ms=15000)
|
||||||
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] = {}
|
self.chat_contexts: Dict[int, ChatContext] = {}
|
||||||
|
|
||||||
async def get_reply(self, chat_id: int, chat_prompt: str,
|
async def get_reply(self, chat_id: int, chat_prompt: str,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
from functools import partial
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from aiogram import Router, F, Bot
|
from aiogram import Router, F, Bot
|
||||||
|
|
@ -93,10 +92,8 @@ async def any_message_handler(message: Message):
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
agent = AiAgent(message.bot.config['openrouter_token'])
|
agent = AiAgent(message.bot.config['openrouter_token'])
|
||||||
|
|
||||||
await message.reply(
|
await message.bot.send_chat_action(chat_id, 'typing')
|
||||||
await utils.run_with_progress(partial(agent.get_reply, chat_id, chat_prompt, ai_message, ai_fwd_messages),
|
await message.reply(await agent.get_reply(chat_id, chat_prompt, ai_message, ai_fwd_messages))
|
||||||
partial(message.bot.send_chat_action, chat_id, 'typing'),
|
|
||||||
interval=4))
|
|
||||||
|
|
||||||
|
|
||||||
def clear_ai_chat_context(chat_id: int):
|
def clear_ai_chat_context(chat_id: int):
|
||||||
|
|
@ -117,6 +114,5 @@ async def get_ai_reply(bot: Bot, chat_id, user: User, message: str, fwd_user: Us
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
agent = AiAgent(bot.config['openrouter_token'])
|
agent = AiAgent(bot.config['openrouter_token'])
|
||||||
|
|
||||||
return await utils.run_with_progress(partial(agent.get_reply, chat_id, chat_prompt, ai_message, ai_fwd_messages),
|
await bot.send_chat_action(chat_id, 'typing')
|
||||||
partial(bot.send_chat_action(chat_id, 'typing')),
|
return await agent.get_reply(chat_id, chat_prompt, ai_message, ai_fwd_messages)
|
||||||
interval=4)
|
|
||||||
|
|
|
||||||
26
utils.py
26
utils.py
|
|
@ -1,6 +1,5 @@
|
||||||
import asyncio
|
|
||||||
from calendar import timegm
|
from calendar import timegm
|
||||||
from typing import Awaitable, Callable, Coroutine, Optional
|
from typing import Optional
|
||||||
|
|
||||||
from pymorphy3 import MorphAnalyzer
|
from pymorphy3 import MorphAnalyzer
|
||||||
from time import gmtime
|
from time import gmtime
|
||||||
|
|
@ -22,26 +21,3 @@ def full_name(first_name: str, last_name: Optional[str]) -> str:
|
||||||
if last_name is not None:
|
if last_name is not None:
|
||||||
return f"{first_name} {last_name}"
|
return f"{first_name} {last_name}"
|
||||||
return first_name
|
return first_name
|
||||||
|
|
||||||
|
|
||||||
async def run_with_progress(main_func: Callable[[], Coroutine], progress_func: Callable[[], Awaitable], interval: int):
|
|
||||||
completion_event = asyncio.Event()
|
|
||||||
|
|
||||||
async def progress():
|
|
||||||
while not completion_event.is_set():
|
|
||||||
await progress_func()
|
|
||||||
wait_event_task = asyncio.create_task(completion_event.wait())
|
|
||||||
wait_timer_task = asyncio.create_task(asyncio.sleep(interval))
|
|
||||||
await asyncio.wait([wait_event_task, wait_timer_task],
|
|
||||||
return_when=asyncio.FIRST_COMPLETED)
|
|
||||||
if completion_event.is_set():
|
|
||||||
wait_timer_task.cancel()
|
|
||||||
|
|
||||||
progress_task = asyncio.create_task(progress())
|
|
||||||
main_task = asyncio.create_task(main_func())
|
|
||||||
|
|
||||||
result = await main_task
|
|
||||||
completion_event.set()
|
|
||||||
await progress_task
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import re
|
import re
|
||||||
from functools import partial
|
|
||||||
from typing import Optional, Tuple, List
|
from typing import Optional, Tuple, List
|
||||||
|
|
||||||
from vkbottle import API
|
from vkbottle import API
|
||||||
|
|
@ -88,10 +87,8 @@ async def any_message_handler(message: Message):
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
agent = AiAgent(message.ctx_api.config['openrouter_token'])
|
agent = AiAgent(message.ctx_api.config['openrouter_token'])
|
||||||
|
|
||||||
await message.reply(
|
await message.ctx_api.messages.set_activity(peer_id=chat_id, type='typing')
|
||||||
await utils.run_with_progress(partial(agent.get_reply, chat_id, chat_prompt, ai_message, ai_fwd_messages),
|
await message.reply(await agent.get_reply(chat_id, chat_prompt, ai_message, ai_fwd_messages))
|
||||||
partial(message.ctx_api.messages.set_activity, peer_id=chat_id, type='typing'),
|
|
||||||
interval=4))
|
|
||||||
|
|
||||||
|
|
||||||
def clear_ai_chat_context(chat_id: int):
|
def clear_ai_chat_context(chat_id: int):
|
||||||
|
|
@ -115,6 +112,5 @@ async def get_ai_reply(api: API, chat_id, message: Tuple[int, str], fwd_messages
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
agent = AiAgent(api.config['openrouter_token'])
|
agent = AiAgent(api.config['openrouter_token'])
|
||||||
|
|
||||||
return await utils.run_with_progress(partial(agent.get_reply, chat_id, chat_prompt, ai_message, ai_fwd_messages),
|
await api.messages.set_activity(peer_id=chat_id, type='typing')
|
||||||
partial(api.messages.set_activity, peer_id=chat_id, type='typing'),
|
return await agent.get_reply(chat_id, chat_prompt, ai_message, ai_fwd_messages)
|
||||||
interval=4)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue