LitServe fastapi endpoint注册简单说明

LitServe 是基于fastapi 开发的,因为服务ai 模型的特殊性(一般都比较耗时,运行时间会比较长),LitServe 对于api endpoint 的注册并不是简单的处理,而是包含了一个自己的spec,基于此spec 实现灵活的定义(比如支持openai 兼容模式)

参考注册

  • spec 接口定义
class LitSpec:
    """Spec will have its own encode, and decode."""

    def __init__(self):
        self._endpoints = []

        self._server: "LitServer" = None

    def setup(self, server: "LitServer"):
        self._server = server
    # 此处可以自定义endpoint  
    def add_endpoint(self, path: str, endpoint: Callable, methods: List[str]):
        """Register an endpoint in the spec."""
        self._endpoints.append((path, endpoint, methods))

    @property
    def endpoints(self):
        return self._endpoints.copy()

    @abstractmethod
    def decode_request(self, request, meta_kwargs):
        """Convert the request payload to your model input."""
        pass

    @abstractmethod
    def encode_response(self, output, meta_kwargs):
        """Convert the model output to a response payload.

        To enable streaming, it should yield the output.

        """
        pass
  • 标准模式 参数不包含spec 的,会提供一个标准的 /predict endpoint,当然此地址是可以调整的 参考处理
#  具体是在LitServer init 方法中的self.setup_server()
if not self._specs:
    stream = self.lit_api.stream
    # In the future we might want to differentiate endpoints for streaming vs non-streaming
    # For now we allow either one or the other
    endpoint = self.api_path
    methods = ["POST"]
    # fastapi的add_api_route 添加路由
    self.app.add_api_route(
        endpoint,
        stream_predict if stream else predict,
        methods=methods,
        dependencies=[Depends(self.setup_auth())],
    )
  • 包含spec 的处理
for spec in self._specs:
    spec: LitSpec
    # TODO check that path is not clashing
    # 不同spec 提供的能力endpoints
    for path, endpoint, methods in spec.endpoints:
        # fastapi的add_api_route 添加路由
        self.app.add_api_route(
            path, endpoint=endpoint, methods=methods, dependencies=[Depends(self.setup_auth())]
        )
  • openai spec 对的注册

openai 的endpoint api 是基于spec 处理的

class OpenAISpec(LitSpec):
    def __init__(
        self,
    ):
        super().__init__()
        # register the endpoint, 实现openai 兼容的处理
        self.add_endpoint("/v1/chat/completions", self.chat_completion, ["POST"])
        self.add_endpoint("/v1/chat/completions", self.options_chat_completions, ["OPTIONS"])

说明

对于fastapi endpoint 的注册实际上就是标准的fastapi 玩法,只是LitServe 提供了一个抽象可以方便自定义以及标准化

参考资料

https://github.com/Lightning-AI/LitServe

posted on   荣锋亮  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-11-13 projectdiscovery 提供的一些安全工具
2023-11-13 nuclei 快速&可自定义的基于DSL的漏洞扫描工具
2022-11-13 rqlite 基于sqlite 的轻量级分布式关系数据库
2021-11-13 fastdfs 集群异常修复实践
2020-11-13 pg_top试用
2020-11-13 offcputime 火焰图
2020-11-13 sadf 方便sar 多格式化输出工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示