LitServe 快速部署类openai 接口服务

以前简单说明过LitServe 支持openai 接口格式的服务api,以下是一个简单试用

参考代码

  • app.py
import litserve as ls

class SimpleLitAPI(ls.LitAPI):
    def setup(self, device):
        self.model = None

    def predict(self, prompt):
        # `prompt` is a list of dictionary containing role and content
        # example: [{'role': 'user', 'content': 'How can I help you today?'}]
        yield "This is a sample generated output"

if __name__ == "__main__":
    # Enable the OpenAISpec in LitServer
    api = SimpleLitAPI()
    server = ls.LitServer(api, spec=ls.OpenAISpec())
    server.run(port=8000)
  • 运行效果

  • 接口调用
curl -X 'POST' \
  'http://0.0.0.0:8000/v1/chat/completions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "",
  "messages": [
    {
      "role": "user",
      "content": "demo"
    }
  ]
}'

  • stream 模式
import litserve as ls

class SimpleLitAPI(ls.LitAPI):
    def setup(self, device):
        self.model = None

    def predict(self, prompt):
        # 基于生成器进行数据生成
        for chunk in "This is a sample generated output".split():
            yield chunk

if __name__ == "__main__":
    api = SimpleLitAPI()
    server = ls.LitServer(api, spec=ls.OpenAISpec())
    server.run(port=8000)

说明

基于LitServe快速开发ai 推理服务接口还是很方便的,快速、简单,让我们将核心能力放到业务处理上

参考资料

https://lightning.ai/docs/litserve/features/open-ai-spec

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

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

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

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2023-11-08 cmake 进行rpm包构建
2022-11-08 go-npm 基于npm 进行跨平台分发golang 二进制程序
2022-11-08 grafana agent 动态配置内部机制简单说明
2022-11-08 minio 一些配置策略
2022-11-08 minio policy 使用
2020-11-08 kotlin 学习 三 maven 集成+java 互调用
2020-11-08 kotlin 学习 二 命令行模式运行

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示