NVIDIA Workbench Example Hybrid RAG

repository·main·Indexed 18 days ago

https://github.com/nvidia/workbench-example-hybrid-rag

An NVIDIA AI Workbench project featuring a customizable Gradio-based chat application for testing Retrieval Augmented Generation (RAG). It supports hybrid inference modes including local Hugging Face Text Generation Inference (TGI), cloud-based NVIDIA API Catalog endpoints, and local or remote NVIDIA NIM microservices. The project includes a FastAPI-based chain server for document uploading, searching, and streaming generation.

Tokens
3.4K
Snippets
5
Records
15
Agent score
63%

What's inside nvidia-workbench-example-hybrid-rag

  1. Project Overview: Hybrid RAG Application

    main

    This project is an NVIDIA AI Workbench project designed for developing Retrieval Augmented Generation (RAG) applications using a customizable Gradio Chat interface. It allows developers to embed documents into a local vector database and perform inference using three distinct modes:

    1. Local Inference: Running models locally via a Hugging Face Text Generation Inference (TGI) server. Supports 4-bit, 8-bit, and no quantization.
    2. Cloud Inference: Using NVIDIA inference endpoints via the NVIDIA API Catalog (build.nvidia.com).
    3. Microservices (NIMs): Using NVIDIA NIMs (NVIDIA Inference Microservices) runnable via docker/podman compose within AI Workbench.

    The application is designed to work interchangeably with these endpoints, making it easy to test RAG workflows across different hosting environments.

  2. Quickstart: Using Cloud Endpoints via NVIDIA API Catalog

    main

    This guide demonstrates how to run RAG using NVIDIA Inference Endpoints hosted on the NVIDIA API Catalog.

    Prerequisites

    • An NGC account to generate an NVIDIA API Key.
    • A valid NVIDIA_API_KEY obtained from an NVIDIA API catalog model card.

    Setup Steps

    1. Install and Open: Install NVIDIA AI Workbench and open it.
    2. Clone Project: In AI Workbench, click Clone Project and enter the repository URL. Wait for the environment build to complete.
    3. Configure Secrets: On the Project Dashboard, resolve the NVIDIA_API_KEY warning by inputting your key.
    4. Launch App: Select Open Chat in the top right to open the Gradio app in your browser.

    Using the Chat App

    1. Initialize Backend: Click Set up RAG Backend in the Gradio UI.
    2. Select Mode: Choose the Cloud option, select a model family and name, and submit a query.
    3. Upload Documents:
      • Click Upload Documents Here in the right-hand panel.
      • Select Click to Upload and choose your text files.
      • Once uploaded, the Toggle to Use Vector Database next to the text input will activate.
    4. Query: Submit questions to query your uploaded documents.
  3. Tutorial 2: Using a Remote NIM microservice

    main

    Use this mode to offload inference to a remote system running an NVIDIA NIM (NVIDIA Inference Microservice) or any service supporting the OpenAI API Specification (e.g., Ollama).

    Prerequisites

    • Access to NVIDIA NIM.
    • The IP address, Port (default is 8000), and model name of the remote service.

    Setup Steps

    1. Open the Chat UI via the Open Chat button.
    2. Click Set up RAG Backend.
    3. Under Inference Settings > Inference Mode, select Self-hosted Microservice.
    4. Input the remote IP address, Port, and model name (default is meta/llama-3.1-8b-instruct).
    5. Start chatting.

    Using RAG

    1. In the Chat UI, select Upload Documents Here and upload text files.
    2. Once uploaded, the Toggle to Use Vector Database will turn on by default.
    3. Query your documents.
  4. Tutorial 3: Using a Local NIM Microservice

    main

    Use this mode to run an NVIDIA NIM container locally on your system using AI Workbench Compose.

    Setup Steps

    1. On the Project Dashboard, select Start under the Compose section to spin up the llama-3.1-8b-instruct NIM container.
    2. Monitor progress via Output > Compose. Wait for the log: Uvicorn running on http://0.0.0.0:8000.
    3. Open the Chat UI via the Open Chat button.
    4. Click Set up RAG Backend.
    5. Under Inference Settings > Inference Mode, select Self-hosted Microservice.
    6. Verify/adjust the Microservice Host, Port, and Model Name (defaults are usually correct for the local compose setup).
    7. Start chatting.

    Using RAG

    1. In the Chat UI, select Upload Documents Here and upload text files.
    2. Once uploaded, the Toggle to Use Vector Database will turn on by default.
    3. Query your documents.
  5. Customize the Gradio Chat UI

    main

    The Chat UI is built with Gradio and can be customized using JupyterLab or VSCode within AI Workbench.

    Customization Steps

    1. Navigate to Environment > Project Container > Apps and select Open JupyterLab.
    2. Locate the code/chatui/ directory.
    3. Edit the frontend files to modify the UI.
    4. To apply changes, stop the Chat UI and restart it.

    You can also use these tools to customize backend components like custom chains, backend servers, scripts, or configurations.

  6. Tutorial 1: Using a local GPU for inference

    main

    Use this mode to run models directly on your local hardware. This requires a GPU with sufficient VRAM depending on the model size and quantization level.

    Hardware Requirements

    vRAMSystem RAMDisk StorageRecommended Model Size & Quantization
    >=12 GB32 GB40 GB7B & int4
    >=24 GB64 GB40 GB7B & int8
    >=40 GB64 GB40 GB7B & none

    Configuration for Gated Models

    If you want to use gated models (e.g., Llama-3-8B-Instruct, Mistral-7B-Instruct-v0.1), you must provide a Hugging Face API token:

    1. Stop the project container in the Project Dashboard.
    2. Navigate to Environment > Project Container > Variables > Add.
    3. Add a new variable:
      • Name: HUGGING_FACE_HUB_TOKEN
      • Value: Your Hugging Face API Key
      • Mark as: Sensitive
    4. Restart the Chat application.

    Setup Steps

    1. Open the Chat UI via the Open Chat button.
    2. Click Set up RAG Backend (one-time initialization).
    3. Under Inference Settings > Inference Mode, select Local System.
    4. Select a model from the dropdown or input a custom Hugging Face model path.
    5. Select a quantization level (supported: full, 8-bit, and 4-bit bitsandbytes).
    6. Click Load Model to download/cache the weights.
    7. Click Start Server to begin inference.

    Using RAG

    1. In the Chat UI, select Upload Documents Here and upload text files.
    2. Once uploaded, the Toggle to Use Vector Database will turn on by default.
    3. Query your documents.
    Variable Name: HUGGING_FACE_HUB_TOKEN
    Variable Value: (Your HF API Key)
  7. Use the Chain Server API for RAG

    main
    The chain_server provides a FastAPI-based interface for performing Hybrid RAG (Retrieval Augmented Generation) tasks. It supports uploading documents to a vector store, searching through ingested documents, and generating answers using either a knowledge base (RAG) or a standard LLM chain. All generation endpoints return a StreamingResponse with text/event-stream media type.
  8. Troubleshoot AI Workbench project setup and execution

    main

    If you encounter issues while using this Hybrid RAG example in AI Workbench, follow these steps:

    Cloning the Repository

    1. Open AI Workbench.
    2. Select a location (e.g., Local).
    3. Click Clone Existing Project (if first project) or Clone Project (top right).
    4. Enter the repository URL and click Clone.

    Monitoring Container Build

    After cloning, the container build process may take several minutes.

    • Check the Build Status widget at the very bottom of the Workbench window.
    • Expand the widget to see real-time build output.
    • Wait until the widget displays Build Ready before proceeding.

    Running the Chat Application

    Once the build is complete (Build Ready), click the green Open Chat button located at the top right of the interface.

    Debugging the Chat Application

    If the Chat application fails or behaves unexpectedly:

    1. Locate the Output widget at the bottom left of the AI Workbench window.
    2. Click to expand it.
    3. Use the dropdown to navigate to Applications > Chat to view real-time debug messages.

    Customizing the Project

    To modify the code or configuration:

    • Ensure the container is built.
    • Click the dropdown next to the Open Chat button at the top right.
    • Select JupyterLab to start editing. You can also configure VSCode support via the AI Workbench documentation.
  9. Configure the local-nim service in compose.yaml

    main

    The compose.yaml file defines the deployment configuration for running a local NVIDIA NIM (NVIDIA Inference Microservice) container. To use this configuration, ensure you have the NVIDIA Container Toolkit installed and an NVIDIA_API_KEY set in your environment.

    Key configuration details:

    • Image: Uses nvcr.io/nim/meta/llama-3.1-8b-instruct:latest.
    • Runtime: Must be set to nvidia to enable GPU acceleration.
    • GPU Reservation: Requires a reservation for 1 device with gpu capabilities.
    • Ports: Maps host port 8000 to container port 8000.
    • Volumes: Binds /tmp on the host to /opt/nim/.cache/ in the container for caching.
    • Environment Variables: Requires NGC_API_KEY, which is sourced from the host's NVIDIA_API_KEY environment variable.
    services:
      local-nim:
        image: nvcr.io/nim/meta/llama-3.1-8b-instruct:latest
        runtime: nvidia
        deploy:
          resources:
            reservations:
              devices:
                - driver: nvidia
                  count: 1
                  capabilities: [ gpu ]
        ports:
          - "8000:8000"
        volumes:
          - type: bind
            source: /tmp
            target: /opt/nim/.cache/
        environment:
          - NGC_API_KEY=${NVIDIA_API_KEY:?Error NVIDIA_API_KEY not set}
    
    networks:
      default:
        name: hybrid-rag
  10. Supported Models by Inference Mode

    main

    The application supports a wide variety of models across Local (TGI), Cloud (NVIDIA API Catalog), and Microservices (NVIDIA NIMs) modes.

    • Default NIM: The project provides a sample flow for the meta/llama-3.1-8b-instruct NIM microservice running locally via docker/podman compose.
    • Local (TGI): Supports models like Llama3-ChatQA-1.5-8B, Mistral-7B-Instruct-v0.2, and Phi-3-Mini-128k-Instruct (some may require gated access).
    • Cloud: Supports a broad range including Nemotron-4-340B-Instruct, Mistral-NeMo 12B Instruct, and Llama-3.1-405B-Instruct.
    • Microservices (NIMs): Supports Llama-3.1-8B-Instruct (default) and many others marked with * in the compatibility table.