Skip to content

Rename for test data class for pytest conflict #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions redisvl/utils/optimize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from redisvl.utils.optimize.base import BaseThresholdOptimizer, EvalMetric
from redisvl.utils.optimize.cache import CacheThresholdOptimizer
from redisvl.utils.optimize.router import RouterThresholdOptimizer
from redisvl.utils.optimize.schema import TestData
from redisvl.utils.optimize.schema import LabeledData

__all__ = [
"CacheThresholdOptimizer",
"RouterThresholdOptimizer",
"EvalMetric",
"BaseThresholdOptimizer",
"TestData",
"LabeledData",
]
8 changes: 4 additions & 4 deletions redisvl/utils/optimize/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from redisvl.extensions.llmcache.semantic import SemanticCache
from redisvl.query import RangeQuery
from redisvl.utils.optimize.base import BaseThresholdOptimizer, EvalMetric
from redisvl.utils.optimize.schema import TestData
from redisvl.utils.optimize.schema import LabeledData
from redisvl.utils.optimize.utils import NULL_RESPONSE_KEY, _format_qrels


def _generate_run_cache(test_data: List[TestData], threshold: float) -> Run:
def _generate_run_cache(test_data: List[LabeledData], threshold: float) -> Run:
"""Format observed data for evaluation with ranx"""
run_dict: Dict[str, Dict[str, int]] = {}

Expand All @@ -30,7 +30,7 @@ def _generate_run_cache(test_data: List[TestData], threshold: float) -> Run:


def _eval_cache(
test_data: List[TestData], threshold: float, qrels: Qrels, metric: str
test_data: List[LabeledData], threshold: float, qrels: Qrels, metric: str
) -> float:
"""Formats run data and evaluates supported metric"""
run = _generate_run_cache(test_data, threshold)
Expand All @@ -46,7 +46,7 @@ def _get_best_threshold(metrics: dict) -> float:


def _grid_search_opt_cache(
cache: SemanticCache, test_data: List[TestData], eval_metric: EvalMetric
cache: SemanticCache, test_data: List[LabeledData], eval_metric: EvalMetric
):
"""Evaluates all thresholds in linspace for cache to determine optimal"""
thresholds = np.linspace(0.01, 0.8, 60)
Expand Down
13 changes: 8 additions & 5 deletions redisvl/utils/optimize/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

from redisvl.extensions.router.semantic import SemanticRouter
from redisvl.utils.optimize.base import BaseThresholdOptimizer, EvalMetric
from redisvl.utils.optimize.schema import TestData
from redisvl.utils.optimize.schema import LabeledData
from redisvl.utils.optimize.utils import NULL_RESPONSE_KEY, _format_qrels


def _generate_run_router(test_data: List[TestData], router: SemanticRouter) -> Run:
def _generate_run_router(test_data: List[LabeledData], router: SemanticRouter) -> Run:
"""Format router results into format for ranx Run"""
run_dict: Dict[Any, Any] = {}

Expand All @@ -26,7 +26,7 @@ def _generate_run_router(test_data: List[TestData], router: SemanticRouter) -> R


def _eval_router(
router: SemanticRouter, test_data: List[TestData], qrels: Qrels, eval_metric: str
router: SemanticRouter, test_data: List[LabeledData], qrels: Qrels, eval_metric: str
) -> float:
"""Evaluate acceptable metric given run and qrels data"""
run = _generate_run_router(test_data, router)
Expand Down Expand Up @@ -55,7 +55,7 @@ def _router_random_search(

def _random_search_opt_router(
router: SemanticRouter,
test_data: List[TestData],
test_data: List[LabeledData],
qrels: Qrels,
eval_metric: EvalMetric,
**kwargs: Any,
Expand All @@ -67,12 +67,15 @@ def _random_search_opt_router(
best_thresholds = router.route_thresholds

max_iterations = kwargs.get("max_iterations", 20)
search_step = kwargs.get("search_step", 0.10)

for _ in range(max_iterations):
route_names = router.route_names
route_thresholds = router.route_thresholds
thresholds = _router_random_search(
route_names=route_names, route_thresholds=route_thresholds
route_names=route_names,
route_thresholds=route_thresholds,
search_step=search_step,
)
router.update_route_thresholds(thresholds)
score = _eval_router(router, test_data, qrels, eval_metric.value)
Expand Down
2 changes: 1 addition & 1 deletion redisvl/utils/optimize/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ulid import ULID


class TestData(BaseModel):
class LabeledData(BaseModel):
id: str = Field(default_factory=lambda: str(ULID()))
query: str
query_match: Optional[str]
Expand Down
8 changes: 4 additions & 4 deletions redisvl/utils/optimize/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from ranx import Qrels

from redisvl.utils.optimize.schema import TestData
from redisvl.utils.optimize.schema import LabeledData

NULL_RESPONSE_KEY = "no_match"


def _format_qrels(test_data: List[TestData]) -> Qrels:
def _format_qrels(test_data: List[LabeledData]) -> Qrels:
"""Utility function for creating qrels for evaluation with ranx"""
qrels_dict = {}

Expand All @@ -21,6 +21,6 @@ def _format_qrels(test_data: List[TestData]) -> Qrels:
return Qrels(qrels_dict)


def _validate_test_dict(test_dict: List[dict]) -> List[TestData]:
def _validate_test_dict(test_dict: List[dict]) -> List[LabeledData]:
"""Convert/validate test_dict for use in optimizer"""
return [TestData(**d) for d in test_dict]
return [LabeledData(**d) for d in test_dict]
2 changes: 1 addition & 1 deletion tests/integration/test_threshold_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_routes_different_distance_thresholds_optimizer_default(

# now run optimizer
router_optimizer = RouterThresholdOptimizer(router, test_data_optimization)
router_optimizer.optimize(max_iterations=10)
router_optimizer.optimize(max_iterations=10, search_step=0.5)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop gap makes intermittent error damn near impossible. Also discovered helpful kwarg gap.


# test that it updated thresholds beyond the null case
for route in routes:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_threshold_optimizer_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ranx import evaluate

from redisvl.utils.optimize import TestData
from redisvl.utils.optimize import LabeledData
from redisvl.utils.optimize.cache import _generate_run_cache
from redisvl.utils.optimize.utils import _format_qrels

Expand All @@ -26,15 +26,15 @@ def test_known_precision_case():
"""
# Setup test data
test_data = [
TestData(
LabeledData(
query="test query 1",
query_match="doc1",
response=[
{"id": "doc1", "vector_distance": 0.2},
{"id": "doc2", "vector_distance": 0.3},
],
),
TestData(
LabeledData(
query="test query 2",
query_match="doc3",
response=[
Expand All @@ -58,7 +58,7 @@ def test_known_precision_case():
def test_known_precision_with_no_matches():
"""Test case where some queries have no matches."""
test_data = [
TestData(
LabeledData(
query="test query 2",
query_match="", # Expecting no match
response=[],
Expand Down