Zum Hauptinhalt springen
Zurück zur Kategorie

LocalAI Einrichtung: OpenAI-kompatible Self-Hosted AI-API

LocalAI Docker-Installation, OpenAI-API-kompatible Self-Hosted AI-Service, Texterzeugung, Bilderzeugung, Sprachsynthese, Embedding und Modellverwaltung Anleitung.

Lesezeit: 14 Min. KI & ML Self-Hosting
localaiopenaiapikünstliche intelligenzself-hostingdockerembedding

Inhaltsverzeichnis

LocalAI Einrichtung: OpenAI-kompatible Self-Hosted AI-API

LocalAI ist ein Open-Source, Self-Hosted AI-API-Server, der vollständig mit der OpenAI-API kompatibel ist. Er vereint alle KI-Fähigkeiten einschließlich Texterzeugung, Bilderzeugung, Sprachsynthese, Spracherkennung und Embedding in einem einzigen Dienst. Seine Fähigkeit, im CPU-Modus ohne GPU zu laufen, ist einer seiner größten Vorteile.

Was ist LocalAI?

LocalAI ist ein Drop-in-Replacement, das das OpenAI-API-Format exakt repliziert. Sie können Ihre bestehenden Anwendungen, die das OpenAI SDK verwenden, einfach durch Ändern der Base-URL auf LocalAI umleiten. Hauptmerkmale:

  • OpenAI-API-Kompatibilität: /v1/chat/completions, /v1/images/generations, /v1/audio/transcriptions
  • Multi-Modell-Unterstützung: LLaMA, Mistral, Phi, Gemma, Whisper, Stable Diffusion
  • CPU und GPU: Funktioniert ohne GPU, NVIDIA/AMD GPU-Unterstützung
  • Embedding: Text-Embedding-Erzeugung (für RAG-Anwendungen)
  • TTS/STT: Text-zu-Sprache und Sprache-zu-Text Konvertierung
  • Bilderzeugung: Stable Diffusion Integration
  • Function Calling: OpenAI Function Calling Unterstützung

Systemanforderungen

KomponenteCPU-ModusGPU-Modus
RAM8 GB+16 GB+
Festplatte20 GB50 GB+
GPUNicht erforderlichNVIDIA 8 GB+ VRAM
OSLinux/macOSLinux (CUDA)
Docker24.0+24.0+ + nvidia-container-toolkit

Docker-Installation

CPU-Modus

hljs bash
docker run -d \
  --name localai \
  -p 8080:8080 \
  -v localai_models:/build/models \
  localai/localai:latest-cpu

GPU-Modus (NVIDIA CUDA)

hljs bash
docker run -d \
  --name localai \
  --gpus all \
  -p 8080:8080 \
  -v localai_models:/build/models \
  localai/localai:latest-gpu-nvidia-cuda-12

Docker Compose

hljs yaml
version: '3.8'
services:
  localai:
    image: localai/localai:latest-gpu-nvidia-cuda-12
    container_name: localai
    ports:
      - "8080:8080"
    volumes:
      - localai_models:/build/models
      - ./config:/build/config
    environment:
      - THREADS=4
      - CONTEXT_SIZE=4096
      - GALLERIES=[{"name":"model-gallery","url":"github:mudler/LocalAI/gallery/index.yaml@master"}]
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/readyz"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  localai_models:

Modellinstallation

Modelle aus der Galerie laden

hljs bash
# Verfügbare Modelle auflisten
curl http://localhost:8080/models/available

# Modell laden (Llama 3)
curl http://localhost:8080/models/apply -d '{
  "id": "huggingface://TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf",
  "name": "llama-3"
}'

# Geladene Modelle auflisten
curl http://localhost:8080/v1/models

Manuelle Modellhinzufügung

hljs bash
wget -O models/mistral-7b-instruct.gguf \
  "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf"

Modellkonfigurationsdatei erstellen:

hljs yaml
# config/mistral.yaml
name: mistral
backend: llama-cpp
parameters:
  model: mistral-7b-instruct.gguf
  temperature: 0.7
  top_p: 0.9
  top_k: 40
  context_size: 4096
  threads: 4
  gpu_layers: 35

API-Nutzung

Chat Completion

hljs bash
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-3",
    "messages": [
      {"role": "system", "content": "Du bist ein hilfreicher Assistent."},
      {"role": "user", "content": "Was ist Docker Compose?"}
    ],
    "temperature": 0.7,
    "max_tokens": 512
  }'

Embedding-Erzeugung

hljs bash
curl http://localhost:8080/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{
    "model": "text-embedding-ada-002",
    "input": "Server-Sicherheit Best Practices"
  }'

Bilderzeugung

hljs bash
curl http://localhost:8080/v1/images/generations \
  -H "Content-Type: application/json" \
  -d '{
    "model": "stablediffusion",
    "prompt": "a futuristic server room, neon lights",
    "size": "512x512"
  }'

Python-Nutzung

Direkt verwendbar mit dem OpenAI Python SDK:

hljs python
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8080/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="llama-3",
    messages=[
        {"role": "system", "content": "Du bist ein Linux-Experte."},
        {"role": "user", "content": "Wie installiert man Nginx?"}
    ],
    max_tokens=1024
)
print(response.choices[0].message.content)

Multi-Modell-Konfiguration

hljs yaml
# config/models.yaml
- name: llama-3
  backend: llama-cpp
  parameters:
    model: llama-3-8b.gguf
    context_size: 4096
    gpu_layers: 35

- name: mistral
  backend: llama-cpp
  parameters:
    model: mistral-7b-instruct.gguf
    context_size: 4096
    gpu_layers: 35

- name: embedding
  backend: llama-cpp
  embeddings: true
  parameters:
    model: all-MiniLM-L6-v2.gguf

Leistungsoptimierung

hljs bash
export THREADS=8
export CONTEXT_SIZE=4096
export GPU_LAYERS=35
export PARALLEL_REQUESTS=true

docker run -d \
  --name localai \
  --gpus all \
  -p 8080:8080 \
  -e THREADS=8 \
  -e CONTEXT_SIZE=4096 \
  -v localai_models:/build/models \
  localai/localai:latest-gpu-nvidia-cuda-12

Betreiben Sie Ihre eigene OpenAI-kompatible AI-API mit LocalAI auf REXE-Servern. Erhalten Sie hochleistungsfähige Inferenz mit unseren GPU-Servern.