Skip to content

Commit

Permalink
chore: remove deprecated code
Browse files Browse the repository at this point in the history
Signed-off-by: Radek Ježek <pc.jezek@gmail.com>
  • Loading branch information
jezekra1 committed May 22, 2024
1 parent 99b77c0 commit 7a13d15
Show file tree
Hide file tree
Showing 35 changed files with 46 additions and 1,136 deletions.
2 changes: 1 addition & 1 deletion scripts/types_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def generate_models(schema_path: Path, output: Path, extra_template_data: Option
"deprecated",
"pydantic.field_validator",
"pydantic.computed_field",
"genai._utils.deprecated_schema_import._print_deprecation_warning",
"genai._utils.deprecation._print_deprecation_warning",
],
extra_template_data=extra_template_data,
enum_field_as_literal=LiteralType.One,
Expand Down
221 changes: 0 additions & 221 deletions scripts/types_generator/schema_aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -819,227 +819,6 @@ replace:

model_extensions:
# Override generated classes by adding custom properties
BaseMessage:
dynamic_fields:
- name: file_ids
deprecated: "'file_ids' property is deprecated, use 'files' instead!"
return_type: Optional[list[str]]
body: return [file.id for file in self.files] if self.files is not None else None

ModerationParameters:
custom_body: |
# TODO: remove in next major release
def model_post_init(self, __context: Any) -> None:
for name in ['stigma', 'implicit_hate']:
if hasattr(self, name):
_print_deprecation_warning(f'({type(self).__name__}): "{name}" has been deprecated, use \"social_bias\" instead.')
# TODO: remove in next major release
def remove_deprecated(self) -> None:
"""Remove attributes which are not supported anymore"""
for name in ["stigma", "implicit_hate"]:
if hasattr(self, name):
_print_deprecation_warning(
f'({type(self).__name__} class): "{name}" has been deprecated, use "social_bias" instead.',
)
delattr(self, name)
# TODO: remove in next major release
@field_validator("hap", mode="before")
@classmethod
def _validate_hap(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationParameters): passing boolean value to the 'hap' parameter is deprecated, use 'ModerationHAP' class instead.")
return ModerationHAP(input=ModerationHAPInput(enabled=value), output=ModerationHAPOutput(enabled=value))
else:
return value
# TODO: remove in next major release
@field_validator("social_bias", mode="before")
@classmethod
def _validate_social_bias(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationParameters): passing boolean value to the 'social_bias' parameter is deprecated, use 'ModerationSocialBias' class instead.")
return ModerationSocialBias(input=ModerationSocialBiasInput(enabled=value), output=ModerationSocialBiasOutput(enabled=value))
else:
return value
ModerationHAP:
custom_body: |
# TODO: remove in next major release
@field_validator("input", mode="before")
@classmethod
def _validate_input(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationHAP): passing boolean value to the 'input' parameter is deprecated, use 'ModerationHAPInput' class instead.")
return ModerationHAPInput(enabled=value)
else:
return value
# TODO: remove in next major release
@field_validator("output", mode="before")
@classmethod
def _validate_output(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationHAP): passing boolean value to the 'output' parameter is deprecated, use 'ModerationHAPOutput' class instead.")
return ModerationHAPOutput(enabled=value)
else:
return value
# TODO: remove in next major release
def model_post_init(self, __context: Any) -> None:
deprecated_attrs = {name: getattr(self, name) for name in ["threshold", "send_tokens"] if getattr(self, name, None) is not None}
if deprecated_attrs:
if self.input is None:
self.input = ModerationHAPInput(enabled=True)
if self.output is None:
self.output = ModerationHAPOutput(enabled=True)
for name, value in deprecated_attrs.items():
_print_deprecation_warning(
f"(ModerationHAP): '{name}' is deprecated! Use 'input' parameter (ModerationHAPInput) / 'output' parameter parameter (ModerationHAPOutput) instead.",
)
setattr(self.input, name, value)
setattr(self.output, name, value)
delattr(self, name)
ModerationStigma:
custom_body: |
# TODO: remove in next major release
@field_validator("input", mode="before")
@classmethod
def _validate_input(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationStigma): passing boolean value to the 'input' parameter is deprecated, use 'ModerationStigmaInput' class instead.")
return ModerationStigmaInput(enabled=value)
else:
return value
# TODO: remove in next major release
@field_validator("output", mode="before")
@classmethod
def _validate_output(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationStigma): passing boolean value to the 'output' parameter is deprecated, use 'ModerationStigmaOutput' class instead.")
return ModerationStigmaOutput(enabled=value)
else:
return value
# TODO: remove in next major release
def model_post_init(self, __context: Any) -> None:
deprecated_attrs = {name: getattr(self, name) for name in ["threshold", "send_tokens"] if getattr(self, name, None) is not None}
if deprecated_attrs:
if self.input is None:
self.input = ModerationStigmaInput(enabled=True)
if self.output is None:
self.output = ModerationStigmaOutput(enabled=True)
for name, value in deprecated_attrs.items():
_print_deprecation_warning(
f"(ModerationStigma): '{name}' is deprecated! Use 'input' parameter (ModerationStigmaInput) / 'output' parameter parameter (ModerationStigmaOutput) instead.",
)
setattr(self.input, name, value)
setattr(self.output, name, value)
delattr(self, name)
ModerationImplicitHate:
custom_body: |
# TODO: remove in next major release
@field_validator("input", mode="before")
@classmethod
def _validate_input(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationImplicitHate): passing boolean value to the 'input' parameter is deprecated, use 'ModerationImplicitHateInput' class instead.")
return ModerationImplicitHateInput(enabled=value)
else:
return value
# TODO: remove in next major release
@field_validator("output", mode="before")
@classmethod
def _validate_output(cls, value: Any):
if isinstance(value, bool):
_print_deprecation_warning(f"(ModerationImplicitHate): passing boolean value to the 'output' parameter is deprecated, use 'ModerationImplicitHateOutput' class instead.")
return ModerationImplicitHateOutput(enabled=value)
else:
return value
# TODO: remove in next major release
def model_post_init(self, __context: Any) -> None:
deprecated_attrs = {name: getattr(self, name) for name in ["threshold", "send_tokens"] if getattr(self, name, None) is not None}
if deprecated_attrs:
if self.input is None:
self.input = ModerationImplicitHateInput(enabled=True)
if self.output is None:
self.output = ModerationImplicitHateOutput(enabled=True)
for name, value in deprecated_attrs.items():
_print_deprecation_warning(
f"(ModerationImplicitHate): '{name}' is deprecated! Use 'input' parameter (ModerationImplicitHateInput) / 'output' parameter parameter (ModerationImplicitHateOutput) instead.",
)
setattr(self.input, name, value)
setattr(self.output, name, value)
delattr(self, name)
ModerationImplicitHateOutput:
fields_overrides:
send_tokens:
default: False
enabled:
default: False

