Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
39b9995
start
aniketmaurya May 25, 2025
ad6b344
update
aniketmaurya May 25, 2025
60098f5
Add LitAPIV2 class and refactor LitServerV2 initialization
aniketmaurya May 25, 2025
21654f4
Refactor LitAPI and LitServer initialization for improved configuration
aniketmaurya May 25, 2025
539d459
Refactor LitAPI loop handling and update test assertions
aniketmaurya May 25, 2025
08024dc
Implement validation for api_path in LitServer initialization
aniketmaurya May 25, 2025
900ad8a
Merge branch 'main' into aniket/multiple-endpoints
aniketmaurya May 25, 2025
4c9f3ed
update
aniketmaurya May 25, 2025
6ef9477
Refactor LitServer and LitAPI initialization for deprecation handling
aniketmaurya May 25, 2025
429bddc
fix
aniketmaurya May 25, 2025
9be1b99
Remove debug print statements from api.py and server.py; add test for…
aniketmaurya May 25, 2025
5bf18f8
Refactor LitServer to utilize _LitAPIConnector for improved API manag…
aniketmaurya May 25, 2025
66000aa
Merge branch 'main' into endpoint-2
aniketmaurya May 26, 2025
8fe8d20
Refactor pre_setup methods in LitAPI and loops for improved handling …
aniketmaurya May 26, 2025
319955c
Enhance worker setup in LitServer for improved inference handling
aniketmaurya May 26, 2025
00ba243
fixes
aniketmaurya May 26, 2025
12464b8
Refactor LitServer and BatchedLoop for improved worker management and…
aniketmaurya May 26, 2025
e81bd53
update
aniketmaurya May 26, 2025
0f8a6e8
Update test_openai_embedding.py to pass spec directly to TestEmbedAPI…
aniketmaurya May 26, 2025
fe522bd
Refactor LitServer and utility functions for improved worker manageme…
aniketmaurya May 27, 2025
6d25c15
Refactor data_streamer method in LitServer for improved accessibility
aniketmaurya May 27, 2025
c6b91bd
fix
aniketmaurya May 27, 2025
d42c5ce
fix tests
aniketmaurya May 27, 2025
f0a3261
fix test
aniketmaurya May 27, 2025
01f59a8
update
aniketmaurya May 27, 2025
7943cc7
update
aniketmaurya May 27, 2025
0055ae6
Apply suggestions from code review
aniketmaurya May 27, 2025
8170b3f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2025
58a04ad
fix
aniketmaurya May 27, 2025
91dd7e1
Request queue for each LitAPI
aniketmaurya May 27, 2025
0afc18b
Refactor LitAPI and LitServer for improved API path handling
aniketmaurya May 27, 2025
0d41e21
fix test
aniketmaurya May 27, 2025
5f2a386
fix
aniketmaurya May 27, 2025
ceb1b7b
fix windows
aniketmaurya May 27, 2025
81fff9a
Implement path collision detection in LitServer
aniketmaurya May 27, 2025
630cffe
Add mixed streaming configuration check in LitServer
aniketmaurya May 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 62 additions & 8 deletions src/litserve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,43 @@
import warnings
from abc import ABC, abstractmethod
from queue import Queue
from typing import Callable, Optional
from typing import TYPE_CHECKING, Callable, Optional, Union

from pydantic import BaseModel

from litserve.specs.base import LitSpec

if TYPE_CHECKING:
from litserve.loops.base import LitLoop


class LitAPI(ABC):
_stream: bool = False
_default_unbatch: Optional[Callable] = None
_spec: Optional[LitSpec] = None
_spec: Optional["LitSpec"] = None
_device: Optional[str] = None
_logger_queue: Optional[Queue] = None
request_timeout: Optional[float] = None

def __init__(self, max_batch_size: int = 1, batch_timeout: float = 0.0, enable_async: bool = False):
def __init__(
self,
max_batch_size: int = 1,
batch_timeout: float = 0.0,
api_path: str = "/predict",
stream: bool = False,
loop: Optional[Union[str, "LitLoop"]] = "auto",
spec: Optional["LitSpec"] = None,
enable_async: bool = False,
):
"""Initialize a LitAPI instance.

Args:
max_batch_size: Maximum number of requests to process in a batch.
batch_timeout: Maximum time to wait for a batch to fill before processing.
api_path: URL path for the prediction endpoint.
stream: Whether to enable streaming responses.
loop: Inference loop to use, or 'auto' to select based on settings.
spec: Specification for the API, such as OpenAISpec or custom specs.
enable_async: Enable async support.

"""
Expand All @@ -47,6 +63,36 @@ def __init__(self, max_batch_size: int = 1, batch_timeout: float = 0.0, enable_a

if batch_timeout < 0:
raise ValueError("batch_timeout must be greater than or equal to 0")

if isinstance(spec, LitSpec):
stream = spec.stream

if loop is None:
loop = "auto"

if isinstance(loop, str) and loop != "auto":
raise ValueError("loop must be an instance of _BaseLoop or 'auto'")

if not api_path.startswith("/"):
raise ValueError(
"api_path must start with '/'. "
"Please provide a valid api path like '/predict', '/classify', or '/v1/predict'"
)

# Check if the batch and unbatch methods are overridden in the lit_api instance
batch_overridden = self.batch.__code__ is not LitAPI.batch.__code__
unbatch_overridden = self.unbatch.__code__ is not LitAPI.unbatch.__code__

if batch_overridden and unbatch_overridden and max_batch_size == 1:
warnings.warn(
"The LitServer has both batch and unbatch methods implemented, "
"but the max_batch_size parameter was not set."
)

self.api_path = api_path
self.stream = stream
self._loop = loop
self._spec = spec
self.max_batch_size = max_batch_size
self.batch_timeout = batch_timeout
self.enable_async = enable_async
Expand Down Expand Up @@ -82,7 +128,6 @@ async def predict(self, x, **kwargs):
@abstractmethod
def setup(self, device):
"""Setup the model so it can be called in `predict`."""
pass

def decode_request(self, request, **kwargs):
"""Convert the request payload to your model input."""
Expand Down Expand Up @@ -165,10 +210,7 @@ def device(self):
def device(self, value):
self._device = value

def pre_setup(self, spec: Optional[LitSpec]):
if self.batch_timeout > self.request_timeout and self.request_timeout not in (False, -1):
raise ValueError("batch_timeout must be less than request_timeout")

def pre_setup(self, spec: Optional["LitSpec"]):
if self.stream:
self._default_unbatch = self._unbatch_stream
else:
Expand Down Expand Up @@ -212,3 +254,15 @@ def health(self) -> bool:

"""
return True

@property
def loop(self):
if self._loop == "auto":
from litserve.loops.loops import get_default_loop

self._loop = get_default_loop(self.stream, self.max_batch_size, self.enable_async)
return self._loop

@loop.setter
def loop(self, value: "LitLoop"):
self._loop = value
3 changes: 1 addition & 2 deletions src/litserve/python_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
client_template = """
# Copyright The Lightning AI team.
client_template = """# Copyright The Lightning AI team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
Loading