This is an automated email from the ASF dual-hosted git repository.

jin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-ai.git


The following commit(s) were added to refs/heads/main by this push:
     new d02ec96  chore(llm): multi-stage building in Dockerfile (#199)
d02ec96 is described below

commit d02ec96ae17ea9fb27012785a5547aa62b498836
Author: imbajin <j...@apache.org>
AuthorDate: Mon Mar 10 19:48:08 2025 +0800

    chore(llm): multi-stage building in Dockerfile (#199)
    
    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
---
 docker/Dockerfile.llm            | 48 +++++++++++++++++++++++++++++++---------
 docker/charts/hg-llm/values.yaml | 41 ++++++++++++++++++++++------------
 docker/docker-compose-llm.yml    | 14 ++++++++----
 hugegraph-llm/pyproject.toml     |  2 +-
 4 files changed, 75 insertions(+), 30 deletions(-)

diff --git a/docker/Dockerfile.llm b/docker/Dockerfile.llm
index db919a8..1d527c6 100644
--- a/docker/Dockerfile.llm
+++ b/docker/Dockerfile.llm
@@ -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 <d...@hugegraph.apache.org>"
-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 <d...@hugegraph.apache.org>"
 
+# 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"]
diff --git a/docker/charts/hg-llm/values.yaml b/docker/charts/hg-llm/values.yaml
index 4683454..3f2a637 100644
--- a/docker/charts/hg-llm/values.yaml
+++ b/docker/charts/hg-llm/values.yaml
@@ -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
@@ -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:
@@ -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: []
diff --git a/docker/docker-compose-llm.yml b/docker/docker-compose-llm.yml
index 884e123..2e0788b 100644
--- a/docker/docker-compose-llm.yml
+++ b/docker/docker-compose-llm.yml
@@ -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
diff --git a/hugegraph-llm/pyproject.toml b/hugegraph-llm/pyproject.toml
index d12054f..3f72aa5 100644
--- a/hugegraph-llm/pyproject.toml
+++ b/hugegraph-llm/pyproject.toml
@@ -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"

Reply via email to