UPR
rankify.models.upr
Document
Represents a document consisting of a question, answers, and contexts.
Attributes:
| Name | Type | Description |
|---|---|---|
question |
Question
|
The question associated with the document. |
answers |
Answer
|
The answers to the question. |
contexts |
list[Context]
|
A list of related contexts. |
reorder_contexts |
list[Context] or None
|
A reordered list of contexts based on relevance. |
Source code in rankify/dataset/dataset.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
__init__(question, answers, contexts=None, id=None)
Initializes a Document instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Question
|
The question associated with the document. |
required |
answers
|
Answer
|
The answers to the question. |
required |
contexts
|
list[Context]
|
A list of contexts related to the question. |
None
|
Example
q = Question("What is the capital of France?")
a = Answer(["Paris"])
c1 = Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris.")
c2 = Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany.")
d = Document(question=q, answers=a, contexts=[c1, c2])
print(d)
Source code in rankify/dataset/dataset.py
from_dict(data, n_docs=100)
classmethod
Creates a Document instance from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
A dictionary containing the question, answers, and contexts. |
required |
n_docs
|
int
|
The number of contexts to include. Defaults to 100. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
Document |
Document
|
A new Document instance. |
Example
data = {
"question": "What is the capital of France?",
"answers": ["Paris"],
"ctxs": [
{"score": 0.9, "has_answer": True, "id": 1, "title": "Paris", "text": "The capital of France is Paris."},
{"score": 0.5, "has_answer": False, "id": 2, "title": "Berlin", "text": "Berlin is the capital of Germany."}
]
}
d = Document.from_dict(data)
print(d.question)
Source code in rankify/dataset/dataset.py
to_dict()
Converts the document into a dictionary representation.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Optional[object]]
|
A dictionary containing the question, answers, and contexts. |
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Document instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted document information. |
Source code in rankify/dataset/dataset.py
BaseRanking
Bases: ABC
An abstract base class for implementing different ranking models.
This class defines the interface for all ranking models, ensuring that all subclasses implement the required methods.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
The name of the ranking method. |
model_name |
str
|
The name of the model being used for ranking. |
api_key |
str
|
An optional API key for accessing remote models or services. |
Source code in rankify/models/base.py
__init__(method=None, model_name=None, api_key=None, **kwargs)
abstractmethod
Initializes the base ranking model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The name of the ranking method. Defaults to None. |
None
|
model_name
|
str
|
The name of the model being used for ranking. Defaults to None. |
None
|
api_key
|
str
|
An optional API key for accessing remote models or services. Defaults to None. |
None
|
Example
Source code in rankify/models/base.py
rank(documents)
abstractmethod
Abstract method to rank a list of documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list[Document]
|
A list of Document instances that need to be ranked. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This method must be implemented by subclasses. |
Example
Source code in rankify/models/base.py
UPR
Bases: BaseRanking
Implements Unsupervised Passage Reranking (UPR) using models like GPT and T5.
This class follows the approach described in UPR paper for unsupervised reranking of retrieved documents by leveraging question generation techniques. The model improves passage retrieval by estimating the likelihood of a query given a passage.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
The name of the model used for reranking. |
method |
str
|
The reranking method. |
model |
PreTrainedModel
|
The pre-trained model utilized for reranking. |
tokenizer |
PreTrainedTokenizer
|
The tokenizer associated with the model. |
use_bf16 |
bool
|
Whether to use bfloat16 precision for computations. |
use_gpu |
bool
|
Whether GPU acceleration is enabled. |
batch_size |
int
|
Batch size for processing contexts. |
shard_size |
int
|
Shard size for processing contexts in chunks. |
include_eos_token |
bool
|
Whether to include the end-of-sequence token during ranking. |
verbalizer_head |
str
|
Prefix to prepend before the passage during input construction. |
verbalizer |
str
|
A prompt instructing the model to generate a question based on the passage. |
References
Devendra Singh Sachan et al. "Improving Passage Retrieval with Zero-Shot Question Generation." Proceedings of the 2022 Annual Conference of the Association for Computational Linguistics (ACL), 2022. Paper
Source code in rankify/models/upr.py
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
__init__(method=None, model_name='google/t5-small-lm-adapt', api_key=None, **kwargs)
Initializes a UPR instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The name of the reranking method. Defaults to None. |
None
|
model_name
|
str
|
The name of the model to be used for reranking.
Defaults to |
'google/t5-small-lm-adapt'
|
api_key
|
str
|
API key for remote model access. Defaults to None. |
None
|
**kwargs
|
Additional parameters for model configuration. |
{}
|
Source code in rankify/models/upr.py
rank_t5(document, model, tokenizer, verbalizer_head, verbalizer, use_gpu, shard_size)
classmethod
Reranks contexts in the document using a T5 model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The document containing contexts to be reranked. |
required |
model
|
T5ForConditionalGeneration
|
The pre-trained T5 model. |
required |
tokenizer
|
T5Tokenizer
|
The tokenizer associated with the T5 model. |
required |
verbalizer_head
|
str
|
Prefix to use before the passage during input construction. |
required |
verbalizer
|
str
|
A prompt that requests the model to generate a question based on the passage. |
required |
use_gpu
|
bool
|
Whether to use GPU for computations. |
required |
shard_size
|
int
|
Shard size for processing contexts in chunks. |
required |
Returns:
| Type | Description |
|---|---|
|
List[Document]: The reordered list of contexts based on their relevance. |
Example
Source code in rankify/models/upr.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
rank_gpt(document, model, tokenizer, verbalizer_head, verbalizer, use_gpu, shard_size, include_eos_token)
classmethod
Reranks contexts in the document using a GPT model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document
|
The document containing contexts to be reranked. |
required |
model
|
AutoModelForCausalLM
|
The pre-trained GPT model. |
required |
tokenizer
|
AutoTokenizer
|
The tokenizer associated with the GPT model. |
required |
verbalizer_head
|
str
|
Prefix to use before the passage during input construction. |
required |
verbalizer
|
str
|
A prompt that requests the model to generate a question based on the passage. |
required |
use_gpu
|
bool
|
Whether to use GPU for computations. |
required |
shard_size
|
int
|
Shard size for processing contexts in chunks. |
required |
include_eos_token
|
bool
|
Whether to include the end-of-sequence token during ranking. |
required |
Returns:
| Type | Description |
|---|---|
|
List[Document]: The reordered list of contexts based on their relevance. |
Example
Source code in rankify/models/upr.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
rank(documents)
Reranks the contexts in each document using the appropriate model (GPT or T5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
List[Document]
|
A list of documents whose contexts need to be reranked. |
required |
Returns:
| Type | Description |
|---|---|
List[Document]
|
List[Document]: The reranked list of documents. |
Notes
- Uses T5 for sequence-to-sequence reranking and GPT for generative reranking.
- Scores passages based on the probability of generating the input query.
Source code in rankify/models/upr.py
set_random_seed(seed)
Sets the random seed for reproducibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
The seed value for random number generators. |
required |