litserve 的多endpoint 处理

默认litserve在处理的时候只支持一个endpoint 但很多时候我们可能需要配置多个,比如推理服务多个地址,或者同时希望基于litserve 添加一些其他endpoint 进行接口提供(配置以及信息维护的),目前来说是有方法进行配置管理上的多endpoinnt(通过litserve内部包装的fastapi app 实例添加新的router)

一些方法

  • app hack 模式
api = ChatAPI()
    server = ls.LitServer(api, spec=ls.OpenAISpec())

    # Add the embedding API route
    server.app.add_api_route(
        "/v1/embeddings",
        embedding_fn,
        methods=["POST"],
        tags=["embedding"],
        dependencies=[Depends(server.setup_auth())], 
    )

  • 新pr 特性(目前还没merge)

参考使用

# server.py
from litserve.server import LitServer, run_all
from litserve.test_examples import SimpleLitAPI


class SimpleLitAPI1(SimpleLitAPI):
    def setup(self, device):
        self.model = lambda x: x**1


class SimpleLitAPI2(SimpleLitAPI):
    def setup(self, device):
        self.model = lambda x: x**2


class SimpleLitAPI3(SimpleLitAPI):
    def setup(self, device):
        self.model = lambda x: x**3


class SimpleLitAPI4(SimpleLitAPI):
    def setup(self, device):
        self.model = lambda x: x**4


if __name__ == "__main__":
    server1 = LitServer(SimpleLitAPI1(), api_path="/predict-1")
    server2 = LitServer(SimpleLitAPI2(), api_path="/predict-2")
    server3 = LitServer(SimpleLitAPI3(), api_path="/predict-3")
    server4 = LitServer(SimpleLitAPI4(), api_path="/predict-4")
    run_all([server1, server2, server3, server4], port=8000)

说明

因为litserve的一些多进程以及基于队列的架构部分处理上会有一些不同,尽管目前github 有一些issue 讨论,但是暂时还没进行开发

参考资料

https://lightning.ai/docs/litserve/features/multiple-endpoints

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

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

posted on 2025-04-17 08:00  荣锋亮  阅读(48)  评论(0)    收藏  举报

导航