Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,103 @@
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
-
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
-
|
22 |
-
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
## Evaluation
|
104 |
-
|
105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
-
|
107 |
-
### Testing Data, Factors & Metrics
|
108 |
-
|
109 |
-
#### Testing Data
|
110 |
-
|
111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
112 |
-
|
113 |
-
[More Information Needed]
|
114 |
-
|
115 |
-
#### Factors
|
116 |
-
|
117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
-
|
119 |
-
[More Information Needed]
|
120 |
-
|
121 |
-
#### Metrics
|
122 |
-
|
123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
-
|
125 |
-
[More Information Needed]
|
126 |
-
|
127 |
-
### Results
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
-
|
141 |
-
## Environmental Impact
|
142 |
-
|
143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
-
|
145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
-
|
147 |
-
- **Hardware Type:** [More Information Needed]
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
-
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
#### Hardware
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
## Glossary [optional]
|
184 |
-
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
-
|
187 |
-
[More Information Needed]
|
188 |
-
|
189 |
-
## More Information [optional]
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
-
|
199 |
-
[More Information Needed]
|
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
+
license: apache-2.0
|
4 |
+
datasets:
|
5 |
+
- Tevatron/msmarco-passage
|
6 |
---
|
7 |
|
8 |
+
# QWEN2.5 based Setwise reranker fine-tuned on MSMARCO dataset
|
9 |
+
|
10 |
+
GitHub repo: https://github.com/ielab/llm-rankers/tree/main/Rank-R1
|
11 |
+
|
12 |
+
# Python code examples:
|
13 |
+
|
14 |
+
Using [llm-rankers](https://github.com/ielab/llm-rankers) library:
|
15 |
+
```Python
|
16 |
+
from llmrankers.setwise import RankR1SetwiseLlmRanker
|
17 |
+
from llmrankers.rankers import SearchResult
|
18 |
+
|
19 |
+
docs = [SearchResult(docid=i, text=f'this is passage {i}', score=None) for i in range(20)]
|
20 |
+
query = 'Give me passage 6'
|
21 |
+
|
22 |
+
ranker = RankR1SetwiseLlmRanker(
|
23 |
+
model_name_or_path='Qwen/Qwen2.5-3B-Instruct',
|
24 |
+
lora_name_or_path='ielabgroup/Setwise-SFT-3B-v0.1',
|
25 |
+
prompt_file='prompt_setwise.toml',
|
26 |
+
num_child=19,
|
27 |
+
k=1,
|
28 |
+
verbose=True
|
29 |
+
)
|
30 |
+
|
31 |
+
print(ranker.rerank(query, docs)[0])
|
32 |
+
```
|
33 |
+
|
34 |
+
The `prompt_setwise.toml` is a .toml file with the following fields:
|
35 |
+
|
36 |
+
```toml
|
37 |
+
prompt_system = "A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant provides the user with the answer enclosed within <answer> </answer> tags, i.e., <answer> answer here </answer>."
|
38 |
+
|
39 |
+
prompt_user = '''Given the query: "{query}", which of the following documents is most relevant?
|
40 |
+
{docs}
|
41 |
+
Please provide only the label of the most relevant document to the query, enclosed in square brackets, within the answer tags. For example, if the third document is the most relevant, the answer should be: <answer>[3]</answer>.'''
|
42 |
+
|
43 |
+
pattern = '<answer>(.*?)</answer>'
|
44 |
+
```
|
45 |
+
|
46 |
+
Internally, the above code is equivalent to the following transformers code:
|
47 |
+
```Python
|
48 |
+
import torch
|
49 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
50 |
+
from peft import PeftModel, PeftConfig
|
51 |
+
|
52 |
+
def get_model(peft_model_name):
|
53 |
+
config = PeftConfig.from_pretrained(peft_model_name)
|
54 |
+
base_model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
|
55 |
+
model = PeftModel.from_pretrained(base_model, peft_model_name)
|
56 |
+
model = model.merge_and_unload()
|
57 |
+
return model
|
58 |
+
|
59 |
+
# Load the tokenizer and model
|
60 |
+
tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen2.5-3B-Instruct')
|
61 |
+
model = get_model('ielabgroup/Setwise-SFT-3B-v0.1').to('cuda:0').eval()
|
62 |
+
|
63 |
+
prompt_system = "A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant provides the user with the answer enclosed within <answer> </answer> tags, i.e., <answer> answer here </answer>."
|
64 |
+
prompt_user = '''Given the query: "{query}", which of the following documents is most relevant?
|
65 |
+
{docs}
|
66 |
+
Please provide only the label of the most relevant document to the query, enclosed in square brackets, within the answer tags. For example, if the third document is the most relevant, the answer should be: <answer>[3]</answer>.'''
|
67 |
+
|
68 |
+
query = 'Give me passage 6'
|
69 |
+
docs = [f'[{i+1}] this is passage {i+1}' for i in range(20)]
|
70 |
+
docs = '\n'.join(docs)
|
71 |
+
|
72 |
+
messages = [
|
73 |
+
{'role': "system", 'content': prompt_system},
|
74 |
+
{'role': "user", 'content': prompt_user.format(query=query, docs=docs)}
|
75 |
+
]
|
76 |
+
|
77 |
+
text = tokenizer.apply_chat_template(
|
78 |
+
messages,
|
79 |
+
tokenize=False,
|
80 |
+
add_generation_prompt=True
|
81 |
+
)
|
82 |
+
model_inputs = tokenizer([text], return_tensors="pt").to('cuda:0')
|
83 |
+
|
84 |
+
generated_ids = model.generate(
|
85 |
+
model_inputs.input_ids,
|
86 |
+
max_new_tokens=2048,
|
87 |
+
do_sample=False,
|
88 |
+
)
|
89 |
+
generated_ids = [
|
90 |
+
output_ids[len(input_ids)-1:] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
91 |
+
]
|
92 |
+
|
93 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
94 |
+
'''
|
95 |
+
<answer>[6]</answer>
|
96 |
+
'''
|
97 |
+
|
98 |
+
# extract the answer
|
99 |
+
import re
|
100 |
+
pattern = '<answer>(.*?)</answer>'
|
101 |
+
answer = re.search(pattern, response, re.DOTALL).group(1) # answer = '[6]'
|
102 |
+
```
|
103 |
+
> Note that this Setwise rerankers are trained with the prompt format shown above, which includes 20 documents. Other numbers of documents should also work fine, but this would represent a "zero-shot" setting for the model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|