Skip to content

Commit

Permalink
chore(llm): multi-stage building in Dockerfile (#199)
Browse files Browse the repository at this point in the history
follow #195

![image](https://github.com/user-attachments/assets/ca4c8457-3999-43a9-9453-14cb0448840a)



After the image uploaded, users could use the following cmd like:
```bash
docker run -d --name rag -p 8001:8001 \
  -v /path/to/.env:/home/work/hugegraph-llm/.env \
  -v /path/to/resources:/home/work/hugegraph-llm/src/hugegraph_llm/resources \
  hugegraph/graphrag:1.5.0
```

or configs in docker-compose.yml
```yaml
    volumes:
      # Mount local '.env' file, could use ${ENV_FILE_PATH:-/dev/null} to avoid error
      - /path/to/.env:/home/work/hugegraph-llm/.env
      # Mount local resources file
      - /path/to/resources:/home/work/hugegraph-llm/src/hugegraph_llm/resources
```

or volume/configmap/pvc in k8s

Build/Test the image in root
```bash
# Build Image
docker build -f docker/Dockerfile.llm -t graphrag .

# Test/Run container
docker run -it --name rag -p 8001:8001 graphrag  bash
```

> [!NOTE]  
> Currently we store the vector data in local by `faiss`, should replace it as a **separate processes/service** make rag services stateless)
> Or the graph database supports small-scale vector indexing itself
  • Loading branch information
imbajin authored Mar 10, 2025
1 parent a316258 commit d02ec96
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
48 changes: 37 additions & 11 deletions docker/Dockerfile.llm
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
# TODO: we could use 'uv' to replace the poetry + python image
FROM python:3.10.16-bookworm
# TODO: we could use 'uv' to replace the poetry + python image (Also use the slime image)
# Stage 1: Build stage (Isolating the build env)
FROM python:3.10.16-bookworm AS builder

LABEL maintainer="HugeGraph Docker Maintainers <[email protected]>"
RUN pip install --no-cache-dir poetry
WORKDIR /build/

WORKDIR /home/work/
# 1.1 Install poetry
RUN pip install --no-cache-dir poetry

COPY --chown=work:work hugegraph-python-client/ ./hugegraph-python-client/
COPY --chown=work:work hugegraph-llm/ ./hugegraph-llm/
# 1.2 Copy source code
COPY hugegraph-python-client/ ./hugegraph-python-client/
COPY hugegraph-llm/ ./hugegraph-llm/

RUN cd /home/work/hugegraph-llm && \
# 1.3 Install dependencies
RUN cd /build/hugegraph-llm && \
poetry config virtualenvs.in-project true && \
poetry lock && \
poetry install --no-interaction --no-ansi --verbose
poetry install --no-interaction --no-ansi --verbose && \
.venv/bin/pip install ../hugegraph-python-client && \
poetry build

# Stage 2: Runtime stage
FROM python:3.10.16-slim-bookworm
LABEL maintainer="HugeGraph Docker Maintainers <[email protected]>"

# Create non-root user & install 'curl' for healthcheck
RUN useradd -m -s /bin/bash work && \
apt-get update && \
apt-get install -y --no-install-recommends curl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

WORKDIR /home/work/

# Copy only the built packages and virtual environment from the builder stage
COPY --from=builder --chown=work:work /build/hugegraph-llm/.venv /home/work/hugegraph-llm/.venv
COPY --from=builder --chown=work:work /build/hugegraph-llm/src /home/work/hugegraph-llm/src

USER work
ENV PATH="/home/work/hugegraph-llm/.venv/bin:$PATH"

WORKDIR /home/work/hugegraph-llm
WORKDIR /home/work/hugegraph-llm/src
VOLUME ["/home/work/hugegraph-llm/src/hugegraph_llm/resources"]
EXPOSE 8001

CMD [ "python", "-m", "hugegraph_llm.demo.rag_demo.app", "--port", "8001" ]
HEALTHCHECK --interval=60s --timeout=10s --start-period=5s --retries=3 CMD curl -f http://localhost:8001/ || exit 1

CMD ["python", "-m", "hugegraph_llm.demo.rag_demo.app", "--host", "0.0.0.0", "--port", "8001"]
41 changes: 27 additions & 14 deletions docker/charts/hg-llm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ imagePullSecrets: []
nameOverride: ""
fullnameOverride: "hg-llm-service"

# TODO: use pvc to store vector & graph-backup data in "src/hugegraph_llm/resources/"
# Replace the default values with the user-configs from the configmap
#volumes:
# - name: env-config
# configMap:
# name: hugegraph-llm-env
# optional: true
# - name: prompt-config
# configMap:
# name: hugegraph-llm-prompt-config
# optional: true

# Volume mounts for the containers
# use 'kubectl create configmap hugegraph-llm-env --from-file=/path/to/.env' &
# 'kubectl create configmap hugegraph-llm-prompt-config --from-file=/path/to/config_prompt.yaml' to create configmap
#volumeMounts:
# - name: env-config
# mountPath: "/home/work/hugegraph-llm/.env"
# subPath: ".env"
# readOnly: true
# - name: prompt-config
# mountPath: "/home/work/hugegraph-llm/src/hugegraph_llm/resources/demo/config_prompt.yaml"
# subPath: "config_prompt.yaml"
# readOnly: true


# This section builds out the service account more information can be found here: https://kubernetes.io/docs/concepts/security/service-accounts/
serviceAccount:
# Specifies whether a service account should be created
Expand Down Expand Up @@ -56,7 +82,7 @@ service:
# This sets the ports more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/#field-spec-ports
port: 8080
nodePort: 8039
targetPort: 8080
targetPort: 8001

# This block is for setting up the ingress for more information can be found here: https://kubernetes.io/docs/concepts/services-networking/ingress/
ingress:
Expand Down Expand Up @@ -106,19 +132,6 @@ autoscaling:
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80

# Additional volumes on the output Deployment definition.
volumes: []
# - name: foo
# secret:
# secretName: mysecret
# optional: false

# Additional volumeMounts on the output Deployment definition.
volumeMounts: []
# - name: foo
# mountPath: "/etc/foo"
# readOnly: true

nodeSelector: {}

tolerations: []
Expand Down
14 changes: 10 additions & 4 deletions docker/docker-compose-llm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ services:
build:
context: ..
dockerfile: ./docker/Dockerfile.llm
container_name: hugegraph-llm-rag
restart: always
ports:
- "8001:8001"
container_name: hugegraph-llm-rag
restart: always
ports:
- "8001:8001"
# If you want to mount local configs to the container, uncomment the following lines
#volumes:
# Mount local '.env' file, could use ${ENV_FILE_PATH:-/dev/null} to avoid error
#- /path/to/.env:/home/work/hugegraph-llm/.env
# Mount local resources file
#- /path/to/resources:/home/work/hugegraph-llm/src/hugegraph_llm/resources
2 changes: 1 addition & 1 deletion hugegraph-llm/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ qianfan = "~0.3.18"
retry = "~0.9.2"
tiktoken = ">=0.7.0"
nltk = "~3.9.1"
gradio = ">=5.0.0"
gradio = ">5.0.0"
jieba = ">=0.42.1"
numpy = "~1.24.4"
python-docx = "~1.1.2"
Expand Down

0 comments on commit d02ec96

Please sign in to comment.