🍱 使用任何开源或自定义 AI 模型构建模型推理 API 和多模型服务系统。👉 加入我们的 Slack 社区!
BentoML 是一个用于构建在线服务系统的 Python 库,专为 AI 应用和模型推理优化。
安装 BentoML:
# 要求 Python≥3.9
pip install -U bentoml
在 service.py 文件中定义 API。
import bentoml
@bentoml.service(
image=bentoml.images.Image(python_version="3.11").python_packages("torch", "transformers"),
)
class Summarization:
def __init__(self) -> None:
import torch
from transformers import pipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
self.pipeline = pipeline('summarization', device=device)
@bentoml.api(batchable=True)
def summarize(self, texts: list[str]) -> list[str]:
results = self.pipeline(texts)
return [item['summary_text'] for item in results]
将 PyTorch 和 Transformers 包安装到您的 Python 虚拟环境中。
pip install torch transformers # 本地运行所需的额外依赖
本地运行服务代码(默认在 http://localhost:3000 提供服务):
bentoml serve
您应该会看到以下输出。
[INFO] [cli] Starting production HTTP BentoServer from "service:Summarization" listening on http://localhost:3000 (Press CTRL+C to quit)
[INFO] [entry_service:Summarization:1] Service Summarization initialized
现在,您可以通过浏览器访问 http://localhost:3000 或使用 Python 脚本运行推理:
import bentoml
with bentoml.SyncHTTPClient('http://localhost:3000') as client:
summarized_text: str = client.summarize([bentoml.__doc__])[0]
print(f"Result: {summarized_text}")
运行 bentoml build 将必要的代码、模型、依赖配置打包成 Bento——这是 BentoML 中标准化的可部署工件:
bentoml build
确保 Docker 正在运行。为部署生成 Docker 容器镜像:
bentoml containerize summarization:latest
运行生成的镜像:
docker run --rm -p 3000:3000 summarization:latest
BentoCloud 为快速可靠的 GenAI 应用提供计算基础设施。它利用云计算资源加速您的 BentoML 开发流程,并简化在生产环境中部署、扩展和运维 BentoML 的方式。
个人使用请 注册 BentoCloud;企业用例请 联系我们的团队。
# 注册后,运行以下命令创建 API 令牌:
bentoml cloud login
# 从当前目录部署:
bentoml deploy

详细说明请阅读 Hello World 示例。
查看 完整列表 获取更多示例代码和用法。
更多教程和指南请参阅 文档。
欢迎参与并加入我们的 社区 Slack 💬,这里有成千上万的 AI/ML 工程师互相帮助、为项目做贡献,并讨论如何构建 AI 产品。
报告错误或提出功能请求,请使用 GitHub Issues。
有多种方式可以为项目做出贡献:
#bentoml-contributors 频道 此处 分享您的反馈并讨论路线图计划。感谢我们所有出色的贡献者!
BentoML 框架会收集匿名使用数据,以帮助我们的社区改进产品。仅报告 BentoML 的内部 API 调用。这排除了任何敏感信息,例如用户代码、模型数据、模型名称或堆栈跟踪。这是用于使用情况跟踪的 代码。您可以通过 --do-not-track CLI 选项选择退出使用情况跟踪:
bentoml [command] --do-not-track
或者通过设置环境变量:
export BENTOML_DO_NOT_TRACK=True