FiD RAG Method
rankify.generator.rag_methods.fid_rag_method
BaseRAGModel
Bases: ABC
Base RAG Model for Retrieval-Augmented Generation (RAG).
This is an abstract base class for implementing LLM endpoints in rankify. It defines the interface for generating responses and optional embedding generation.
Methods:
| Name | Description |
|---|---|
generate |
str, **kwargs) -> str: Abstract method to generate a response based on the given prompt. |
embed |
str, **kwargs) -> List[float]: Optional method to generate embeddings for the given text. |
Notes
- This class serves as a blueprint for RAG models like
OpenAIModelandHuggingFaceModel. - The
embedmethod is optional and can be implemented if needed. - This class needs to be extended to include new LLM endpoints in Rankify.
Source code in rankify/generator/models/base_rag_model.py
generate(prompt, **kwargs)
abstractmethod
embed(text, **kwargs)
Optional: Generate embeddings for the given text.
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
BaseRAGMethod
Bases: ABC
Base RAG Method for Retrieval-Augmented Generation (RAG) techniques.
This abstract base class defines the blueprint for implementing RAG methods in Rankify. Each RAG method (e.g., zero-shot, chain-of-thought, Fusion-in-Decoder) should inherit from this class and implement the logic for answering questions using a provided RAG model.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
BaseRAGModel
|
The RAG model instance used for generation. |
Methods:
| Name | Description |
|---|---|
answer_questions |
List[Document], custom_prompt=None, **kwargs) -> List[str]: Abstract method to answer questions based on a list of documents and optional custom prompt. |
Notes
- Extend this class to implement new RAG techniques or strategies.
- The
answer_questionsmethod must be implemented by all subclasses. - This class enables modularity and extensibility for different retrieval-augmented generation approaches.
Source code in rankify/generator/rag_methods/base_rag_method.py
__init__(model, **kwargs)
Initialize the BaseRAGMethod.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseRAGModel
|
The RAG model instance used for generation. |
required |
**kwargs
|
Additional configuration parameters for the RAG method. |
{}
|
Source code in rankify/generator/rag_methods/base_rag_method.py
answer_questions(documents, custom_prompt=None, **kwargs)
abstractmethod
Abstract method to answer questions based on a list of documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
List[Document]
|
List of Document objects containing questions and contexts. |
required |
custom_prompt
|
str
|
Custom prompt to override default prompt generation. |
None
|
**kwargs
|
Additional parameters for the answering logic. |
{}
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: List of generated answers, one per document. |
Notes
- Must be implemented by subclasses to define the RAG technique's answering logic.
- Enables flexible integration of different prompting or generation strategies.
Source code in rankify/generator/rag_methods/base_rag_method.py
FiDRAGMethod
Bases: BaseRAGMethod
FiD RAG Method for Open-Domain Question Answering.
This class implements a retrieval-augmented generation (RAG) method using the Fusion-in-Decoder (FiD) approach. The FiD model aggregates information from multiple retrieved passages to generate context-aware answers.
References
- Izacard & Grave Leveraging Passage Retrieval with Generative Models for Open-Domain QA
Paper
Attributes:
| Name | Type | Description |
|---|---|---|
model |
BaseRAGModel
|
The underlying FiD model used for text generation. |
Methods:
| Name | Description |
|---|---|
answer_questions |
List[Document], **kwargs) -> List[str]: Generates answers for a list of documents using the FiD model. |
See Also
FiDModel: Class for FiDModel, containing the FiD specific logic.
Example
from rankify.dataset.dataset import Document, Question, Answer, Context
from rankify.generator.generator import Generator
# Define question and answer
question = Question("What is the capital of France?")
answers = Answer([""])
contexts = [
Context(id=1, title="France", text="The capital of France is Paris.", score=0.9),
Context(id=2, title="Germany", text="Berlin is the capital of Germany.", score=0.5)
]
# Construct document
doc = Document(question=question, answers=answers, contexts=contexts)
# Initialize Generator (e.g., Meta Llama)
generator = Generator(method="fid", model_name='nq_reader_base', backend="fid")
# Generate answer
generated_answers = generator.generate([doc])
print(generated_answers) # Output: ["Paris"]
Notes
- This class was created to keep the unified interface of RAG methods.
- Since FiD is a specific RAG technique that relies on the full transformer architecture, the logic is included in the model, see
Source code in rankify/generator/rag_methods/fid_rag_method.py
answer_questions(documents, custom_prompt=None, **kwargs)
Answer questions for a list of documents using the FiDModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
List[Document]
|
A list of Document objects containing questions and contexts. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
Lists[str]: An answer based on the given documents and question. |