ColBERT Retriever
rankify.retrievers.colbert_retriever
Run
Bases: object
Source code in rankify/utils/retrievers/colbert/colbert/infra/run.py
__new__()
Singleton Pattern. See https://python-patterns.guide/gang-of-four/singleton/
Source code in rankify/utils/retrievers/colbert/colbert/infra/run.py
RunConfig
dataclass
ColBERTConfig
dataclass
Bases: RunSettings, ResourceSettings, DocSettings, QuerySettings, TrainingSettings, IndexingSettings, SearchSettings, BaseConfig, TokenizerSettings
Source code in rankify/utils/retrievers/colbert/colbert/infra/config/config.py
Searcher
Source code in rankify/utils/retrievers/colbert/colbert/searcher.py
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 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 | |
BaseRetriever
Bases: ABC
Abstract base class for all retrieval methods in the rankify framework.
This class defines the common interface that all retrievers must implement, ensuring consistency across different retrieval methods (BM25, DPR, etc.).
Source code in rankify/retrievers/base_retriever.py
__init__(n_docs=10, batch_size=36, threads=30, **kwargs)
Initialize the base retriever.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_docs
|
int
|
Number of documents to retrieve per query |
10
|
batch_size
|
int
|
Number of queries to process in a batch |
36
|
threads
|
int
|
Number of parallel threads for retrieval |
30
|
**kwargs
|
Additional parameters specific to each retriever |
{}
|
Source code in rankify/retrievers/base_retriever.py
retrieve(documents)
abstractmethod
Retrieve relevant contexts for the given documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
List[Document]
|
List of documents containing queries |
required |
Returns:
| Type | Description |
|---|---|
List[Document]
|
List[Document]: Documents updated with retrieved contexts |
Source code in rankify/retrievers/base_retriever.py
IndexManager
Manages downloading, caching, and loading of retrieval indexes.
Handles both prebuilt indexes (wiki, msmarco) and custom user indexes.
Source code in rankify/retrievers/index_manager.py
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 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 346 347 348 | |
get_index_path(method, index_type, custom_path=None)
Get the path to the index for a given method and index type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Retrieval method (e.g., 'bm25', 'dpr-multi', 'ance') |
required |
index_type
|
str
|
Index type ('wiki', 'msmarco', or 'custom') |
required |
custom_path
|
str
|
Path to custom index if index_type is 'custom' |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path to the index |
Source code in rankify/retrievers/index_manager.py
load_id_mapping(index_path)
Load ID mapping if available.
Source code in rankify/retrievers/index_manager.py
load_corpus(index_path)
Load corpus data from index path (supports multiple formats).
Source code in rankify/retrievers/index_manager.py
download_and_extract_index(url)
Download and extract index from URL, return local path.
Source code in rankify/retrievers/index_manager.py
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
Context
Represents a context with metadata such as score and title.
Attributes:
| Name | Type | Description |
|---|---|---|
score |
float
|
The relevance score of the context. |
has_answer |
bool
|
Whether the context contains an answer. |
id |
int
|
The identifier of the context. |
title |
str
|
The title of the context. |
text |
str
|
The text of the context. |
Source code in rankify/dataset/dataset.py
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 | |
__init__(score=None, has_answer=None, id=None, title=None, text=None)
Initializes a Context instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score
|
float
|
The relevance score. |
None
|
has_answer
|
bool
|
Whether the context contains an answer. |
None
|
id
|
int
|
The identifier of the context. |
None
|
title
|
str
|
The title of the context. |
None
|
text
|
str
|
The text of the context. |
None
|
Example
Source code in rankify/dataset/dataset.py
to_dict(save_text=False)
Converts the Context instance to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_text
|
bool
|
Whether to include text in the output dictionary. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Optional[object]]
|
The context data. |
Example
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Context instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted context. |
Example
Source code in rankify/dataset/dataset.py
ColBERTRetriever
Bases: BaseRetriever
ColBERT retriever with backward compatibility for prebuilt indices.
Supports two modes: 1. Prebuilt indices (wiki, msmarco) - Original format with passages.tsv 2. Custom indices - New format with collection.tsv + ID mappings
Source code in rankify/retrievers/colbert_retriever.py
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 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
retrieve(documents)
Retrieve relevant contexts using ColBERT.