From fde0139bf7e261c52f0f8de62fc25cede6bfad7d Mon Sep 17 00:00:00 2001 From: Jannik Streidl Date: Fri, 22 Mar 2024 09:31:35 +0100 Subject: [PATCH] All in one Dockerfile for including Ollama --- Dockerfile | 35 ++++++++++++++++++++++++++--------- Dockerfile-cuda | 11 ++++++++--- Dockerfile-ollama | 2 +- backend/config.py | 6 +++++- backend/start.sh | 19 +++++++++++++------ 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index d94b6ec05..4a6baa0a2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ # Initialize device type args ARG USE_CUDA=false ARG USE_MPS=false +ARG INCLUDE_OLLAMA=false ######## WebUI frontend ######## FROM node:21-alpine3.19 as build @@ -29,10 +30,12 @@ FROM python:3.11-slim-bookworm as base # Use args ARG USE_CUDA ARG USE_MPS +ARG INCLUDE_OLLAMA ## Basis ## ENV ENV=prod \ - PORT=8080 + PORT=8080 \ + INCLUDE_OLLAMA_ENV=${INCLUDE_OLLAMA} ## Basis URL Config ## ENV OLLAMA_BASE_URL="/ollama" \ @@ -88,14 +91,28 @@ RUN if [ "$USE_CUDA" = "true" ]; then \ python -c "import os; from chromadb.utils import embedding_functions; sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=os.environ['RAG_EMBEDDING_MODEL'], device=os.environ['DEVICE_TYPE'])"; \ fi -# install required packages -RUN apt-get update \ - # Install pandoc and netcat - && apt-get install -y --no-install-recommends pandoc netcat-openbsd \ - # for RAG OCR - && apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 \ - # cleanup - && rm -rf /var/lib/apt/lists/* + +RUN if [ "$INCLUDE_OLLAMA" = "true" ]; then \ + apt-get update && \ + # Install pandoc and netcat + apt-get install -y --no-install-recommends pandoc netcat-openbsd && \ + # for RAG OCR + apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 && \ + # install helper tools + apt-get install -y --no-install-recommends curl && \ + # install ollama + curl -fsSL https://ollama.com/install.sh | sh && \ + # cleanup + rm -rf /var/lib/apt/lists/*; \ + else \ + apt-get update && \ + # Install pandoc and netcat + apt-get install -y --no-install-recommends pandoc netcat-openbsd && \ + # for RAG OCR + apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 && \ + # cleanup + rm -rf /var/lib/apt/lists/*; \ + fi diff --git a/Dockerfile-cuda b/Dockerfile-cuda index 782d9745e..8caadce3a 100644 --- a/Dockerfile-cuda +++ b/Dockerfile-cuda @@ -81,9 +81,14 @@ WORKDIR /app/backend # apk del /var/cache/apk/*.tbz2 # Install only the dependencies in the container, python will come from the base image used -RUN apk update && \ - apk add --no-install-recommends ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \ - apk del /var/cache/apk/*.tbz2 +RUN apt-get update && \ + apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \ + rm -rf /var/cache/apk/*.tbz2 + +# Install python and pip +RUN apt-get update && \ + apt-get install -y python3 python3-pip && \ + rm -rf /var/cache/apk/*.tbz2 COPY ./backend/requirements.txt ./requirements.txt RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 --no-cache-dir && \ diff --git a/Dockerfile-ollama b/Dockerfile-ollama index 35066e748..01900344c 100644 --- a/Dockerfile-ollama +++ b/Dockerfile-ollama @@ -64,7 +64,7 @@ RUN pip3 install -r requirements.txt --no-cache-dir # Install pandoc and netcat # RUN python -c "import pypandoc; pypandoc.download_pandoc()" RUN apt-get update \ - && apt-get install -y pandoc netcat-openbsd \ + && apt-get install -y pandoc netcat-openbsd curl \ && rm -rf /var/lib/apt/lists/* # preload embedding model diff --git a/backend/config.py b/backend/config.py index 7200b9126..1a36c3242 100644 --- a/backend/config.py +++ b/backend/config.py @@ -208,6 +208,7 @@ OLLAMA_API_BASE_URL = os.environ.get( ) OLLAMA_BASE_URL = os.environ.get("OLLAMA_BASE_URL", "") +INCLUDE_OLLAMA = os.environ.get("OLLAMA_BASE_URL", "false") if OLLAMA_BASE_URL == "" and OLLAMA_API_BASE_URL != "": @@ -219,7 +220,10 @@ if OLLAMA_BASE_URL == "" and OLLAMA_API_BASE_URL != "": if ENV == "prod": if OLLAMA_BASE_URL == "/ollama": - OLLAMA_BASE_URL = "http://host.docker.internal:11434" + if INCLUDE_OLLAMA: + OLLAMA_BASE_URL = "http://localhost:11434" + else: + OLLAMA_BASE_URL = "http://host.docker.internal:11434" OLLAMA_BASE_URLS = os.environ.get("OLLAMA_BASE_URLS", "") diff --git a/backend/start.sh b/backend/start.sh index f9ed59485..fe14444db 100755 --- a/backend/start.sh +++ b/backend/start.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +INCLUDE_OLLAMA=${INCLUDE_OLLAMA_ENV:-false} + SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd "$SCRIPT_DIR" || exit @@ -7,16 +9,21 @@ KEY_FILE=.webui_secret_key PORT="${PORT:-8080}" if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then - echo No WEBUI_SECRET_KEY provided + echo "No WEBUI_SECRET_KEY provided" if ! [ -e "$KEY_FILE" ]; then - echo Generating WEBUI_SECRET_KEY + echo "Generating WEBUI_SECRET_KEY" # Generate a random value to use as a WEBUI_SECRET_KEY in case the user didn't provide one. - echo $(head -c 12 /dev/random | base64) > $KEY_FILE + echo $(head -c 12 /dev/random | base64) > "$KEY_FILE" fi - echo Loading WEBUI_SECRET_KEY from $KEY_FILE - WEBUI_SECRET_KEY=`cat $KEY_FILE` + echo "Loading WEBUI_SECRET_KEY from $KEY_FILE" + WEBUI_SECRET_KEY=$(cat "$KEY_FILE") fi -WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" exec uvicorn main:app --host 0.0.0.0 --port "$PORT" --forwarded-allow-ips '*' \ No newline at end of file +if [ "$INCLUDE_OLLAMA" = "true" ]; then + echo "INCLUDE_OLLAMA is set to true, starting ollama serve." + ollama serve & +fi + +WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" exec uvicorn main:app --host 0.0.0.0 --port "$PORT" --forwarded-allow-ips '*'