RAG Foundry: A Framework for Enhancing LLMs for Retrieval Augmented Generation

Daniel Fleischer, Moshe Berchansky, Moshe Wasserblat, Peter IzsakarXiv 2024

Abstract

Implementing Retrieval-Augmented Generation (RAG) systems is inherently complex, requiring deep understanding of data, use cases, and intricate design decisions. Additionally, evaluating these systems presents significant challenges, necessitating assessment of both retrieval accuracy and generative quality through a multi-faceted approach. We introduce RAG Foundry, an open-source framework for augmenting large language models for RAG use cases. RAG Foundry integrates data creation, training, inference and evaluation into a single workflow, facilitating the creation of data-augmented datasets for training and evaluating large language models in RAG settings. This integration enables rapid prototyping and experimentation with various RAG techniques, allowing users to easily generate datasets and train RAG models using internal or specialized knowledge sources. We demonstrate the framework effectiveness by augmenting and fine-tuning Llama-3 and Phi-3 models with diverse RAG configurations, showcasing consistent improvements across three knowledge-intensive datasets. Code is released as open-source in https://github.com/IntelLabs/RAGFoundry.

Introduction

While Retrieval-Augmented Generation (RAG) is the go-to solution for reducing LLM hallucinations and providing up-to-date information, implementing it effectively is notoriously difficult. Developers face a "combinatorial explosion" of design choices: which embedding model to use, how to chunk data, how to rank retrieved documents, and how to craft the final prompt.

Furthermore, the field suffers from a reproducibility crisis. Experiments are often fragmented across different scripts and libraries, making it hard to compare techniques fairly. Evaluation is also a dual challenge, requiring researchers to measure both the accuracy of the retrieved information and the quality of the generated text. RAG Foundry addresses this by providing a modular, configuration-driven environment that standardizes these processes.

The RAG Foundry Framework

The core of RAG Foundry is its modular architecture, which is divided into four primary stages, all controlled via YAML configuration files. This unified workflow treats RAG as a holistic process rather than a series of disconnected steps.

The RAG Foundry pipeline architecture, illustrating the flow from Data Augmentation to Evaluation.
  1. Data Creation and Processing: This module creates "context-enhanced" datasets. It uses Global Steps (operations on the whole dataset, like shuffling or filtering) and Local Steps (operations on individual examples, like calling a retriever or formatting a prompt). A key feature is step caching, which prevents redundant computations.
  2. Training: Unlike many RAG tools that focus only on prompting, RAG Foundry facilitates fine-tuning. It integrates with Hugging Face’s TRL (Transformer Reinforcement Learning) and PEFT (Parameter-Efficient Fine-Tuning), specifically supporting LoRA to train models on RAG-specific interactions.
  3. Inference: This module generates predictions using the processed datasets and trained models, keeping the generation logic separate from evaluation to manage compute-heavy tasks efficiently.
  4. Evaluation: It includes a suite of metrics ranging from standard NLP scores (Exact Match, F1, ROUGE) to specialized RAG metrics like RAGAS (which measures Faithfulness and Relevancy) and BERTScore.

Experimental Setup

The authors demonstrated the framework using three knowledge-intensive datasets:

  • TriviaQA: A large-scale reading comprehension dataset.
  • ASQA: A dataset for factoid questions that require long-form, multi-faceted answers.
  • PubmedQA: A biomedical research QA dataset requiring "Yes/No/Maybe" answers based on abstracts.
  • Knowledge Source: For retrieval, they used a Wikipedia corpus processed via a dense retriever.

Key Results and Insights

The researchers tested the framework on Llama-3 (8B) and Phi-3 (3.8B). The results demonstrate that RAG Foundry enables significant performance gains through specialized techniques.

Performance comparison of Phi-3 and Llama-3 models across TriviaQA, ASQA, and PubmedQA datasets using various RAG methods.

Performance Gains

  • TriviaQA: Llama-3 8B’s Exact Match (EM) score jumped from 0.722 (baseline) to 0.916 using RAG-specific fine-tuning (RAG-sft).
  • ASQA: Phi-3 3.8B saw its STR-EM score nearly quadruple, moving from 0.109 (baseline) to 0.386 with CoT-sft.
  • PubmedQA: Both models reached their peak performance using RAG-sft, with Llama-3 hitting an accuracy of 0.770 compared to its 0.560 baseline.

Key Insights

  • Fine-tuning is a Force Multiplier: Simply providing context in a prompt (standard RAG) is often insufficient. Fine-tuning the model on RAG-specific data (RAG-sft) consistently outperformed baseline models and often surpassed standard RAG prompting.
  • The Chain-of-Thought (CoT) Paradox: Implementing CoT in RAG is highly task-dependent. For complex, long-form tasks like ASQA, CoT provided a massive boost. However, for simpler tasks like TriviaQA, CoT sometimes decreased performance, suggesting that the overhead of reasoning can occasionally distract the model from simple fact retrieval.
  • Configuration as Code: By using YAML files to define every step from data loading to evaluation, RAG Foundry ensures that an entire experiment can be shared and replicated with a single file, addressing the reproducibility gap in RAG research.

Conclusion

RAG Foundry provides a robust, open-source framework for the entire RAG lifecycle. While the authors acknowledge that some highly specialized RAG workflows might still require custom Python classes, the framework's modularity significantly lowers the barrier to entry for advanced RAG experimentation. Future plans include expanding the library to support a wider array of RAG techniques (such as reranking) and more diverse evaluation metrics.

Summary updated

BibTeX
@misc{fleischer2024rag,
  author = {Daniel Fleischer and Moshe Berchansky and Moshe Wasserblat and Peter Izsak},
  title = {{RAG Foundry: A Framework for Enhancing LLMs for Retrieval Augmented Generation}},
  year = {2024},
  eprint = {2408.02545},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.02545},
}
Figure