Skip to content

Commit

Permalink
add deprecation decorator to create_chat_openai_model
Browse files Browse the repository at this point in the history
  • Loading branch information
hmasdev committed Sep 7, 2024
1 parent 8cab405 commit 463685c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions werewolf/utils/openai.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from functools import lru_cache
from langchain_openai import ChatOpenAI
from ..const import DEFAULT_MODEL
from ..utils.utils import deprecate


@deprecate(msg='Use werewolf.chat_models.create_chat_model instead.')
@lru_cache(maxsize=None)
def create_chat_openai_model(
llm: ChatOpenAI | str | None = None,
Expand Down
35 changes: 35 additions & 0 deletions werewolf/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from contextlib import contextmanager
from functools import partial
from logging import Logger, getLogger
from typing import Callable, Generator, Iterable, TypeVar

InputType = TypeVar('InputType')
Expand Down Expand Up @@ -47,3 +49,36 @@ def consecutive_string_generator(
while True:
yield f'{prefix}{idx}'
idx += step


def deprecate(
func: Callable[[InputType], OutputType] | None = None,
*,
msg: str = '',
logger: Logger = getLogger(__name__),
) -> (
Callable[[InputType], OutputType]
| Callable[[Callable[[InputType], OutputType]], Callable[[InputType], OutputType]]
):
"""Decorator to deprecate a function.
Args:
func (Callable[[InputType], OutputType] | None, optional): function to deprecate. Defaults to None.
msg (str, optional): message to show. Defaults to ''.
logger (Logger, optional): logger. Defaults to getLogger(__name__).
Returns:
Callable[[InputType], OutputType] | Callable[[Callable[[InputType], OutputType]], Callable[[InputType], OutputType]]: decorated function or decorator
""" # noqa

if func is None:
return partial(deprecate, msg=msg, logger=logger)

def wrapper(*args, **kwargs):
if hasattr(func, '__name__'):
logger.warning(' '.join([f'{func.__name__} is planned to be deprecated.', msg])) # noqa
else:
logger.warning(' '.join([f'{func} is planned to be deprecated.', msg])) # noqa
return func(*args, **kwargs)

return wrapper

0 comments on commit 463685c

Please sign in to comment.