Custom Dependencies#

Note

The time to complete this tutorial is approximately 15 minutes.

Some guardrail configurations might require additional Python dependencies because NeMo Guardrails includes only the essential dependencies. You can perform the following steps to add additional dependencies:

  1. Create a requirements.txt file that lists the additional dependencies.

  2. Create a Dockerfile that uses the NeMo Guardrails image as the base and which installs the additional dependencies:

    # The base image is distroless, but has a constraints.txt file that lists the dependencies.
    FROM nvcr.io/nvidia/nemo-microservices/guardrails:25.06 as base
    
    # The builder image is a can install additional dependencies.
    FROM python:3.11-slim as builder
    
    ENV VIRTUAL_ENV=/app/.venv
    ENV PATH="/app/.venv/bin:$PATH"
    
    WORKDIR /app
    
    # Copy the constraints.txt file from the base image.
    COPY --from=base /app/services/guardrails/constraints.txt /etc/constraints.txt
    
    # Install operating system packages.
    RUN apt-get update && apt-get install -y --no-install-recommends \
      curl \
      <additional-dependencies> \
      && rm -rf /var/lib/apt/lists/*
    
    # Create a virtual environment and install the additional dependencies.
    RUN python3 -m venv $VIRTUAL_ENV \
      && pip install --upgrade pip \
      && pip install --no-cache-dir -r requirements.txt -c /etc/constraints.txt
    
    # Create a final image using the original distroless image.
    FROM nvcr.io/nvidia/nemo-microservices/guardrails:25.06 as final
    
    WORKDIR /app/services/guardrails
    
    # Copy the application directory from the builder stage.
    COPY --from=builder /app/ /app/
    
    # Copy the virtual environment from the builder stage.
    COPY --from=builder /app/.venv/lib/python3.11/site-packages/ /app/.venv/lib/python3.11/site-packages/
    
  3. Build a new image:

    $ docker build -t guardrails-with-additional-dependencies .
    
  4. Use the new container to start the microservice:

    $ docker run -d \
      --name nemo-guardrails-ms \
      -p 7331:7331 \
      -e NIM_ENDPOINT_API_KEY="${NVIDIA_API_KEY}" \
      -e NVIDIA_API_KEY="${NVIDIA_API_KEY}" \
      -e DEMO=True \
      guardrails-with-additional-dependencies:latest