REARANK: Reasoning Re-ranking Agent via Reinforcement Learning
Paper • 2505.20046 • Published • 18
query stringclasses 177
values | hits listlengths 20 20 | data_source stringclasses 1
value | prompt listlengths 44 44 | ability stringclasses 1
value | reward_model dict | extra_info dict | metrics dict | best_metrics dict | new_rank sequencelengths 20 20 |
|---|---|---|---|---|---|---|---|---|---|
what does prenatal care include | [
{
"content": "What is a prenatal visit? Prenatal care is the medical care you receive during pregnancy. Prenatal care visits start early on in your pregnancy and continue regularly until you deliver the baby. They typically include a physical exam, a weight check, and various tests.",
"docid": "msmarco_pass... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 0,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.03762,
"MAP@5": 0.02234,
"NDCG@1": 0.66667,
"NDCG@10": 0.4473,
"NDCG@5": 0.47532,
"Recall@1": 0.00694,
"Recall@10": 0.04861,
"Recall@5": 0.02778
} | {
"MAP@1": 0.00694,
"MAP@10": 0.05556,
"MAP@5": 0.03472,
"NDCG@1": 1,
"NDCG@10": 0.6303,
"NDCG@5": 0.77972,
"Recall@1": 0.00694,
"Recall@10": 0.05556,
"Recall@5": 0.03472
} | [
15,
0,
2,
4,
5,
8,
9,
3,
1,
6,
7,
10,
11,
12,
13,
14,
16,
17,
18,
19
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 1,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.02827,
"MAP@5": 0.02083,
"NDCG@1": 0.33333,
"NDCG@10": 0.24766,
"NDCG@5": 0.24091,
"Recall@1": 0.00694,
"Recall@10": 0.03472,
"Recall@5": 0.02083
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.52126,
"NDCG@5": 0.62293,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
6,
9,
18,
19,
0,
1,
2,
10,
14,
17,
3,
4,
5,
7,
8,
11,
12,
13,
15,
16
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 2,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.05163,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.44185,
"NDCG@5": 0.53881,
"Recall@1": 0.00694,
"Recall@10": 0.05556,
"Recall@5": 0.03472
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 1,
"NDCG@10": 0.6736,
"NDCG@5": 0.77972,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
11,
0,
3,
4,
6,
10,
15,
1,
2,
8,
9,
12,
14,
16,
17,
19,
5,
7,
13,
18
] |
what does prenatal care include | [
{
"content": "How much does prenatal care cost? On average, the total prenatal visits will cost anywhere from $1,700 to $3,000 for the entire nine months during the pregnancy. This is without insurance and doesn’t include the baby delivery.",
"docid": "msmarco_passage_49_610467021",
"qid": 646091,
"... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 3,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.01956,
"MAP@5": 0.01678,
"NDCG@1": 0.33333,
"NDCG@10": 0.23113,
"NDCG@5": 0.32349,
"Recall@1": 0.00694,
"Recall@10": 0.02778,
"Recall@5": 0.02083
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 1,
"NDCG@10": 0.62301,
"NDCG@5": 0.77972,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
10,
2,
3,
14,
15,
0,
9,
11,
13,
18,
1,
4,
5,
6,
7,
8,
12,
16,
17,
19
] |
what does prenatal care include | [
{
"content": "1. Understand the responsibilities. Before you can know if prenatal nursing is the right profession for you, it is important to understand exactly what a prenatal nurse does. Prenatal nurses provide care to expectant mothers, new mothers, and their babies. Prenatal nurses are also sometimes referr... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 4,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.01667,
"MAP@5": 0.01157,
"NDCG@1": 0.33333,
"NDCG@10": 0.23331,
"NDCG@5": 0.22611,
"Recall@1": 0.00694,
"Recall@10": 0.02778,
"Recall@5": 0.01389
} | {
"MAP@1": 0.00694,
"MAP@10": 0.04861,
"MAP@5": 0.03472,
"NDCG@1": 1,
"NDCG@10": 0.58271,
"NDCG@5": 0.77972,
"Recall@1": 0.00694,
"Recall@10": 0.04861,
"Recall@5": 0.03472
} | [
13,
2,
8,
9,
12,
17,
0,
1,
3,
4,
5,
6,
7,
10,
11,
14,
15,
16,
18,
19
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 5,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.03675,
"MAP@5": 0.02778,
"NDCG@1": 0.66667,
"NDCG@10": 0.42003,
"NDCG@5": 0.50787,
"Recall@1": 0.00694,
"Recall@10": 0.04167,
"Recall@5": 0.02778
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.62337,
"NDCG@5": 0.66667,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
0,
2,
3,
7,
8,
14,
17,
18,
1,
12,
13,
4,
5,
6,
9,
10,
11,
15,
16,
19
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 6,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.02932,
"MAP@5": 0.02083,
"NDCG@1": 0.66667,
"NDCG@10": 0.34073,
"NDCG@5": 0.41049,
"Recall@1": 0.00694,
"Recall@10": 0.03472,
"Recall@5": 0.02083
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.54964,
"NDCG@5": 0.66667,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
0,
2,
5,
16,
18,
1,
8,
10,
17,
19,
3,
4,
6,
7,
9,
11,
12,
13,
14,
15
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 7,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.03459,
"MAP@5": 0.02083,
"NDCG@1": 0.66667,
"NDCG@10": 0.40892,
"NDCG@5": 0.48182,
"Recall@1": 0.00694,
"Recall@10": 0.04167,
"Recall@5": 0.02083
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.57578,
"NDCG@5": 0.66667,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
0,
1,
2,
6,
15,
16,
5,
9,
10,
17,
18,
3,
4,
7,
8,
11,
12,
13,
14,
19
] |
what does prenatal care include | [
{
"content": "What services does MPE cover? You can get outpatient services like prenatal checkups, doctor visits, lab tests, prenatal vitamins, medicine, specialty medical care, eye care, dental care, emergency room care, mental health and substance abuse services, transportation to get medical care and other ... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 8,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.03813,
"MAP@5": 0.02639,
"NDCG@1": 0.33333,
"NDCG@10": 0.28159,
"NDCG@5": 0.35597,
"Recall@1": 0.00694,
"Recall@10": 0.04167,
"Recall@5": 0.02778
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 0.66667,
"NDCG@10": 0.48967,
"NDCG@5": 0.57424,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
1,
17,
19,
0,
2,
4,
5,
6,
11,
16,
3,
7,
8,
9,
10,
12,
13,
14,
15,
18
] |
what does prenatal care include | [
{
"content": "Pregnancy and prenatal care go hand in hand. During the first trimester, prenatal care includes blood tests, a physical exam, conversations about lifestyle and more. Prenatal care is an important part of a healthy pregnancy. Whether you choose a family physician, obstetrician, midwife or group pre... | TREC-DL-TOP100 | [
{
"content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer.",
"role": "system"
},
{
"content": "I will provide you with passages, e... | rerank | {
"ground_truth": "",
"style": "rule"
} | {
"index": 9,
"split": "train"
} | {
"MAP@1": 0.00694,
"MAP@10": 0.02866,
"MAP@5": 0.02083,
"NDCG@1": 0.33333,
"NDCG@10": 0.2957,
"NDCG@5": 0.31224,
"Recall@1": 0.00694,
"Recall@10": 0.03472,
"Recall@5": 0.02083
} | {
"MAP@1": 0.00694,
"MAP@10": 0.06944,
"MAP@5": 0.03472,
"NDCG@1": 1,
"NDCG@10": 0.71988,
"NDCG@5": 0.85105,
"Recall@1": 0.00694,
"Recall@10": 0.06944,
"Recall@5": 0.03472
} | [
14,
15,
1,
6,
8,
10,
12,
0,
2,
11,
18,
3,
4,
5,
7,
9,
13,
16,
17,
19
] |
Reranking dataset built from only 179 annotated queries with random sampling technique to increase final sample amount. This dataset is used to train Rearank model in the paper - REARANK: Reasoning Re-ranking Agent via Reinforcement Learning
Below are the data generation code
# Preprocess dataset for countdown task - given a target number and N numbers, generate equations to reach target
import re
import os
from datasets import Dataset, load_dataset
from random import randint, seed, choice
from typing import List, Tuple
from tqdm import tqdm
from hdfs_io import copy, makedirs
import argparse
import sys
from trec_eval import eval_rerank
def make_prefix(dp, template_type):
query = dp['query']
hits = dp['hits']
num_hits = len(hits)
# NOTE: also need to change reward_score/countdown.py
if template_type == 'base':
raise NotImplementedError
elif template_type == 'qwen-instruct':
"""This works for Qwen Instruct Models"""
prefix = (
f"I will provide you with passages, each indicated by number identifier []. Rank the passages based on their relevance to the search query."
f"Search Query: {query}. \nRank the {num_hits} passages above based on their relevance to the search query."
f"The passages should be listed in descending order using identifiers. The most relevant passages should be listed first. The output format should be <answer> [] > [] </answer>, e.g., <answer> [1] > [2] </answer>."
)
return prefix
raw_dataset = load_dataset('le723z/DeepRerank', split='train')
all_metric, scores = eval_rerank('combined_qrels.txt', raw_dataset)
unsolvable_qids = []
for qid, metrics in scores.items():
if metrics['ndcg_cut_10'] == 0:
unsolvable_qids.append(qid)
unsolvable_qids, len(unsolvable_qids)
# Filter out samples with qids in unsolvable_qids
filtered_dataset = raw_dataset.filter(lambda x: str(x['hits'][0]['qid']) not in unsolvable_qids)
print(len(filtered_dataset))
def make_map_fn(split):
def process_fn(example, idx):
question = make_prefix(example, template_type="qwen-instruct")
messages = [
{"role": "system", "content": "You are DeepRerank, an intelligent assistant that can rank passages based on their relevancy to the search query. You first thinks about the reasoning process in the mind and then provides the user with the answer."},
{"role": "user","content": question},
{"role": "assistant", "content": "Okay, please provide the passages."}
]
rank = 0
for hit in example['hits']:
rank += 1
content = hit['content']
content = content.replace('Title: Content: ', '')
content = content.strip()
content = ' '.join(content.split()[:int(300)]) # max token fea each passage is 300
messages.append({"role": "user", "content": f"[{rank}] {content}"})
messages.append({"role": "assistant", "content": f"Received passage [{rank}]."})
messages.append({
"role": "user",
"content": f"""Please rank these passages according to their relevance to the search query: "{example['query']}"
Follow these steps exactly:
1. First, within <think> tags, analyze EACH passage individually:
- Evaluate how well it addresses the query
- Note specific relevant information or keywords
2. Then, within <answer> tags, provide ONLY the final ranking in descending order of relevance using the format: [X] > [Y] > [Z]"""
})
data = {
"data_source": "TREC-DL-TOP100",
"prompt": messages,
"ability": "rerank",
"reward_model": {
"style": "rule",
"ground_truth": ""
},
"extra_info": {
'split': split,
'index': idx,
}
}
return data
return process_fn
train_dataset = filtered_dataset.map(function=make_map_fn('train'), with_indices=True)
local_dir = './'
hdfs_dir = None
train_dataset.to_parquet(os.path.join(local_dir, 'filtered_train.parquet'))
if hdfs_dir is not None:
makedirs(hdfs_dir)
copy(src=local_dir, dst=hdfs_dir)
Then filter out the dataset
from multiprocessing import Pool
from functools import partial
def process_example(example, idx):
all_metric, scores = eval_rerank('combined_qrels.txt', example)
example['metrics'] = all_metric
if all_metric['NDCG@10'] < 0.1:
print(f"Discarding example {idx} with metrics: {all_metric}")
return None
return example
def filter_dataset_parallel(dataset, num_processes=8):
with Pool(processes=num_processes) as pool:
results = list(pool.starmap(process_example, [(example, idx) for idx, example in enumerate(dataset)]))
# Filter out None values and create new dataset
filtered_examples = [ex for ex in results if ex is not None]
filtered_dataset = dataset.from_list(filtered_examples)
print(f"\nKept {len(filtered_dataset)} examples")
return filtered_dataset
filtered_dataset = filter_dataset_parallel(dataset)
We also add possible best scores for each passages list
from datasets import load_dataset
from multiprocessing import Pool
from functools import partial
import copy
import random
import sys
sys.path.append('/home/mila/l/le.zhang/scratch/DeepRerank')
from trec_eval import EvalFunction, eval_rerank, trec_eval
qrels = EvalFunction.load_qrels('combined_qrels.txt')
dataset = load_dataset("parquet", data_files='filtered_train_high_ndcg.parquet')["train"]
def convert_to_trec_format(rank_dict):
"""Convert rank_results directly to the format needed for pytrec_eval"""
run_dict = {}
for index, hit in enumerate(rank_dict):
qid = str(hit['qid']) # Ensure qid is a string
docid = str(hit['docid']) # Ensure docid is a string
score = float(hit['score']) # Ensure score is a float
if qid not in run_dict:
run_dict[qid] = {}
run_dict[qid][docid] = score
return run_dict
def add_best_metrics(dataset_example, qrels):
"""
Adds the best possible metrics to a dataset example by reordering hits based on relevance scores.
Args:
dataset_example: A single example from the dataset
qrels: The relevance judgments dictionary
Returns:
The dataset example with added best_hits and metrics
"""
# Get the query ID from the first hit
qid = dataset_example['hits'][0]['qid']
corresponding_qrels = qrels[str(qid)]
hits = dataset_example['hits']
# Get relevance scores for each hit
hit_scores = []
for hit in hits:
docid = hit['docid']
rel_score = corresponding_qrels.get(docid, 0) # Default to 0 if not found
# Create a deep copy of the hit to avoid modifying the original
hit_copy = dict(hit)
hit_scores.append((hit_copy, rel_score))
# Sort hits by relevance score in descending order
sorted_hits = sorted(hit_scores, key=lambda x: x[1], reverse=True)
best_hits = [h[0] for h in sorted_hits]
# Update the score of the best_hits without affecting original hits
for org_idx, hit in enumerate(best_hits):
best_hits[org_idx]['score'] = 20-org_idx
# Get indices of where each original rank ended up after sorting
original_ranks = [h[0]['rank'] for h in hit_scores]
new_rank = [original_ranks.index(h[0]['rank']) for h in sorted_hits]
results = convert_to_trec_format(best_hits)
best_metric, _ = trec_eval(qrels, results, k_values=(1, 5, 10))
# Store the best hits separately from the original hits
dataset_example['best_metrics'] = best_metric
dataset_example['new_rank'] = new_rank
# Optionally store the best_hits if needed
# dataset_example['best_hits'] = best_hits
return dataset_example
map_fn = partial(add_best_metrics, qrels=qrels)
dataset = dataset.map(map_fn, num_proc=32)
and finally
import os
filtered_dataset = dataset.filter(lambda x: x['best_metrics']['NDCG@10'] > x['metrics']['NDCG@10'])
# upload to huggingface hub
filtered_dataset.push_to_hub('le723z/rearank_12k', 'filtered_train_high_ndcg_ideal')