Ana içeriğe geç
Kategoriye Dön

LocalAI Kurulumu: OpenAI Uyumlu Self-Hosted AI API

LocalAI Docker kurulumu, OpenAI API uyumlu self-hosted AI servisi, metin üretimi, görsel oluşturma, ses sentezi, embedding ve model yönetimi rehberi.

Okuma süresi: 14 dk AI & ML Self-Hosting
localaiopenaiapiyapay zekaself-hostingdockerembedding

İçindekiler

LocalAI Kurulumu: OpenAI Uyumlu Self-Hosted AI API

LocalAI, OpenAI API ile tam uyumlu, self-hosted çalışan açık kaynaklı bir AI API sunucusudur. Metin üretimi, görsel oluşturma, ses sentezi, ses tanıma ve embedding gibi tüm AI yeteneklerini tek bir serviste birleştirir. GPU olmadan da CPU modunda çalışabilmesi en büyük avantajlarından biridir.

LocalAI Nedir?

LocalAI, OpenAI API formatını birebir taklit eden bir drop-in replacement'tır. Mevcut OpenAI SDK kullanan uygulamalarınızı sadece base URL değiştirerek LocalAI'ya yönlendirebilirsiniz. Temel özellikleri:

  • OpenAI API Uyumluluğu: /v1/chat/completions, /v1/images/generations, /v1/audio/transcriptions
  • Çoklu Model Desteği: LLaMA, Mistral, Phi, Gemma, Whisper, Stable Diffusion
  • CPU ve GPU: GPU olmadan da çalışır, NVIDIA/AMD GPU desteği
  • Embedding: Metin embedding üretimi (RAG uygulamaları için)
  • TTS/STT: Metin-ses ve ses-metin dönüşümü
  • Görsel Üretimi: Stable Diffusion entegrasyonu
  • Fonksiyon Çağrısı: OpenAI function calling desteği

Sistem Gereksinimleri

BileşenCPU ModuGPU Modu
RAM8 GB+16 GB+
Disk20 GB50 GB+
GPUGerekli değilNVIDIA 8 GB+ VRAM
OSLinux/macOSLinux (CUDA)
Docker24.0+24.0+ + nvidia-container-toolkit

Docker ile Kurulum

CPU Modu

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

GPU Modu (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:

Model Kurulumu

Gallery'den Model Yükleme

LocalAI, model gallery sistemi ile kolay model kurulumu sağlar:

hljs bash
# Mevcut modelleri listele
curl http://localhost:8080/models/available

# Model yükleme (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"
}'

# Yüklü modelleri listele
curl http://localhost:8080/v1/models

Manuel Model Ekleme

hljs bash
# GGUF model dosyasını indirme
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"

Model yapılandırma dosyası oluşturun:

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
template:
  chat_message: |
    [INST] {{.Input}} [/INST]
  chat: |
    {{.Input}}

API Kullanımı

Chat Completion (Sohbet)

hljs bash
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-3",
    "messages": [
      {"role": "system", "content": "Sen yardımcı bir asistansın."},
      {"role": "user", "content": "Docker compose nedir?"}
    ],
    "temperature": 0.7,
    "max_tokens": 512
  }'

Embedding Oluşturma

hljs bash
curl http://localhost:8080/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{
    "model": "text-embedding-ada-002",
    "input": "Sunucu güvenliği en iyi uygulamalar"
  }'

Görsel Üretimi

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"
  }'

Ses Tanıma (Whisper)

hljs bash
curl http://localhost:8080/v1/audio/transcriptions \
  -F "model=whisper-1" \
  -F "file=@audio.mp3"

Ses Sentezi (TTS)

hljs bash
curl http://localhost:8080/v1/audio/speech \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tts-1",
    "input": "Merhaba, ben LocalAI ses sentezi motoruyum.",
    "voice": "alloy"
  }' --output speech.mp3

Python ile Kullanım

OpenAI Python SDK ile doğrudan kullanılabilir:

hljs python
from openai import OpenAI

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

# Chat completion
response = client.chat.completions.create(
    model="llama-3",
    messages=[
        {"role": "system", "content": "Sen bir Linux uzmanısın."},
        {"role": "user", "content": "Nginx nasıl kurulur?"}
    ],
    max_tokens=1024
)
print(response.choices[0].message.content)

# Embedding
embedding = client.embeddings.create(
    model="text-embedding-ada-002",
    input="Docker container yönetimi"
)
print(f"Embedding boyutu: {len(embedding.data[0].embedding)}")

Çoklu Model Yapılandırması

Birden fazla modeli aynı anda çalıştırabilirsiniz:

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

Performans Optimizasyonu

hljs bash
# Ortam değişkenleri
export THREADS=8                    # CPU thread sayısı
export CONTEXT_SIZE=4096            # Context window
export GPU_LAYERS=35                # GPU'ya yüklenen katman sayısı
export PARALLEL_REQUESTS=true       # Paralel istek desteği

# Docker ile
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

Nginx Reverse Proxy

hljs nginx
server {
    listen 443 ssl;
    server_name ai.example.com;

    ssl_certificate /etc/letsencrypt/live/ai.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ai.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
        client_max_body_size 50M;
    }
}

REXE sunucularında LocalAI ile OpenAI uyumlu kendi AI API'nizi çalıştırabilirsiniz. GPU sunucularımız ile yüksek performanslı inference elde edin.