Livrare LOT 1 - Didi

This commit is contained in:
Dezvoltari Evotech 2026-06-25 14:13:25 -07:00
commit 5380c3fc63
990 changed files with 133308 additions and 0 deletions

View file

@ -0,0 +1,43 @@
# LLM Inference API Server
# Multi-stage build for smaller final image
FROM python:3.11.12-slim AS builder
# Install uv
COPY --from=ghcr.io/astral-sh/uv:0.10 /uv /usr/local/bin/uv
WORKDIR /app
# Copy project files
COPY pyproject.toml uv.lock README.md ./
COPY src/ ./src/
# Install dependencies
RUN uv sync --frozen --no-dev
# Production image
FROM python:3.11.12-slim
WORKDIR /app
# Copy virtual environment from builder
COPY --from=builder /app/.venv /app/.venv
# Copy source code
COPY src/ ./src/
# Set environment variables
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONUNBUFFERED=1
ENV LLM_HOST=0.0.0.0
ENV LLM_PORT=14011
# Health check (using Python since curl not available in slim image)
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:14011/health')" || exit 1
# Expose port
EXPOSE 14011
# Run the server
CMD ["python", "-m", "llm_inference.cli"]

View file

@ -0,0 +1,154 @@
#!/usr/bin/env bash
#
# Docker Compose Startup Script for LLM Inference
#
# Usage: ./deploy/docker-start.sh [OPTIONS]
#
# Options:
# --profile <api|vllm|llamacpp|full> Docker compose profile
# --detach Run in detached mode
# --down Stop and remove containers
# --logs Show logs
# --help Show this help message
#
# Required: Set environment variables in .env file or export them before running.
# See .env.example for the full list of required variables.
set -euo pipefail
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load .env file if it exists (check deploy/ first, then module root)
ENV_FILE=""
if [[ -f "$SCRIPT_DIR/.env" ]]; then
ENV_FILE="$SCRIPT_DIR/.env"
elif [[ -f "$SCRIPT_DIR/../.env" ]]; then
ENV_FILE="$SCRIPT_DIR/../.env"
fi
if [[ -n "$ENV_FILE" ]]; then
echo "Loading environment from: $ENV_FILE"
set -a
source "$ENV_FILE"
set +a
fi
PROFILE=""
DETACH=""
ACTION="up"
show_help() {
sed -n '2,16p' "$0" | sed 's/^# //' | sed 's/^#//'
exit 0
}
check_required_var() {
local var_name="$1"
if [[ -z "${!var_name:-}" ]]; then
echo "ERROR: Required environment variable $var_name is not set"
echo "Set it in .env file or export it before running this script"
exit 1
fi
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--profile)
PROFILE="$2"
shift 2
;;
--detach|-d)
DETACH="-d"
shift
;;
--down)
ACTION="down"
shift
;;
--logs)
ACTION="logs"
shift
;;
--help|-h)
show_help
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Require profile to be specified
if [[ -z "$PROFILE" && "$ACTION" == "up" ]]; then
echo "ERROR: --profile is required"
echo "Options: api, vllm, llamacpp, full"
exit 1
fi
# Check required variables based on profile
check_required_var "LLM_DEFAULT_BACKEND"
check_required_var "LLM_ENABLE_VLLM"
check_required_var "LLM_ENABLE_LLAMACPP"
check_required_var "LLM_EXTERNAL_URL"
# API keys - at least one MUST be set for litellm backend (fail-fast)
if [[ "$LLM_DEFAULT_BACKEND" == "litellm" ]]; then
if [[ -z "${OPENROUTER_API_KEY:-}" && -z "${OPENAI_API_KEY:-}" && -z "${ANTHROPIC_API_KEY:-}" ]]; then
echo "ERROR: litellm backend requires at least one API key"
echo "Set one of: OPENROUTER_API_KEY, OPENAI_API_KEY, or ANTHROPIC_API_KEY"
exit 1
fi
fi
# Check model-specific variables for vllm/llamacpp profiles
case $PROFILE in
vllm|full)
check_required_var "MODELS_DIR"
check_required_var "VLLM_MODEL"
;;
llamacpp|full)
check_required_var "MODELS_DIR"
check_required_var "LLAMACPP_MODEL"
check_required_var "LLAMACPP_THREADS"
check_required_var "LLAMACPP_PARALLEL"
;;
esac
cd "$SCRIPT_DIR"
case $ACTION in
up)
echo "Starting LLM Inference with profile: $PROFILE"
echo " Default backend: $LLM_DEFAULT_BACKEND"
echo " vLLM enabled: $LLM_ENABLE_VLLM"
echo " llama.cpp enabled: $LLM_ENABLE_LLAMACPP"
if [[ "$PROFILE" == "vllm" || "$PROFILE" == "full" ]]; then
echo " vLLM model: $VLLM_MODEL"
fi
if [[ "$PROFILE" == "llamacpp" || "$PROFILE" == "full" ]]; then
echo " llama.cpp model: $LLAMACPP_MODEL"
fi
echo ""
# shellcheck disable=SC2086
exec docker compose --profile "$PROFILE" up $DETACH
;;
down)
if [[ -z "$PROFILE" ]]; then
echo "ERROR: --profile is required with --down"
exit 1
fi
echo "Stopping LLM Inference containers..."
exec docker compose --profile "$PROFILE" down
;;
logs)
if [[ -z "$PROFILE" ]]; then
echo "ERROR: --profile is required with --logs"
exit 1
fi
exec docker compose --profile "$PROFILE" logs -f
;;
esac

