backend/python/README.md
This directory contains Python-based AI backends for LocalAI, providing support for various AI models and hardware acceleration targets.
The Python backends use a unified build system based on libbackend.sh that provides:
uv and pipuv package manager (recommended) or pipEach backend can be installed individually:
# Navigate to a specific backend
cd backend/python/transformers
# Install dependencies
make transformers
# or
bash install.sh
# Run the backend
make run
# or
bash run.sh
The libbackend.sh script provides consistent commands across all backends:
# Source the library in your backend script
source $(dirname $0)/../common/libbackend.sh
# Install requirements (automatically handles hardware detection)
installRequirements
# Start the backend server
startBackend $@
# Run tests
runUnittests
The build system automatically detects and configures for different hardware:
Backends can specify hardware-specific dependencies:
requirements.txt - Base requirementsrequirements-cpu.txt - CPU-specific packagesrequirements-cublas12.txt - CUDA 12 packagesrequirements-cublas13.txt - CUDA 13 packagesrequirements-intel.txt - Intel-optimized packagesrequirements-mps.txt - Apple Silicon packagesPYTHON_VERSION - Python version (default: 3.10)PYTHON_PATCH - Python patch version (default: 18)BUILD_TYPE - Force specific build targetUSE_PIP - Use pip instead of uv (default: false)PORTABLE_PYTHON - Enable portable Python buildsLIMIT_TARGETS - Restrict backend to specific targets# In your backend script
LIMIT_TARGETS="cublas12"
source $(dirname $0)/../common/libbackend.sh
# In your backend script
LIMIT_TARGETS="intel"
source $(dirname $0)/../common/libbackend.sh
backend/python/common/template/backend.py with the required gRPC interfacelibbackend.sh for consistent build and execution# Run backend tests
make test
# or
bash test.sh
# Install dependencies
make <backend-name>
# Clean build artifacts
make clean
Each backend follows a consistent structure:
backend-name/
├── backend.py # Main backend implementation
├── requirements.txt # Base dependencies
├── requirements-*.txt # Hardware-specific dependencies
├── install.sh # Installation script
├── run.sh # Execution script
├── test.sh # Test script
├── Makefile # Build targets
└── test.py # Unit tests
BUILD_TYPE matches your systemensureVenv to create/activate environmentsWhen adding new backends or modifying existing ones:
libbackend.sh for consistent behavior