LocalAI provides an OpenAI-compatible REST API backed by local models — any application that uses the OpenAI API can be pointed at LocalAI instead, with zero code changes.
Why LocalAI
- 100% OpenAI API compatible — drop-in replacement
- Supports text, image generation, embeddings and speech
- No GPU required for smaller models
- Single binary — no Python dependencies
- Docker or bare metal deployment
Step 1 — Install with Docker
docker run -d -p 8080:8080 -v /models:/models --name localai --restart always localai/localai:latest
Step 2 — Download a Model
# Download Mistral 7B
wget -O /models/mistral-7b.gguf https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf
# Create model config
cat > /models/mistral.yaml << EOF
name: mistral
parameters:
model: mistral-7b.gguf
temperature: 0.7
top_p: 0.9
backend: llama-cpp
EOF
Step 3 — Test OpenAI Compatible API
# List models (same as OpenAI)
curl http://localhost:8080/v1/models
# Chat completion (identical to OpenAI API)
curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer any-key" -d '{
"model": "mistral",
"messages": [{"role":"user","content":"Explain what a reverse proxy is"}]
}'
Step 4 — Point Existing Apps to LocalAI
# In any OpenAI SDK app, just change the base URL:
# Python
import openai
client = openai.OpenAI(
api_key="any-key",
base_url="http://your-server:8080/v1"
)
# Environment variable approach
export OPENAI_API_BASE=http://your-server:8080/v1
export OPENAI_API_KEY=any-key
Conclusion
LocalAI provides OpenAI compatibility with complete data privacy. Our team deploys self-hosted AI infrastructure including LocalAI with existing application integration.
Comments