View file

@ -0,0 +1,142 @@
# LLM Inference Module - Docker Compose Configuration
#
# Port Allocation:
# 14001 - vLLM Qwen3.5-35B-A3B (text + vision MoE, native multimodal)
# 14011 - LLM Inference API (unified router)
#
# Profiles:
# api - API server only (uses external LLM services)
# vllm - API + vLLM servers (GPU required)
#
# Model:
# - Qwen3.5-35B-A3B (~57GB weights BF16, gpu-util 0.45)
# - Native vision support (replaces separate Qwen3-VL)
#
# Hardware: 1x NVIDIA H200 NVL (~143GB VRAM)
# - GPU 0: Qwen3.5-35B-A3B + Whisper (~2GB)
#
# Naming Convention: didiAI-{module}-{service}
#
# Network:
# Uses deploy_default network (shared with other modules)
networks:
deploy_default:
external: true
services:
# ==========================================================================
# LLM Inference API Server
# ==========================================================================
llm-api:
container_name: didiAI-llm-api
image: didiai-llm-api
build:
context: ..
dockerfile: deploy/Dockerfile
ports:
- "14011:14011"
networks:
- deploy_default
environment:
- LLM_PORT=14011
- LLM_EXTERNAL_URL=${LLM_EXTERNAL_URL}
- LLM_DEFAULT_BACKEND=${LLM_DEFAULT_BACKEND}
- LLM_ENABLE_VLLM=${LLM_ENABLE_VLLM}
- LLM_ENABLE_LLAMACPP=${LLM_ENABLE_LLAMACPP}
- LLM_VLLM_BASE_URL=http://didiAI-vllm-qwen3.5:14001
- LLM_LLAMACPP_BASE_URLS=${LLM_LLAMACPP_BASE_URLS:-}
- LLM_API_TOKENS=${LLM_API_TOKENS:-}
- LLM_DASHBOARD_URL=${LLM_DASHBOARD_URL:-http://didiAI-dashboard:51300}
- OPENROUTER_API_KEY=${OPENROUTER_API_KEY:-}
- OPENAI_API_KEY=${OPENAI_API_KEY:-}
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:14011/health')"]
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
restart: unless-stopped
profiles:
- api
- vllm
# ==========================================================================
# vLLM Server - Qwen3.5-35B-A3B (Unified Text + Vision MoE Model)
# ==========================================================================
# Native multimodal - replaces separate Qwen3 text + Qwen3-VL vision
vllm-qwen3.5:
container_name: didiAI-vllm-qwen3.5
image: vllm/vllm-openai:qwen3_5
ports:
- "14001:14001"
networks:
- deploy_default
volumes:
- ${HF_CACHE_DIR}:/root/.cache/huggingface
environment:
- HF_HOME=/root/.cache/huggingface
- HUGGING_FACE_HUB_TOKEN=${HF_TOKEN:-}
- CUDA_VISIBLE_DEVICES=0
- VLLM_ALLOW_LONG_MAX_MODEL_LEN=1
command: >
--model Qwen/Qwen3.5-35B-A3B
--host 0.0.0.0
--port 14001
--served-model-name qwen3.5
--tensor-parallel-size 1
--max-model-len 32000
--gpu-memory-utilization 0.65
--trust-remote-code
--enable-prefix-caching
--disable-log-requests
--enable-auto-tool-choice
--tool-call-parser hermes
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0']
capabilities: [gpu]
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:14001/health"]
interval: 30s
timeout: 10s
retries: 10
start_period: 600s
restart: unless-stopped
profiles:
- vllm
# ==========================================================================
# llama.cpp Server (CPU/Metal Inference) - Optional
# ==========================================================================
llamacpp:
image: ghcr.io/ggml-org/llama.cpp:server-b4769
expose:
- "14011"
volumes:
- ${MODELS_DIR:-/cai2_ds_storage/models}:/models:ro
command: >
--model /models/${LLAMACPP_MODEL:-model.gguf}
--host 0.0.0.0
--port 14011
--ctx-size 4096
--threads ${LLAMACPP_THREADS:-4}
--parallel ${LLAMACPP_PARALLEL:-1}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:14011/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
restart: unless-stopped
profiles:
- llamacpp
- full
volumes:
vllm-cache:
name: llm-inference-vllm-cache

View file

@ -0,0 +1,49 @@
upstream llm_api {
server llm-api:8100;
keepalive 32;
}
server {
listen 80;
server_name _;
# Timeouts for slow LLM responses
proxy_connect_timeout 60s;
proxy_send_timeout 120s;
proxy_read_timeout 300s;
# Health checks (no logging)
location /health {
access_log off;
proxy_pass http://llm_api/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
location /ready {
access_log off;
proxy_pass http://llm_api/ready;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
# API endpoints
location / {
proxy_pass http://llm_api;
proxy_http_version 1.1;
# Forward client info
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# SSE streaming support
proxy_set_header Connection '';
proxy_buffering off;
proxy_cache off;
chunked_transfer_encoding off;
# Large request bodies for long conversations
client_max_body_size 10M;
}
}