-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from lancedb/rag
Add reranker latency support
- Loading branch information
Showing
4 changed files
with
60 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import copy | ||
from ..retriever.base import Metric | ||
import lancedb | ||
import logging | ||
import pandas as pd | ||
from lancedb.pydantic import LanceModel, Vector | ||
from lancedb.embeddings import get_registry | ||
import time | ||
|
||
from ...search_utils import QueryType, deduce_query_type, search_table, QueryConfigError | ||
from ..retriever.hit_rate import Metric | ||
from ...results import RerankerResult | ||
|
||
# Set logging level to INFO | ||
logger = logging.getLogger("lancedb") | ||
logger.setLevel(logging.INFO) | ||
|
||
class RerankerLatency: | ||
def __init__(self, metric: Metric) -> None: | ||
if metric.reranker is None: | ||
raise ValueError("Reranker must be provided to evaluate reranker latency") | ||
self.metric = metric | ||
|
||
def evaluate(self, query_type:str, top_k: int = 5) -> RerankerResult: | ||
if query_type == QueryType.HYBRID: | ||
raise ValueError("HYBRID query type is not supported for reranker latency evaluation") | ||
elif query_type == QueryType.VECTOR: | ||
qt, reranked_qt = QueryType.VECTOR, QueryType.RERANK_VECTOR | ||
elif query_type == QueryType.FTS: | ||
qt, reranked_qt = QueryType.FTS, QueryType.RERANK_FTS | ||
else: | ||
raise ValueError(f"Invalid query type: {query_type}. Supported query types are VECTOR and FTS") | ||
|
||
t1 = time.time() | ||
eval_reranker = self.metric.evaluate_query_type(reranked_qt, top_k) | ||
t2 = time.time() | ||
reranker_latency = t2 - t1 | ||
|
||
t3 = time.time() | ||
eval_wo_reranker = self.metric.evaluate_query_type(qt, top_k) | ||
t4 = time.time() | ||
wo_reranker_latency = t4 - t3 | ||
|
||
delta_accuracy = eval_reranker - eval_wo_reranker | ||
data_size = len(self.metric.dataset.to_pandas()) | ||
latency = (reranker_latency - wo_reranker_latency) / data_size | ||
|
||
return RerankerResult(latency=latency, delta_accuracy=delta_accuracy) | ||
|
||
|
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,2 @@ | ||
from .hit_rate import HitRate | ||
from .base import QueryType | ||
from .base import Metric |
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