云
使用 txtai 可以构建可扩展的云原生应用。支持以下云运行时。
- 容器编排系统(例如 Kubernetes)
- Docker Engine
- 无服务器计算
- txtai.cloud(未来计划)
txtai 镜像可在 Docker Hub 上获取,包括适用于 CPU 和 GPU 安装的版本。由于 CPU 镜像体积小得多,因此在没有 GPU 可用时建议使用 CPU 版本。
基础 txtai 镜像没有安装任何模型,每次容器启动时都会下载模型。建议缓存模型,因为这将显著减少容器启动时间。有几种不同的方法可以做到这一点。
- 创建一个已缓存模型的容器
- 设置 transformers 缓存环境变量,并在启动镜像时挂载该卷
docker run -v <local dir>:/models -e TRANSFORMERS_CACHE=/models --rm -it <docker image>
构建 txtai 镜像
Docker Hub 上的 txtai 镜像配置用于支持大多数情况。可以根据需要使用不同的选项在本地构建此镜像。
下面是构建命令示例。
# Get Dockerfile
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/base/Dockerfile
# Build Ubuntu 22.04 image running Python 3.10
docker build -t txtai --build-arg BASE_IMAGE=ubuntu:22.04 --build-arg PYTHON_VERSION=3.10 .
# Build image with GPU support
docker build -t txtai --build-arg GPU=1 .
# Build minimal image with the base txtai components
docker build -t txtai --build-arg COMPONENTS= .
容器镜像模型缓存
如前所述,建议缓存模型以减少容器启动时间。以下命令演示了这一点。在所有情况下,都假设本地目录中存在一个包含所需配置的 config.yml 文件。
API
本节构建一个缓存模型并启动 API 服务的镜像。config.yml 文件应配置为包含希望通过 API 公开的组件。
以下是创建一个嵌入 API 服务的 config.yml 示例文件。
# config.yml
writable: true
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: true
下一节构建镜像并启动一个实例。
# Get Dockerfile
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/api/Dockerfile
# CPU build
docker build -t txtai-api .
# GPU build
docker build -t txtai-api --build-arg BASE_IMAGE=neuml/txtai-gpu .
# Run
docker run -p 8000:8000 --rm -it txtai-api
服务
本节构建一个定时工作流服务。有关定时工作流的更多信息可在此处找到。
# Get Dockerfile
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/service/Dockerfile
# CPU build
docker build -t txtai-service .
# GPU build
docker build -t txtai-service --build-arg BASE_IMAGE=neuml/txtai-gpu .
# Run
docker run --rm -it txtai-service
工作流
本节构建一个单次运行工作流。示例工作流可在此处找到。
# Get Dockerfile
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/workflow/Dockerfile
# CPU build
docker build -t txtai-workflow .
# GPU build
docker build -t txtai-workflow --build-arg BASE_IMAGE=neuml/txtai-gpu .
# Run
docker run --rm -it txtai-workflow <workflow name> <workflow parameters>
无服务器计算
txtai 最强大的功能之一是采用“构建一次,随处运行”的方法构建 YAML 配置的应用。API 实例和工作流可以在本地、服务器上、集群上或无服务器环境中运行。
txtai 的无服务器实例支持 AWS Lambda、Google Cloud Functions、Azure Cloud Functions 以及结合 Kubernetes 和 Knative 的框架。
AWS Lambda
以下步骤展示了如何使用 AWS SAM 构建无服务器 API 实例的基本示例。
- 创建 config.yml 和 template.yml
# config.yml
writable: true
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: true
# template.yml
Resources:
txtai:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
MemorySize: 3000
Timeout: 20
Events:
Api:
Type: Api
Properties:
Path: "/{proxy+}"
Method: ANY
Metadata:
Dockerfile: Dockerfile
DockerContext: ./
DockerTag: api
-
安装 AWS SAM
-
运行以下命令
# Get Dockerfile and application
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/aws/api.py
wget https://raw.githubusercontent.com/neuml/txtai/master/docker/aws/Dockerfile
# Build the docker image
sam build
# Start API gateway and Lambda instance locally
sam local start-api -p 8000 --warm-containers LAZY
# Verify instance running (should return 0)
curl http://localhost:8080/count
如果成功,现在会有一个本地 API 实例以“无服务器”方式运行。可以使用 SAM 将此配置部署到 AWS。请参阅此链接了解更多信息。
Kubernetes 与 Knative
txtai 可以与容器编排系统一起扩展。这可以是自托管的,也可以与云提供商合作,例如 Amazon Elastic Kubernetes Service、Google Kubernetes Engine 和 Azure Kubernetes Service。还有其他提供托管 Kubernetes 服务的小型提供商。
关于如何使用 Knative 在 Kubernetes 上构建无服务器 txtai 应用的完整示例可在此处找到。
txtai.cloud
txtai.cloud 是一个计划中的项目,它将提供一种简便安全的方式来运行托管的 txtai 应用。