Turn any PDF into an Alpaca-style instruction-tuning dataset with GPT.
This project reads a book or document, splits it into overlapping chunks, sends each chunk to GPT, and writes a clean JSON file of {instruction, input, output} examples. The result is ready for supervised fine-tuning of instruction-following models.
| Input | Process | Output |
|---|---|---|
data/book.pdf |
Load → chunk → generate with GPT | output/alpaca.json |
Each source chunk produces 5 instruction / response pairs, grounded in that passage instead of generic trivia.
- PDF ingestion with LangChain
PyPDFLoader - Recursive chunking (1000 characters, 200 overlap) so context is not lost at page boundaries
- GPT-4o-mini via the Metis OpenAI-compatible API
- Alpaca schema used by Stanford Alpaca and most open instruction-tuning pipelines
- Robust JSON cleanup so markdown fences from the model do not break parsing
- Progress bar with
tqdmfor long documents
PDF pages
│
▼
LangChain loader
│
▼
RecursiveCharacterTextSplitter
chunk_size=1000 overlap=200
│
▼
GPT-4o-mini (5 Alpaca examples / chunk)
│
▼
JSON parse + merge
│
▼
output/alpaca.json
Every record follows the Alpaca instruction format:
{
"instruction": "Explain the main claim of this passage.",
"input": "",
"output": "A grounded answer derived from the source chunk."
}| Field | Role |
|---|---|
instruction |
The task the model should perform |
input |
Extra context (often empty) |
output |
The target completion |
This matches the layout used by Stanford Alpaca and is compatible with common SFT loaders (Hugging Face datasets, Axolotl, LLaMA-Factory, Unsloth, and similar tools).
git clone https://github.com/EbiAraz/Build-Dataset-with-GPT.git
cd Build-Dataset-with-GPTpython -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activatepip install -r requirements.txtCopy the example env file and add your Metis key:
copy .env.example .env # Windows
cp .env.example .env # macOS / LinuxMETIS_API_KEY=your_metis_api_key_hereThe client talks to https://api.metisai.ir/openai/v1. Do not commit .env.
Place your source document at:
data/book.pdf
python generate_dataset.pyWhen it finishes you will see page count, chunk count, and dataset size. The file is written to:
output/alpaca.json
Edit generate_dataset.py to match your document and budget:
| Setting | Default | Notes |
|---|---|---|
pdf_path |
data/book.pdf |
Source document |
chunk_size |
1000 |
Larger chunks = more context, higher token cost |
chunk_overlap |
200 |
Keeps sentences from being cut in half |
model |
gpt-4o-mini |
Swap for another Metis-supported chat model |
temperature |
0.3 |
Lower = more faithful to the source text |
| examples per chunk | 5 |
Change the prompt if you want more or fewer |
A 200-page book at these settings typically yields on the order of thousands of instruction pairs. Cost scales with page count × chunks × 5 examples.
.
├── generate_dataset.py # PDF → chunks → GPT → Alpaca JSON
├── requirements.txt
├── .env.example # METIS_API_KEY placeholder
├── data/
│ └── book.pdf # you provide this
└── output/
└── alpaca.json # generated dataset
- Load every page of the PDF as LangChain documents.
- Split into overlapping character chunks so each prompt stays inside a reasonable context window.
- Prompt GPT as a dataset generator: return only JSON, no markdown, no commentary.
- Parse the reply, strip accidental
```jsonfences, andjson.loadsit. - Append successful batches to one list. Failed chunks are logged and skipped so a single bad reply does not abort the run.
- Dump the full list as UTF-8 JSON with
ensure_ascii=False, so non-English text (Persian, Arabic, etc.) is preserved.
- Python 3.10+
- A Metis API key with access to
gpt-4o-mini(or another chat model you set in the script) - A readable PDF at
data/book.pdf
Python packages:
langchain
langchain-community
langchain-text-splitters
pypdf
openai
python-dotenv
tqdm
- Prefer a clean digital PDF over a scanned image-only file (this pipeline does not OCR).
- Keep
temperaturelow if you want answers tightly bound to the book. - After generation, skim
output/alpaca.jsonand drop empty, duplicated, or off-topic rows before training. - For domain-specific fine-tuning, use a single coherent source (one textbook, one manual) rather than a mixed dump of unrelated PDFs.
- If a chunk fails JSON parsing, the script continues; re-run or lower temperature if too many chunks are skipped.
Use this tooling for your own documents. Do not publish copyrighted books or generated datasets derived from them unless you have the right to do so.