Online Retriever
rankify.retrievers.online_retriever
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
WebSearchTool
Bases: Tool
Source code in rankify/tools/Tools.py
create_search_api(search_provider='SERPER', api_key=None)
staticmethod
Instantiate a search API client to fetch SERP results from a SERP provider e.g., SERPER.dev. Args: search_provider (str): The name of the search provider. Api_key (str): The API key for the search provider. Can be stored and loaded from env vars. Returns: SearchAPIClient: An instance of the search API client. E.g., SerpAPIClient.
Source code in rankify/tools/Tools.py
setup()
Performs time-consuming setup operations here e.g., model loading.
Chunker
A modular text chunking class that splits text into smaller, overlapping segments.
This class provides a flexible way to break down large texts into smaller chunks while maintaining context through configurable overlap. It uses RecursiveCharacterTextSplitter from langchain under the hood.
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_size |
int
|
The target size for each text chunk. |
chunk_overlap |
int
|
The number of characters to overlap between chunks. |
separators |
List[str]
|
List of separators to use for splitting, in order of preference. |
length_function |
callable
|
Function to measure text length (default: len). |
Source code in rankify/tools/websearch/context/chunker.py
__init__(chunk_size=1200, chunk_overlap=100, min_chunk_size=800, separators=None, length_function=len)
Initialize the Chunker with specified parameters.
Args:
chunk_size (int, optional): Target size for each chunk. Defaults to 250.
chunk_overlap (int, optional): Number of characters to overlap. Defaults to 50.
separators (List[str], optional): Custom separators for splitting.
Defaults to ["
", " ", " "]. length_function (callable, optional): Function to measure text length. Defaults to len.
Source code in rankify/tools/websearch/context/chunker.py
split_text(text)
Split text and combine small chunks.
Source code in rankify/tools/websearch/context/chunker.py
remove_links(text)
Remove various types of links from text.
Source code in rankify/tools/websearch/context/chunker.py
split_texts(texts)
Split multiple texts into chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
List[str]
|
A list of input texts to be split into chunks. |
required |
Returns:
| Type | Description |
|---|---|
List[List[str]]
|
List[List[str]]: A list of lists, where each inner list contains the chunks for one input text. |