File size: 4,796 Bytes
7f69342
441479b
7f69342
4ec7108
7f69342
 
6e29991
1a4efad
7f69342
09e70ff
950f41b
fc0860f
 
950f41b
4ec7108
 
f64a284
7f69342
950f41b
0e913e4
c33410f
0e913e4
7f69342
1a4efad
c33410f
 
 
 
 
309e664
1a4efad
 
 
 
 
c33410f
950f41b
 
0e913e4
db57dc8
 
441479b
fc0860f
 
 
 
 
7f69342
 
7b82554
f64a284
7f69342
09e70ff
7f69342
d9a4451
 
f64a284
7f69342
441479b
8c68c1f
7f69342
8c68c1f
fc0860f
8c68c1f
 
 
ba2be63
dde400a
 
 
 
 
 
 
7763bf4
dde400a
7f69342
6e29991
7f69342
cba98c9
6e29991
dde400a
 
7763bf4
dde400a
ba2be63
7f69342
6e29991
7f69342
ba2be63
7f69342
dde400a
 
ba2be63
 
 
dde400a
9a590ac
 
 
 
 
 
 
 
 
 
fe7089d
 
fc0860f
 
fe7089d
fc0860f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a590ac
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
FROM debian:bookworm-slim AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    build-essential \
    cmake \
    ninja-build \
    ca-certificates \
    libcurl4-openssl-dev \
    libssl-dev \
    libboost-dev \
    nlohmann-json3-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Clone and build llama.cpp with SSL support for HuggingFace Hub
WORKDIR /build
ARG CACHEBUST=6
ARG LLAMA_CPP_REF=master
ARG BUILD_JOBS=1
RUN git clone --depth 1 --branch ${LLAMA_CPP_REF} https://github.com/ggerganov/llama.cpp.git && \
    cd llama.cpp && \
    cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_C_FLAGS_RELEASE="-O1 -DNDEBUG" \
        -DCMAKE_CXX_FLAGS_RELEASE="-O1 -DNDEBUG" \
        -DLLAMA_BUILD_TESTS=OFF \
        -DLLAMA_BUILD_EXAMPLES=OFF \
        -DLLAMA_BUILD_SERVER=ON \
        -DGGML_NATIVE=OFF \
        -DGGML_AVX2=ON \
        -DGGML_AVX=ON \
        -DGGML_FMA=ON \
        -DGGML_F16C=ON \
        -DGGML_OPENMP=ON \
        -DGGML_BLAS=OFF \
        -DLLAMA_CURL=ON \
        -DLLAMA_OPENSSL=ON && \
    cmake --build build --config Release --target llama-server -j"${BUILD_JOBS}" && \
    echo "=== Binary dependencies ===" && \
    ldd build/bin/llama-server || true

# Build C++ manager (Boost.Beast + JSON)
COPY cpp/llm_manager.cpp /build/llm_manager.cpp
RUN g++ -std=c++17 -O2 -DBOOST_ERROR_CODE_HEADER_ONLY -pthread \
    /build/llm_manager.cpp -o /build/llm-manager

# Runtime stage
FROM debian:bookworm-slim

# Install runtime dependencies including SSL/HTTPS support
RUN apt-get update && apt-get install -y --no-install-recommends \
    libcurl4 \
    ca-certificates \
    libgomp1 \
    libstdc++6 \
    openssl \
    && rm -rf /var/lib/apt/lists/*

# Copy llama-server binary and all shared libraries from builder
COPY --from=builder /build/llama.cpp/build/bin/llama-server /usr/local/bin/llama-server
COPY --from=builder /build/llama.cpp/build/bin/*.so.* /usr/local/lib/
COPY --from=builder /build/llm-manager /usr/local/bin/llm-manager

# Update library cache
RUN ldconfig

# Install Python and FastAPI dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Install Python packages
RUN pip3 install --no-cache-dir fastapi uvicorn aiohttp pydantic duckduckgo-search beautifulsoup4 lxml --break-system-packages

# Create non-root user
RUN useradd -m -u 1000 user && \
    mkdir -p /home/user/.cache/llama.cpp && \
    chown -R user:user /home/user

# Copy application code
COPY --chown=user:user app.py /home/user/app.py
COPY --chown=user:user logger.py /home/user/logger.py

USER user
WORKDIR /home/user

# Set environment variables
ENV HOME=/home/user \
    LLAMA_CACHE=/home/user/.cache/llama.cpp \
    PATH=/home/user/.local/bin:$PATH \
    PYTHONUNBUFFERED=1

EXPOSE 7860

# Start FastAPI app (which manages llama-server internally)
#CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]

# --- Optional: run llama.cpp C++ server directly (temporary rollout) ---
# Keep the FastAPI CMD above as the default. Uncomment ONE of the following
# to run the C++ server directly instead of the Python app.
#
# Example DeepSeek (4k context):
# CMD ["llama-server", "-hf", "TheBloke/deepseek-llm-7B-chat-GGUF:deepseek-llm-7b-chat.Q4_K_M.gguf",
#      "--host", "0.0.0.0", "--port", "7860", "-c", "4096", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "512"]
#
# Example Mixtral-8x7B-Instruct (known loader incompatibilities on newer llama.cpp + too large for 10GB RAM):
# CMD ["llama-server", "-hf", "TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF:mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf", "--host", "0.0.0.0", "--port", "7860", "-c", "4096", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "64", "--ubatch-size", "32"]

# CMD ["llama-server", "-hf", "QuantFactory/Qwen2.5-7B-Instruct-GGUF:q4_k_m", "--host", "0.0.0.0", "--port", "7860", "-c", "8192", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "128", "--ubatch-size", "64"]
#
# Active manager process:
# - loads default model at startup
# - supports /switch-model runtime model change
# - proxies /v1/chat/completions to active worker
ENV DEFAULT_MODEL=QuantFactory/Qwen2.5-7B-Instruct-GGUF:q4_k_m \
    MANAGER_HOST=0.0.0.0 \
    MANAGER_PORT=7860 \
    WORKER_BASE_PORT=8080 \
    SWITCH_TIMEOUT_SEC=300 \
    MODEL_N_CTX=8192 \
    MODEL_THREADS=4 \
    MODEL_NGL=0 \
    MODEL_BATCH=128 \
    MODEL_UBATCH=64

CMD ["llm-manager"]
#
# Example Qwen2.5-Coder 7B Instruct (32k context):
# CMD ["llama-server", "-hf", "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Qwen2.5-Coder-7B-Instruct.Q4_K_M.gguf",
#      "--host", "0.0.0.0", "--port", "7860", "-c", "32768", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "512"]