Skip to content

Commit

Permalink
refactor: clean up deprecated warnings and improve code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ohdearquant committed Dec 14, 2024
1 parent 2f3e939 commit a2f7b75
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 84 deletions.
1 change: 0 additions & 1 deletion lion_service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from .rate_limiter import RateLimiter, RateLimitError
from .service import Service, register_service
from .service_util import invoke_retry
from .version import __version__

__all__ = [
"Service",
Expand Down
4 changes: 3 additions & 1 deletion lion_service/complete_request_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class CompleteRequestInfo(BaseModel):


class CompleteRequestTokenInfo(CompleteRequestInfo):
token_usage: int = Field(description="Number of tokens used in the request")
token_usage: int = Field(
description="Number of tokens used in the request"
)
72 changes: 2 additions & 70 deletions lion_service/imodel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings

from lion_service.service import Service
from lion_service.service_match_util import (
from .service import Service
from .service_match_util import (
match_parameters,
match_service,
match_task_method,
Expand All @@ -25,74 +25,6 @@ def __init__(
elif api_key_schema is not None:
api_key = api_key_schema

# Deprecation
if "config" in kwargs:
warnings.warn(
"'config' is deprecated in 'iModel'. Please pass configurations directly as keyword arguments. "
"If 'config' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "provider_schema" in kwargs:
warnings.warn(
"'provider_schema' is deprecated in 'iModel'. Please refer to the corresponding service's "
"Pydantic models for the schema structure. "
"If 'provider_schema' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "endpoint" in kwargs:
warnings.warn(
"'endpoint' is deprecated in 'iModel'. Please specify 'task' instead. "
"If 'endpoint' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
task = kwargs["endpoint"]
if "token_encoding_name" in kwargs:
warnings.warn(
"'token_encoding_name' is deprecated in 'iModel' as it is now automatically detected. "
"To explicitly set the encoding method, please refer to TokenCalculator.",
DeprecationWarning,
stacklevel=2,
)
if "interval" in kwargs:
warnings.warn(
"'interval' is deprecated in 'iModel' as the unit is now set per minute. "
"If 'interval' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "service" in kwargs:
warnings.warn(
"'service' parameter is deprecated in 'iModel'. Please use 'provider' instead. For more "
"details about service configurations, refer to 'lion_service' or the corresponding service's package. "
"If 'service' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "allowed_parameters" in kwargs:
warnings.warn(
"'allowed_parameters' is deprecated in 'iModel'. If 'allowed_parameters' is a parameter "
"name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "device" in kwargs:
warnings.warn(
"'device' is deprecated in 'iModel'. "
"If 'device' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)
if "costs" in kwargs:
warnings.warn(
"'cost' is deprecated in 'iModel'. "
"If 'costs' is a parameter name for further request settings, it will still be passed along.",
DeprecationWarning,
stacklevel=2,
)

if isinstance(provider, str):
self.service = match_service(provider, api_key=api_key, **kwargs)
elif isinstance(provider, Service):
Expand Down
21 changes: 16 additions & 5 deletions lion_service/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

from pydantic import BaseModel, Field

from .complete_request_info import CompleteRequestInfo, CompleteRequestTokenInfo
from .complete_request_info import (
CompleteRequestInfo,
CompleteRequestTokenInfo,
)


class RateLimiter(BaseModel):
Expand Down Expand Up @@ -48,7 +51,8 @@ def release_tokens(self):
self.last_check_timestamp = datetime.now(UTC).timestamp()
while self.unreleased_requests:
if (
datetime.now(UTC).timestamp() - self.unreleased_requests[0].timestamp
datetime.now(UTC).timestamp()
- self.unreleased_requests[0].timestamp
> 60
):
release_info = self.unreleased_requests.popleft()
Expand All @@ -62,7 +66,9 @@ def release_tokens(self):
else:
break

def update_rate_limit(self, request_datetime_header, total_token_usage: int = None):
def update_rate_limit(
self, request_datetime_header, total_token_usage: int = None
):
# rate limiter tokens only tracks if there are token usage info
# otherwise, tracks requests num
date_format = "%a, %d %b %Y %H:%M:%S GMT" # the format of the date string according to RFC 1123
Expand All @@ -76,14 +82,19 @@ def update_rate_limit(self, request_datetime_header, total_token_usage: int = No
timestamp=request_timestamp, token_usage=total_token_usage
)
else:
complete_request_info = CompleteRequestInfo(timestamp=request_timestamp)
complete_request_info = CompleteRequestInfo(
timestamp=request_timestamp
)
self.append_complete_request_token_info(complete_request_info)

def check_availability(
self, request_token_len: int = 0, estimated_output_len: int = 0
):
if self.remaining_tokens is not None:
if request_token_len + estimated_output_len > self.remaining_tokens:
if (
request_token_len + estimated_output_len
> self.remaining_tokens
):
return False
if self.remaining_requests is not None:
if self.remaining_requests <= 0:
Expand Down
18 changes: 13 additions & 5 deletions lion_service/service_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from .rate_limiter import RateLimitError


def invoke_retry(max_retries: int = 3, base_delay: int = 1, max_delay: int = 60):
def invoke_retry(
max_retries: int = 3, base_delay: int = 1, max_delay: int = 60
):
def decorator(func):
@wraps(func)
async def wrapper(request_model, *args, **kwargs):
Expand All @@ -24,7 +26,10 @@ async def wrapper(request_model, *args, **kwargs):

# RateLimitError for Model only
if isinstance(e, RateLimitError):
if e.requested_tokens > request_model.rate_limiter.limit_tokens:
if (
e.requested_tokens
> request_model.rate_limiter.limit_tokens
):
raise ValueError(
"Requested tokens exceed the model's token limit. "
"Please modify the input, adjust the expected output tokens, or increase the token limit. "
Expand All @@ -42,11 +47,14 @@ async def wrapper(request_model, *args, **kwargs):
elif error_code := getattr(
e, "status", None
): # http request errors
if error_code == 429 and "exceeded your current quota" in str(
e
if (
error_code == 429
and "exceeded your current quota" in str(e)
): # RateLimitError (account quota reached)
raise e
if error_code == 429 or error_code >= 500: # ServerError
if (
error_code == 429 or error_code >= 500
): # ServerError
if retry_after := getattr(e, "headers", {}).get(
"Retry-After"
):
Expand Down
4 changes: 3 additions & 1 deletion lion_service/token_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def tokenize(
):
enc = tiktoken.get_encoding(self.encoding_name)
enc_text = self.encode(text)
tokens = [enc.decode_single_token_bytes(token_id) for token_id in enc_text]
tokens = [
enc.decode_single_token_bytes(token_id) for token_id in enc_text
]
if decode_byte_str:
tokens = [token.decode(decoder) for token in tokens]
return tokens
2 changes: 1 addition & 1 deletion lion_service/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.0"
__version__ = "1.1.1"

0 comments on commit a2f7b75

Please sign in to comment.