|
| 1 | +# Fine-Tuning a RAG Model with Feast on OpenShift AI |
| 2 | + |
| 3 | +This project provides an end-to-end example of how to fine-tune a Retrieval-Augmented Generation (RAG) model on **OpenShift AI**. It uses the **Feast** (Feature Store) for efficient retrieval of context and the **Kubeflow Training SDK** to orchestrate the distributed fine-tuning job on the cluster. |
| 4 | + |
| 5 | +The core idea is to enhance a generator model (like BART) by providing it with relevant documents retrieved from a knowledge base at runtime. This notebook handles the entire lifecycle: ingesting data into the feature store, fine-tuning the RAG model on synthetically generated Q&A pairs, and testing the final artifact. |
| 6 | + |
| 7 | +*** |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Before you begin, ensure you have the following setup: |
| 12 | + |
| 13 | +* An OpenShift cluster with OpenShift AI (RHOAI) 2.20+ installed: |
| 14 | + * The `dashboard`, `trainingoperator` and `workbenches` components enabled. |
| 15 | +* Workbench with medium size container, 1 NVIDIA GPU / 1 AMD GPU accelerator, and cluster storage of 200GB. |
| 16 | +* Sufficient worker nodes for your configuration(s) with NVIDIA GPUs (Ampere-based or newer recommended) or AMD GPUs depending on your environment. |
| 17 | +* A dynamic storage provisioner supporting RWX PVC provisioning. |
| 18 | +* A standalone Milvus deployment. See example [here](https://github.com/rh-aiservices-bu/llm-on-openshift/tree/main/vector-databases/milvus#deployment). |
| 19 | + |
| 20 | +*** |
| 21 | + |
| 22 | +## Workbench Setup |
| 23 | + |
| 24 | +You must run this notebook from within an OpenShift AI Workbench. Follow these steps to create one: |
| 25 | + |
| 26 | +* Access the OpenShift AI dashboard: |
| 27 | +* Log in, then go to _Data Science Projects_ and create a project. |
| 28 | +* Once the project is created, click on _Create a workbench_. |
| 29 | +* Then create a workbench with a preferred name and with the following settings: |
| 30 | + * Select the `PyTorch` (or the `ROCm-PyTorch`) workbench image with the recommended version. |
| 31 | + * Select the `Medium` as the deployment container size. |
| 32 | + * Add one NVIDIA / AMD accelerator (GPU) depending on environment. |
| 33 | + * Create a storage that'll be shared between the workbench and the fine-tuning runs. |
| 34 | + Make sure it uses a storage class with RWX capability and give it enough capacity according to the size of the model you want to fine-tune. |
| 35 | + > [!NOTE] |
| 36 | + > You can attach an existing shared storage if you already have one instead. |
| 37 | + * Review the storage configuration and click "Create workbench" |
| 38 | +* From the "Workbenches" page, click the icon  on the workbench you've just created once it becomes ready. |
| 39 | +* From the workbench, clone this repository, i.e., `https://github.com/opendatahub-io/distributed-workloads.git` |
| 40 | +* Navigate to the `distributed-workloads/examples/kfto-sft-feast-rag` directory and open the `sft_feast_rag_model` notebook |
| 41 | + |
| 42 | +You can now proceed with the instructions from the notebook. |
| 43 | +*** |
| 44 | + |
| 45 | +## Workflow Overview |
| 46 | + |
| 47 | +The notebook is structured to guide you through the following key stages: |
| 48 | + |
| 49 | +* **feature_repo/feature_store.yaml** |
| 50 | + This is the core configuration file for the RAG project's feature store, configuring a Milvus online store on a local provider. |
| 51 | + * In order to configure Milvus you should: |
| 52 | + - Update `feature_store.yaml` with your Milvus connection details: |
| 53 | + - host |
| 54 | + - port (default: 19530) |
| 55 | + - credentials (if required) |
| 56 | +* **Environment Setup**: Installs all necessary Python libraries, including the Kubeflow Training SDK, Feast, and Hugging Face Transformers. |
| 57 | +* **Data Ingestion with Feast**: |
| 58 | + * Loads the `facebook/wiki_dpr` dataset. |
| 59 | + * Chunks the documents into smaller, manageable passages. |
| 60 | + * Generates vector embeddings for each passage using a `sentence-transformers` model. |
| 61 | + * Initializes a Feast feature store and ingests the passages and their embeddings into the online store. |
| 62 | +* **Distributed Training**: |
| 63 | + * The core training logic is defined in a `main` function. |
| 64 | + * It defines a `RagSequenceForGeneration` model, combining a question-encoder with a generator model. |
| 65 | + * It uses a custom `FeastRAGRetriever` to connect the RAG model to the Feast feature store. |
| 66 | + * The notebook uses the Kubeflow `TrainingClient` to submit this `main` function as a distributed `PyTorchJob` to the OpenShift cluster. |
| 67 | +* **Monitoring**: You can monitor the job's progress directly through its logs and visualize metrics using the integrated TensorBoard dashboard. |
| 68 | +* **Inference and Testing**: After the training job is complete, the final, fine-tuned RAG model is loaded from shared storage for testing. |
| 69 | + |
| 70 | +*** |
0 commit comments