A Streamlit application for generating Retrieval Augmented Fine-Tuning (RAFT) training data from PDF documents. Based on the RAFT methodology from UC Berkeley's Gorilla project.
This tool converts PDF documents into high-quality training data for fine-tuning language models with Retrieval Augmented Generation capabilities. The generated data includes question-answer pairs with chain-of-thought reasoning, along with oracle and distractor documents to teach models how to distinguish relevant from irrelevant information.
- PDF Processing: Converts PDF pages to images and extracts markdown content using GPT-4 Vision
- Document Chunking: Splits documents into meaningful chunks using markdown headers and recursive character splitting
- QA Pair Generation: Creates questions and chain-of-thought answers for each document chunk
- Distractor Documents: Includes irrelevant document chunks to train models on context discrimination
- Dataset Splitting: Automatically splits generated data into training, validation, and test sets
- Multiple Export Formats: Supports JSONL (chat format and full data) and CSV exports
- Configurable Parameters: Adjust questions per chunk, distractor count, oracle probability, temperature, and dataset splits
- Python 3.8 or higher
- OpenAI API key
- Poppler utilities (for PDF to image conversion)
macOS:
brew install popplerUbuntu/Debian:
apt-get install poppler-utilsWindows: Download and install from poppler-windows
pip install -r requirements.txt- Start the Streamlit application:
streamlit run streamlit_app.py-
Enter your OpenAI API key in the sidebar
-
Configure the generation parameters:
- Vision Model: Select model for PDF to markdown conversion (GPT-4o or GPT-4o-mini)
- QA Model: Select model for question-answer generation
- Questions per Chunk: Number of questions to generate per document chunk
- Distractor Documents: Number of irrelevant documents to include with each question
- Oracle Probability: Probability of including the relevant document in context
- Temperature: Creativity level for question generation
- Dataset Split: Configure training/validation/test split percentages
-
Upload a PDF file and click "Process PDF and Generate Training Data"
-
Review the generated datasets and download in your preferred format
datageneration/
├── streamlit_app.py # Main entry point
├── raft_datagen.py # Core RAFT data generation logic
├── data_generation.py # Question and answer generation functions
├── pdf_processor.py # PDF to image and markdown conversion
├── text_processing.py # Text cleaning and document chunking
├── requirements.txt # Python dependencies
└── packages.txt # System dependencies
The tool generates datasets with the following columns:
id: Unique identifier for the data pointtype: Type of the data (typically "general")question: The generated questioncontext: Collection of documents including oracle and distractor documentsoracle_context: The original document chunk containing answer informationcot_answer: Chain-of-thought answer generated from the oracle contextinstruction: Formatted input combining context documents with the question
- JSONL (Chat Format): Messages format suitable for OpenAI fine-tuning API
- JSONL (Full Data): Complete data structure with all fields
- CSV: Tabular format for analysis and inspection
This implementation follows the RAFT (Retrieval Augmented Fine-Tuning) approach described in the paper "RAFT: Adapting Language Model to Domain Specific RAG" by Zhang et al. The key principles:
- Train models to answer questions using provided context
- Include distractor documents to teach context discrimination
- Vary oracle document inclusion to teach when information is missing
- Generate chain-of-thought reasoning for improved answer quality
Customize the system prompts for question and answer generation in the sidebar to adapt to your specific domain or requirements.
Document chunking uses:
- Markdown header splitting (H1, H2)
- Recursive character splitting with 1024 token chunks and 50 token overlap
- Minimum content threshold filtering
MIT License