ModerationImplicitHateInput:
fields_overrides:
send_tokens:
default: False
enabled:
default: False

ModerationStigmaInput:
fields_overrides:
send_tokens:
default: False
enabled:
default: False

ModerationStigmaOutput:
fields_overrides:
send_tokens:
default: False
enabled:
default: False

TextCreateResponseModeration:
dynamic_fields:
- name: stigma
deprecated: "'stigma' property is deprecated, use 'social_bias' instead!"
return_type: Optional[list[TextModeration]]
body: return None
- name: implicit_hate
deprecated: "'implicit_hate' property is deprecated, use 'social_bias' instead!"
return_type: Optional[list[TextModeration]]
body: return None

TextGenerationStreamCreateResponse:
dynamic_fields:
- name: moderation
deprecated: "'moderation' property is deprecated, use 'moderations' instead!"
return_type: Optional[TextCreateResponseModeration]
body: return self.moderations

TextGenerationResult:
dynamic_fields:
- name: moderation
deprecated: "'moderation' property is deprecated, use 'moderations' instead!"
return_type: Optional[TextCreateResponseModeration]
body: return self.moderations

TextChatStreamCreateResponse:
dynamic_fields:
- name: moderation
deprecated: "'moderation' property is deprecated, use 'moderations' instead!"
return_type: Optional[TextCreateResponseModeration]
body: return self.moderations

PromptModerationParameters:
custom_base_class: ModerationParameters
37 changes: 0 additions & 37 deletions src/genai/_utils/deprecated_schema_import.py

This file was deleted.

18 changes: 18 additions & 0 deletions src/genai/_utils/deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import warnings

_CACHED_WARNINGS: set[str] = set()


