Skip to content

Commit c886e00

Browse files
Merge pull request #28 from pitangainnovare/impl/llama-or-not-llama
Cria capacidade de usar sistema com ou sem suporte ao LLaMa
2 parents 4079e8b + cb2021a commit c886e00

File tree

17 files changed

+459
-99
lines changed

17 files changed

+459
-99
lines changed

.envs/.local/.django

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ CELERY_FLOWER_PASSWORD=QgScyefPrYhHgO6onW61u0nazc5xdBuP4sM7jMRrBBFuA2RjsFhZLp7xb
1616

1717
# Timeout fetch_data
1818
# ------------------------------------------------------------------------------
19-
FETCH_DATA_TIMEOUT=2
19+
FETCH_DATA_TIMEOUT=2
20+
21+
22+
HF_TOKEN=

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,6 @@ cython_debug/
171171
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
172172
#.idea/
173173

174-
# End of https://www.toptal.com/developers/gitignore/api/django
174+
# End of https://www.toptal.com/developers/gitignore/api/django
175+
176+
llama3/llama-3.2/*

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ build_date: ## Show build date
3838
build: ## Build app using $(compose)
3939
@docker compose -f $(compose) build
4040

41+
build_llama: ## Build app using $(compose) with llama enabled
42+
@docker compose -f llama.$(compose) build
43+
4144
build_no_cache: ## Build app using $(compose)
4245
@docker compose -f $(compose) build --no-cache
4346

compose/local/django/Dockerfile

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ RUN apt-get update && \
2727
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 50 && \
2828
apt-get clean && rm -rf /var/lib/apt/lists/*
2929

30-
# Instalar ninja-build y cmake
31-
RUN apt-get install -y ninja-build cmake
32-
33-
# Configurar variables de entorno para compilar con BLAS y SIMD condicionalmente
34-
ARG ENABLE_OPTIMIZATIONS=true
35-
ARG ENABLE_OPTIMIZATIONS=true
36-
ENV CFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
37-
CXXFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
38-
CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"
39-
4030
# Actualizar pip, setuptools y wheel antes de instalar dependencias
4131
RUN python -m pip install --upgrade pip setuptools wheel
4232

@@ -63,12 +53,6 @@ ENV PYTHONUNBUFFERED 1
6353
ENV PYTHONDONTWRITEBYTECODE 1
6454
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
6555

66-
# Disable AVX support for llama-cpp-python if needed
67-
ARG DISABLE_AVX=false
68-
69-
# Set the version of llama-cpp-python
70-
ARG LLAMA_VERSION=0.3.14
71-
7256
WORKDIR ${APP_HOME}
7357

7458
RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list
@@ -89,17 +73,10 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
8973
# copy python dependency wheels from python-build-stage
9074
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
9175

92-
# Use wheels to install python dependencies (excluding llama-cpp-python)
93-
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") \
76+
# Use wheels to install python dependencies
77+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") \
9478
&& rm -rf /wheels/
9579

96-
# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes with or without AVX support
97-
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
98-
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
99-
else \
100-
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
101-
fi
102-
10380
COPY ./compose/production/django/entrypoint /entrypoint
10481
RUN sed -i 's/\r$//g' /entrypoint
10582
RUN chmod +x /entrypoint
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
ARG PYTHON_VERSION=3.11-bullseye
2+
3+
# define an alias for the specfic python version used in this file.
4+
FROM python:${PYTHON_VERSION} AS python
5+
6+
# Python build stage
7+
FROM python AS python-build-stage
8+
9+
ARG BUILD_ENVIRONMENT=local
10+
11+
# Install apt packages
12+
RUN apt-get update && apt-get install --no-install-recommends -y \
13+
# dependencies for building Python packages
14+
build-essential \
15+
git \
16+
# psycopg2 dependencies
17+
libpq-dev \
18+
# other dependencies
19+
software-properties-common \
20+
libopenblas-dev \
21+
libomp-dev
22+
23+
# Instalar gcc-10 y g++-10 en Debian Bullseye
24+
RUN apt-get update && \
25+
apt-get install -y gcc-10 g++-10 ninja-build cmake && \
26+
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 50 && \
27+
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 50 && \
28+
apt-get clean && rm -rf /var/lib/apt/lists/*
29+
30+
# Configurar variables de entorno para compilar con BLAS y SIMD condicionalmente
31+
ARG ENABLE_OPTIMIZATIONS=true
32+
ENV CFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
33+
CXXFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
34+
CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"
35+
36+
# Actualizar pip, setuptools y wheel antes de instalar dependencias
37+
RUN python -m pip install --upgrade pip setuptools wheel
38+
39+
# Requirements are installed here to ensure they will be cached.
40+
COPY ./requirements .
41+
42+
# Create application directory to hold DOCX layouts
43+
COPY ./docx_layouts .
44+
45+
# Update pip
46+
RUN python -m pip install --upgrade pip
47+
48+
# Create Python Dependency and Sub-Dependency Wheels.
49+
RUN pip wheel --wheel-dir /usr/src/app/wheels \
50+
-r ${BUILD_ENVIRONMENT}.txt \
51+
-r extra-llama.txt
52+
53+
# Python 'run' stage
54+
FROM python AS python-run-stage
55+
56+
ARG BUILD_ENVIRONMENT=local
57+
ARG APP_HOME=/app
58+
59+
ENV PYTHONUNBUFFERED 1
60+
ENV PYTHONDONTWRITEBYTECODE 1
61+
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
62+
63+
WORKDIR ${APP_HOME}
64+
65+
RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list
66+
67+
# Install required system dependencies
68+
RUN apt-get update && apt-get install --no-install-recommends -y \
69+
# psycopg2 dependencies
70+
libpq-dev \
71+
# Translations dependencies
72+
gettext \
73+
# libreoffice for document conversions
74+
default-jre libreoffice libreoffice-java-common ttf-mscorefonts-installer fonts-liberation fonts-liberation2 fonts-crosextra-carlito fonts-crosextra-caladea fonts-dejavu fonts-noto \
75+
# cleaning up unused files
76+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
77+
&& rm -rf /var/lib/apt/lists/*
78+
79+
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
80+
# copy python dependency wheels from python-build-stage
81+
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
82+
83+
# Use wheels to install python dependencies
84+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") \
85+
&& rm -rf /wheels/
86+
87+
COPY ./compose/production/django/entrypoint /entrypoint
88+
RUN sed -i 's/\r$//g' /entrypoint
89+
RUN chmod +x /entrypoint
90+
91+
COPY ./compose/local/django/start /start
92+
RUN sed -i 's/\r$//g' /start
93+
RUN chmod +x /start
94+
95+
COPY ./compose/local/django/celery/worker/start /start-celeryworker
96+
RUN sed -i 's/\r$//g' /start-celeryworker
97+
RUN chmod +x /start-celeryworker
98+
99+
COPY ./compose/local/django/celery/beat/start /start-celerybeat
100+
RUN sed -i 's/\r$//g' /start-celerybeat
101+
RUN chmod +x /start-celerybeat
102+
103+
COPY ./compose/local/django/celery/flower/start /start-flower
104+
RUN sed -i 's/\r$//g' /start-flower
105+
RUN chmod +x /start-flower
106+
107+
# copy application code to WORKDIR
108+
COPY . ${APP_HOME}
109+
110+
ENTRYPOINT ["/entrypoint"]

compose/production/django/Dockerfile

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ ENV PYTHONUNBUFFERED 1
3939
ENV PYTHONDONTWRITEBYTECODE 1
4040
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
4141

42-
# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
43-
ARG DISABLE_AVX=true
44-
45-
# Set the version of llama-cpp-python
46-
ARG LLAMA_VERSION=0.3.14
47-
4842
WORKDIR ${APP_HOME}
4943

5044
RUN addgroup --system django \
@@ -68,15 +62,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
6862
# copy python dependency wheels from python-build-stage
6963
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
7064

71-
# use wheels to install python dependencies (excluding llama-cpp-python)
72-
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") && rm -rf /wheels/
73-
74-
# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
75-
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
76-
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
77-
else \
78-
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
79-
fi
65+
# use wheels to install python dependencies
66+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") && rm -rf /wheels/
8067

8168
COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
8269
RUN sed -i 's/\r$//g' /entrypoint
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
ARG PYTHON_VERSION=3.11-bullseye
2+
3+
# define an alias for the specfic python version used in this file.
4+
FROM python:${PYTHON_VERSION} as python
5+
6+
# Python build stage
7+
FROM python as python-build-stage
8+
9+
ARG BUILD_ENVIRONMENT=production
10+
11+
# Install apt packages
12+
RUN apt-get update && apt-get install --no-install-recommends -y \
13+
# dependencies for building Python packages
14+
git \
15+
build-essential \
16+
# psycopg2 dependencies
17+
libpq-dev
18+
19+
# Requirements are installed here to ensure they will be cached.
20+
COPY ./requirements .
21+
22+
# Create application directory to hold DOCX layouts
23+
COPY ./docx_layouts .
24+
25+
# Update pip
26+
RUN python -m pip install --upgrade pip
27+
28+
# Create Python Dependency and Sub-Dependency Wheels.
29+
RUN pip wheel --wheel-dir /usr/src/app/wheels \
30+
-r ${BUILD_ENVIRONMENT}.txt
31+
32+
# Python 'run' stage
33+
FROM python as python-run-stage
34+
35+
ARG BUILD_ENVIRONMENT=production
36+
ARG APP_HOME=/app
37+
38+
ENV PYTHONUNBUFFERED 1
39+
ENV PYTHONDONTWRITEBYTECODE 1
40+
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
41+
42+
# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
43+
ARG DISABLE_AVX=true
44+
45+
# Set the version of llama-cpp-python
46+
ARG LLAMA_VERSION=0.3.14
47+
48+
WORKDIR ${APP_HOME}
49+
50+
RUN addgroup --system django \
51+
&& adduser --system --ingroup django django
52+
53+
RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list
54+
55+
# Install required system dependencies
56+
RUN apt-get update && apt-get install --no-install-recommends -y \
57+
# psycopg2 dependencies
58+
libpq-dev \
59+
# libreoffice for document conversions
60+
default-jre libreoffice libreoffice-java-common ttf-mscorefonts-installer fonts-liberation fonts-liberation2 fonts-crosextra-carlito fonts-crosextra-caladea fonts-dejavu fonts-noto \
61+
# Translations dependencies
62+
gettext \
63+
# cleaning up unused files
64+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
65+
&& rm -rf /var/lib/apt/lists/*
66+
67+
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
68+
# copy python dependency wheels from python-build-stage
69+
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
70+
71+
# use wheels to install python dependencies (excluding llama-cpp-python)
72+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") && rm -rf /wheels/
73+
74+
# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
75+
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
76+
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
77+
else \
78+
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
79+
fi
80+
81+
COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
82+
RUN sed -i 's/\r$//g' /entrypoint
83+
RUN chmod +x /entrypoint
84+
85+
COPY --chown=django:django ./compose/production/django/start /start
86+
RUN sed -i 's/\r$//g' /start
87+
RUN chmod +x /start
88+
89+
COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker
90+
RUN sed -i 's/\r$//g' /start-celeryworker
91+
RUN chmod +x /start-celeryworker
92+
93+
COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat
94+
RUN sed -i 's/\r$//g' /start-celerybeat
95+
RUN chmod +x /start-celerybeat
96+
97+
COPY ./compose/production/django/celery/flower/start /start-flower
98+
RUN sed -i 's/\r$//g' /start-flower
99+
RUN chmod +x /start-flower
100+
101+
# copy application code to WORKDIR
102+
COPY --chown=django:django . ${APP_HOME}
103+
104+
# make django owner of the WORKDIR directory as well.
105+
RUN chown django:django ${APP_HOME}
106+
107+
USER django
108+
109+
ENTRYPOINT ["/entrypoint"]

config/settings/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
2525
# core/
2626
APPS_DIR = ROOT_DIR / "core"
27-
LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2"
28-
MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf"
2927

3028
env = environ.Env()
3129
READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False)
@@ -294,4 +292,9 @@
294292
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
295293
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
296294
# "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
297-
}
295+
}
296+
297+
# LLAMA
298+
LLAMA_ENABLED = env.bool("LLAMA_ENABLED", default=False)
299+
LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2"
300+
MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf"

0 commit comments

Comments
 (0)