Skip to content

Commit c20fa2e

Browse files
committed
Update README
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent d9ceafa commit c20fa2e

File tree

1 file changed

+169
-18
lines changed

1 file changed

+169
-18
lines changed

backend/python/README.md

Lines changed: 169 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,189 @@
1-
# Common commands about conda environment
1+
# Python Backends for LocalAI
22

3-
## Create a new empty conda environment
3+
This directory contains Python-based AI backends for LocalAI, providing support for various AI models and hardware acceleration targets.
44

5-
```
6-
conda create --name <env-name> python=<your version> -y
5+
## Overview
76

8-
conda create --name autogptq python=3.11 -y
9-
```
7+
The Python backends use a unified build system based on `libbackend.sh` that provides:
8+
- **Automatic virtual environment management** with support for both `uv` and `pip`
9+
- **Hardware-specific dependency installation** (CPU, CUDA, Intel, MLX, etc.)
10+
- **Portable Python support** for standalone deployments
11+
- **Consistent backend execution** across different environments
12+
13+
## Available Backends
14+
15+
### Core AI Models
16+
- **transformers** - Hugging Face Transformers models (PyTorch-based)
17+
- **vllm** - High-performance LLM inference engine
18+
- **mlx** - Apple Silicon optimized ML framework
19+
- **exllama2** - ExLlama2 quantized models
20+
21+
### Audio & Speech
22+
- **bark** - Text-to-speech synthesis
23+
- **coqui** - Coqui TTS models
24+
- **faster-whisper** - Fast Whisper speech recognition
25+
- **kitten-tts** - Lightweight TTS
26+
- **mlx-audio** - Apple Silicon audio processing
27+
28+
### Computer Vision
29+
- **diffusers** - Stable Diffusion and image generation
30+
- **mlx-vlm** - Vision-language models for Apple Silicon
31+
- **rfdetr** - Object detection models
32+
33+
### Specialized
34+
- **chatterbox** - Chat model backends
35+
- **kokoro** - Specialized AI models
36+
- **rerankers** - Text reranking models
37+
38+
## Quick Start
39+
40+
### Prerequisites
41+
- Python 3.10+ (default: 3.10.18)
42+
- `uv` package manager (recommended) or `pip`
43+
- Appropriate hardware drivers for your target (CUDA, Intel, etc.)
44+
45+
### Installation
1046

11-
## To activate the environment
47+
Each backend can be installed individually:
1248

13-
As of conda 4.4
49+
```bash
50+
# Navigate to a specific backend
51+
cd backend/python/transformers
52+
53+
# Install dependencies
54+
make transformers
55+
# or
56+
bash install.sh
57+
58+
# Run the backend
59+
make run
60+
# or
61+
bash run.sh
1462
```
15-
conda activate autogptq
63+
64+
### Using the Unified Build System
65+
66+
The `libbackend.sh` script provides consistent commands across all backends:
67+
68+
```bash
69+
# Source the library in your backend script
70+
source $(dirname $0)/../common/libbackend.sh
71+
72+
# Install requirements (automatically handles hardware detection)
73+
installRequirements
74+
75+
# Start the backend server
76+
startBackend $@
77+
78+
# Run tests
79+
runUnittests
1680
```
1781

18-
The conda version older than 4.4
82+
## Hardware Targets
83+
84+
The build system automatically detects and configures for different hardware:
1985

86+
- **CPU** - Standard CPU-only builds
87+
- **CUDA** - NVIDIA GPU acceleration (supports CUDA 11/12)
88+
- **Intel** - Intel XPU/GPU optimization
89+
- **MLX** - Apple Silicon (M1/M2/M3) optimization
90+
- **HIP** - AMD GPU acceleration
91+
92+
### Target-Specific Requirements
93+
94+
Backends can specify hardware-specific dependencies:
95+
- `requirements.txt` - Base requirements
96+
- `requirements-cpu.txt` - CPU-specific packages
97+
- `requirements-cublas11.txt` - CUDA 11 packages
98+
- `requirements-cublas12.txt` - CUDA 12 packages
99+
- `requirements-intel.txt` - Intel-optimized packages
100+
- `requirements-mps.txt` - Apple Silicon packages
101+
102+
## Configuration Options
103+
104+
### Environment Variables
105+
106+
- `PYTHON_VERSION` - Python version (default: 3.10)
107+
- `PYTHON_PATCH` - Python patch version (default: 18)
108+
- `BUILD_TYPE` - Force specific build target
109+
- `USE_PIP` - Use pip instead of uv (default: false)
110+
- `PORTABLE_PYTHON` - Enable portable Python builds
111+
- `LIMIT_TARGETS` - Restrict backend to specific targets
112+
113+
### Example: CUDA 12 Only Backend
114+
115+
```bash
116+
# In your backend script
117+
LIMIT_TARGETS="cublas12"
118+
source $(dirname $0)/../common/libbackend.sh
20119
```
21-
source activate autogptq
120+
121+
### Example: Intel-Optimized Backend
122+
123+
```bash
124+
# In your backend script
125+
LIMIT_TARGETS="intel"
126+
source $(dirname $0)/../common/libbackend.sh
22127
```
23128

24-
## Install the packages to your environment
129+
## Development
130+
131+
### Adding a New Backend
25132

26-
Sometimes you need to install the packages from the conda-forge channel
133+
1. Create a new directory in `backend/python/`
134+
2. Copy the template structure from `common/template/`
135+
3. Implement your `backend.py` with the required gRPC interface
136+
4. Add appropriate requirements files for your target hardware
137+
5. Use `libbackend.sh` for consistent build and execution
27138

28-
By using `conda`
139+
### Testing
140+
141+
```bash
142+
# Run backend tests
143+
make test
144+
# or
145+
bash test.sh
29146
```
30-
conda install <your-package-name>
31147

32-
conda install -c conda-forge <your package-name>
148+
### Building
149+
150+
```bash
151+
# Install dependencies
152+
make <backend-name>
153+
154+
# Clean build artifacts
155+
make clean
33156
```
34157

35-
Or by using `pip`
158+
## Architecture
159+
160+
Each backend follows a consistent structure:
36161
```
37-
pip install <your-package-name>
162+
backend-name/
163+
├── backend.py # Main backend implementation
164+
├── requirements.txt # Base dependencies
165+
├── requirements-*.txt # Hardware-specific dependencies
166+
├── install.sh # Installation script
167+
├── run.sh # Execution script
168+
├── test.sh # Test script
169+
├── Makefile # Build targets
170+
└── test.py # Unit tests
38171
```
172+
173+
## Troubleshooting
174+
175+
### Common Issues
176+
177+
1. **Missing dependencies**: Ensure all requirements files are properly configured
178+
2. **Hardware detection**: Check that `BUILD_TYPE` matches your system
179+
3. **Python version**: Verify Python 3.10+ is available
180+
4. **Virtual environment**: Use `ensureVenv` to create/activate environments
181+
182+
## Contributing
183+
184+
When adding new backends or modifying existing ones:
185+
1. Follow the established directory structure
186+
2. Use `libbackend.sh` for consistent behavior
187+
3. Include appropriate requirements files for all target hardware
188+
4. Add comprehensive tests
189+
5. Update this README if adding new backend types

0 commit comments

Comments
 (0)