gasolsun nielsr HF Staff commited on
Commit
6f18dcf
·
verified ·
1 Parent(s): f32fc6f

Add pipeline tag and library name (#1)

Browse files

- Add pipeline tag and library name (241220f9c073b4cc1a8948a7b7ca46cdde3148bd)


Co-authored-by: Niels Rogge <nielsr@users.noreply.huggingface.co>

Files changed (1) hide show
  1. README.md +429 -4
README.md CHANGED
@@ -1,9 +1,434 @@
1
  ---
2
- license: apache-2.0
 
3
  datasets:
4
  - gasolsun/DynamicRAG-Eval-Data
5
  language:
6
  - en
7
- base_model:
8
- - meta-llama/Meta-Llama-3-8B
9
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ base_model:
3
+ - meta-llama/Meta-Llama-3-8B
4
  datasets:
5
  - gasolsun/DynamicRAG-Eval-Data
6
  language:
7
  - en
8
+ license: apache-2.0
9
+ pipeline_tag: question-answering
10
+ library_name: transformers
11
+ ---
12
+
13
+ # DynamicRAG: Leveraging Outputs of Large Language Model as Feedback for Dynamic Reranking in Retrieval-Augmented Generation
14
+
15
+ <div style='display:flex; gap: 0.25rem; flex-wrap: wrap; align-items: center;'>
16
+ <a href='LICENCE'>
17
+ <img src='https://img.shields.io/badge/License-Apache%202.0-g.svg'>
18
+ </a>
19
+ <a href='https://arxiv.org/abs/2505.07233'>
20
+ <img src='https://img.shields.io/badge/Paper-PDF-red'>
21
+ </a>
22
+ <a href='https://x.com/SunJiashuo36/status/1922117916788404665'>
23
+ <img src='https://img.shields.io/twitter/url/https/twitter.com/cloudposse.svg?style=social&label=Follow%20%40Us'>
24
+ </a>
25
+ <a href='https://huggingface.co/datasets/gasolsun/DynamicRAG-Eval-Data'>
26
+ <img src='https://img.shields.io/badge/🤗-Eval_Data-9C276A.svg' alt='eval_data'>
27
+ </a>
28
+ <a href='https://huggingface.co/gasolsun/DynamicRAG-7B'>
29
+ <img src='https://img.shields.io/badge/🤗-DynamicRAG--7B-FFD21E.svg' alt='model'>
30
+ </a>
31
+ <a href='https://huggingface.co/gasolsun/DynamicRAG-8B'>
32
+ <img src='https://img.shields.io/badge/🤗-DynamicRAG--8B-FFD21E.svg' alt='model'>
33
+ </a>
34
+ </div>
35
+
36
+ **DynamicRAG** is an innovative framework for Retrieval-Augmented Generation (RAG) that dynamically adjusts both the **order** and **number** of retrieved documents per query. A reinforcement learning (RL) agent serves as the reranker, optimizing document retrieval based on feedback from a **Large Language Model (LLM)**. The training process is divided into two main stages:
37
+
38
+ 1. **Supervised Fine-Tuning (SFT) via Behavior Cloning**:
39
+ - Trains the reranker with expert trajectories.
40
+ - Simplifies the action space and establishes a baseline.
41
+ 2. **Reinforcement Learning (RL) with LLM Feedback**:
42
+ - Uses interactive feedback from the generator.
43
+ - Explores improved trajectories and further optimizes the reranker.
44
+
45
+ ## How to cite
46
+ If you extend or use this work, please cite the [paper](https://arxiv.org/abs/2212.07249) where it was introduced:
47
+
48
+ ```
49
+ @misc{sun2025dynamicragleveragingoutputslarge,
50
+ title={DynamicRAG: Leveraging Outputs of Large Language Model as Feedback for Dynamic Reranking in Retrieval-Augmented Generation},
51
+ author={Jiashuo Sun and Xianrui Zhong and Sizhe Zhou and Jiawei Han},
52
+ year={2025},
53
+ eprint={2505.07233},
54
+ archivePrefix={arXiv},
55
+ primaryClass={cs.CL},
56
+ url={https://arxiv.org/abs/2505.07233},
57
+ }
58
+ ```
59
+
60
+ ## 🔥 Update
61
+ * [2025-05-13]: 🚀 We release the paper: [https://arxiv.org/abs/2505.07233](https://arxiv.org/abs/2505.07233)
62
+ * [2025-05-07]: 🚀 We release the [DynamicRAG-7B](https://huggingface.co/gasolsun/DynamicRAG-7B) and [DynamicRAG-8B](https://huggingface.co/gasolsun/DynamicRAG-8B) and [eval-datas](https://huggingface.co/datasets/gasolsun/DynamicRAG-Eval-Data).
63
+ * [2025-05-05]: 🚀 We release the code for training and evaluation.
64
+
65
+
66
+
67
+ ## Table of Contents
68
+
69
+ - [DynamicRAG Overview](#dynamicrag-overview)
70
+ - [Project Visualizations](#project-visualizations)
71
+ - [📌 Data Processing Pipeline](#-data-processing-pipeline)
72
+ - [🎯 Supervised Fine-Tuning (SFT) Training](#-supervised-fine-tuning-sft-training)
73
+ - [🤖 Interactive Data Collection](#-interactive-data-collection)
74
+ - [📈 Direct Preference Optimization (DPO) Training](#-direct-preference-optimization-dpo-training)
75
+ - [🔍 Inference and Evaluation](#-inference-and-evaluation)
76
+ - [📄 Licensing and Claims](#-licensing-and-claims)
77
+
78
+ ---
79
+
80
+ ## DynamicRAG Overview
81
+
82
+ DynamicRAG adjusts the retrieval process on-the-fly by:
83
+ - Dynamically reordering and selecting the number of documents per query.
84
+ - Leveraging a reranker trained with RL and LLM feedback to improve retrieval quality.
85
+
86
+ ---
87
+
88
+ ## 💡 Preliminaries
89
+ You should install the enviroment by `pip install -r requirements.txt`, and running:
90
+ ```python
91
+ apt-get update
92
+ apt-get install libtiff5
93
+ ```
94
+ Moreover, you need to config the retriever corpus, e.g. official 2018 English Wikipedia embeddings. We use the exact same config with [Self-RAG](https://github.com/AkariAsai/self-rag). You can read their Retriever Setup.
95
+
96
+
97
+ ## 📌 Data Processing Pipeline
98
+ Example: Training LLaMA3-8B with Top-40 Documents
99
+
100
+ ### **1. Prepare BC Data Pipeline**
101
+ #### **Step 1: Retrieve Top-40 Documents**
102
+ Run the retrieval script:
103
+ ```bash
104
+ #!/bin/bash
105
+
106
+ NUM_GPUS=8
107
+ INPUT_FILE="data/rag_training_data.json"
108
+ SPLIT_DIR="data/splits"
109
+
110
+ python split_data.py --input_file $INPUT_FILE --output_dir $SPLIT_DIR --num_splits $NUM_GPUS
111
+
112
+ for GPU_ID in $(seq 0 $((NUM_GPUS - 1))); do
113
+ SPLIT_FILE="${SPLIT_DIR}/split_${GPU_ID}.json"
114
+ OUTPUT_FILE="output/retrieval_split_${GPU_ID}.json"
115
+ log_file="logs/retriever_split_${GPU_ID}.log"
116
+ CUDA_VISIBLE_DEVICES=$GPU_ID python retriever.py \
117
+ --model_name_or_path models/retriever \
118
+ --passages data/psgs_w100.tsv \
119
+ --passages_embeddings "data/wikipedia_embeddings/*" \
120
+ --query $SPLIT_FILE \
121
+ --output_dir $OUTPUT_FILE \
122
+ --n_docs 50 \
123
+ 1>"$log_file" 2>&1 &
124
+
125
+ echo "Started process on GPU $GPU_ID with input $SPLIT_FILE"
126
+ done
127
+
128
+ wait
129
+ echo "All processes completed."
130
+
131
+ ```
132
+
133
+ #### **Step 2: Aggregate Retrieved Data**
134
+ ```bash
135
+ python aggregate.py
136
+ ```
137
+
138
+ #### **Step 3: Rerank Documents**
139
+ ```bash
140
+ python reranker.py --model_name_or_path models/reranker/monot5 \
141
+ --input_file output/retrieval_data.jsonl \
142
+ --output_file output/retrieval_data_rerank.jsonl \
143
+ --device cuda
144
+ ```
145
+ Outputs: `retrieval_data_rerank.jsonl`
146
+
147
+ > 💡 If you running above command slowly, consider running it with multi-gpus like retriever and then combine the results.
148
+
149
+
150
+ #### **Step 4: Compute True/False in Reranking**
151
+ ```bash
152
+ python process_training_data.py
153
+ ```
154
+ Outputs:
155
+ - `retrieval_data_rerank_sequence.json` (for Reranker BC training)
156
+ - `retrieval_data_rerank_normal.json` (for SFT & DPO training)
157
+
158
+ #### **Step 5: Convert Reranker Data for Training**
159
+ ```bash
160
+ python reranker_sequence.py
161
+ ```
162
+ Output: `reranker_bc_data.json` (formatted for **LLaMA-Factory**)
163
+
164
+ #### **Step 6: Split SFT & DPO Data**
165
+ ```bash
166
+ python split_for_sft_dpo.py
167
+ ```
168
+
169
+ #### **Step 7: Construct Generator SFT Data**
170
+ ```bash
171
+ python construct_generator_sft.py
172
+ ```
173
+
174
+ ---
175
+
176
+ ## 🎯 Supervised Fine-Tuning (SFT) Training
177
+ We use **LLaMA-Factory** as the training framework. Install it from [here](https://github.com/hiyouga/LLaMA-Factory).
178
+
179
+ ### **1. Configure `dataset_info.json`**
180
+ Modify `LLaMA-Factory/data/dataset_info.json`:
181
+ ```json
182
+ {
183
+ "generator_sft": {
184
+ "file_name": "generator_sft_training.json",
185
+ "columns": {"prompt": "instruction", "query": "input", "response": "output", "system": "system"}
186
+ },
187
+
188
+ "reranker_bc": {
189
+ "file_name": "reranker_bc_training.json",
190
+ "columns": {"prompt": "instruction", "query": "input", "response": "output", "system": "system"}
191
+ },
192
+
193
+ "alpaca_data": {
194
+ "file_name": "alpaca_data_cleaned_system.json",
195
+ "columns": {"prompt": "instruction", "query": "input", "response": "output", "system": "system"}
196
+ }
197
+ }
198
+ ```
199
+
200
+ ### **2. Train the Model**
201
+ Modify `llama8b.yaml` and run:
202
+ ```bash
203
+ llamafactory-cli train examples/train_full/llama8b.yaml
204
+ ```
205
+ > 🛠️ Requires at least **8 A100-80G GPUs**.
206
+
207
+ ---
208
+
209
+ ## 🤖 Interactive Data Collection
210
+ We use **vLLM** for faster sampling.
211
+
212
+ ### **1. Sample Interaction Trajectories**
213
+ ```bash
214
+ python sampling_dpo_trajectories.py \
215
+ --template template/llama3.jinja \
216
+ --llm-model DynamicRAG_llama3_8b \
217
+ --input-jsonl training_data/training_data_dpo.jsonl \
218
+ --output-json results/training_data_dpo_sampling.json
219
+
220
+ ```
221
+
222
+ ### **2. Collect Rewards for Trajectories**
223
+ ```bash
224
+ ython reward_trajectories.py \
225
+ --input_file results/training_data_dpo_sampling.json \
226
+ --output_file training_data/llama3_8b_output_dpo.jsonl \
227
+ ```
228
+
229
+ ### **3. Construct DPO Training Data**
230
+ ```bash
231
+ python construct_dpo.py
232
+ ```
233
+
234
+ ---
235
+
236
+ ## 📈 Direct Preference Optimization (DPO) Training
237
+
238
+ ### **1. Configure `dataset_info.json`**
239
+ ```json
240
+ {
241
+ "llama3_generator_dpo": {
242
+ "file_name": "llama3_8b_generator_dpo.json",
243
+ "ranking": true,
244
+ "columns": {"prompt": "instruction", "query": "input", "chosen": "chosen", "rejected": "rejected"}
245
+ },
246
+
247
+ "llama3_reranker_dpo": {
248
+ "file_name": "llama3_8b_reranker_dpo.json",
249
+ "ranking": true,
250
+ "columns": {"prompt": "instruction", "query": "input", "chosen": "chosen", "rejected": "rejected"}
251
+ }
252
+ }
253
+ ```
254
+
255
+ ### **2. Train the Model**
256
+ ```bash
257
+ llamafactory-cli train examples/train_full/llama8b_dpo.yaml
258
+ ```
259
+ > 🛠️ Requires at least **8 A100-80G GPUs**.
260
+
261
+ ---
262
+
263
+ ## 🔍 Inference and Evaluation
264
+ We use **vLLM** for efficient inference.
265
+
266
+ ### **1. Run Inference**
267
+ ```bash
268
+ #!/bin/bash
269
+
270
+
271
+ LOG_DIR="eval_logs"
272
+ mkdir -p $LOG_DIR
273
+
274
+ run_inference() {
275
+ local input_file=$1
276
+ local output_file=$2
277
+ local remain_output_file=$3
278
+
279
+ echo "Running inference for $input_file..."
280
+ python inference.py \
281
+ --template template/llama3.jinja \
282
+ --llm-model DynamicRAG_llama3_8b \
283
+ --input-json $input_file \
284
+ --output-json $output_file \
285
+ --remain-output-json $remain_output_file \
286
+ >> $LOG_DIR/$(basename $output_file .json)_log.txt 2>&1
287
+
288
+ sleep 5
289
+ }
290
+
291
+
292
+ run_inference "eval_data/triviaqa.jsonl" \
293
+ "results/llama3_8b_triviaqa.json" \
294
+ "results/llama3_8b_triviaqa_remain.json"
295
+
296
+ run_inference "eval_data/nq.jsonl" \
297
+ "results/llama3_8b_nq.json" \
298
+ "results/llama3_8b_nq_remain.json"
299
+
300
+ run_inference "eval_data/hotpotqa.jsonl" \
301
+ "results/llama3_8b_hotpotqa.json" \
302
+ "results/llama3_8b_hotpotqa_remain.json"
303
+
304
+ run_inference "eval_data/2wikimqa.jsonl" \
305
+ "results/llama3_8b_2wikimqa.json" \
306
+ "results/llama3_8b_2wikimqa_remain.json"
307
+
308
+ run_inference "eval_data/fever.jsonl" \
309
+ "results/llama3_8b_fever.json" \
310
+ "results/llama3_8b_fever_remain.json"
311
+
312
+ run_inference "eval_data/eli5.jsonl" \
313
+ "results/llama3_8b_eli5.json" \
314
+ "results/llama3_8b_eli5_remain.json"
315
+
316
+ run_inference "eval_data/asqa_eval_gtr_top100.jsonl" \
317
+ "results/llama3_8b_asqa.json" \
318
+ "results/llama3_8b_asqa_remain.json"
319
+
320
+ echo "All tasks completed. Logs are available in $LOG_DIR."
321
+
322
+ ```
323
+ Evaluates **7 different benchmarks**.
324
+
325
+ ### **2. Evaluate Performance**
326
+ ```bash
327
+ # install nltk, rouge_score, spacy
328
+ # python -m spacy download en_core_web_sm
329
+
330
+ # for example, when we evaluate nq
331
+ python evaluate.py \\
332
+ --results_file results/llama3_8b_nq.json \\
333
+ --metric match
334
+ ```
335
+
336
+ ### **3. Run DynamicRAG on 500+ Documents**
337
+ ```bash
338
+ #!/bin/bash
339
+
340
+ TEMPLATE="template/llama3.jinja"
341
+ LLM_MODEL="DynamicRAG_llama3_8b"
342
+ INPUT_JSONL="eval_data/nq_top500.jsonl"
343
+ MAX_CONTEXT_WINDOW=40
344
+
345
+ TOPN_VALUES=(50 100 150 200 300 500)
346
+
347
+ for TOPN in "${TOPN_VALUES[@]}"; do
348
+ LOG_FILE="top_logs/llama3_8b_nq_top_${TOPN}.log"
349
+
350
+ python top_inference.py \\
351
+ --template "$TEMPLATE" \\
352
+ --llm-model "$LLM_MODEL" \\
353
+ --input-jsonl "$INPUT_JSONL" \\
354
+ --output-json "results/llama3_8b_top_${TOPN}_nq.json" \\
355
+ --remain-output-json "results/llama3_8b_top_${TOPN}_nq_remain.json" \\
356
+ --max-context-window "$MAX_CONTEXT_WINDOW" \\
357
+ --topn "$TOPN" >> "$LOG_FILE" 2>&1
358
+
359
+ sleep 3
360
+ done
361
+
362
+ ```
363
+
364
+ ---
365
+
366
+
367
+
368
+ ## Project Visualizations
369
+
370
+ Explore the key components and performance of DynamicRAG through the following images:
371
+
372
+ - **Introduction of DynamicRAG:**
373
+
374
+ <div style="text-align: center;">
375
+ <a href="https://imgse.com/i/pEe80tx">
376
+ <img src="https://s21.ax1x.com/2025/02/05/pEe80tx.png" alt="DynamicRAG Intro" style="width:600px; height:auto;" />
377
+ </a>
378
+ </div>
379
+
380
+
381
+ - **Pipeline of DynamicRAG:**
382
+
383
+ <div style="text-align: center;">
384
+ <a href="https://imgse.com/i/pEe86je">
385
+ <img src="https://s21.ax1x.com/2025/02/05/pEe86je.png" alt="DynamicRAG Pipeline" style="width:1000px; height:auto;" />
386
+ </a>
387
+ </div>
388
+
389
+ - **Generator Experiment:**
390
+
391
+ <div style="text-align: center;">
392
+ <a href="https://imgse.com/i/pEe8s1O">
393
+ <img src="https://s21.ax1x.com/2025/02/05/pEe8s1O.png" alt="Generator Experiment" style="width:1000px; height:auto;" />
394
+ </a>
395
+ </div>
396
+
397
+
398
+ - **Reranker Experiment:**
399
+
400
+ <div style="text-align: center;">
401
+ <a href="https://imgse.com/i/pEe8ycD">
402
+ <img src="https://s21.ax1x.com/2025/02/05/pEe8ycD.png" alt="Reranker Experiment" style="width:1000px; height:auto;" />
403
+ </a>
404
+ </div>
405
+
406
+
407
+
408
+ - **Efficiency of DynamicRAG:**
409
+
410
+ <div style="text-align: center;">
411
+ <a href="https://imgse.com/i/pEe8wA1">
412
+ <img src="https://s21.ax1x.com/2025/02/05/pEe8wA1.png" alt="Efficiency" style="width:500px; height:auto;" />
413
+ </a>
414
+ </div>
415
+
416
+ - **Case Study:**
417
+
418
+ <div style="text-align: center;">
419
+ <a href="https://imgse.com/i/pEe8r9K">
420
+ <img src="https://s21.ax1x.com/2025/02/05/pEe8r9K.png" alt="Case Study 1" style="width:600px; height:auto;" />
421
+ </a>
422
+ </div>
423
+ <div style="text-align: center;">
424
+ <a href="https://imgse.com/i/pEe8Bh6">
425
+ <img src="https://s21.ax1x.com/2025/02/05/pEe8Bh6.png" alt="Case Study 2" style="width:600px; height:auto;" />
426
+ </a>
427
+ </div>
428
+
429
+
430
+ ---
431
+
432
+
433
+ ## 📄 Licensing and Claims
434
+ This project is licensed under the Apache 2.0 protocol. The project assumes no legal responsibility for any output generated by the models and will not be held liable for any damages resulting from the use of the provided resources and outputs.