def _print_deprecation_warning(msg: str):
with warnings.catch_warnings():
warnings.simplefilter("always", DeprecationWarning)
warnings.warn(msg, category=DeprecationWarning, stacklevel=4) # the original import is 4 levels higher


def _log_deprecation_warning(key: str, msg: str):
if key in _CACHED_WARNINGS:
return

_print_deprecation_warning(msg)

_CACHED_WARNINGS.add(key)
2 changes: 1 addition & 1 deletion src/genai/_utils/service/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pydantic import BaseModel, ConfigDict

from genai._utils.api_client import ApiClient
from genai._utils.deprecated_schema_import import _print_deprecation_warning
from genai._utils.deprecation import _print_deprecation_warning
from genai._utils.general import to_model_instance
from genai._utils.http_client.httpx_client import AsyncHttpxClient, HttpxClient
from genai._utils.service.metadata import inherit_metadata
Expand Down
4 changes: 2 additions & 2 deletions src/genai/extensions/langchain/chat_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ def send_chunk(*, text: str = "", generation_info: dict):
run_manager.on_llm_new_token(token=text, chunk=chunk, response=response) # noqa: B023
# Function definition does not bind loop variable `response`: linter is probably just confused here

if response.moderation:
generation_info = create_generation_info_from_response(response, result=response.moderation)
if response.moderations:
generation_info = create_generation_info_from_response(response, result=response.moderations)
yield from send_chunk(generation_info=generation_info)

for result in response.results or []:
Expand Down
2 changes: 1 addition & 1 deletion src/genai/extensions/langchain/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def send_chunk(
for response in self.client.text.generation.create_stream(
**self._prepare_stream_request(input=prompt, stop=stop, **kwargs)
):
if response.moderation:
if response.moderations:
generation_info = create_generation_info_from_response(response, result=response.moderation)
yield from send_chunk(generation_info=generation_info, response=response)

Expand Down
4 changes: 2 additions & 2 deletions src/genai/extensions/llama_index/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def stream_chat(self, messages: Sequence[ChatMessage], formatted: bool = False,
for response in self.client.text.chat.create_stream(
**self._prepare_request(self._identifying_chat_params)(messages=to_genai_messages(messages), **kwargs)
):
if response.moderation:
generation_info = create_generation_info_from_response(response, result=response.moderation)
if response.moderations:
generation_info = create_generation_info_from_response(response, result=response.moderations)
message = ChatMessage(role=MessageRole.ASSISTANT, content=text)
yield ChatResponse(message=message, delta="", additional_kwargs=generation_info)

Expand Down
10 changes: 0 additions & 10 deletions src/genai/file/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
"""Modules containing functionalities related to static assets (user's files)"""

from genai.file.file_service import *


def __getattr__(name: str):
"""Allow additional imports for backward compatibility with old import system "from genai.service_name import"."""
if name in globals():
return globals()[name]

from genai._utils.deprecated_schema_import import _deprecated_schema_import

return _deprecated_schema_import(name, __name__)
10 changes: 0 additions & 10 deletions src/genai/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
"""Modules containing functionalities related to models."""

from genai.model.model_service import *


def __getattr__(name: str):
"""Allow additional imports for backward compatibility with old import system "from genai.service_name import"."""
if name in globals():
return globals()[name]

from genai._utils.deprecated_schema_import import _deprecated_schema_import

return _deprecated_schema_import(name, __name__)
10 changes: 0 additions & 10 deletions src/genai/prompt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
"""Modules containing functionalities related to prompts"""

from genai.prompt.prompt_service import *


def __getattr__(name: str):
"""Allow additional imports for backward compatibility with old import system "from genai.service_name import"."""
if name in globals():
return globals()[name]

from genai._utils.deprecated_schema_import import _deprecated_schema_import

return _deprecated_schema_import(name, __name__)
10 changes: 0 additions & 10 deletions src/genai/request/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
"""Modules containing functionalities related to text requests (their history)"""

from genai.request.request_service import *


def __getattr__(name: str):
"""Allow additional imports for backward compatibility with old import system "from genai.service_name import"."""
if name in globals():
return globals()[name]

from genai._utils.deprecated_schema_import import _deprecated_schema_import

return _deprecated_schema_import(name, __name__)
Loading

0 comments on commit 7a13d15

Please sign in to comment.