ai

Text Generation WebUI กับ SaaS Architecture —

text generation webui saas architecture
Text Generation WebUI กับ SaaS Architecture —

Text Generation WebUI คืออะไร

Text Generation WebUI กับ SaaS Architecture —

Text Generation WebUI เป็น Open-source Web Interface สำหรับรัน Large Language Model (LLM) แบบ Self-hosted พัฒนาโดย oobabooga รองรับ Model หลากหลายเช่น Llama 3, Mistral, Phi-3, Qwen และ Gemma ผ่าน Backend หลายตัวเช่น llama.cpp, ExLlamaV2, Transformers และ vLLM ผู้ใช้สามารถ Chat กับ AI, ปรับ Parameters, สลับ Model และใช้ Extensions ต่างๆผ่าน Browser

เมื่อต้องการให้บริการ Text Generation ให้ผู้ใช้หลายคน เช่น ภายในองค์กรหรือเป็น SaaS Product ต้องออกแบบ Architecture ที่รองรับ Multi-tenant, GPU Resource Management, Authentication, Rate Limiting และ Billing ซึ่งมีความซับซ้อนมากกว่าการรันบนเครื่องส่วนตัว

SaaS Architecture Overview

  • API Gateway (Kong/Nginx): จัดการ Authentication, Rate Limiting, Load Balancing
  • Auth Service: JWT Token, API Key Management, Tenant Isolation
  • Request Queue (Redis/RabbitMQ): จัดคิว Inference Requests ตาม Priority
  • GPU Scheduler: จัดสรร GPU ให้แต่ละ Request ตาม Tenant Plan
  • Inference Workers (vLLM/TGI): รัน LLM Inference จริง บน GPU Nodes
  • Model Registry: จัดการ Model Versions, Hot-swap Models
  • Usage Tracker: บันทึก Token Usage สำหรับ Billing
  • Billing Service: คำนวณค่าบริการตาม Token Usage

การตั้งค่า vLLM เป็น Inference Backend

# docker-compose.yml สำหรับ Text Generation SaaS
version: "3.8"

services:
  # API Gateway
  kong:
    image: kong:3.5
    environment:
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /kong/kong.yml
      KONG_PROXY_LISTEN: "0.0.0.0:8000, 0.0.0.0:8443 ssl"
    volumes:
      - ./kong.yml:/kong/kong.yml
    ports:
      - "8000:8000"
      - "8443:8443"

  # Redis สำหรับ Queue และ Rate Limiting
  redis:
    image: redis:7-alpine
    command: redis-server --maxmemory 2gb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data

  # vLLM Inference Server (GPU Node 1)
  vllm-worker-1:
    image: vllm/vllm-openai:latest
    command: >
      --model meta-llama/Llama-3.1-8B-Instruct
      --max-model-len 8192
      --gpu-memory-utilization 0.90
      --max-num-seqs 32
      --tensor-parallel-size 1
      --port 8080
      --api-key 
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    environment:
      HUGGING_FACE_HUB_TOKEN: 

  # vLLM Inference Server (GPU Node 2 — Model อื่น)
  vllm-worker-2:
    image: vllm/vllm-openai:latest
    command: >
      --model mistralai/Mistral-7B-Instruct-v0.3
      --max-model-len 8192
      --gpu-memory-utilization 0.90
      --max-num-seqs 32
      --port 8080
      --api-key 
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    environment:
      HUGGING_FACE_HUB_TOKEN: 

  # Auth + API Service
  api-server:
    build: ./api-server
    environment:
      REDIS_URL: redis://redis:6379
      DATABASE_URL: postgresql://app:@postgres:5432/saas
      JWT_SECRET: 
      VLLM_ENDPOINTS: "vllm-worker-1:8080, vllm-worker-2:8080"
    depends_on:
      - redis
      - postgres

  # PostgreSQL สำหรับ User/Billing Data
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: saas
      POSTGRES_USER: app
      POSTGRES_PASSWORD: 
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  redis_data:
  postgres_data:

---
# kong.yml — API Gateway Configuration
_format_version: "3.0"

services:
  - name: llm-api
    url: http://api-server:3000
    routes:
      - name: llm-route
        paths:
          - /v1/chat/completions
          - /v1/completions
          - /v1/models
        strip_path: false

plugins:
  - name: rate-limiting
    config:
      minute: 60
      policy: redis
      redis_host: redis
      redis_port: 6379

  - name: key-auth
    config:
      key_names:
        - Authorization
        - X-API-Key

  - name: cors
    config:
      origins:
        - "*"
      methods:
        - GET
        - POST
        - OPTIONS
      headers:
        - Authorization
        - Content-Type
Text Generation WebUI กับ SaaS Architecture —

GPU Scheduling และ Autoscaling

# kubernetes/gpu-autoscaler.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vllm-llama
  namespace: llm-saas
spec:
  replicas: 2
  selector:
    matchLabels:
      app: vllm-llama
  template:
    metadata:
      labels:
        app: vllm-llama
    spec:
      containers:
        - name: vllm
          image: vllm/vllm-openai:latest
          args:
            - "--model"
            - "meta-llama/Llama-3.1-8B-Instruct"
            - "--max-model-len"
            - "8192"
            - "--gpu-memory-utilization"
            - "0.90"
            - "--max-num-seqs"
            - "32"
          resources:
            limits:
              nvidia.com/gpu: 1
              memory: 32Gi
            requests:
              nvidia.com/gpu: 1
              memory: 24Gi
          ports:
            - containerPort: 8000
          readinessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 120
            periodSeconds: 10
      nodeSelector:
        gpu-type: a100
      tolerations:
        - key: nvidia.com/gpu
          operator: Exists
          effect: NoSchedule
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: vllm-llama-hpa
  namespace: llm-saas
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: vllm-llama
  minReplicas: 1
  maxReplicas: 8
  metrics:
    - type: Pods
      pods:
        metric:
          name: vllm_num_requests_running
        target:
          type: AverageValue
          averageValue: "20"
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 2
          periodSeconds: 120
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Pods
          value: 1
          periodSeconds: 300

Monitoring และ Cost Management

  • GPU Utilization: ใช้ NVIDIA DCGM Exporter + Prometheus ติดตาม GPU Memory, Compute Utilization และ Temperature
  • Request Metrics: ติดตาม Requests/sec, Latency P50/P95/P99, Queue Length, Error Rate แยกตาม Model และ Tenant
  • Token Throughput: วัด Tokens/sec สำหรับทั้ง Input และ Output เพื่อ Capacity Planning
  • Cost per Token: คำนวณต้นทุนต่อ Token จาก GPU Cost + Infra Cost หารด้วย Total Tokens เพื่อกำหนดราคา
  • Tenant Usage Dashboard: แสดง Token Usage, Request Count และ Cost ต่อ Tenant ต่อวัน

Text Generation WebUI คืออะไร

Text Generation WebUI เป็น Open-source Web Interface สำหรับรัน LLM แบบ Local รองรับ Model เช่น Llama, Mistral, Phi ผ่าน Backend หลายตัว ให้ผู้ใช้ Chat กับ AI ผ่าน Browser โดยไม่ต้องส่งข้อมูลไป Cloud เหมาะสำหรับองค์กรที่ต้องการ Data Privacy

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง