Dmitry Afanasyev e9f76d0ea9
update chat service (#31)
* rename chatgpt service

* add zeus tool for new provider

* add zeus tool for new provider

* update chat service

* update README.md
2023-10-10 23:22:41 +03:00

40 lines
1.1 KiB
Python

from datetime import datetime, timedelta
from functools import cache, wraps
from inspect import cleandoc
from typing import Any, Callable
def timed_lru_cache(
microseconds: int = 0,
milliseconds: int = 0,
seconds: int = 0,
minutes: int = 0,
hours: int = 0,
) -> Any:
def _wrapper(func: Any) -> Callable[[Any], Any]:
update_delta = timedelta(
microseconds=microseconds, milliseconds=milliseconds, seconds=seconds, minutes=minutes, hours=hours
)
next_update = datetime.utcnow() + update_delta
cached_func = cache(func)
@wraps(func)
def _wrapped(*args: Any, **kwargs: Any) -> Callable[[Any], Any]:
nonlocal next_update
now = datetime.utcnow()
if now >= next_update:
cached_func.cache_clear()
next_update = now + update_delta
return cached_func(*args, **kwargs)
return _wrapped
return _wrapper
def clean_doc(cls: Any) -> str | None:
if cls.__doc__ is None:
return None
return cleandoc(cls.__doc__)