-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
299 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ defaults: | |
- setup: baseline | ||
- _self_ | ||
|
||
task: | ||
name: document-search | ||
|
||
neptune: | ||
project: ragbits | ||
run: False |
8 changes: 6 additions & 2 deletions
8
packages/ragbits-evaluate/examples/document-search/config/data/hf-docs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
path: "m-ric/huggingface_doc_qa_eval" | ||
split: "train" | ||
ingest: | ||
path: "micpst/hf-docs" | ||
split: "train" | ||
eval: | ||
path: "micpst/hf-docs-retrieval" | ||
split: "train" |
2 changes: 1 addition & 1 deletion
2
packages/ragbits-evaluate/examples/document-search/config/setup/baseline.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
name: BASELINE | ||
name: Baseline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 10 additions & 5 deletions
15
packages/ragbits-evaluate/src/ragbits/evaluate/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
from .evaluator import Evaluator | ||
from .loaders import DataLoader, HuggingFaceDataLoader | ||
from .metrics import Metric, MetricSet | ||
from .pipelines import DocumentSearchEvaluationPipeline | ||
from .utils import save | ||
from .metrics.base import Metric, MetricSet | ||
from .pipelines.base import EvaluationPipeline, EvaluationResult | ||
from .pipelines.document_search import DocumentSearchPipeline, DocumentSearchResult | ||
from .utils import log_to_file, log_to_neptune | ||
|
||
__all__ = [ | ||
"Evaluator", | ||
"DataLoader", | ||
"HuggingFaceDataLoader", | ||
"MetricSet", | ||
"Metric", | ||
"DocumentSearchEvaluationPipeline", | ||
"save", | ||
"EvaluationPipeline", | ||
"DocumentSearchPipeline", | ||
"EvaluationResult", | ||
"DocumentSearchResult", | ||
"log_to_file", | ||
"log_to_neptune", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/ragbits-evaluate/src/ragbits/evaluate/metrics/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .base import Metric, MetricSet | ||
from .document_search import DocumentSearchPrecision, DocumentSearchRecall | ||
|
||
__all__ = ["Metric", "MetricSet"] | ||
__all__ = ["Metric", "MetricSet", "DocumentSearchPrecision", "DocumentSearchRecall"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/ragbits-evaluate/src/ragbits/evaluate/metrics/document_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from typing import Any, Optional | ||
|
||
from omegaconf import DictConfig | ||
|
||
from ragbits.evaluate.metrics.base import Metric | ||
from ragbits.evaluate.pipelines.document_search import DocumentSearchResult | ||
|
||
|
||
class DocumentSearchPrecision(Metric[DocumentSearchResult]): | ||
""" | ||
Precision measures the accuracy of the retrieved documents. It is the ratio of the number of relevant | ||
documents retrieved to the total number of documents retrieved. | ||
Precision = Total Number of Relevent Documents Retrieved / Total Number of Documents Retrieved | ||
Precision evaluates: "Out of all the documents that the system retrieved, how many were actually relevant?” | ||
""" | ||
|
||
def compute(self, results: list[DocumentSearchResult]) -> dict[str, Any]: | ||
""" | ||
Compute the metric. | ||
Args: | ||
results: The evaluation results. | ||
Returns: | ||
The computed metric. | ||
""" | ||
total_relevant_documents_retrieved = sum( | ||
len(set(result.reference_passages) & set(result.predicted_passages)) for result in results | ||
) | ||
total_documents_retrieved = sum(len(set(result.predicted_passages)) for result in results) | ||
|
||
return { | ||
"DOCUMENT_SEARCH/PRECISION": (total_relevant_documents_retrieved / total_documents_retrieved) | ||
if total_documents_retrieved > 0 | ||
else 0.0, | ||
} | ||
|
||
|
||
class DocumentSearchRecall(Metric[DocumentSearchResult]): | ||
""" | ||
Recall measures the comprehensiveness of the retrieved documents. It is the ratio of the number of relevant | ||
documents retrieved to the total number of relevant documents in the database for the given query. | ||
Recall = Total Number of Relevant Documents Retrieved / Total Number of Relevant Documents in the Database | ||
Recall evaluates: "Out of all the relevant documents that exist in the database, | ||
how many did the system manage to retrieve?" | ||
""" | ||
|
||
def compute(self, results: list[DocumentSearchResult]) -> dict[str, Any]: | ||
""" | ||
Compute the metric. | ||
Args: | ||
results: The evaluation results. | ||
Returns: | ||
The computed metric. | ||
""" | ||
total_relevant_documents_retrieved = sum( | ||
len(set(result.reference_passages) & set(result.predicted_passages)) for result in results | ||
) | ||
total_relevant_documents = sum(len(set(result.reference_passages)) for result in results) | ||
|
||
return { | ||
"DOCUMENT_SEARCH/RECALL": (total_relevant_documents_retrieved / total_relevant_documents) | ||
if total_relevant_documents > 0 | ||
else 0.0, | ||
} | ||
|
||
|
||
class DocumentSearchF1(Metric[DocumentSearchResult]): | ||
""" | ||
F1 Score is the harmonic mean of precision and recall. It is the weighted average of Precision and Recall. | ||
F1 = 2 * (Precision * Recall) / (Precision + Recall) | ||
""" | ||
|
||
def __init__(self, config: Optional[DictConfig] = None) -> None: | ||
""" | ||
Initializes the metric. | ||
Args: | ||
config: The metric configuration. | ||
""" | ||
super().__init__(config) | ||
self.precision = DocumentSearchPrecision(config) | ||
self.recall = DocumentSearchRecall(config) | ||
|
||
def compute(self, results: list[DocumentSearchResult]) -> dict[str, Any]: | ||
""" | ||
Compute the metric. | ||
Args: | ||
results: The evaluation results. | ||
Returns: | ||
The computed metric. | ||
""" | ||
precision = self.precision.compute(results)["DOCUMENT_SEARCH/PRECISION"] | ||
recall = self.recall.compute(results)["DOCUMENT_SEARCH/RECALL"] | ||
|
||
return { | ||
"DOCUMENT_SEARCH/F1": (2 * (precision * recall) / (precision + recall)) | ||
if (precision + recall) > 0 | ||
else 0.0, | ||
} |
5 changes: 3 additions & 2 deletions
5
packages/ragbits-evaluate/src/ragbits/evaluate/pipelines/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from .base import EvaluationPipeline, EvaluationResult | ||
from .document_search import DocumentSearchEvaluationPipeline | ||
from .document_search import DocumentSearchPipeline, DocumentSearchResult | ||
|
||
__all__ = [ | ||
"DocumentSearchEvaluationPipeline", | ||
"DocumentSearchPipeline", | ||
"DocumentSearchResult", | ||
"EvaluationPipeline", | ||
"EvaluationResult", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.